Search code examples
clinuxpathstat

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 can overcome this error?


Solution

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