This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Detect if stdin is a terminal or pipe in C/C++/Qt

By: a guest on Jan 2nd, 2012  |  syntax: None  |  size: 2.85 KB  |  views: 61  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <io.h>
  3. ...    
  4. if (isatty(fileno(stdin)))
  5.     printf( "stdin is a terminaln" );
  6. else
  7.     printf( "stdin is a file or a pipen");
  8.        
  9. struct stat stats;
  10. fstat(0, &stats);
  11. if (S_ISCHR(stats.st_mode)) {
  12.     // Looks like a tty, so we're in interactive mode.
  13. } else if (S_ISFIFO(stats.st_mode)) {
  14.     // Looks like a pipe, so we're in non-interactive mode.
  15. }
  16.        
  17. #include <stdio.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <termios.h>
  21. #include <unistd.h>
  22. #include <iostream>
  23. using namespace std;
  24. int main() {
  25.   char tty[L_ctermid+1] = {0};
  26.   ctermid(tty);
  27.   cout << "ID: " << tty << 'n';
  28.   int fd = ::open(tty, O_RDONLY);
  29.   if (fd < 0) perror("Could not open terminal");
  30.   else {
  31.     cout << "Opened terminaln";
  32.     struct termios term;
  33.     int r = tcgetattr(fd, &term);
  34.     if (r < 0) perror("Could not get attributes");
  35.     else cout << "Got attributesn";
  36.   }
  37.   if (isatty(fileno(stdin))) cout << "Is a terminaln";
  38.   else cout << "Is not a terminaln";
  39.   struct stat stats;
  40.   int r = fstat(fileno(stdin), &stats);
  41.   if (r < 0) perror("fstat failed");
  42.   else {
  43.     if (S_ISCHR(stats.st_mode)) cout << "S_ISCHRn";
  44.     else if (S_ISFIFO(stats.st_mode)) cout << "S_ISFIFOn";
  45.     else if (S_ISREG(stats.st_mode)) cout << "S_ISREGn";
  46.     else cout << "unknown stat moden";
  47.   }
  48.   return 0;
  49. }
  50.        
  51. #include <stdio.h>
  52. #include <sys/stat.h>
  53. #include <fcntl.h>
  54. #include <termios.h>
  55. #include <unistd.h>
  56. #include <iostream>
  57. using namespace std;
  58. int main() {
  59.   char tty[L_ctermid+1] = {0};
  60.   ctermid(tty);
  61.   cout << "ID: " << tty << 'n';
  62.   int fd = ::open(tty, O_RDONLY);
  63.   if (fd < 0) perror("Could not open terminal");
  64.   else {
  65.     cout << "Opened terminaln";
  66.     struct termios term;
  67.     int r = tcgetattr(fd, &term);
  68.     if (r < 0) perror("Could not get attributes");
  69.     else cout << "Got attributesn";
  70.   }
  71.   if (isatty(fileno(stdin))) cout << "Is a terminaln";
  72.   else cout << "Is not a terminaln";
  73.   struct stat stats;
  74.   int r = fstat(fileno(stdin), &stats);
  75.   if (r < 0) perror("fstat failed");
  76.   else {
  77.     if (S_ISCHR(stats.st_mode)) cout << "S_ISCHRn";
  78.     else if (S_ISFIFO(stats.st_mode)) cout << "S_ISFIFOn";
  79.     else if (S_ISREG(stats.st_mode)) cout << "S_ISREGn";
  80.     else cout << "unknown stat moden";
  81.   }
  82.   return 0;
  83. }
  84.        
  85. /* Parse input from a file and execute it */
  86.  
  87. int
  88. PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
  89.                      PyCompilerFlags *flags)
  90. {
  91.     if (filename == NULL)
  92.         filename = "???";
  93.     if (Py_FdIsInteractive(fp, filename)) {
  94.         int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
  95.        
  96. /*
  97.  * The file descriptor fd is considered ``interactive'' if either
  98.  *   a) isatty(fd) is TRUE, or
  99.  *   b) the -i flag was given, and the filename associated with
  100.  *      the descriptor is NULL or "<stdin>" or "???".
  101.  */
  102. int
  103. Py_FdIsInteractive(FILE *fp, const char *filename)
  104. {
  105.     if (isatty((int)fileno(fp)))
  106.         return 1;
clone this paste RAW Paste Data