The fcntl() function performs various actions on open descriptors. Its syntax is:
int fcntl(int descriptor, int command, ...)
read Return value :
-1 and then fcntl() failed. The global variable errno is set to indicate an error.
this code:
#include <sys/types.h> #include <unistd.h> #include <fcntl.h> int main(){ int flags; if((flags = fcntl(-1,F_GETFL,0)) < 0){ perror("fcntl: "); } printf("\n %d\n", flags); }
:
~$ gcc xx.c ~$ ./a.out fcntl: : Bad file descriptor -1
Note that the printed value of flags is -1 , which indicates a successful call to fcntl(-1,F_GETFL,0); because -1 not a valid file descriptor. And valid file descriptors start at 0 . (this is what perror() prints the error message Bad file descriptor , EBADF)
note: I am running this code on a Linux system.
Edit :
F_GETFL is for the GET flags command in fcntl ().
source share