Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1786095
  • 博文数量: 413
  • 博客积分: 8399
  • 博客等级: 中将
  • 技术积分: 4325
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-09 10:44
文章分类

全部博文(413)

文章存档

2015年(1)

2014年(18)

2013年(39)

2012年(163)

2011年(192)

分类: C/C++

2011-09-19 20:21:11

在上一篇博文http://blog.chinaunix.net/space.php?uid=25909722&do=blog&id=2856481中,我们学习了实现ls命令要涉及到的相关的结构体和函数。有了前面的基础,本文我们实实在在的实现ls命令:

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <dirent.h>
  4. #include <sys/stat.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <pwd.h>
  8. #include <grp.h>

  9. void do_ls(char[]);
  10. void do_stat(char*);
  11. void show_file_info(char*, struct stat*);
  12. void mode_str(int, char[]);
  13. char *uid_str(uid_t);
  14. char *gid_str(gid_t);

  15. int main(int argc, char *argv[])
  16. {
  17.         if(argc == 1)
  18.                 do_ls(".");
  19.         else
  20.                 while(--argc){
  21.                         printf("%s:\n", *++argv);
  22.                         do_ls(*argv);
  23.                 }

  24.         return 0;
  25. }

  26. void do_ls(char dirname[])
  27. {
  28.         DIR *dir_ptr;           /* the directory */
  29.         struct dirent *direntp; /* each entry */
  30.         char full_path[256];

  31.         if((dir_ptr = opendir(dirname)) == NULL){
  32.                 fprintf(stderr, "ls2: cannot open %s\n", dirname);
  33.         }else{
  34.                 while((direntp = readdir(dir_ptr)) != NULL){
  35.                         strcpy(full_path, dirname);
  36.                         int dir_len = strlen(dirname);
  37.                         if(dirname[dir_len - 1] != '/'){  /* 处理目录字符串最后没有‘/’的情况 */
  38.                                 full_path[dir_len] = '/';
  39.                                 strcpy(full_path + dir_len + 1, direntp->d_name);
  40.                         }else
  41.                                 strcpy(full_path + dir_len, direntp->d_name);

  42.                         do_stat(full_path);
  43.                 }
  44.                 closedir(dir_ptr);
  45.         }
  46. } 
  47. void do_stat(char *filename/* 获得目录中文件的相关信息 */
  48. {
  49.         struct stat info;
  50.         
  51.         /* 如果filename最后没有‘/’的话,stat调用失败 */
  52.         if(stat(filename, &info) == -1){ /* cannot stat */
  53.                 perror("stat"); /* say why */
  54.                 printf("filename:%s\n", filename);
  55.         }else{
  56.                 char *pname = strrchr(filename, '/');
  57.                 show_file_info(pname + 1, &info);
  58.         }
  59. }

  60. void show_file_info(char *filename, struct stat *info_p/* 打印文件的相关信息 */
  61. {
  62.         char modestr[11];

  63.         mode_str(info_p->st_mode, modestr);

  64.         printf("%s", modestr);
  65.         printf("%3d ", (int)info_p->st_nlink);
  66.         printf("%-8s", uid_str(info_p->st_uid));
  67.         printf("%-8s", gid_str(info_p->st_gid));
  68.         printf("%4ld ", (long)info_p->st_size);
  69.         printf("%.12s ", 4 + ctime(&info_p->st_mtime));
  70.         printf("%s\n", filename);
  71. }

  72. void mode_str(int mode, char str[]/* 将文件的相关信息转化成 “crw-rw---"的形式 */
  73. {
  74.         strcpy(str, "----------"); /* default = no perms */
  75.         if(S_ISDIR(mode)) str[0] = 'd'; /* directory */
  76.         if(S_ISCHR(mode)) str[0] = 'c'; /* char device */
  77.         if(S_ISBLK(mode)) str[0] = 'b'; /* block device */
  78.         if(S_ISLNK(mode)) str[0] = 'l';

  79.         if(mode & S_IRUSR) str[1] = 'r'; /* 3 bits for user */
  80.         if(mode & S_IWUSR) str[2] = 'w';
  81.         if(mode & S_IXUSR) str[3] = 'x';

  82.         if(mode & S_IRGRP) str[4] = 'r'; /* 3 bits for group */
  83.         if(mode & S_IWGRP) str[5] = 'w';
  84.         if(mode & S_IXGRP) str[6] = 'x';

  85.         if(mode & S_IROTH) str[7] = 'r'; /* 3 bits for other */
  86.         if(mode & S_IWOTH) str[8] = 'w';
  87.         if(mode & S_IXOTH) str[9] = 'x';
  88. }

  89. char *uid_str(uid_t uid/* 将uid转化成username */
  90. {
  91.         static char numstr[10];
  92.         struct passwd *pw_ptr;

  93.         if((pw_ptr = getpwuid(uid)) == NULL){
  94.                 sprintf(numstr, "%d", uid);
  95.                 return numstr;
  96.         }else
  97.                 return pw_ptr->pw_name;
  98. }

  99. char *gid_str(gid_t gid/* 将gid转化成groupname */
  100. {
  101.         static char numstr[10];
  102.         struct group *grp_ptr;

  103.         if((grp_ptr = getgrgid(gid)) == NULL){
  104.                 sprintf(numstr, "% d", gid);
  105.                 return numstr;
  106.         }else
  107.                 return grp_ptr->gr_name;
  108. }
编译和运行结果:
digdeep@ubuntu:~/uulp$ gcc -Wall -o ls1 ls1.c
digdeep@ubuntu:~/uulp$ ./ls1  /etc/network/
/etc/network/:
drwxr-x---  2 root    root     4096 Apr 26 07:03 if-down.d
drwxr-x---  2 root    root     4096 Jul 14 10:47 if-up.d
drwxr-x---  2 root    root     4096 Apr 26 07:05 if-pre-up.d
drwxr-x---  6 root    root     4096 Jun 14 05:34 .
drwxr-x---  2 root    root     4096 Apr 26 07:05 if-post-down.d
-rwrr-----   1 root    root      196 Jun 14 05:33 interfaces
drwxr-x---147 root    root    12288 Sep 19 20:33 ..

阅读(7493) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~