UNIX & GNU/Linux - System calls - Using open()

The open() system call function is used to open a file.
We have to pass the pathname of the file we want to open, then a flag in the second parameter. This second one has to be a flag among the following ones:
O_RDONLY, O_WRONLY or O_RDWR.

An important thing is that this function will always return a non negative integer on success.
This return value will be used by others functions, such as read() or write() for example, as a file descriptor.
As all system calls, if the function returns -1, it will mean that an error has occurred. Errors are POSIX standard.
The value 0, 1 and 2 are already reserved by the system, so if we open a new file that exists, its integer value will be 3 or more.
Let's see why.
Write it successively on your favourite terminal:
ls -la /dev/ stdin
ls -la /dev/stdout
ls -la /dev/stderr

You will see the following links appear:
/dev/stdin -> /proc/self/fd/0
/dev/stdout -> /proc/self/fd/1
/dev/stderr -> /proc/self/fd/2

Indeed:
0 means that we want something be written with a standard input, typically the keyboard.
1 means that we want something be displayed with a standard output, typically the screen.
And 2 is used to capture the error message output of a file and to redirect this output into the input of another file.

Do not forget to close this file descriptor with the close() function!

Let's take an example of the open() system call function with the flag O_RDONLY:

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

int     main()
{
  int	fd;

  fd = open("myFile.txt", O_RDONLY);
  printf("fd = %d\n", fd);
  close(fd);
  return (0);
}

Of course, it returns -1 because this file does not exist!
Let's create one with the open() function, we will use the O_CREAT in addition to the O_RDONLY flag:

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

int     main()
{
  int	fd;

  fd = open("myFile.txt", O_RDONLY | O_CREAT);
  printf("fd = %d\n", fd);
  close(fd);
  return (0);
}

But that's not enough.
Indeed, if you look a the rights of your new file:

$ ls -la myFile.txt

you will see something like that:

--wsr-S---

Let's remove this file:

$ rm myFile.txt

And let's add a thirth parameter to change these rights:

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

int     main()
{
  int	fd;

  fd = open("myFile.txt", O_RDONLY | O_CREAT, S_IRWXU);
  printf("fd = %d\n", fd);
  close(fd);
  return (0);
}

Rights are now:

-rwx------

Let's see a last example of this stderr (file descriptor with the number 2):

$ hello > &sorry

This command line will create the sorry file with the error message output. Just open this file and you will see something like that:

// sorry
hello: Command not found.

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.