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

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