Created
April 13, 2013 07:36
-
-
Save selfboot/5377484 to your computer and use it in GitHub Desktop.
判断stdout是否被重定向..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
重定向: | |
zsh➜ $ ./file.o | |
before redirect... | |
zsh➜ $ cat demo | |
after redirect... | |
Redirected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Description: | |
* Created: 2013年04月13日 14时44分50秒 | |
*/ | |
#include <stdio.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
int main(){ | |
printf("before redirect...\n"); | |
struct stat buf, newbuf; | |
fstat(STDOUT_FILENO, &buf); | |
FILE *fp = freopen("demo", "w", stdout); | |
printf("after redirect...\n"); | |
fstat(STDOUT_FILENO, &newbuf); | |
if(buf.st_ino != newbuf.st_ino){ | |
printf("Redirected\n"); | |
} | |
else{ | |
printf("no redirect...\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment