home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / dos / io / _write.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-24  |  1.1 KB  |  60 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <go32.h>
  7. #include <dpmi.h>
  8. #include <io.h>
  9. #include <sys/fsext.h>
  10.  
  11. #include <libc/dosio.h>
  12.  
  13. int
  14. _write(int handle, const void* buffer, size_t count)
  15. {
  16.   size_t j, i;
  17.   int nput;
  18.   unsigned long tbsize;
  19.   __dpmi_regs r;
  20.  
  21.   __FSEXT_Function *func = __FSEXT_get_function(handle);
  22.   if (func)
  23.   {
  24.     int rv;
  25.     if (func(__FSEXT_write, &rv, &handle))
  26.       return rv;
  27.   }
  28.  
  29.   tbsize = _go32_info_block.size_of_transfer_buffer;
  30.   nput = 0;
  31.   do {
  32.     j = (count <= tbsize) ? count : tbsize;
  33.     if (j)
  34.       dosmemput(buffer, j, __tb);
  35.     r.x.ax = 0x4000;
  36.     r.x.bx = handle;
  37.     r.x.cx = j;
  38.     r.x.dx = __tb & 15;
  39.     r.x.ds = __tb / 16;
  40.     __dpmi_int(0x21, &r);
  41.     if (r.x.flags & 1)
  42.     {
  43.       errno = __doserr_to_errno(r.x.ax);
  44.       return -1;
  45.     }
  46.     i = r.x.ax;
  47.     count -= i;
  48.     buffer = (void *)((int)buffer + i);
  49.     nput += i;
  50.   } while(count && (i == j));
  51.  
  52.   if (count && nput == 0)
  53.   {
  54.     errno = ENOSPC;
  55.     return -1;
  56.   }
  57.  
  58.   return nput;
  59. }
  60.