linux系统调用之文件:递归实现tree命令

Code:
  1. /*打å?°ç›®å½•下所有文件的大å°?*/  
  2. #include <sys/types.h>  
  3. #include <dirent.h>  
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6. #include <sys/stat.h>  
  7. #include <unistd.h>  
  8. #include <string.h>  
  9. #define BUF_LEN 1024  
  10. //int stat(const char *path, struct stat *buf);  
  11. //int fstat(int filedes, struct stat *buf);  
  12. //int lstat(const char *path, struct stat *buf);  
  13.   
  14. //DIR *opendir(const char *name);  
  15. //struct dirent *readdir(DIR *dir);  
  16.   
  17. void check_mode(struct stat *st, char mode[])  
  18. {  
  19.     if(S_ISREG(st->st_mode)) mode[0] = '-';  
  20.     else if(S_ISDIR(st->st_mode)) mode[0] = 'd';  
  21.     else if(S_ISCHR(st->st_mode)) mode[0] = 'c';  
  22.     else if(S_ISBLK(st->st_mode)) mode[0] = 'b';  
  23.     else if(S_ISFIFO(st->st_mode)) mode[0] = 'f';  
  24.     else if(S_ISLNK(st->st_mode)) mode[0] = 'l';  
  25.     else if(S_ISSOCK(st->st_mode)) mode[0] = 's';  
  26.   
  27.     if(S_IRUSR & st->st_mode) mode[1] = 'r';  
  28.     else mode[1] = '-';  
  29.     if(S_IWUSR & st->st_mode) mode[2] = 'w';  
  30.     else mode[2] = '-';  
  31.     if(S_IXUSR & st->st_mode) mode[3] = 'x';  
  32.     else mode[3] = '-';  
  33.     if(S_IRGRP & st->st_mode) mode[4] = 'r';  
  34.     else mode[4] = '-';  
  35.     if(S_IWGRP & st->st_mode) mode[5] = 'w';  
  36.     else mode[5] = '-';  
  37.     if(S_IXGRP & st->st_mode) mode[6] = 'x';  
  38.     else mode[6] = '-';  
  39.     if(S_IROTH & st->st_mode) mode[7] = 'r';  
  40.     else mode[7] = '-';  
  41.     if(S_IWOTH & st->st_mode) mode[8] = 'w';  
  42.     else mode[8] = '-';  
  43.     if(S_IXOTH & st->st_mode) mode[9] = 'x';  
  44.     else mode[9] = '-';  
  45. }  
  46.   
  47. void ls(char *name, char argv)  
  48. {  
  49.     char buf[BUF_LEN];  
  50.     struct dirent *read_dir, *read_last = NULL;  
  51.     struct stat st;  
  52.     DIR *dir;  
  53.     char mode[10];  
  54.     static int deep = 0;  
  55.     int i;  
  56.   
  57.     if((dir = opendir(name)) == NULL)  
  58.     {  
  59.         fprintf(stderr, "Can not open %s/n", name);  
  60.         exit(1);  
  61.     }  
  62.       
  63.     while((read_dir = readdir(dir)) != NULL)  
  64.     {  
  65.         strcpy(buf, name);  
  66.         strcat(buf, "/");  
  67.         strcat(buf, read_dir->d_name);  
  68.   
  69.         if(stat(buf, &st) < 0)  
  70.         {  
  71.             fprintf(stderr, "Stat Error/n");  
  72.             exit(0);  
  73.         }  
  74. //      check_mode(&st, mode);  
  75. //      printf("%s  %s  %d/n",mode, read_dir->d_name, st.st_size);  
  76.           
  77.         if(read_last != NULL)  
  78.         {     
  79.             putchar('|');  
  80.             for(i = 0; i < deep; i++)  
  81.             {  
  82.                 printf("   |");  
  83.             }  
  84.             printf("-- %s/n", read_last->d_name);  
  85.         }  
  86.         read_last = read_dir;         
  87.         if(S_ISDIR(st.st_mode) && argv == 'R'   
  88.            && strcmp(read_dir->d_name, ".") != 0   
  89.            && strcmp(read_dir->d_name, "..") != 0 && !S_ISLNK(st.st_mode))   
  90.         {  
  91.             deep++;  
  92.             ls(buf, argv);  
  93.             deep--;  
  94.         }  
  95.     }  
  96.   
  97.     if(deep > 0) putchar('|');  
  98.     else putchar('`');  
  99.   
  100.     for(i = 0; i < deep - 1; i++)  
  101.     {  
  102.         printf("   |");  
  103.     }  
  104.     if(deep > 0)   
  105.     {  
  106.         printf("   `");  
  107.     }  
  108.     printf("-- %s/n", read_last->d_name);  
  109. }  
  110.   
  111. int main(int argc, char **argv)  
  112. {  
  113.     char *name;  
  114.   
  115.     if(argc > 3)  
  116.     {  
  117.         fprintf(stderr, "Usage <%s><dirname>[-R]/n", argv[0]);  
  118.         exit(1);  
  119.     }  
  120.     else if(argc > 1)  
  121.     {  
  122.         name = argv[1];  
  123.     }  
  124.     else  
  125.     {  
  126.         name = ".";  
  127.     }  
  128.       
  129.     putchar('/n');  
  130.     printf("%s/n", argv[1]);  
  131.     if(argc < 3) ls(name, 0);  
  132.     else ls(name, argv[2][1]);  
  133.   
  134.     putchar('/n');  
  135.     return 0;  
  136.   
  137. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值