Modify File Permissions Programmatically in C
To modify file permissions programmatically in C, you can use functions such as chmod(), fchmod(), and even system() to invoke shell commands. These methods allow you to set the desired permission bits on files by specifying the appropriate mode flags.
Example 1: Using chmod() to Change File Permissions
In this example, we will modify the permissions of a file using the chmod() function. We will set the permissions to rw-r--r-- (read and write for the owner, and read-only for the group and others).
main.c
#include <stdio.h>
#include <sys/stat.h>
int main() {
const char *filename = "example.txt";
// Change permissions to rw-r--r--
if (chmod(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
perror("chmod failed");
return 1;
}
printf("Permissions changed successfully.\n");
return 0;
}
Explanation:
- We include the headers
<stdio.h>for input/output and<sys/stat.h>for file permission functions. - The variable
filenamestores the name of the file whose permissions are to be modified. - The
chmod()function is called with the desired modeS_IRUSR | S_IWUSR | S_IRGRP | S_IROTHwhich sets the file permissions torw-r--r--. - If
chmod()fails,perror()prints an error message and the program exits with a non-zero status. - If successful, a success message is printed to the console.
Output:
Permissions changed successfully.
Example 2: Using fchmod() with a File Descriptor
In this example, we will open a file using open() and then use fchmod() to change its permissions. We will set the permissions to rwxr-xr-x (read, write, execute for owner and read, execute for group and others).
main.c
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
const char *filename = "example2.txt";
int fd = open(filename, O_RDWR);
if (fd == -1) {
perror("open failed");
return 1;
}
// Change permissions to rwxr-xr-x using fchmod()
if (fchmod(fd, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) {
perror("fchmod failed");
close(fd);
return 1;
}
printf("Permissions changed successfully using fchmod().\n");
close(fd);
return 0;
}
Explanation:
- Headers
<stdio.h>,<sys/stat.h>,<fcntl.h>, and<unistd.h>are included for standard I/O, file status, file control, and POSIX functions. - The variable
filenameholds the name of the file to be modified. - The
open()function opens the file in read-write mode and returns a file descriptor stored infd. - The
fchmod()function changes the permissions of the file associated withfdtoS_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH, setting permissions torwxr-xr-x. - Error handling is done using
perror()if eitheropen()orfchmod()fails, and the file descriptor is closed usingclose().
Output:
Permissions changed successfully using fchmod().
Example 3: Using system() to Invoke Shell chmod Command
In this example, we will build and execute a shell command using the system() function to change the permissions of a file to rw-r--r-- (644). This approach leverages the shell’s chmod command.
main.c
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *filename = "example3.txt";
char command[100];
// Build the shell command to change permissions to 644 (rw-r--r--)
snprintf(command, sizeof(command), "chmod 644 %s", filename);
int ret = system(command);
if (ret == -1) {
perror("system call failed");
return 1;
}
printf("File permissions modified using system() call.\n");
return 0;
}
Explanation:
- The headers
<stdio.h>and<stdlib.h>are included for standard I/O and system functions. - The variable
filenamestores the name of the file whose permissions are to be modified. - A command string is constructed using
snprintf()to form the shell commandchmod 644 filename, which sets the permissions torw-r--r--. - The
system()function executes the command in the shell, and error handling is performed usingperror()if the command fails. - If the command executes successfully, a confirmation message is printed.
Output:
File permissions modified using system() call.
Conclusion
In this tutorial, we explored three different methods to modify file permissions in C:
- Using
chmod(): Directly changes file permissions by providing the file path and desired mode. - Using
fchmod(): Modifies file permissions via an open file descriptor, useful when the file is already opened. - Using
system(): Executes a shell command to change file permissions, leveraging the existingchmodcommand.
