回文章列表
作者joujou.bbs@ms1.me.nctu.edu.tw (還是我...), 信區: unix
標題input redirection 問題
時間交大機械 BBS 站 (Thu Nov 11 13:00:22 2004)
轉信站: GIBBS!news2.ncku!ccnews.ncku!news.ccns.ncku!news.mksh.phc!news.nsysu!ne


最近在看 advanced programming in unix environment 這本書,
到第三章的時候,看到 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#>");
   else
       printf("argv[1] = %s\n", argv[1]);

   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);
}
===================================================

在執行的結果是
$a.out 0 < /dev/tty
read only

看程式的內容是需要一個參數,可是如果我輸入的參數單單是 0 的話,
$a.out 0
read write
所得的結果並不是 0 這個 file descriptor 的 flag,
這是為什麼呢?為什麼一定得要重新導向才行呢?
--

                                                          是我...UIC

--
※ Origin: 交大機械工廠  ◆ From: adsl-61-56-234-34.NHON.sparqnet.net

文章選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻