Since you use file pointers, you need both isatty() and fileno() :
#include <unistd.h> #include <stdio.h> int main(int argc, char* argv[]) { FILE* fp = stdin; if(isatty(fileno(fp))) { fprintf(stderr, "A nice msg.\n"); exit(1); } /* carry on... */ return 0; }
Actually, this is a long way. In short, file pointers should not be used:
#include <unistd.h> int main(int argc, char* argv[]) { if(isatty(STDIN_FILENO)) { fprintf(stderr, "A nice msg.\n"); exit(1); } /* carry on... */ return 0; }
Several standard Unix programs perform this test to change their behavior. For example, if you configured ls to give you beautiful colors, it will turn off colors if you pass it stdout to another program.
Mike de simon
source share