apue3.14节的一个例子

#include "apue.h"
#include <fcntl.h>

int
main(int argc, char *argv[])
{
	int		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]));

	switch (val & O_ACCMODE) {
	case O_RDONLY:
		printf("read only");
		break;

	case O_WRONLY:
		printf("write only");
		break;

	case O_RDWR:
		printf("read write");
		break;

	default:
		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_C_SOURCE) && defined(O_FSYNC)
	if (val & O_FSYNC)
		printf(", synchronous writes");
#endif
	putchar('\n');
	exit(0);
}

  关于这里的

./a.out 5 5<>temp.foo 
执行 ./a.out 5 命令, 并通过 5<>temp.foo 将temp.foo 文件以可读写方式打开作为 a.out 命令的 5 号文件描述符。

./a.out 2 2>>temp.foo
执行 ./a.out 2 命令, 通过 2>>temp.foo 将命令的标准错误输出添加到 temp.foo 文件的尾部。

posted on 2012-11-01 20:14  追寻前人的脚步  阅读(289)  评论(0编辑  收藏  举报

导航