Is there a C function to get permissions of a file?

13,906

Solution 1

The permission bits can be ascertained using the st_mode field of the struct returned by the stat function. The individual bits can be extracted using the constants S_IRUSR (User Read), S_IWUSR (User Write), S_IRGRP (Group Read) etc.

Example:

struct stat statRes;
if(stat(file, &statRes) < 0)return 1;
mode_t bits = statRes.st_mode;
if((bits & S_IRUSR) == 0){
    //User doesn't have read privilages
}

In terms of passing this to chmod, mode_t is just a typedef of a uint_32, so that should be simple enough.

Solution 2

The permission can be checked using stat(2), extracting information from S_* flags. Here a function using stat(2):

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>


int getChmod(const char *path){
    struct stat ret;

    if (stat(path, &ret) == -1) {
        return -1;
    }

    return (ret.st_mode & S_IRUSR)|(ret.st_mode & S_IWUSR)|(ret.st_mode & S_IXUSR)|/*owner*/
        (ret.st_mode & S_IRGRP)|(ret.st_mode & S_IWGRP)|(ret.st_mode & S_IXGRP)|/*group*/
        (ret.st_mode & S_IROTH)|(ret.st_mode & S_IWOTH)|(ret.st_mode & S_IXOTH);/*other*/
}

int main(){

    printf("%0X\n",getChmod("/etc/passwd"));

    return 0;
}
Share:
13,906

Related videos on Youtube

trawww
Author by

trawww

Updated on September 15, 2022

Comments

  • trawww
    trawww about 1 year

    I am writing a c program to be run on UNIX, and attempting to utilize the chmod command. After consulting the man pages, i know that chmod needs two parameters. first is the permission bits, second is the file to be changed. I want to take the bitwise OR of the file's current permission bits and those entered by the user, and feed that to chmod() to change the file's permissions.

    I found the access() function, but am having trouble figuring out how to use it to get the permission bits of the specified file.

    What i have right now is:

    octalPermissionString = strtol(argv[1], (char**)NULL, 8);
    if(chmod(argv[2], octalPermissionString | (access(argv[2], octalPermissionString)) < 0) {
                        fprintf(stderr, "Permissions of file %s were not changed.\n");
                    }
    

    where:

    argv[1] contains a string of a three digit decimal number entered by the user to be converted to octal and then be used as the permission bits to be bitwise OR'ed,

    argv[2] is the the file to have it's permission changed, also specified by the user.

    octalPermissionString is a long to hold the octal conversion of user input.

    Is/Are there any other functions that can return the permission bits of a given file?

    EDIT: missing close parenthesis

    • David Heffernan
      David Heffernan almost 10 years
      There is nothing in C to do this. It's implementation specific.
    • Sinkingpoint
      Sinkingpoint almost 10 years
      The st_mode field of stat() might help...
  • trawww
    trawww almost 10 years
    Just tried it out on my program, and everything seems to be working as I want it. after calling stat and retrieving the bits, I call: chmod(argv[2], bits | octalPermissionBits); and I get the output I was looking for. Thank you!