繁体   English   中英

在Linux下,如何在C的文件路径中找到子文件夹的权限?

[英]How can I find the permissions for sub-folders in file's path in C under Linux?

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

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

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

如果路径是"long/path/to/the/file.txt" ,那么您将需要在"long""long/path""long/path/to""long/path/to/the"上调用stat() "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 */
}

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

我已经解决了

首先,我从路径中删除了第一个“ /”(我不完全理解为什么),然后我将代码更改为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, '/')));

谢谢caf和所有人的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM