LinuxSir.cn,穿越时空的Linuxsir!

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

创建深目录的程序,帮忙看看错在哪了

[复制链接]
发表于 2006-10-28 22:40:42 | 显示全部楼层 |阅读模式
在我的机子上的输出结果:
$ ./a.out
................................
getcwd failed , size = 7397: File name too long
getcwd failed , size = 7497: File name too long
getcwd failed , size = 7597: File name too long
getcwd failed , size = 7697: File name too long
getcwd failed , size = 7797: File name too long
getcwd failed , size = 7897: File name too long
getcwd failed , size = 7997: File name too long
getcwd failed , size = 8097: File name too long
giving up






  1. #include        <sys/types.h>
  2. #include        <sys/stat.h>
  3. #include        <fcntl.h>
  4. #include        "ourhdr.h"$

  5. #define        DEPTH        100
  6. #define        MYHOME        "/bak/cai"
  7. #define        NAME        "alonglonglonglonglonglonglonglonglonglongname"
  8. #define        MAXSZ        8192

  9. int        main(void)
  10. {
  11.         int        i, size;
  12.         char        *path;

  13.         if ( chdir(MYHOME) < 0)
  14.                 err_sys("chdir failed");

  15.         for ( i = 0; i < DEPTH; i++ ) {
  16.                 if ( mkdir(NAME, DIR_MODE) < 0)
  17.                         err_sys("mkdir failed, i = %d", i);
  18.                 if ( chdir(NAME) < 0)
  19.                         err_sys("chdir failed, i = %d", i);
  20.         }


  21.         if ( creat ("afile", FILE_MODE) < 0)
  22.                 err_sys("creat failed");

  23.         path = path_alloc(&size);
  24.         for ( ; ; ) {
  25.                 if ( getcwd(path, size) != NULL)
  26.                         break;
  27.                 else {
  28.                         err_ret("getcwd failed , size = %d", size);
  29.                         size += 100;
  30.                         if ( size > MAXSZ)
  31.                                 err_quit("giving up");
  32.                         if ( (path = realloc(path, size) ) == NULL)
  33.                                 err_sys("realloc error");
  34.                 }
  35.         }

  36.         printf("length = %d\n%s\n", strlen(path), path);


  37. exit(0);
  38. }
复制代码
发表于 2006-10-29 01:17:48 | 显示全部楼层
應該是getcwd處理不了太長的CWD:
  1.     char *buf = getcwd(NULL, 0);                                                
  2.                                                                                 
  3.     if (buf == NULL)                                                            
  4.         perror("getcwd");   
复制代码

getcwd: File name too long
回复 支持 反对

使用道具 举报

发表于 2006-10-29 01:30:16 | 显示全部楼层
pathchk -p /.../alonglonglonglonglonglonglonglonglonglongname
pathchk: limit 255 exceeded by length 4095 of file name
回复 支持 反对

使用道具 举报

发表于 2006-10-29 04:49:02 | 显示全部楼层
perl -e 'while(1){mkdir "A";chdir "A";}'
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-10-29 10:00:47 | 显示全部楼层
楼上两位,能不能解释一下下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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