home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / WRITE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  3.7 KB  |  118 lines

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