home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / posix / unistd / read.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-15  |  1.2 KB  |  52 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <io.h>
  7.  
  8. #include <libc/dosio.h>
  9. #include <libc/ttyprvt.h>
  10.  
  11. int (*__libc_read_termios_hook)(int handle, void *buffer, size_t count,
  12.                 ssize_t *rv) = NULL;
  13.  
  14. ssize_t
  15. read(int handle, void* buffer, size_t count)
  16. {
  17.   ssize_t ngot;
  18.  
  19.   /* termios special hook */
  20.   if (__libc_read_termios_hook != NULL)
  21.     {
  22.       ssize_t rv;
  23.       if (__libc_read_termios_hook(handle, buffer, count, &rv) != 0)
  24.         return rv;
  25.     }
  26.  
  27.   ngot = _read(handle, buffer, count);
  28.   if(ngot <= 0)
  29.     return ngot;
  30.   if (__file_handle_modes[handle] & O_TEXT)
  31.   {
  32.     /* check for Ctrl-Z EOF character */
  33.     int i;
  34.     for (i=0; i<ngot; i++)
  35.       if (((char *)buffer)[i] == 0x1a) /* Ctrl-Z */
  36.       {
  37.     lseek(handle, i-ngot, SEEK_CUR); /* back up to make ^Z first char read next time */
  38.     ngot = i;
  39.     break;
  40.       }
  41.  
  42.     if (ngot > 0)
  43.     {
  44.       /* convert CR/LF to NL */
  45.       ngot = crlf2nl(buffer, ngot);
  46.       if(!ngot)
  47.     return read(handle, buffer, count);        /* if all CR's read more data */
  48.     }
  49.   }
  50.   return ngot;
  51. }
  52.