Subversion Repositories Kolibri OS

Rev

Rev 4874 | Rev 6536 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /* read.c -- read bytes from a input device.
  2.  *
  3.  * Copyright (c) 1995 Cygnus Support
  4.  *
  5.  * The authors hereby grant permission to use, copy, modify, distribute,
  6.  * and license this software and its documentation for any purpose, provided
  7.  * that existing copyright notices are retained in all copies and that this
  8.  * notice is included verbatim in any distributions. No written agreement,
  9.  * license, or royalty fee is required for any of the authorized uses.
  10.  * Modifications to this software may be copyrighted by their authors
  11.  * and need not follow the licensing terms described here, provided that
  12.  * the new terms are clearly indicated on the first page of each file where
  13.  * they apply.
  14.  */
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <alloca.h>
  20. #include <sys/kos_io.h>
  21. #include "glue.h"
  22. #include "io.h"
  23.  
  24. #undef erro
  25. extern int errno;
  26.  
  27. ssize_t read(int fd, void *buf, size_t cnt)
  28. {
  29.     _ssize_t ret;
  30.  
  31.     _ssize_t  read_len, total_len;
  32.     unsigned  reduce_idx, finish_idx;
  33.     unsigned  iomode_flags;
  34.     char     *buffer = buf;
  35.     int       rc;
  36.     int       h;
  37.     unsigned amount_read;
  38.     int err;
  39.  
  40.     __io_handle *ioh;
  41.  
  42.     if( (fd < 0) || (fd >=64) )
  43.     {
  44.         errno = EBADF;
  45.         return -1;
  46.     };
  47.  
  48.     ioh = &__io_tab[fd];
  49.  
  50.     iomode_flags = ioh->mode;
  51.  
  52.     if( iomode_flags == 0 )
  53.     {
  54.         errno = EBADF;
  55.         return( -1 );
  56.     }
  57.  
  58.     if( !(iomode_flags & _READ) )
  59.     {
  60.         errno = EACCES;
  61.         return( -1 );
  62.     }
  63.  
  64.     if( iomode_flags & _BINARY )   /* if binary mode */
  65.     {
  66.         err = read_file(ioh->name, buffer, ioh->offset, cnt, &amount_read);
  67.         ioh->offset+= amount_read;
  68.         total_len  = amount_read;
  69.  
  70.         if(err)
  71.             if ( amount_read == 0)
  72.                 return (-1);
  73.     }
  74.     else
  75.     {
  76.         total_len = 0;
  77.         read_len = cnt;
  78.         do
  79.         {
  80.             err=read_file(ioh->name,buffer, ioh->offset, cnt, &amount_read);
  81.             ioh->offset+=amount_read;
  82.  
  83.             if( amount_read == 0 )
  84.                 break;                   /* EOF */
  85.  
  86.             reduce_idx = 0;
  87.             finish_idx = reduce_idx;
  88.             for( ; reduce_idx < amount_read; ++reduce_idx )
  89.             {
  90.                 if( buffer[ reduce_idx ] == 0x1a )     /* EOF */
  91.                 {
  92.                     lseek(fd, ((long)reduce_idx - (long)amount_read)+1L, SEEK_CUR );
  93.                     total_len += finish_idx;
  94.                     return( total_len );
  95.                 }
  96.                 if( buffer[ reduce_idx ] != '\r' )
  97.                 {
  98.                     buffer[ finish_idx++ ] = buffer[ reduce_idx ];
  99.                 };
  100.             }
  101.  
  102.             total_len += finish_idx;
  103.             buffer += finish_idx;
  104.             read_len -= finish_idx;
  105.             if( iomode_flags & _ISTTY )
  106.             {
  107.                 break;
  108.             }
  109.         } while( read_len != 0 );
  110.     }
  111.     return( total_len );
  112. }
  113.