0

我正在尝试在文件路径中查找具有“其他 exec”权限的所有子文件夹。

我试图用来strtok(path_str,"/")破坏路径字符串,但是当stat()用于我运行的进程根目录的子目录时,我得到“不是文件或文件夹”错误。

关于如何克服这个错误的任何建议?

4

2 回答 2

1

如果路径是,那么"long/path/to/the/file.txt"您将需要调用stat()、和。如果您不在乎检查这些的顺序,最简单的方法可能是重复使用:"long""long/path""long/path/to""long/path/to/the"strrchr()

char *s;

while (s = strrchr(path, '/'))
{
    *s = 0;
    if (strlen(path) > 0)
        stat(path, &statbuf);
    else
        stat("/", &statbuf);

    /* Do something with statbuf */
}

(特殊情况是用于以 , 开头的路径/来检查根目录本身)。

于 2010-12-30T12:04:25.980 回答
0

我修好了

首先,我从路径中删除了第一个“/”(我不完全理解为什么会这样),然后我将代码更改为 do-while 以在最后访问文件。所以这里是整个代码:

do{
    int retval;
    if (temp_ptr != NULL) //before the first strrchr its null
        *temp_ptr = 0;
    if (*temp_path)
       retval = stat(temp_path, statbuf);
    else
        retval = stat("/", statbuf);
    if (retval < 0){
        perror("stat");
    }
     printf("%s\n",temp_path);

    if(S_ISDIR(statbuf->st_mode)){
        printf("\tis a directory\n");
    }else if(S_ISREG(statbuf->st_mode)){
        printf("\tis a regular file\n");
    }


}   while ((temp_ptr = strrchr(temp_path, '/')));

感谢咖啡馆和所有人的帮助。

于 2011-01-02T09:34:34.793 回答