小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

ls命令的實(shí)現(xiàn)(2)

 lchjczw 2013-03-21
  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/* 獲得目錄中文件的相關(guān)信息 */
  48. {
  49.         struct stat info;
  50.         
  51.         /* 如果filename最后沒有‘/’的話,stat調(diào)用失敗 */
  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/* 打印文件的相關(guān)信息 */
  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[]/* 將文件的相關(guān)信息轉(zhuǎn)化成 “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轉(zhuǎn)化成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轉(zhuǎn)化成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. }
編譯和運(yùn)行結(jié)果:
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 ..

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多