用C语言自己编写一个ls程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
#include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <string.h> #include <unistd.h> #include <pwd.h> #include <grp.h> void do_ls(char *); void dostat(char *); void show_file_info( char *, struct stat *); void mode_to_letters( int , char * ); char *uid_to_name( uid_t ); char *gid_to_name( gid_t ); /************关于本文档******************************************** *filename:用C语言自己编写一个ls程序 *purpose:自己编写的一个ls程序 *wrote by: zhoulifa([email protected]) 周立发(http://zhoulifa.bokee.com) Linux爱好者 Linux知识传播者 SOHO族 开发者 最擅长C语言编程 *date time:2006-07-16 16:00:00 *Note: 任何人可以任意复制代码并运用这些文档,当然包括你的商业用途 * 但请遵循GPL。 *Hope:希望越来越多的人贡献自己的力量,为科学技术发展出力 *********************************************************************/ int main(int argc, char **argv) { if ( argc == 1 ) do_ls( "." ); else while ( --argc ){ printf("%s:\n", *++argv ); chdir(*argv); //切换到指定目录后再显示该目录的内容 do_ls( *argv ); chdir(""); //再回到当前工作目录来 } return 0; } void do_ls( char * dirname ) { DIR *dir_ptr; struct dirent *direntp; if ( ( dir_ptr = opendir( dirname ) ) == NULL ) {/*打开目录*/ fprintf(stderr,"ls1: cannot open %s, not a directory. treat as a file shown below:\n", dirname); dostat( dirname ); //如果不是目录就当作文件来显示其属性 } else { while ( ( direntp = readdir( dir_ptr ) ) != NULL ) dostat( direntp->d_name );/*逐个显示目录里文件信息*/ closedir(dir_ptr); } } void dostat( char *filename ) { struct stat info; if ( stat(filename, &info) == -1 ) /*取文件信息失败*/ perror( filename ); else /*显示文件信息*/ show_file_info( filename, &info ); } void show_file_info( char *filename, struct stat *info_p ) { char modestr[11]; mode_to_letters( info_p->st_mode, modestr ); printf( "%s" , modestr ); printf( "%4d " , (int) info_p->st_nlink); printf( "%-8s " , uid_to_name(info_p->st_uid) ); printf( "%-8s " , gid_to_name(info_p->st_gid) ); printf( "%8ld " , (long)info_p->st_size); printf( "%.12s ", 4+ctime(&info_p->st_mtime)); printf( "%s\n" , filename ); } void mode_to_letters( int mode, char * str ) { 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_IRGRP ) str[4] = 'r'; /* 组权限 */ if ( mode & S_IWGRP ) str[5] = 'w'; if ( mode & S_IXGRP ) str[6] = 'x'; if ( mode & S_IROTH ) str[7] = 'r'; /* 其人的权限 */ if ( mode & S_IWOTH ) str[8] = 'w'; if ( mode & S_IXOTH ) str[9] = 'x'; } 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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
./a.out / /: drwxr-xr-x 22 root root 4096 Jul 13 12:37 . drwxr-xr-x 22 root root 4096 Jul 13 12:37 .. drwxr-xr-x 2 root root 49152 May 21 03:00 lost+found drwxr-xr-x 130 root root 8192 Jul 16 15:01 etc drwxr-xr-x 3 root root 4096 May 21 03:01 media drwxr-xr-x 2 root root 4096 May 21 03:01 cdrom -rw-r--r-- 1 root root 6773545 Jul 13 08:06 initrd.img drwxr-xr-x 15 root root 4096 Jun 9 08:40 var drwxr-xr-x 18 root root 8192 Jul 13 12:37 lib drwxr-xr-x 11 root root 4096 May 21 03:08 usr drwxr-xr-x 2 root root 4096 Jul 14 09:41 bin drwxr-xr-x 3 root root 4096 Jul 13 12:37 boot drwxr-xr-x 15 root root 15260 Jul 16 14:37 dev drwxr-xr-x 5 root root 4096 Jun 1 09:22 home drwxr-xr-x 6 root root 4096 Jul 10 11:30 mnt dr-xr-xr-x 185 root root 0 Jul 16 22:37 proc drwxr-xr-x 36 root root 4096 Jul 9 15:45 root drwxr-xr-x 2 root root 8192 Jul 14 09:44 sbin drwxrwxrwx 17 root root 4096 Jul 16 16:15 tmp drwxr-xr-x 10 root root 0 Jul 16 22:37 sys drwxr-xr-x 2 root root 4096 May 21 03:02 srv drwxr-xr-x 5 root root 4096 May 29 13:03 opt drwxr-xr-x 2 root root 4096 May 21 03:02 initrd -rw-r--r-- 1 root root 1414702 Jul 8 05:22 vmlinuz drwxr-xr-x 34 administrator administrator 4096 Jul 12 15:31 data |
发表评论
要发表评论,您必须先登录。