C Programming

C fcntl function usage

As the name indicates that fcntl is abbreviated as ‘file’ control. It means it is based on the file handling process. The fcntl is a system call. It allows the program to place a read or a write lock. This function can be used to amend the file properties that are either opened already or can be opened through any action applied to it. It is a versatile function and is used to modify files in many ways like open, read and write, etc. This article is about the control functions on files.

Fcntl in Linux

In the Linux operating system, the fcntl call is done through the descriptors. For instance, a read lock is placed on a readable file descriptor, and a similar case is for the write lock. A file descriptor represents the file number that is opened. It is convenient for the program to remember which file it is working on. When we open a file, the number that is not assigned already and is free is given to the file in the descriptor table of the processes file. And in the case of closing a file, that assigned number is removed from the process’s descriptor table.

Syntax

#include <fcntl.h>

int fcntl (integer descriptor, integer cmd)

Firstly we define the fcntl library to make the process accomplish easily. The function call mainly contains two arguments in the parameter. One is the descriptor, as defined above; it defines the file on which the command of control is to be applied. In other words, on which the attributes need to be changed. The second one is the command that is applied to the specified descriptors.

How fcntl changes file properties

Fcntl function is used for five different purposes, mainly including duplication, setting flags, etc.; each one is described here in detail.

Cmd = F_DUPFD

Duplicate the descriptor of files. The new duplicated value is returned to the function. This value is the lowest one that is not opened already or allotted to any other descriptor. It is always taken as an integer, and the value is always greater than the third argument. Furthermore, the duplicated value has its file descriptor flags. The new descriptor has the same entry in the table as the original descriptor has.

Cmd = GETFD

This function has the specialty to return the flags of the descriptor as the function value. As the name indicates, we get the flag after setting it.

Cmd = SETFD

Like getting the flag, this function is used to set the flag of the descriptor. The program set the flag to either 0, do not close on exec, or on 1, to close on exec.

Cmd = F_GETFL

This function returns the flags for file status as the value of a function. When the status for the flag is described as an open flag, then we describe the status flags.

Cmd = F_SETFL

It sets the status flag to the file. As GETFL is used to return the file status.

Cmd = F_GETOWN

This function is related to the process identity as it returns the process ID and the process group id.

Cmd = F_SETOWN

This function tends to create and set process id or group process id.

The return value from the fcntl depends on the command used. If commands encounter an error, it returns -1. If every function undergoes no issue, then any other value except -1 is returned. Whereas in the case of F_GETOWN, the id returned may be a positive value or a negative value.

Now we will add some elementary examples here. To implement the codes of fcntl, you need to have a text editor and Linux terminal to get the resultant value.

Example 1

Consider an example in which we created and then wrote a new line in a sample text file. This example will not use the involvement of fcntl as a function. This function’s features will be implemented in the code only by using a library.

#include<fcntl.h>

We will write a string to the code, so we need to use the string library. We have taken an array of file descriptors here. Furthermore, a character array has been taken that is directly initialized with a string of some characters. With the help of the file descriptor, we will use some file operations of the file flag status like reading and writing in the file, etc. If the file is already created, then you need to open it and write the string in it.

Fd[0] = open("sample.txt", O_RDWR);

This statement will open the file named ‘sample.txt’ by using the O_RDWR flag. Now to add the string defines earlier, it will be entered in the file.

Write(fd[0], Bf1, strlen (buf1));

Through the read-option, the string will be displayed on the execution of the code from the file. Both the file descriptors are closed at the end.

After saving the file, we will use a GCC compiler for the execution of the file.

$ gcc –o file file.c

$ ./file

When the code is executed, you will see that a string is displayed that we have written in the code to a file. This statement has been fetched from the file. When you go to the files in Ubuntu, you will see the sample.txt file. You will notice that the string is written in the file through the code on opening the file.

Example 2

This is an example of the F_GETFL command. It returns the file status flag as the function value. First, the file will be opened; if it is not already created, then O_CREAT will create the file; first, all these flag status functions are stated below to make it easy to understand.

O_RDONLY: This feature opens the file for the reading purpose only.

O_WRONLY: Deals with the writing purpose only.

O_RDWR: It is for the reading and writing process as well.

O_APPEND: Appends on each writing function on the current file.

These are the main flag that is used in the example. Coming back to the example, if the returned value by opening the file is less than 0, then an error message is displayed.

After opening, you need to get the file; if the status of the file is -1, an error message is displayed; otherwise, the file status will be received. Now with the help of access mode, we will get the flag of the file descriptor. All the options will be checked through the if-else statement. The option according to the file status will have opted respectively. In the end, if the file descriptor status is greater than 1, it means the file is closed now.

Compile the code; you will see that all the statements are displayed according to the fd status either the file is opened or closed.

Conclusion

The article ‘C: Fcntl function usage’ contains the features of the fcntl function. The file descriptor plays a vital role in the file handling process regarding any feature. We have used some flag examples, too, in the case of reading and writing to the file. Both examples explained here will be helpful for you in the utilization of Fcntl in the C programming language.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.