home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / WRITE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.2 KB  |  132 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - write.c
  3.  *
  4.  * function(s)
  5.  *    write - writes to a file
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #include <io.h>
  18. #include <_io.h>
  19. #include <fcntl.h>
  20. #include <RtlData.h>
  21. #include <stdio.h>
  22.  
  23. #define  SIZE 128
  24.  
  25. /*---------------------------------------------------------------------*
  26.  
  27. Name            write - writes to a file
  28.  
  29. Usage           int write(int handle, const void *buf, unsigned nbyte);
  30.  
  31. Related
  32. functions usage int _write(int handle, const void *buf, unsigned nbyte);
  33.  
  34. Prototype in    io.h
  35.  
  36. Description     Both write and _write are functions that write a
  37.                 buffer of data to the file or device named by the given
  38.                 handle.
  39.  
  40.                 handle is a file handle obtained from a creat, open, dup,
  41.                 dup2, or fcntl call.
  42.  
  43.                 These functions attempt to write nbyte bytes from the buffer
  44.                 pointed to by buf to the file associated with handle. Except
  45.                 when write is used to write to a text file, the number of
  46.                 bytes written to the file will be no more than the number
  47.                 requested.
  48.  
  49.                 On text files, when write sees a linefeed (LF) character, it
  50.                 outputs a CR-LF pair. _write does no such translation, since
  51.                 all of its files are binary files.
  52.  
  53.                 If the number of bytes actually written is less than that
  54.                 requested, the condition should be considered an error and
  55.                 probably indicates a full disk.
  56.  
  57.                 For disk or diskette files, writing always proceeds from the
  58.                 current file pointer (see lseek). For devices, bytes are
  59.                 directly sent to the device.
  60.  
  61.                 For files opened with the O_APPEND option, the file pointer is
  62.                 positioned to EOF by write (but not by _write) before writing
  63.                 the data.
  64.  
  65. Return value    The number of bytes written are returned by both
  66.                 functions. A write to a text file does not count generated
  67.                 carriage returns. In case of error, each function returns -1
  68.                 and sets the global variable errno to one of the following:
  69.  
  70.                     EACCES  Permission denied
  71.                     EBADF   Bad file number
  72.  
  73. *---------------------------------------------------------------------*/
  74.  
  75. /* NOTE: This function is named __write for internal library use.
  76.  * The user entry point is in writeu.asm.
  77.  */
  78.  
  79. #pragma warn -sig
  80. int _CType _FARFUNC __write(int fd, const void *buf, unsigned int len)
  81. {
  82.     register unsigned chunk;
  83.     unsigned    res;
  84.     register char   *tbuf;
  85.     unsigned    remainder;
  86.     char        c;
  87.     const char  *sbuf;
  88.     char        crbuf [1 + SIZE];
  89.     _QRTLDataBlock;
  90.  
  91.     if ((unsigned)fd >= _QRTLInstanceData(_nfile))
  92.         return __IOerror(e_badHandle);
  93.  
  94.     if ((len +1) < 2)
  95.         return (0);              /* can't write 0 or 65535 bytes */
  96.  
  97.     if (_QRTLInstanceData(_openfd)[fd] & O_APPEND)
  98.         lseek (fd, 0L, SEEK_END);
  99.  
  100.     if ((_QRTLInstanceData(_openfd)[fd] & O_TEXT) == 0)
  101.         return (_write (fd, buf, len));
  102.  
  103.     _QRTLInstanceData(_openfd)[fd] &= ~_O_EOF;  /* assume we go beyond ^Z EOF   */
  104.  
  105.     sbuf = buf;
  106.     remainder = len;
  107.     tbuf = crbuf;
  108.     while (remainder)
  109.     {
  110.         remainder--;
  111.         if ((c = *sbuf++) == '\n')
  112.             *tbuf++ = '\r';
  113.  
  114.         *tbuf++ = c;
  115.         if ((tbuf - crbuf) >= SIZE)
  116.         {
  117.             chunk = tbuf - crbuf;
  118.             if ((res = _write (fd, crbuf, chunk)) != chunk)
  119.                                 return( (res == ~0U) ? -1 : (len - remainder + res - chunk));
  120.             tbuf = crbuf;
  121.         }
  122.     }
  123.  
  124.     if ((chunk = (tbuf - crbuf)) > 0)
  125.         if ((res = _write (fd, crbuf, chunk)) != chunk)
  126.                         return ((res == ~0U) ? -1 : (len + res - chunk));
  127.  
  128.         return( len );
  129. }
  130.  
  131. #pragma warn .sig
  132.