O_ACCMODE

O_ACCMODE<0003>:读写文件操作时,用于取出flag的低2位

O_RDONLY<00>:只读打开

O_WRONLY<01>:只写打开

O_RDWR<02>:读写打开

可以很好的理解下面的程序:

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 }

以上代码选自《Unix高级环境编程》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值