Check if File has Write Permission in C

To check if a file has write permission in C, you can use several approaches such as using the access() function, the stat() function, or by attempting to open the file in write mode using fopen().

Example 1: Check Write Permission using access()

In this example, we will use the access() function from <unistd.h> with the W_OK flag to check if a file has write permission.

main.c

</>
Copy
#include <stdio.h>
#include <unistd.h>

int main() {
    const char *filename = "example.txt";
    
    // Check if file has write permission using access()
    if (access(filename, W_OK) == 0) {
        printf("File '%s' has write permission.\n", filename);
    } else {
        printf("File '%s' does not have write permission.\n", filename);
    }
    
    return 0;
}

Explanation:

  1. We include <unistd.h> to use the access() function and <stdio.h> for input/output operations.
  2. The constant pointer filename stores the name of the file to check.
  3. The access() function is called with the file name and W_OK flag. It returns 0 if the file is writable.
  4. An if statement checks the return value of access() to print whether the file has write permission or not.

Output:

File 'example.txt' has write permission.

Example 2: Check Write Permission using stat()

In this example, we will use the stat() function from <sys/stat.h> to retrieve file metadata and then check the permission bits to see if the file is writable by its owner.

main.c

</>
Copy
#include <stdio.h>
#include <sys/stat.h>

int main() {
    const char *filename = "example.txt";
    struct stat fileStat;
    
    // Retrieve file status using stat()
    if (stat(filename, &fileStat) == -1) {
        perror("stat");
        return 1;
    }
    
    // Check if the file is writable by the owner using permission bits
    if (fileStat.st_mode & S_IWUSR) {
        printf("File '%s' is writable by the owner.\n", filename);
    } else {
        printf("File '%s' is not writable by the owner.\n", filename);
    }
    
    return 0;
}

Explanation:

  1. We include <sys/stat.h> for the stat() function and <stdio.h> for I/O operations.
  2. The filename constant holds the name of the file, and a struct stat variable fileStat is declared to store file information.
  3. The stat() function fills the fileStat structure with the file’s metadata; if it fails, an error is reported using perror().
  4. The bitwise AND operation checks if the S_IWUSR flag is set in fileStat.st_mode, indicating the file is writable by the owner.

Output:

File 'example.txt' is writable by the owner.

Example 3: Check Write Permission by Attempting to Open File

In this example, we will try opening the file in append mode using fopen(). Successfully opening the file indicates that it has write permission.

main.c

</>
Copy
#include <stdio.h>

int main() {
    const char *filename = "example.txt";
    
    // Attempt to open the file in append mode to check write permission
    FILE *file = fopen(filename, "a");
    if (file != NULL) {
        printf("File '%s' can be opened for writing.\n", filename);
        fclose(file);
    } else {
        printf("File '%s' cannot be opened for writing.\n", filename);
    }
    
    return 0;
}

Explanation:

  1. We include <stdio.h> for file input/output operations.
  2. The constant filename holds the name of the file to check.
  3. The fopen() function is used with the mode "a" (append) which requires write permission. If the file opens successfully, it indicates write access.
  4. An if statement checks if the file pointer is not NULL; if true, the file is writable, otherwise it is not.

Output:

File 'example.txt' can be opened for writing.

Conclusion

In this tutorial, we explored multiple methods to check if a file has write permission in C. We learned how to use the access() function for a straightforward check, the stat() function to inspect file permission bits, and fopen() to verify writability by attempting to open the file in append mode.