home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / isatty.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  1KB  |  45 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 <ioctl.h>
  9. #include <unistd.h>
  10.  
  11. struct __open_file __open_stat[__NHANDLES];
  12.  
  13. int isatty(fd)
  14. int fd;
  15. {
  16.   int rc;
  17.   long oldloc, seekval;
  18.   int handle = __OPEN_INDEX(fd);
  19.  
  20.   if (handle < __NHANDLES)
  21.     if (__open_stat[handle].status != FH_UNKNOWN)
  22.         return(__open_stat[handle].status == FH_ISATTY);
  23.   oldloc = Fseek (0L, fd, SEEK_CUR);    /* seek zero bytes from current loc */
  24.   if ((seekval = Fseek (1L, fd, SEEK_SET)) != 0) /* try to seek ahead one byte */
  25.     if ((seekval > 0) || (seekval == ((long)(-EBADARG)))) /* out of range... */
  26.       rc = 0;            /* file, not a tty */
  27.     else 
  28.       {
  29.       errno = EBADF;        /* any other error returns "invalid handle" */
  30.                 /* because you can't tell */
  31.       rc = 0;
  32.       }
  33.   else
  34.     rc = 1;            /* yes, tty (Fseek returns 0 only for ttys) */
  35.   (void)Fseek(oldloc, fd, SEEK_SET);    /* seek back to original location */
  36.   if (handle < __NHANDLES)
  37.     if (rc) {
  38.         __open_stat[handle].status = FH_ISATTY;
  39.         __open_stat[handle].flags = CRMOD|ECHO;
  40.     }
  41.     else
  42.         __open_stat[handle].status = FH_ISAFILE;
  43.   return (rc);            /* return true, false, or error */
  44. }
  45.