C Program For File Permissions

Accessing files can include a lot of permissions and a complex sequence of codes. Don’t worry, we’re here to help. This post deals with a c program for file permissions which would help you in checking and analysing file access permissions. 

Let’s get started! 

What are File Access Permissions?

File permissions are permissions that manage what users are permitted to use which files by granting them permission. Hence, File permissions are important for security purposes of the system files. They keep the crucial files protected from potential hackers and their attacks. 

Their importance increases for internal files. File permissions extend their support by providing least access to these files. Thus, they reduce the risk of attacks. 

Types of File Permissions:

For keeping your files safe, the computer system has differentiated three types of permissions. These include:

  1. Read type permissions: As the name suggests, the read permission grants readability to the files. The user accessing a file which has read access permission, can only read the file because the system does not permit any other action.
  2. Write type permissions: This increases the access control of a user over the file. For write labeled files, the user can write, modify, correct or even remove the contents of the file. If someone tries to access these types of files, they will exercise some high level of accessibility. In this way, the user can alter the file the way he or she wishes without any restrictions 
  3. Execute type permissions: If a user has execute type permission, they have high level of accessibility and control over the said file. The execute file access permission allows the user to run and execute the file as a program.

How to find these Permissions?

In order to determine the permissions granted to various files in our system, we need to first find where the permissions are mentioned. For this, inode can help. inode is a constituent of a file which contains file size, file access modes and types and other information. The file mode possessed by this inode helps us to know the status of accessibility of the files by a user. 

Usually these file permissions are visible in the form of long directory listing. For example, -rw–r–r–. 

There are other symbols in the inode form of course. ‘L’ for symbolic link, ‘d’ for directory or ‘-’ for a regular file. There are rwx for read, write and execute permissions. In the listing, the first set is for the file owner, second is for the user and the third is for the users not belonging to a specific group. 

Note: A file owner has all the access to the  file. It is the user who wants to access the file from outside who needs these permissions. 

The command chmod is used by the file owner if he or she wishes to change the permissions. If we go for the C language, the file permissions are under the jurisdiction of stat() function. It easily returns the file details and information stored in its inode.

Now  we know how to find the file permissions for a particular file. Thus, we just need to put our coding brains to use and come up with something which is easily understandable by the user. 

Making inode form readable by the user:

The information of file permission which the inode forms returns is not easily interpreted by someone who wants to know about it. To make it less complex, we use macros and defined constants present in the sys/stat.h header file which also contains the stat() function.

Macros in the stat.h header files are useful for various purposes. For example, while determining a file’s type, we use st_mode macro and its members. 

How are we going to retrieve the permission in our code?

The macro st_mode has a member which can check for the regular type of file. The macro S_ISREG returns true if the file is of regular type. The statement if(S_ISREG(fs.st_mode)) can check for our file’s type and can be used in our code. 

Next, we will make AND comparisons with the values in the st_mode member. This way, we will have the information regarding the owner, group and we can further determine permissions for read, write and execute. 

We will now use S_IROTH defined constant in which R is for ‘read’ and OTH is for ‘other’ group. If we check if the file comes under ‘other’ group, we can say that the file is only a read type of file. Likewise, we will include several macros to classify the file types and in the end we will be able to determine the permission. 

C program for file permissions

//C Program to check file access permissions
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main()
{
const char filename[] = "Geekstocode.txt";
struct stat fs;
int r;
printf("Obtaining permission mode for '%s':\n",filename);
r = stat(filename,&fs);
if( r==-1 )
{
fprintf(stderr,"File error\n");
exit(1);
}
// file permissions belong to the st_mode member
// The S_ISREG() macro tests for regular files
if( S_ISREG(fs.st_mode) )
puts("Regular file");
else
puts("Not a regular file");
printf("Owner permissions: ");
if( fs.st_mode & S_IRUSR )
printf("read ");
if( fs.st_mode & S_IWUSR )
printf("write ");
if( fs.st_mode & S_IXUSR )
printf("execute");
putchar('\n');
printf("Group permissions: ");
if( fs.st_mode & S_IRGRP )
printf("read ");
if( fs.st_mode & S_IWGRP )
printf("write ");
if( fs.st_mode & S_IXGRP )
printf("execute");
putchar('\n');
printf("Others permissions: ");
if( fs.st_mode & S_IROTH )
printf("read ");
if( fs.st_mode & S_IWOTH )
printf("write ");
if( fs.st_mode & S_IXOTH )
printf("execute");
putchar('\n');
return(0);
}

Output:

After running the above code for the file Geekstocode.txt, we have obtained the following results:

Obtaining permission mode for 'Geekstocode.txt':
Regular file
Owner permissions: read write
Group permissions: read
Others permissions: read


As you can see, this output clearly shows the permissions associated with the file. Now we have obtained the file in readable form and we can judge the accessibility of the file.

Program Explanation:

For further explanation and understanding follow these steps which are very simple replica of what the code did :

  1. We assign the file required for checking the access.
  2. The file mode is retrieved in the next block.
  3. We check if the file is regular through the macro ISREG.
  4. Different macros are used in the code for particular type of groups of the files.
  5. We segregate and put the files into different access permission categories using results of these macros.
  6. Result is displayed.

And, we have successfully completed c program for file permissions.

Ending note :

Checking the file access permissions before trying to access the file is necessary. As, It could save you your time instead of going through the file and realising it cannot be accessed.

Knowing the limits of accessing the files beforehand will not only benefit the user but also will provide clarity. It can also serve as a medium of alert for you when you need to contact the owner of the file requesting the access.

Hope this code benefited you and you were able to understand the detailed walk through of the procedure. 

Looking forward to your views related to the C program for file permissions

Leave a Comment

Your email address will not be published. Required fields are marked *