C Programming

How to Use Open System Call in C

The system calls are used in the Linux distributions to provide a doorway within the Linux OS and the programs. The Linux operating system uses the Glibc library to support system calls in it while using the C language. There are a lot more ways to use system calls as well. In this article guide, we will be discussing the open system call in the Linux system. The “Open” system call has been used to open the file specified in the path quickly. It let us know about the file descriptor of a user-created file. We have been using Ubuntu 20.04 to get some hands-on “Open” system call.

Install Libraries

The C language requires some additional library packages to be installed as prerequisites. First of all, you need to install the manpages-dev package to check the information regarding system calls. Open the command line terminal from the activity area and execute the below instruction to install manpages.

$ sudo apt install manpages-dev

The output for the completion of installation has been given below.

Now we will check for the open system call using the “man” command as below.

$ man 2 open

The output man page for the “Open” system call has been shown below. You can see the additional information regarding it. Press the “q” button to exit it.

The above syntax shows the first three lines of necessary libraries to be added for system calls. There are three syntaxes available for the open system call. The first syntax shows the” pathname,” which is the name of a file to be opened. The second parameter, “flags,” shows the mode of a file, e.g., read or write. The second syntax can be used when the file doesn’t exist. The parameter mode is used to show the privileges on the file.

Now it’s time to install the GCC compiler for the debugging of C code in the Ubuntu 20.04 Linux distribution. For that, execute the below instruction in the shell to do so.

$ sudo apt install gcc

It requires you to tap “Y” to continue installing the GCC compiler, otherwise hit the “n” button to close the installation. So hit the “y” button and press Enter to continue.

The output for the completion is shown below.

Example 01

Let’s look at the very simple example of an “Open” system call. First, of all open the command shell and create a new text file “test.txt” using the simple touch command as follows:

$ touch test.txt

Add some data to it manually. You can see the data in a file using the below cat command.

$ cat test.txt

Now let’s create a new C file using a nano command in the shell, as shown below.

$ nano new.c

The below GNU nano file will be opened. You have to type the below C language code in it. You can see we have added the necessary library packages for the C code to be executed properly. After that, we have defined the main function to work on an Open system call. We have declared two integer variables. The “n” variable is used to count buffer values, and the “fd” is used for the file descriptor. We have declared the character type buffer array as “buf” having size 50. The open system call has been used to read the content from the file “test.txt” and return it to the file descriptor. The “O_RDONLY” has been used for reading purposes. The next line shows the read system call to collect the 10 bytes from the buffer and return it into the integer n. Also, the write command has been used to write the content or buffer data into file descriptor, which is the output screen In our case right now. Save this file using Ctrl+S and close it using the Ctrl+X command.

#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main()
{
 int n, fd;
 char buf[50];
 fd=open(“test.txt, O_RDONLY);
 n=read(fd , buf, 10);
 write(1 , buf, 10);

Let’s first compile the C code for an open system call using gcc command below.

$ gcc new.c

Now it’s time to check the output for the C code for an open system call. Let’s use the below a.out command in the shell. The output displays the 10 bytes from the content of a file “test.txt”.

$ ./a.out

Let’s take another example to write the contents of one file into another file using an Open system call. To create a new C file using the nano command as below.

$ nano new.c

So take the very same code with a minor change in it. We have defined another file descriptor in the code as “fd1”. All the code is the same except for one additional line. The second last line of the code has used the open system call to create a new file named “target” using the O_CREAT and the mode, write-only as O_WRONLY. 0642 shows the privileges assigned to this file. And the file has been returned to a new file descriptor. The next line has shown a command to write the bytes of content in the buffer according to its file descriptor. Save the file and close it.

Execute the gcc compile command to debug the C language file.

$ gcc new.c

To display the output of C code, try the a.out instruction in the shell as below. There is no output because the output has been redirected to the newly created file “target”.

$ ./a.out

Let’s check the data of a file target using the cat command. The output shows that the file has 20 bytes in it.

$ cat target

Conclusion

In the above tutorial, we explained to you to use an open system call in C language by using the gcc compiler. With the help of different examples explained in this guide will lead you to implement your desired program.

About the author

Aqsa Yasin

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.