LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1267|回复: 4

这个目录扫描程序里有一个函数不明白!!

[复制链接]
发表于 2003-11-18 23:24:14 | 显示全部楼层 |阅读模式

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <stdlib.h>

  7. void printdir(char *dir,int depth);

  8. int main(void)
  9. {
  10.         printf("Directory scan of /home/cai:\n");
  11.         printdir("/home/cai",0);
  12.         printf("done.\n");
  13. return 0;
  14. }

  15. void printdir(char *dir,int depth)
  16. {
  17.         DIR *dp;
  18.         struct dirent *entry;
  19.         struct stat statbuf;
  20.        
  21.         if((dp=opendir(dir)) == NULL) {
  22.                 fprintf(stderr,"cannot open directory:%s\n",dir);
  23.                 return;
  24.         }
  25.         chdir(dir);
  26.         while((entry=readdir(dp)) != NULL) {
  27.                 lstat(entry->d_name,&statbuf);
  28.                 if(S_ISDIR(statbuf.st_mode)) {     [color=red] /*S_ISDIR这个函数有什么功能*/[/color]
  29.                         if(strcmp(".",entry->d_name) == 0 ||
  30.                                 strcmp("..",entry->d_name) == 0)
  31.                                 continue;
  32.                         printf("%*s%s/\n",depth," ",entry->d_name);
  33.                         printdir(entry->d_name,depth+4);
  34.                 }
  35.                 else
  36.                         printf("%*s%s\n",depth," ",entry->d_name);
  37.         }
  38.         chdir("..");
  39.         closedir(dp);
  40. }
复制代码
 楼主| 发表于 2003-11-18 23:29:44 | 显示全部楼层
再贴一次!!!

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <stdlib.h>

  7. void printdir(char *dir,int depth);

  8. int main(void)
  9. {
  10.         printf("Directory scan of /home/cai:\n");
  11.         printdir("/home/cai",0);
  12.         printf("done.\n");
  13. return 0;
  14. }

  15. void printdir(char *dir,int depth)
  16. {
  17.         DIR *dp;
  18.         struct dirent *entry;
  19.         struct stat statbuf;
  20.        
  21.         if((dp=opendir(dir)) == NULL) {
  22.                 fprintf(stderr,"cannot open directory:%s\n",dir);
  23.                 return;
  24.         }
  25.         chdir(dir);
  26.         while((entry=readdir(dp)) != NULL) {
  27.                 lstat(entry->d_name,&statbuf);
  28.                 if(S_ISDIR(statbuf.st_mode)) {   [color=red]/*S_ISDIR这个函数有什么功能*/[/color]
  29.                         if(strcmp(".",entry->d_name) == 0 ||
  30.                                 strcmp("..",entry->d_name) == 0)
  31.                                 continue;
  32.                         printf("%*s%s/\n",depth," ",entry->d_name);
  33.                         printdir(entry->d_name,depth+4);
  34.                 }
  35.                 else
  36.                         printf("%*s%s\n",depth," ",entry->d_name);
  37.         }
  38.         chdir("..");
  39.         closedir(dp);
  40. }
复制代码
发表于 2003-11-19 10:04:18 | 显示全部楼层
不要重复发贴子,这样子不好的,请兄弟不要再做了。
 楼主| 发表于 2003-11-19 21:32:00 | 显示全部楼层
抱歉!
因为第一次没贴好(后面又改了回来),所以贴了第二次(因为论坛上的功能我还没有完全熟悉)
发表于 2003-11-19 21:44:57 | 显示全部楼层
S_ISDIR(statbuf.st_mode)判断是否为目录
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表