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

发布于 2024-10-09 20:34:19 字数 178 浏览 0 评论 0原文

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

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

关于如何克服这个错误有什么建议吗?

I'm trying to find all subfolders in a file's path that have 'others exec' permission.

I've tried to use strtok(path_str,"/") to break the path string, but when using stat() for the sub-directories of the root of the process I run, I get the "not a file or folder" error.

Any suggestions on how I can overcome this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傻比既视感 2024-10-16 20:34:19

如果路径是 "long/path/to/the/file.txt",那么您需要对 "long" 调用 stat()代码>,<代码>“长/路径”,<代码>“长/路径/到”和<代码>“长/路径/到/该”。如果您不关心检查这些内容的顺序,最简单的方法可能是重复使用 strrchr():(

char *s;

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

    /* Do something with statbuf */
}

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

If the path is "long/path/to/the/file.txt", then you will need to call stat() on "long", "long/path", "long/path/to" and "long/path/to/the". If you don't care in what order you check these, the easiest way is probably to repeatedly use strrchr():

char *s;

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

    /* Do something with statbuf */
}

(The special-casing is for paths beginning with /, to check the root directory itself).

不喜欢何必死缠烂打 2024-10-16 20:34:19

我已经修复了它,

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

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

I've fixed it,

First I removed the first '/' from the path (I'm not fully understand why it so)
Than I Changed the code into do-while to access the file at the end.
So here is the entire code:

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, '/')));

Thank You caf and all for your assistance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文