Skip to content

Instantly share code, notes, and snippets.

@selfboot
Created April 13, 2013 07:36
Show Gist options
  • Save selfboot/5377484 to your computer and use it in GitHub Desktop.
Save selfboot/5377484 to your computer and use it in GitHub Desktop.
判断stdout是否被重定向..
重定向:
zsh➜ $ ./file.o
before redirect...
zsh➜ $ cat demo
after redirect...
Redirected
/*
* 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