home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / libsrc87 / isatty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  1.4 KB  |  51 lines

  1. /* from the original GCC TOS library by jrd */
  2. /* this algorithm is due to Allan Pratt @ Atari.  Thanks Allan! */
  3.  
  4. #include <errno.h>
  5. #include <osbind.h>
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <device.h>
  10.  
  11. int isatty(fd)
  12. int fd;
  13. {
  14.   int rc;
  15.   long oldloc, seekval;
  16.   int handle = __OPEN_INDEX(fd);
  17.   struct _device *dev;
  18.  
  19.   if ((short)handle < 0)
  20.     return(1);
  21.  
  22.   dev = _dev_fd(fd);
  23.   if (dev && ((dev->open) || (dev->dev == 0xfffd)))
  24.   {    /* a special device or PRN: */
  25.       return 0;
  26.   }
  27.   
  28.   if (handle < __NHANDLES)
  29.     if (__open_stat[handle].status != FH_UNKNOWN)
  30.         return(__open_stat[handle].status == FH_ISATTY);
  31.   oldloc = Fseek (0L, fd, SEEK_CUR);    /* seek zero bytes from current loc */
  32.   if ((seekval = Fseek (1L, fd, SEEK_SET)) != 0) /* try to seek ahead one byte */
  33.     if ((seekval > 0) || (seekval == ((long)(-EBADARG)))) /* out of range... */
  34.       rc = 0;            /* file, not a tty */
  35.     else 
  36.       {
  37.       errno = EBADF;        /* any other error returns "invalid handle" */
  38.                 /* because you can't tell */
  39.       rc = 0;
  40.       }
  41.   else
  42.     rc = 1;            /* yes, tty (Fseek returns 0 only for ttys) */
  43.   (void)Fseek(oldloc, fd, SEEK_SET);    /* seek back to original location */
  44.   if (handle < __NHANDLES)
  45.     if (rc)
  46.         __open_stat[handle].status = FH_ISATTY;
  47.         else
  48.         __open_stat[handle].status = FH_ISAFILE;
  49.   return (rc);            /* return true, false, or error */
  50. }
  51.