home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 316 / libsrc / isatty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  1.1 KB  |  39 lines

  1.  
  2. /* this algorithm is due to Allan Pratt @ Atari.  Thanks Allan! */
  3.  
  4. #include <errno.h>
  5. #include <osbind.h>
  6. #include <file.h>
  7.  
  8. int isatty(handle)
  9. int handle;
  10. {
  11.   int rc;
  12.   long oldloc, seekval;
  13.  
  14.   if (handle < 0)
  15.     return(1);
  16.   if (handle < N_HANDLES)
  17.     if (__handle_stat[handle] != FH_UNKNOWN)
  18.         return(__handle_stat[handle] == FH_ISATTY);
  19.   oldloc = Fseek (0L, handle, L_INCR);    /* seek zero bytes from current loc */
  20.   if (seekval = Fseek (1L, handle, L_INCR))    /* try to seek ahead one byte */
  21.     if ((seekval > 0) || (seekval == EBADARG))    /* out of range... */
  22.       rc = 0;            /* file, not a tty */
  23.     else 
  24.       {
  25.       errno = EBADF;        /* any other error returns "invalid handle" */
  26.                 /* because you can't tell */
  27.       rc = 0;
  28.       }
  29.   else
  30.     rc = 1;            /* yes, tty (Fseek returns 0 only for ttys) */
  31.   Fseek(oldloc, handle, L_SET);        /* seek back to original location */
  32.   if (handle < N_HANDLES)
  33.     if (rc)
  34.         __handle_stat[handle] = FH_ISATTY;
  35.         else
  36.         __handle_stat[handle] = FH_ISAFILE;
  37.   return (rc);            /* return true, false, or error */
  38. }
  39.