O_ACCMODE <0003>: When reading and writing files, it is used to retrieve the low 2-bit flag
O_RDONLY <00>: Read-only
O_WRONLY <01>: Write-only open
O_RDWR <02>: read/write enabled
The following program can be well understood:
1 # include <sys/types. h>
2 # include <fcntl. h>
3 # include "ourhdr. h"
4
5 int
6 main (int argc, char * argv [])
7 {
8 int accmode, val;
9 if (argc! = 2)
10 err_quit ("usage: a. out <descriptor #> ");
11
12 if (val = fcntl (atoi (argv [1]), F_GETFL, 0) <0)
13 err_sys ("fcntl error for fd % d", atoi (argv [1]);
14
15 accmode = val & O_ACCMODE;
16 if (accmode = O_RDONLY) printf ("read only ");
17 else if (accmode = O_WRONLY) printf ("write only ");
18 else if (accmode = O_RDWR) printf ("read write ");
19 else err_dump ("unknown access mode ");
20
21 if (val & O_APPEND) printf (", append ");
22 if (val & O_NONBLOCK) printf (", nonblocking ");
23 # if! Defined (_ POSIX_SOURCE) & defined (O_SYNC)
If (val & O_SYNC) printf (", synchronous writes ");
25 # endif
26 putchar ('\ n ');
27 exit (0 );
28}
The above code is selected from Unix Advanced environment programming.
O_ACCMODE