日期:2014-05-16  浏览次数:20653 次

unix环境高级编程中关于指定的描述符打印文件标志
#include <sys/types.h>
#include <fcntl.h>
#include "ourhdr.h"

int main(int argc, char *argv[])
{
int accmode, val;

if (argc != 2)
err_quit("usage: a.out <descriptor#>");

if ( (val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0)
err_sys("fcntl error for fd %d", atoi(argv[1]));

accmode = val & O_ACCMODE;
if (accmode == O_RDONLY) printf("read only");
else if (accmode == O_WRONLY) printf("write only");
else if (accmode == O_RDWR) printf("read write");
else err_dump("unknown access mode");

if (val & O_APPEND) printf(", append");
if (val & O_NONBLOCK) printf(", nonblocking");
#if defined (O_SYNC)
if(val&O_SYNC)
printf(",synchronous writes");
#endif
#if !defined(_POSIX_SOURCE) && defined(O_SYNC)
if (val & O_SYNC) printf(", synchronous writes");
#endif
putchar('\n');
exit(0);
}


1
./a.out 0 < /dev/tty
read only
2
./a.out 1 > temp.foo
cat temp.foo
write only

3
./a.out 2 2>>temp.foo
write only,append
4
./a.out 5 5<> temp.foo
read write

红色的代码是什么意思呢?
1的结果明白,2 3 4 5 的结果是怎么出来的啊?
我就知道 ">" 是命令的重定向 "<"是命令输入,标准文件输入

------解决方案--------------------
O_SYNC 是一个特殊标志

Have each write wait for physical I/O to complete, including I/O necessary to update file attributes modified as a result of the write.
------解决方案--------------------
可能在有些系统中并没有那个特性,所以才有预编译的方式