Introduce old redir program
[lcapit-junk-code.git] / books / apue / fd-flags.c
blobada959b47062de51f502be0f106384236e64a1a9
1 /*
2 * Print flags for specified file-descriptor
3 */
4 #include <stdio.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #include <sys/types.h>
12 int main(int argc, char *argv[])
14 int accmode, val;
16 if (argc != 2) {
17 fprintf(stderr, "usage: a.out <descriptor#>\n");
18 exit(1);
21 if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0) {
22 fprintf(stderr, "fcntl error for fd %d: %s\n", atoi(argv[1]),
23 strerror(errno));
24 exit(1);
27 accmode = val & O_ACCMODE;
28 if (accmode == O_RDONLY)
29 printf("read only");
30 else if (accmode == O_WRONLY)
31 printf("write only");
32 else if (accmode == O_RDWR)
33 printf("read write");
34 else
35 printf("unknow access mode");
37 if (val & O_APPEND)
38 printf(", append");
40 if (val & O_NONBLOCK)
41 printf(", nonblocking");
43 if (val & O_SYNC)
44 printf(", synchornous writes");
46 putchar('\n');
47 return 0;