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

StackOverflow https://stackoverflow.com/questions/4561082

  •  14-10-2019
  •  | 
  •  

سؤال

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?

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top