免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1980 | 回复: 4
打印 上一主题 下一主题

[C] 请帮我分析一下为啥stat()时报错了,实在分析不出来 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-03-12 23:23 |只看该作者 |倒序浏览
大家好,我最近在学习linux下的编程,照着书上的例子练习写ls 命令,
这个版本无法支持目录参数,会莫名其妙的让stat()函数失败,实在分析不出来原因了,
恳请指点,谢谢。
  1. song@song-tp:~/learn_c/practice/uulp$ cat ls3.c
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<sys/types.h>
  5. #include<dirent.h>
  6. #include<sys/stat.h>
  7. #include<stdlib.h>
  8. #include<errno.h>
  9. #include<pwd.h>
  10. #include<grp.h>
  11. #include<time.h>
  12. #include<unistd.h>

  13. #define RWX_LEN 11
  14. #define MAX_RECORD 1024  /* 最大文件记录数,应该用平台提供的限制来设置此值 */
  15. #define MAX_LEN 1024

  16. void do_ls2(char *);
  17. void do_stat(char *, struct stat *);

  18. void perm(mode_t, char *);
  19. char *uid_to_name(uid_t);
  20. char *gid_to_name(gid_t);
  21. char *lastime(time_t);

  22. int main(int argc, char *argv[])
  23. {
  24.     char name[MAX_LEN];
  25.     int i;

  26.     if(argc == 1)
  27.     {
  28.         do_ls2(".");
  29.     }
  30.     else
  31.     {
  32.         for(i = 1; i < argc; i++)
  33.         {
  34.             ++argv;
  35.             fprintf(stdout, "path:%s\n", *argv);
  36.             do_ls2(*argv);
  37.         }
  38.         
  39.     }
  40.     return 0;
  41. }

  42. void do_ls2(char *dirname)
  43. {
  44.     DIR *dirp;
  45.     struct dirent *direntp;
  46.     struct stat statbuf[1];

  47.     if((dirp = opendir(dirname)) == NULL)
  48.         fprintf(stderr, "ls:cannot open %s\n", dirname);
  49.     else
  50.     {
  51.         while((direntp = readdir(dirp)) != NULL)
  52.         {
  53.             do_stat(direntp->d_name, statbuf);
  54.         }
  55.         closedir(dirp);
  56.     }
  57. }

  58. void do_stat(char *file, struct stat *buf)
  59. {
  60.     char rwx[RWX_LEN];
  61.     fprintf(stdout, "name:%s\n", file);
  62.     file[strlen(file)] = '\0';

  63.     if(stat(file, buf) == -1)
  64.     {
  65.         perror("stat");
  66.         exit(EXIT_FAILURE);
  67.     }
  68.     else
  69.     {
  70.         perm(buf->st_mode, rwx);
  71.         fprintf(stdout, "%s", rwx);
  72.         fprintf(stdout, "%4d ", (int)buf->st_nlink);
  73.         fprintf(stdout, "%-8s ", uid_to_name(buf->st_uid));
  74.         fprintf(stdout, "%-8s ", gid_to_name(buf->st_gid));
  75.         fprintf(stdout, "%8ld ", buf->st_size);
  76.         fprintf(stdout, "%.12s ", (lastime(buf->st_mtime) +4));
  77.         fprintf(stdout, "%s\n", file);
  78.     }
  79. }


  80. void perm(mode_t mode, char *str)
  81. {
  82.     snprintf(str, RWX_LEN, "%c%c%c%c%c%c%c%c%c%c",
  83.         S_ISREG(mode) ? '-':S_ISDIR(mode) ?'d':S_ISLNK(mode) ? 'l' :'-',
  84.         (mode & S_IRUSR) ? 'r':'-',
  85.         (mode & S_IWUSR) ? 'w':'-',
  86.         (mode & S_IXUSR) ? 'x':'-',
  87.         (mode & S_IRGRP) ? 'r':'-',
  88.         (mode & S_IWGRP) ? 'w':'-',
  89.         (mode & S_IXGRP) ? 'x':'-',
  90.         (mode & S_IROTH) ? 'r':'-',
  91.         (mode & S_IWOTH) ? 'w':'-',
  92.         (mode & S_IXOTH) ? 'x':'-');
  93. }

  94. char *uid_to_name(uid_t uid)
  95. {
  96.     struct passwd *pwd;
  97.     static char numstr[10]; /* 把函数内的局部变量内容传递出去 */
  98.     errno = 0;

  99.     pwd = getpwuid(uid);
  100.     if(pwd == NULL)
  101.     {
  102.         if(errno == 0)
  103.         {
  104.             sprintf(numstr, "%d", uid);
  105.             return numstr;
  106.         }
  107.         else
  108.         {
  109.             perror("getpwuid()");
  110.             exit(EXIT_FAILURE);
  111.         }
  112.     }
  113.     return pwd->pw_name; /* getpwuid()函数是内部分配静态空间,故只需要单纯的传递出去即可 */
  114. }

  115. char *gid_to_name(gid_t gid)
  116. {
  117.     struct group *grp;
  118.     static char numstr[10];
  119.     errno = 0;

  120.     grp = getgrgid(gid);
  121.     if(grp == NULL)
  122.     {
  123.         if(errno == 0)
  124.         {
  125.             sprintf(numstr, "%d", gid);
  126.             return numstr;
  127.         }
  128.         else
  129.         {
  130.             perror("getgrgid()");
  131.             exit(EXIT_FAILURE);
  132.         }
  133.     }
  134.     return grp->gr_name;
  135. }


  136. char *lastime(time_t time)
  137. {
  138.     char *tp;

  139.     tp = ctime(&time);
  140.     if(tp == NULL)
  141.     {
  142.         perror("ctime");
  143.         exit(EXIT_FAILURE);
  144.     }
  145.     return tp;
  146. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2013-03-12 23:25 |只看该作者
本帖最后由 lylesong 于 2013-03-12 23:26 编辑

比如报如下的错误
  1. ./ls test/
  2. path:test/
  3. name:..
  4. drwxrwxr-x  10 song     song         4096 Mar  4 18:01 ..
  5. name:test
  6. drwxrwxr-x   3 song     song         4096 Mar 12 22:44 test
  7. name:cp
  8. -rwxrwxr-x   1 song     song        11453 Mar  5 16:02 cp
  9. name:.
  10. drwxrwxr-x   5 song     song         4096 Mar 12 23:22 .
  11. name:cp2
复制代码
stat: No such file or directory

论坛徽章:
0
3 [报告]
发表于 2013-03-13 09:56 |只看该作者
看看是哪个file
gdb

论坛徽章:
0
4 [报告]
发表于 2013-03-13 10:23 |只看该作者
调用 do_stat的时候,写相对路径或全路径,把dirname弄在前面,否则stat怎么知道你要看哪里

论坛徽章:
4
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11IT运维版块每日发帖之星
日期:2016-08-11 06:20:00IT运维版块每日发帖之星
日期:2016-08-15 06:20:00
5 [报告]
发表于 2013-03-15 17:40 |只看该作者
LS正解。stat文件的时候,没有带上目录名称。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP