Chinaunix首页 | 论坛 | 博客
  • 博客访问: 690335
  • 博文数量: 214
  • 博客积分: 5015
  • 博客等级: 大校
  • 技术积分: 2285
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-18 17:02
文章分类

全部博文(214)

文章存档

2008年(43)

2007年(171)

我的朋友

分类:

2007-09-06 10:40:03

在unix环境高级编程的第十一章中有一个测试isatty的程序。
 

#include <termios.h>
#include <stdio.h>

int isatty(int fd)
{
    struct termios term;
    return(tcgetattr(fd,&term)!=-1);
}

int main(void)
{
    printf("fd 0: %s\n",isatty(0)?"tty":"not a tty");
    printf("fd 1: %s\n",isatty(1)?"tty":"not a tty");
    printf("fd 2: %s\n",isatty(2)?"tty":"not a tty");
    exit(0);
}

测试程序如下:
$ a.out
fd 0: tty
fd 1: tty
fd 2: tty

$ a.out /dev/null
fd 0: not a tty
fd 1: tty
fd 2: not a tty

起初对第二中测试不理解,后来想到
STDIN_FILENO 0 /* Standard input. */
STDOUT_FILENO 1 /* Standard output. */
STDERR_FILENO 2 /* Standard error output. */
通过重定向,将标准输入改为/etc/passwd,标准出错改为/dev/null,那当然就不是tty了。

阅读(1156) | 评论(0) | 转发(0) |
0

上一篇:php中substr

下一篇:IPC

给主人留下些什么吧!~~