程序3-4 对指定的描述符打印文件标志

 1 /*
 2  ============================================================================
 3  Name        : test.c
 4  Author      : blank
 5  Version     :
 6  Copyright   : Your copyright notice
 7  Description : 程序3-4 对指定的描述符打印文件标志
 8  ============================================================================
 9 */
10 
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include "ourhdr.h"
15 
16 int main(int argc, char *argv[])
17 {
18     int val;
19 
20     if (argc != 2){
21         err_quit("usage: ./test <descriptor#>");
22     }
23 
24     if ((val = fcntl(atoi(argv[1]), F_GETFD, 0)) < 0){
25         err_sys("fcntl error for fd: %d\n", atoi(argv[1]));
26     }
27 
28     switch(val & O_ACCMODE){
29     case O_RDONLY:
30         printf("read only");
31         break;
32     case O_WRONLY:
33         printf("write only");
34         break;
35     case O_RDWR:
36         printf("read write");
37         break;
38     default:
39         err_dump("unknown access mode");
40     }
41 
42     if (val & O_APPEND){
43         printf(", append");
44     }
45 
46     if(val & O_NONBLOCK){
47         printf(", nonblock");
48     }
49 #if defined(O_SYNC)
50     if (val & O_SYNC){
51         printf(", synchronous writes");
52     }
53 #endif
54 
55 #if !defined(_POSIX_C_SOURCE) && defined(O_FSYN)
56     if (val & O_FSYN){
57         printf(", synchronous writes.");
58     }
59 #endif
60 
61     putchar('\n');
62     exit(0);
63 }

 

posted @ 2014-04-25 21:51  blankqdb  阅读(260)  评论(0编辑  收藏  举报