home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / read.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  3KB  |  146 lines

  1. /*
  2.  * _read: like read, but takes a long instead of an int. Written by
  3.  * Eric R. Smith and placed in the public domain.
  4.  */
  5.  
  6. #include <fcntl.h>
  7. #include <osbind.h>
  8. #include <errno.h>
  9. #include <ioctl.h>
  10. #include <signal.h>
  11. #include <unistd.h>
  12. #include <support.h>
  13. #include "lib.h"
  14.  
  15. #define CTRL(x) (x & 0x1f)
  16. #define UNDEF -1
  17.  
  18. struct tchars __tchars = {
  19.     CTRL('C'),        /* interrupt */
  20.     CTRL('\\'),        /* quit */
  21.     CTRL('Q'),        /* start */
  22.     CTRL('S'),        /* stop */
  23.     CTRL('D'),        /* EOF */
  24.     '\r'            /* alternate end of line */
  25. };
  26.  
  27. struct ltchars __ltchars = {
  28.     CTRL('Z'),        /* suspend */
  29.     CTRL('Y'),        /* suspend after read */
  30.     CTRL('R'),        /* reprint */
  31.     UNDEF,            /* flush output */
  32.     UNDEF,            /* erase word */
  33.     UNDEF            /* quote next char */
  34. };
  35.  
  36. /*
  37.  * BUGS: in tos, turning ECHO off but leaving RAW and CBREAK alone doesn't
  38.  * work properly
  39.  */
  40.  
  41. long
  42. _read(fd, buf, size)
  43.     int fd;
  44.     void *buf;
  45.     unsigned long size;
  46. {
  47.     char *foo;
  48.     long r;
  49.     extern int __mint;
  50.     int indx;
  51.     int flags;
  52. #ifdef EIO
  53.     long tty_pgrp;
  54.     long omask;
  55.     __Sigfunc osigt;
  56. #endif
  57.  
  58.     if (isatty(fd)) {
  59. /* work around a bug in TOS; 4096 bytes should be plenty for terminal reads */
  60.         if (size > 4096) size = 4096;
  61.         indx = __OPEN_INDEX(fd);
  62.         if (indx < 0 || indx >= __NHANDLES)
  63.             indx = __NHANDLES - 1;
  64.         flags = __open_stat[indx].flags;
  65.     }
  66.     else
  67.         flags = -1;
  68.  
  69.     if ( (__mint > 0) || (flags  == -1) ||
  70.          ( ((flags & (RAW|CBREAK)) == 0) && (flags & ECHO) ) ) {
  71. #ifdef EIO
  72.                 if (__mint && _isctty(fd)) {
  73.           (void) ioctl(fd, TIOCGPGRP, &tty_pgrp);
  74.           if (tty_pgrp != getpgrp()) {
  75. #if 0
  76.             /* This isn't really what we mean here...we really want to
  77.                know if our process group has no controlling terminal.
  78.             */
  79.             if (fd == -1 && __open_stat[indx] == FH_ISAFILE) {
  80.               errno = EIO;
  81.               return -1;
  82.             }
  83. #endif
  84.             omask = sigblock(~0L);
  85.             osigt = signal(SIGTTIN, SIG_IGN);
  86.             (void) signal(SIGTTIN, osigt);
  87.             (void) sigsetmask(omask);
  88.             if ((omask & sigmask(SIGTTIN)) || (osigt == SIG_IGN)) {
  89.               errno = EIO;
  90.               return -1;
  91.             }
  92.           }
  93.         }
  94. #endif /* EIO */
  95.         r = Fread(fd, size, buf);
  96.  
  97.         if (r < 0) {
  98.             errno = (int) -r;
  99.             return -1;
  100.         }
  101.  
  102.     /* watch out for TTYs */
  103.         if (__mint == 0 && isatty(fd)) {
  104.             foo = (char *)buf;
  105.             if (*foo == 4)         /* ^D for EOF?? */
  106.                 return 0;
  107.             foo[r] = '\n';        /* %!#@ TOS doesn't put in the LF */
  108.             Cconout('\n');        /* not quite right if fd != 0 */
  109.             r++;
  110.         }
  111.     }
  112.     else {
  113. again:
  114.         r = _console_read_byte(fd) & 0x00ff;
  115.         if (flags & ECHO) {
  116.             _console_write_byte(fd, (int)r);
  117.         }
  118.         if (flags & CRMOD) {
  119.             if (r == '\r') r = '\n';
  120.         }
  121.         if (!(flags & RAW)) {
  122.             if (r == __tchars.t_intrc) {
  123.                 raise(SIGINT);
  124.                 goto again;
  125.             }
  126.             else if (r == __tchars.t_quitc) {
  127.                 raise(SIGQUIT);
  128.                 goto again;
  129.             }
  130.         }
  131.         *((char *)buf) = r;
  132.         r = 1;
  133.     }
  134.  
  135.     return r;
  136. }
  137.  
  138. int
  139. read(fd, buf, size)
  140.     int fd;
  141.     void *buf;
  142.     unsigned size;
  143. {
  144.     return (int) _read(fd, buf, (unsigned long)size);
  145. }
  146.