home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / READ.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  5.0 KB  |  175 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - read.cas
  3.  *
  4.  * function(s)
  5.  *        read - reads from 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. #pragma inline
  18. #include <asmrules.h>
  19. #include <io.h>
  20. #include <_io.h>
  21. #include <fcntl.h>
  22. #include <RtlData.h>
  23.  
  24. #define _ctlZ  26
  25.  
  26. /*-----------------------------------------------------------------------*
  27.  
  28. Name            read - reads from file
  29.  
  30. Usage           int read(int handle, void *buf, unsigned nbyte);
  31.  
  32. Related
  33. functions usage int _read(int handle, void *buf, unsigned nbyte);
  34.  
  35. Prototype in    io.h
  36.  
  37. Description     read and _read attempt to read nbyte bytes from the
  38.                 file  associated with handle into the buffer pointed to by buf.
  39.                 _read is a direct call to the MS-DOS read system call.
  40.  
  41.                 For a file opened in text mode, read removes carriage returns
  42.                 and reports end-of-file when a <Ctrl-Z> character is read. No
  43.                 such removal or reporting is performed by _read.
  44.  
  45.                 handle is a file handle obtained from a creat, open, dup, dup2,
  46.                 or fcntl call.
  47.  
  48.                 On disk files, these functions begin reading at the current
  49.                 file pointer. When the reading is complete, they increment the
  50.                 file pointer by the number of bytes read. On devices, the bytes
  51.                 are read directly from the device.
  52.  
  53. *------------------------------------------------------------------------*/
  54.  
  55. /* NOTE: This function is named __read for internal library use.
  56.  * The user entry point is in readu.asm.
  57.  */
  58.  
  59. int _CType _FARFUNC __read (int fd, void *buf, unsigned int len)
  60. {
  61. #if 0
  62.         char    *top;       // CHECK
  63.         char    *bottom;
  64. #endif
  65.         register unsigned       dlen;
  66.         char    c;
  67.         _QRTLDataBlock;
  68.  
  69.         if ((unsigned)fd >= _QRTLInstanceData(_nfile))
  70.                 return __IOerror(e_badHandle);
  71.  
  72.         if ((len + 1) < 2 || _QRTLInstanceData(_openfd)[fd] & _O_EOF)
  73.                 return( 0 );  /* 0, -1 are not allowed lengths */
  74.  
  75.         do {
  76.                 dlen = _read (fd, buf, len);
  77.                 if ((dlen + 1) < 2  ||
  78.                      (_QRTLInstanceData(_openfd)[fd] & O_TEXT) == 0)
  79.                         return(dlen);
  80.  
  81.                 /* Squeeze out carriage returns */
  82. #if 0
  83.                 top = bottom = buf;
  84.                 do {
  85.                         if ((c = *top++) == _ctlZ) {
  86.                                 lseek (fd, -dlen, SEEK_END);
  87.                 _QInstanceData(_openfd)[fd] |= _O_EOF; /* note that ^Z EOF has been seen */
  88.                 return(bottom - buf);
  89.                         }
  90.                         else {
  91.                                 if (c != '\r')
  92.                                         *bottom++ = c;
  93.                                 else if (1 == dlen) {
  94.                                         _read (fd, &c, 1);
  95.                                         *bottom++ = c;
  96.                                 }
  97.                         }
  98.                 } while (--dlen);
  99.         } while ((bottom - buf) == 0);
  100.  
  101.         return bottom - buf;
  102.  
  103. #endif
  104.  
  105. asm     mov     cx, dlen
  106. asm     LES_    si, buf
  107. #if (! LDATA)
  108. asm     push    DS
  109. asm     pop     ES
  110. #endif
  111. asm     mov     di, si
  112. asm     mov     bx, si
  113. asm     cld
  114. squeeze:
  115. asm             lods    BY0 (ES_ [si])
  116. asm             cmp     al, _ctlZ
  117. asm             je      endSeen
  118. asm             cmp     al, 0Dh         /* '\r' */
  119. asm             je      elseSqueeze
  120. asm             stosb
  121. whileSqueeze:
  122. asm             loop    squeeze
  123. asm             jmp     short  squeezeBreak
  124.  
  125. elseSqueeze:
  126. asm             loop    squeeze         /* if (1 == dlen)  */
  127. asm             push    ES
  128. asm             push    bx
  129.  
  130. asm             mov     ax, 1
  131. asm             push    ax
  132. asm             lea     ax, c
  133. #if LDATA
  134. asm             push    SS
  135. #endif
  136. asm             push    ax
  137. asm             push    fd              /*     _read (fd, &c, 1);  */
  138. asm             call    EXTPROC (_read)
  139. asm             add     sp, 4 + dPtrSize
  140.  
  141. asm             pop     bx
  142. asm             pop     ES
  143. asm             cld
  144. asm             mov     al, c
  145.  
  146. asm             stosb                   /*     *bottom++ = c; */
  147.  
  148. squeezeBreak: ;
  149.         } while (_DI == _BX);
  150. asm     jmp  short doneText
  151.  
  152. endSeen:
  153. asm     push bx              /* keep safe */
  154.  
  155. asm     mov  ax, SEEK_CUR
  156. asm     push    ax
  157. asm     neg     cx
  158. asm     sbb     ax, ax
  159. asm     push    ax
  160. asm     push    cx
  161. asm     push    W0 (fd)
  162. asm     call    EXTPROC(lseek)  /* lseek (fd, -dlen, SEEK_CUR); */
  163. asm     add     sp, 8
  164.  
  165.     _QRTLInstanceData(_openfd)[fd] |= _O_EOF;   /* note that ^Z EOF has been seen  */
  166.  
  167. asm     pop     bx
  168.  
  169. doneText:
  170. asm     sub     di, bx
  171. asm     xchg    ax, di
  172.  
  173. return _AX;
  174. }
  175.