home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / isatty.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  2KB  |  63 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. #include <string.h>
  11. #include <support.h>
  12. #include "lib.h"
  13.  
  14. struct __open_file __open_stat[__NHANDLES];
  15.  
  16. int isatty(fd)
  17. int fd;
  18. {
  19.   int rc;
  20.   long oldloc;
  21.   int handle = __OPEN_INDEX(fd);
  22.  
  23.   if (handle < __NHANDLES)
  24.     if (__open_stat[handle].status != FH_UNKNOWN)
  25.         return(__open_stat[handle].status == FH_ISATTY);
  26.   oldloc = Fseek(0L, fd, SEEK_CUR);    /* save current location */
  27.   if (Fseek(1L, fd, SEEK_CUR) != 0) {    /* try to seek ahead one byte */
  28.     /* got either a file position or an error (usually EBADARG indicating
  29.        a range error from trying to seek past EOF), so it is not a tty */
  30.     rc = 0;
  31.     (void) Fseek(oldloc, fd, SEEK_SET);    /* seek back to original location */
  32.   }
  33.   else
  34.     rc = 1;                /* yes, tty */
  35.   if (handle < __NHANDLES)
  36.     if (rc) {
  37.         __open_stat[handle].status = FH_ISATTY;
  38.         __open_stat[handle].flags = CRMOD|ECHO;
  39.     }
  40.     else
  41.         __open_stat[handle].status = FH_ISAFILE;
  42.   return (rc);            /* return true, false, or error */
  43. }
  44.  
  45. /* _isctty():  determine if a file descriptor refers to this process's
  46.    controlling tty.
  47. */
  48. int
  49. _isctty(fd)
  50.   int fd;
  51. {
  52.   char ctty_name[L_ctermid];
  53.   char ftty_name[L_ctermid];
  54.  
  55.   if (!(isatty(fd)) || !(isatty(-1)))
  56.     return 0;
  57.   if (fd == -1)
  58.     return 1;
  59.   (void) ctermid(ctty_name);
  60.   (void) strncpy(ftty_name, ttyname(fd), L_ctermid);
  61.   return !(strncmp(ctty_name, ftty_name, L_ctermid));
  62. }
  63.