Subversion Repositories Kolibri OS

Rev

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

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
  28. *               DESCRIBE IT HERE!
  29. *
  30. ****************************************************************************/
  31.  
  32.  
  33. #include "variety.h"
  34. #include <stdio.h>
  35. #include <io.h>
  36. #include <fcntl.h>
  37. #include <errno.h>
  38. #include "iomode.h"
  39. #include "fileacc.h"
  40. #include "rtcheck.h"
  41. #include "rtdata.h"
  42. #include "seterrno.h"
  43. #include "lseek.h"
  44.  
  45. typedef struct
  46. {
  47.   char     *name;
  48.   unsigned int offset;
  49. }__file_handle;
  50.  
  51.  
  52. int _stdcall read_file (const char *name,char *buff,unsigned offset, unsigned count,unsigned *reads);
  53.  
  54. _WCRTLINK int read( int handle, void *buf, unsigned len )
  55. {
  56.     unsigned read_len, total_len;
  57.     unsigned reduce_idx, finish_idx;
  58.     unsigned iomode_flags;
  59.     char *buffer = buf;
  60.     BOOL    rc;
  61.     HANDLE  h;
  62.     unsigned amount_read;
  63.     __file_handle *fh;
  64.  
  65.     __handle_check( handle, -1 );
  66.     __ChkTTYIOMode( handle );
  67.     iomode_flags = __GetIOMode( handle );
  68.     if( iomode_flags == 0 )
  69.     {
  70.         __set_errno( EBADF );
  71.         return( -1 );
  72.     }
  73.     if( !(iomode_flags & _READ) )
  74.     {
  75.         __set_errno( EACCES );     /* changed from EBADF to EACCES 23-feb-89 */
  76.         return( -1 );
  77.     }
  78.  
  79.  
  80.     fh = (__file_handle*) __getOSHandle( handle );
  81.  
  82.     if( iomode_flags & _BINARY )   /* if binary mode */
  83.     {
  84.  
  85.       if(read_file(fh->name,buffer,fh->offset,len,&amount_read))
  86.       {
  87.         if ( amount_read == 0)
  88.           return (-1);  
  89.       }
  90.       fh->offset+=amount_read;
  91.       total_len = amount_read;
  92.     }
  93.     else
  94.     {
  95.         total_len = 0;
  96.         read_len = len;
  97.         do
  98.         {
  99.           if(read_file(fh->name,buffer,fh->offset,len,&amount_read))
  100.           {
  101.             if( amount_read == 0 )
  102.             {                    /* EOF */
  103.                 break;
  104.             }
  105.           }  
  106.           reduce_idx = 0;
  107.           finish_idx = reduce_idx;
  108.           for( ; reduce_idx < amount_read; ++reduce_idx )
  109.           {
  110.             if( buffer[ reduce_idx ] == 0x1a )     /* EOF */
  111.             {
  112.                __lseek( handle,
  113.                            ((long)reduce_idx - (long)amount_read)+1L,
  114.                            SEEK_CUR );
  115.                total_len += finish_idx;
  116.                return( total_len );
  117.             }
  118.             if( buffer[ reduce_idx ] != '\r' )
  119.             {
  120.                 buffer[ finish_idx++ ] = buffer[ reduce_idx ];
  121.             };    
  122.           }
  123.  
  124.           total_len += finish_idx;
  125.           buffer += finish_idx;
  126.           read_len -= finish_idx;
  127.           if( iomode_flags & _ISTTY )
  128.           {
  129.                 break;  /* 04-feb-88, FWC */
  130.           }
  131.         } while( read_len != 0 );
  132.     }
  133.     return( total_len );
  134. }
  135.  
  136.