int fcntl (int fd,int cmd,...)
The function Fcntl provides a very rich feature. The main dependencies are the various parameters of CMD:
- Copy an existing file descriptor F_dupfd,f_dupfd_cloexec
- Gets the flag for the set file descriptor F_getfd,f_setfd
- Get the settings file status flag F_GETFL,F_SETFL
- Gets the ownership of the set asynchronous IO F_GETOWN,F_SETFL
- Get Set record lock f_getlk,f_setlk,f_setlkw
When cmd= F_GETFL, the function of fcntl is to obtain the file status of FD flag. Unlike anywhere else, the returned value is not one of each of the two symbols that make up the binary. Because O_rdonly,o_wronly,
The O_RDWR is a mutually exclusive state. When processing the return value, use the O_accmode macro to take out the read and write state of the FD file.
The O_rdonly,o_wronly,o_rdwr,o_accmode is printed out separately 0,1,2,3, with 3 to go in the 0,1,2 result is obvious.
DEMO:
1#include <unistd.h>2#include <fcntl.h>3#include <stdlib.h>4#include <stdio.h>5 6 intMainintargcChar**argv)7 {8 if(ARGC! =2)9 return-1;Ten intFL =0; One if(fl = Fcntl (atoi (argv[1]), F_GETFL) <0) { Aprintf"GETFL failed."); -Exit (-1); - } the - Switch(FL &O_accmode) { - Caseo_rdonly: -printf"o_rdonly\n"); + Break; - Caseo_wronly: +printf"o_wronly\n"); A Break; at CaseO_rdwr: -printf"o_rdwr\n"); - Break; - } - - if(FL &o_append) inprintf"o_append\n"); - if(FL &O_nonblock) toprintf"o_nonblock\n"); + if(FL &O_sync) -printf"o_sync\n"); the if(FL &O_fsync) *printf"o_fsync\n"); $ Panax Notoginseng}
Results:
$a. out 0 < 1 .c o_rdonly $a. out 1 > . C $cat 1 .c o_wronly $a. out 1 1 >> 1 .c $cat 1 .c o_wronly o_append $a. out 2 2 <> 1 .c o_rdwr
Using redirection above, 0,1 redirect to file separately. So to FD GETFL is to this file GETFL, the result shows o_accmode feasible.
Fcntl Get file Status flag