SagaciousKJB
November 22nd, 2014, 06:02 PM
How can I use stat() to test if the current working directory has proper write permissions? I tried using some of the example code from the manual page but it didn't work as expected.
switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
They use this switch to print what type of file it is. Couldn't I make a similar switch with st_mode and the S_IRWXU to test which permissions the user has in the folder? So like...
switch (sb.st_mode & S_IRWXU) {
case S_IWUSR: printf("Owner has write permission\n"); break;
.....................................
}
The problem is the switch loop doesn't seem to read the S_IWUSR flag and will just go to the default option. Haven't tired it with group or other permissions yet, seems like I'm doing something wrong.
switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
They use this switch to print what type of file it is. Couldn't I make a similar switch with st_mode and the S_IRWXU to test which permissions the user has in the folder? So like...
switch (sb.st_mode & S_IRWXU) {
case S_IWUSR: printf("Owner has write permission\n"); break;
.....................................
}
The problem is the switch loop doesn't seem to read the S_IWUSR flag and will just go to the default option. Haven't tired it with group or other permissions yet, seems like I'm doing something wrong.