LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 852|回复: 1

问一个《unix环境高级编程》上的问题,请指教

[复制链接]
发表于 2006-3-31 21:00:03 | 显示全部楼层 |阅读模式
书上3.13节有这么一段程序:
程序3-4  对于指定的描述符打印文件标志
  #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(_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
    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

程序指定的命令参数是2个
可 a.out 0 < /dev/tty 和a.out 2 2>>temp.foo意思是??参数4个??
a.out 1 > temp.foo的意思我倒明白,是把命令a.out 1的结果放到文件temp.foo里面去。


还有,_POSIX_SOURCE宏到底有什么用处阿??那书上第二章说了,但整个那一章就基本上完全看不明白。。。
发表于 2006-4-2 00:01:58 | 显示全部楼层
可 a.out 0 < /dev/tty 和a.out 2 2>>temp.foo意思是??参数4个??

参数2个,a.out ,0
< /dev/tty : 重定向进程的输入到/dev/tty

2>>temp.foo : 在文件描述符2的输出追加到temp.foo

5<>temp.foo : 在文件描述符5上打开temp.foo
打开的flags参考下面的代码


bash中有关重定向的代码片段:
  1.     672   switch (instruction)
  2.     673     {
  3.     674
  4.     675     case r_output_direction:        /* >foo */
  5.     676     case r_output_force:        /* >| foo */
  6.     677     case r_err_and_out:         /* command &>filename */
  7.     678       temp->flags = O_TRUNC | O_WRONLY | O_CREAT;
  8.     679       break;
  9.     680
  10.     681     case r_appending_to:        /* >>foo */
  11.     682       temp->flags = O_APPEND | O_WRONLY | O_CREAT;
  12.     683       break;
  13.     684
  14.     685     case r_input_direction:     /* <foo */
  15.     686     case r_inputa_direction:        /* foo & makes this. */
  16.     687       temp->flags = O_RDONLY;
  17.     688       break;
  18.     689
  19.     690     case r_input_output:        /* <>foo */
  20.     691       temp->flags = O_RDWR | O_CREAT;
  21.     692       break;
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表