I am writing a c program to run on UNIX and am trying to use the chmod command. After consulting the man pages, I know that chmod needs two parameters. first the permission bit, the second is the file that needs to be changed. I want to take the bitwise OR of the current file permission bits and those entered by the user, and pass this to chmod () to change the file permissions.
I found a function access()
, but it's hard for me to figure out how to use it to get the permission bit of the specified file.
What do I mean now:
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 three-digit decimal number string entered by the user to convert to octal, and then used as permission bits for bitwise OR'ed,
argv [2] is the file that changed its resolution, also specified by the user.
octalPermissionString holds the octal conversion of user input for a long time.
Are there / Are there any other functions that can return a permission bit for a given file?
EDIT: missing closing bracket
source
share