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

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