开发者

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

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开发者_Python百科 can overcome this error?


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜