home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / local / read.c.sav < prev    next >
Text File  |  1992-03-12  |  2KB  |  96 lines

  1. #define INCL_DOSFILEMGR
  2. #define INCL_DOSERRORS
  3. #include <os2.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6.  
  7. extern int __textio[20];
  8. extern char __textbuf[8096];
  9.  
  10. ULONG Dos32Read() asm ("Dos32Read");
  11.  
  12. int read (int fd, char *buf, int nbytes)
  13. {
  14.    ULONG rc;
  15.    ULONG BytesRead;
  16.  
  17.    if (__textio[fd] & O_BINARY)
  18.    {
  19.       rc = Dos32Read (fd, (void *)buf, nbytes, &BytesRead);
  20.  
  21.       if (rc && BytesRead == 0)
  22.       {
  23.          if (rc == ERROR_INVALID_HANDLE)
  24.          {
  25.             errno = EBADF;
  26.             return (-1);
  27.          }
  28.  
  29.          if (rc == ERROR_ACCESS_DENIED || rc == ERROR_LOCK_VIOLATION)
  30.          {
  31.             errno = EACCES;
  32.             return (-1);
  33.          }
  34.  
  35.          errno = EIO;
  36.          return (-1);
  37.       }
  38.  
  39.       return (BytesRead);
  40.    }
  41.    else
  42.    {
  43.       char *tbuf;
  44.       int bcount = nbytes;
  45.       register char c;
  46.  
  47.       while (bcount)
  48.       {
  49.          int thistime = bcount > 8096 ? 8096 : bcount;
  50.  
  51.          rc = Dos32Read (fd, (void *)__textbuf, thistime, &BytesRead);
  52.  
  53.          if (BytesRead == 0)
  54.             return (nbytes - bcount);
  55.  
  56.          if (rc)
  57.          {
  58.             if (rc == ERROR_INVALID_HANDLE)
  59.             {
  60.                errno = EBADF;
  61.                return (-1);
  62.             }
  63.  
  64.             if (rc == ERROR_ACCESS_DENIED || rc == ERROR_LOCK_VIOLATION)
  65.             {
  66.                errno = EACCES;
  67.                return (-1);
  68.             }
  69.  
  70.             errno = EIO;
  71.             return (-1);
  72.          }
  73.  
  74.          tbuf = (char *)__textbuf;
  75.  
  76.          for (thistime = BytesRead; 0 < thistime && 0 < bcount; --thistime)
  77.          {
  78.             c = *tbuf++;
  79.             if (c == 0x1a)
  80.                return (nbytes - bcount);
  81.  
  82.             if (c == '\r')
  83.             {
  84.             }
  85.             else
  86.             {
  87.                *buf++ = c;
  88.                --bcount;
  89.             }
  90.          }
  91.       }
  92.       return (nbytes - bcount);
  93.    }
  94. }
  95.  
  96.