I am trying to understand what this line of code means:
flags = fcntl(-1,F_GETFL,0);
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.
The fcntl() function provides for control over open files. The fildes argument is a file descriptor. The available values for cmd are defined in the header <fcntl.
If O_NDELAY or O_NONBLOCK is set: The open will return without waiting for the device to be ready or available; subsequent behavior of the device is device specific. If O_NDELAY and O_NONBLOCK are clear: The open will block until the device is ready or available.
Macro: int O_NONBLOCK. This prevents open from blocking for a “long time” to open the file. This is only meaningful for some kinds of files, usually devices such as serial ports; when it is not meaningful, it is harmless and ignored.
The usual reason for calling fcntl()
with the F_GETFL
flag is to modify the flags and set them with fcntl()
and F_SETFL
; the alternative reason for calling fcntl()
with F_GETFL
is to find out the characteristics of the file descriptor. You can find the information about which flags can be manipulated by reading (rather carefully) the information about <fcntl.h>
. The flags include:
Plus (POSIX 2008) O_ACCMODE which can then be used to distinguish O_RDONLY
, O_RDWR
, and O_WRONLY
, if I'm reading the referenced pages correctly.
However, it makes no sense whatsoever to call fcntl()
with a definitively invalid file descriptor such as -1
. All that happens is that the function returns -1
indicating failure and sets errno
to EBADF
(bad file descriptor).
Assuming we are talking about the function described by man 2 fcntl
:
flags = fcntl(-1,F_GETFL,0);
tries to perform some action on an invalid file descriptor (-1
) and therefore will never do anything else but returning -1
and set errno
to EBADF
.
I'd say you can savely replace this line by:
flags = -1; errno = EBADF;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With