home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / WRITEA.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  3.0 KB  |  95 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - writea.cas
  3.  *
  4.  * function(s)
  5.  *      _write - write to a file (untranslated)
  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. #pragma inline
  19. #include <asmrules.h>
  20. #include <io.h>
  21. #include <fcntl.h>
  22. #include <_io.h>
  23.  
  24. /*-----------------------------------------------------------------------*
  25.  
  26. Name        _write - writes to a file
  27.  
  28. Usage        int _write(int handle, void *buf, unsigned nbyte);
  29.  
  30. Prototype in    io.h
  31.  
  32. Description    Both write and _write are functions that write a
  33.         buffer of data to the file or device named by the given
  34.         handle.
  35.  
  36.         handle is a file handle obtained from a creat, open, dup,
  37.         dup2, or fcntl call.
  38.  
  39.         These functions attempt to write nbyte bytes from the buffer
  40.         pointed to by buf to the file associated with handle. Except
  41.         when write is used to write to a text file, the number of
  42.         bytes written to the file will be no more than the number
  43.         requested.
  44.  
  45.         On text files, when write sees a linefeed (LF) character, it
  46.         outputs a CR-LF pair. _write does no such translation, since
  47.         all of its files are binary files.
  48.  
  49.         If the number of bytes actually written is less than that
  50.         requested, the condition should be considered an error and
  51.         probably indicates a full disk.
  52.  
  53.         For disk or diskette files, writing always proceeds from the
  54.         current file pointer (see lseek). For devices, bytes are directly
  55.         sent to the device.
  56.  
  57.         For files opened with the O_APPEND option, the file pointer is
  58.         positioned to EOF by write (but not by _write) before writing
  59.         the data.
  60.  
  61. Return value    The number of bytes written are returned by both
  62.         functions. A write to a text file does not count generated
  63.         carriage returns. In case of error, each function returns -1
  64.         and sets the global variable errno to one of the following:
  65.  
  66.             EACCES    Permission denied
  67.             EBADF    Bad file number
  68.  
  69.  
  70. *------------------------------------------------------------------------*/
  71. int _CType _write(int fd, void *buf, unsigned len)
  72. {
  73.     if (_openfd[fd] & O_APPEND)
  74.         lseek (fd, 0L, SEEK_END);
  75.  
  76.     pushDS_
  77. asm    mov    ah,40h
  78. asm    mov    bx,fd
  79. asm    mov    cx,len
  80. asm    LDS_    dx, buf
  81. asm    int    21h
  82.     popDS_
  83. asm    jc    _writeFailed
  84. asm    push    ax
  85.  
  86.     _openfd [fd] |= O_CHANGED;
  87.  
  88. asm    pop    ax
  89.     return    _AX;
  90.  
  91. _writeFailed:
  92.  
  93.     return __IOerror (_AX);
  94. }
  95.