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

</>
Copy
#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:

  1. We include the headers <stdio.h> for input/output and <sys/stat.h> for file permission functions.
  2. The variable filename stores the name of the file whose permissions are to be modified.
  3. The chmod() function is called with the desired mode S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH which sets the file permissions to rw-r--r--.
  4. If chmod() fails, perror() prints an error message and the program exits with a non-zero status.
  5. 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

</>
Copy
#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:

  1. Headers <stdio.h>, <sys/stat.h>, <fcntl.h>, and <unistd.h> are included for standard I/O, file status, file control, and POSIX functions.
  2. The variable filename holds the name of the file to be modified.
  3. The open() function opens the file in read-write mode and returns a file descriptor stored in fd.
  4. The fchmod() function changes the permissions of the file associated with fd to S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH, setting permissions to rwxr-xr-x.
  5. Error handling is done using perror() if either open() or fchmod() fails, and the file descriptor is closed using close().

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

</>
Copy
#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:

  1. The headers <stdio.h> and <stdlib.h> are included for standard I/O and system functions.
  2. The variable filename stores the name of the file whose permissions are to be modified.
  3. A command string is constructed using snprintf() to form the shell command chmod 644 filename, which sets the permissions to rw-r--r--.
  4. The system() function executes the command in the shell, and error handling is performed using perror() if the command fails.
  5. 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:

  1. Using chmod(): Directly changes file permissions by providing the file path and desired mode.
  2. Using fchmod(): Modifies file permissions via an open file descriptor, useful when the file is already opened.
  3. Using system(): Executes a shell command to change file permissions, leveraging the existing chmod command.