如果我们要拒绝用户将打印输出被重定向输出到其他地方,比如文件或者/dev/null,那么我们可以使用isatty函数来判定程序执行是否有被重定向输出。
示例代码如下:
01 | #include<stdio.h> |
02 | #include<unistd.h> |
03 | #include<stdlib.h> |
04 |
05 | int main( int argc, char **argv){ |
06 |
07 | if (!isatty(fileno(stdout))){ |
08 | fprintf (stderr,"you are not a terminal! |
09 | "); |
10 | exit (1); |
11 | } |
12 |
13 | /*********逻辑代码*********/ |
14 |
15 | exit (0); |
16 | } |