C program to test file permissions in Linux

One way to check the file permissions is using “access” system call.

 int access(const char *pathname, int mode); 

This system call requires a test mode to be passed, R_OK, W_OK, and X_OK test whether the file exists and grants read, write, and execute permissions, respectively.

 $ vim check_file_permissions.c 
#include <stdio.h> // for printf
#include <stdlib.h> // for malloc
#include <string.h> // for strcpy
#include <unistd.h> // for access
 
int main(int argc, char **argv) {
    int result;
    //allocate memory of 512 bytes
    char *filename = (char *)malloc(512);
    if (argc < 2) {
        strcpy(filename, "/usr/bin/adb");
    } else {
        strcpy(filename, argv[1]);
    }
    result = access (filename, R_OK); // R_OK for readable
    if ( result == 0 ) {
        printf("%s is readable\n",filename);    
    } else {
        printf("%s is not readable\n",filename);    
    }
 
    result = access (filename, W_OK); // W_OK for writeable
    if ( result == 0 ) {
        printf("%s is Writeable\n",filename);   
    } else {
        printf("%s is not Writeable\n",filename);   
    }
 
    result = access (filename, X_OK); // X_OK for executable
    if ( result == 0 ) {
        printf("%s is executable\n",filename);  
    } else {
        printf("%s is not executable\n",filename);  
    }
    //free allocated memory
    free(filename);
    return 0;
}
 $ gcc -o check_file_permissions check_file_permissions.c 
$ ./check_file_permissions check_file_permissions.c
check_file_permissions.c is readable
check_file_permissions.c is Writeable
check_file_permissions.c is not executable

Now, lets check the file permissions of a binary file adb which is already installed as part of android utilities.

$ ./check_file_permissions
/usr/bin/adb is readable
/usr/bin/adb is not Writeable
/usr/bin/adb is executable
Hive OS Linux commands for beginners x
Hive OS Linux commands for beginners

1 thought on “C program to test file permissions in Linux”

  1. char *filename = (char *)malloc(512);

    Why 512? If you are going to use a fixed size, just create a fixed size array otherwise using malloc is pointless.

    The most memory efficient way is to use strlen to get length of the filename, add ONE for the terminating NULL character, then use malloc with size of char multiplied by length + 1.

    Reply

Leave a Comment

Sign up to our newsletter!

ADB Commands Android Android Applications Android Build system Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development, Debugging and Performance Tools Development and Build Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia NDK / Middleware / HAL Operating System Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web Design and Development Website Hosting Wordpress Yocto Embedded Linux