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 ...