home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / read.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  2KB  |  116 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 "lib.h"
  13.  
  14. #define CTRL(x) (x & 0x1f)
  15. #define UNDEF -1
  16.  
  17. struct tchars __tchars = {
  18.     CTRL('C'),        /* interrupt */
  19.     CTRL('\\'),        /* quit */
  20.     CTRL('Q'),        /* start */
  21.     CTRL('S'),        /* stop */
  22.     CTRL('D'),        /* EOF */
  23.     '\r'            /* alternate end of line */
  24. };
  25.  
  26. struct ltchars __ltchars = {
  27.     CTRL('Z'),        /* suspend */
  28.     CTRL('Y'),        /* suspend after read */
  29.     CTRL('R'),        /* reprint */
  30.     UNDEF,            /* flush output */
  31.     UNDEF,            /* erase word */
  32.     UNDEF            /* quote next char */
  33. };
  34.  
  35. /*
  36.  * BUGS: in tos, turning ECHO off but leaving RAW and CBREAK alone doesn't
  37.  * work properly
  38.  */
  39.  
  40. long
  41. _read(fd, buf, size)
  42.     int fd;
  43.     void *buf;
  44.     unsigned long size;
  45. {
  46.     char *foo;
  47.     long r;
  48.     extern int __mint;
  49.     int indx;
  50.     int flags;
  51.  
  52.     if (isatty(fd)) {
  53. /* work around a bug in TOS; 4096 bytes should be plenty for terminal reads */
  54.         if (size > 4096) size = 4096;
  55.         indx = __OPEN_INDEX(fd);
  56.         if (indx < 0 || indx >= __NHANDLES)
  57.             indx = __NHANDLES - 1;
  58.         flags = __open_stat[indx].flags;
  59.     }
  60.     else
  61.         flags = -1;
  62.  
  63.     if ( (__mint > 0) || (flags  == -1) ||
  64.          ( ((flags & (RAW|CBREAK)) == 0) && (flags & ECHO) ) ) {
  65.         r = Fread(fd, size, buf);
  66.  
  67.         if (r < 0) {
  68.             errno = (int) -r;
  69.             return -1;
  70.         }
  71.  
  72.     /* watch out for TTYs */
  73.         if (__mint == 0 && isatty(fd)) {
  74.             foo = (char *)buf;
  75.             if (*foo == 4)         /* ^D for EOF?? */
  76.                 return 0;
  77.             foo[r] = '\n';        /* %!#@ TOS doesn't put in the LF */
  78.             Cconout('\n');        /* not quite right if fd != 0 */
  79.             r++;
  80.         }
  81.     }
  82.     else {
  83. again:
  84.         r = _console_read_byte(fd) & 0x00ff;
  85.         if (flags & ECHO) {
  86.             _console_write_byte(fd, (int)r);
  87.         }
  88.         if (flags & CRMOD) {
  89.             if (r == '\r') r = '\n';
  90.         }
  91.         if (!(flags & RAW)) {
  92.             if (r == __tchars.t_intrc) {
  93.                 raise(SIGINT);
  94.                 goto again;
  95.             }
  96.             else if (r == __tchars.t_quitc) {
  97.                 raise(SIGQUIT);
  98.                 goto again;
  99.             }
  100.         }
  101.         *((char *)buf) = r;
  102.         r = 1;
  103.     }
  104.  
  105.     return r;
  106. }
  107.  
  108. int
  109. read(fd, buf, size)
  110.     int fd;
  111.     void *buf;
  112.     unsigned size;
  113. {
  114.     return (int) _read(fd, buf, (unsigned long)size);
  115. }
  116.