免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1230 | 回复: 3
打印 上一主题 下一主题

apue的fcntl里面有个demo,不过不是很懂,各位请指教一下 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-29 15:55 |只看该作者 |倒序浏览
demo是判断一个文件的file status,但是对里面的example data不是很明白,能告诉我原理吗?谢谢!
     $ ./a.out 0 < /dev/tty
     read only
     $ ./a.out 1 > temp.foo
     $ cat temp.foo
     write only
     $ ./a.out 2 2>>temp.foo
     write only, append
     $ ./a.out 5 5<>temp.foo
     read write

代码如下
#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);
}

ps:我刚学linux,但是知道< >是重定向,但是对怎么会产生这个结果不是很明白.

论坛徽章:
0
2 [报告]
发表于 2006-11-29 16:21 |只看该作者
有没有人知道啊,在线等啊~~~

论坛徽章:
0
3 [报告]
发表于 2006-11-29 20:07 |只看该作者
看书啦,每个 example 都对应相关章节的一些内容

论坛徽章:
0
4 [报告]
发表于 2006-11-29 20:35 |只看该作者
谢谢,不过我看过了,并没有涉及对于重定向相关的东西,也没有解释,所以我想到论坛上问问,貌似没有多少人啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP