- 论坛徽章:
- 0
|
可以显示指定目录
显示total及结果按文件名排序(不过total的计算MS有问题)
其中用了静态数组,可以改进成链表,懒得弄了
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <math.h> /* in order to ceil */
#define MAXFILES 150
#define BLOCKSIZE 4 /* BLOCKSIZE 4K */
struct my_struct{
char modestr[11];
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
off_t st_size;
time_t my_mtime; /* 这里大问题啊..弄了好久...不能用名字st_mtime*/
char *my_filename;
};
typedef struct my_struct NODE;
int cmp ( const void *a , const void *b ); /* use to sort the filename */
void do_ls(char *);
void dostat( char *, NODE * );
void set_file_info( char *, struct stat * ,NODE *);
void mode_to_letters( int , char * );
void print_file_info(NODE *);
char *uid_to_name( uid_t );
char *gid_to_name( gid_t );
long total;
NODE node[MAXFILES];
/*
*edit by bo-liu
*as the ls -l
*07-04-12
*/
int main(int argc, char **argv){
total = 0;
if ( argc == 1 )
do_ls( "." );
else
while ( --argc ){
chdir(*++argv); /* change to the des dirtory */
do_ls( *argv );
chdir(""); /* come back */
}
return 0;
}
void do_ls( char * dirname ){
DIR *dir_ptr;
struct dirent *direntp;
int i,j;
i = 0;
if ( ( dir_ptr = opendir( dirname ) ) == NULL ) {
dostat( dirname,&node[i++]); /* des is not a dirtory just treate as a file */
}
else{
for(i = 0; ( direntp = readdir( dir_ptr ) ) != NULL && i < MAXFILES; i++){
dostat( direntp->d_name,&node);
}
}
j=i; /* now j is the files counter number */
qsort(node,j,sizeof(node[0]),cmp); /* sort the filename */
printf("total %ld\n",total*BLOCKSIZE); /* cacu the total */
for(i = 0;i < j;i++)
print_file_info(&node); /* print the file's info */
closedir(dir_ptr);
}
void dostat( char *filename, NODE *info_node ){
struct stat info;
if ( stat(filename, &info) == -1 ) /*err to get the file info */
perror( filename );
else /*save the file info to our own struct*/
set_file_info( filename, &info ,info_node);
}
void mode_to_letters( int mode, char * str ){ /* to deal with the mode */
strcpy( str, "----------" ); /* default=无参数 */
if ( S_ISDIR(mode) ) str[0] = 'd'; /* 目录 */
if ( S_ISCHR(mode) ) str[0] = 'c'; /* 字符设备 */
if ( S_ISBLK(mode) ) str[0] = 'b'; /* 块设备 */
if ( mode & S_IRUSR ) str[1] = 'r'; /* 用户权限 */
if ( mode & S_IWUSR ) str[2] = 'w';
if ( mode & S_IXUSR ) str[3] = 'x';
if ( mode & S_ISUID ) str[3] = 's'; /* 先写在这里,才会覆盖原来的s*/
if ( mode & S_IRGRP ) str[4] = 'r'; /* 组权限 */
if ( mode & S_IWGRP ) str[5] = 'w';
if ( mode & S_IXGRP ) str[6] = 'x';
if ( mode & S_ISGID ) str[6] = 's';
if ( mode & S_IROTH ) str[7] = 'r'; /* 其人的权限 */
if ( mode & S_IWOTH ) str[8] = 'w';
if ( mode & S_IXOTH ) str[9] = 'x';
if ( mode & S_ISVTX ) str[9] = 's'; /* 若一目录具有sticky 位(S_ISVTX)*/
}
char *uid_to_name( uid_t uid ){
struct passwd *pw_ptr;
static char numstr[10];
if ( ( pw_ptr = getpwuid( uid ) ) == NULL ){
sprintf(numstr,"%d", uid);
return numstr;
}
else
return pw_ptr->pw_name ;
}
char *gid_to_name( gid_t gid ){
struct group *grp_ptr;
static char numstr[10];
if ( ( grp_ptr = getgrgid(gid) ) == NULL ){
sprintf(numstr,"%d", gid);
return numstr;
}
else
return grp_ptr->gr_name;
}
/*save the file info to our own struct*/
void set_file_info( char *filename, struct stat *info_p ,NODE *info_node){
mode_to_letters( info_p->st_mode, info_node->modestr );
info_node->st_nlink = info_p->st_nlink;
info_node->st_uid = info_p->st_uid;
info_node->st_gid = info_p->st_gid;
info_node->st_size = info_p->st_size;
info_node->my_mtime = info_p->st_mtime;
info_node->my_filename = filename;
total += ceil( (double)(info_node->st_size)/1024 ); /* IO_BLOCKSIZE 1024 因文件系统而异 */
}
/* printf the info */
void print_file_info(NODE *info_node){
printf( "%s" , info_node->modestr );
printf( "%4d " , (int) info_node->st_nlink);
printf( "%-8s " , uid_to_name(info_node->st_uid) );
printf( "%-8s " , gid_to_name(info_node->st_gid) );
printf( "%8ld " , (long)info_node->st_size);
printf( "%.12s ", 4+ctime(&info_node->my_mtime));
printf( "%s\n" , info_node->my_filename );
}
/* sort the filename ,used in the qsort */
int cmp ( const void *a , const void *b ){
NODE *c = ( NODE *)a;
NODE *d = (NODE *)b;
return strcmp( c->my_filename , d->my_filename );
} |
|