update for HEAD-2003021201
[reactos.git] / lib / crtdll / stdio / fread.c
1 #include <msvcrt/stdio.h>
2 #include <msvcrt/stdlib.h>
3 #include <msvcrt/string.h>
4 #include <msvcrt/errno.h>
5 #include <msvcrt/internal/file.h>
6
7
8 // carriage return line feed conversion is done in filbuf and  flsbuf
9 #if 0
10 size_t
11 fread(void *p, size_t size, size_t count, FILE *iop)
12 {
13   char *ptr = (char *)p;
14   int to_read;
15   
16   to_read = size * count;
17  
18
19         
20   while ( to_read > 0 ) {
21         *ptr = getc(iop) ;
22         if ( *ptr == EOF )
23                 break;
24         to_read--;
25         ptr++;
26   }
27
28         
29
30   return count- (to_read/size);
31 }
32
33
34 #else
35 size_t fread(void *vptr, size_t size, size_t count, FILE *iop)
36 {
37         char *ptr = (char *)vptr;
38         size_t  to_read ,n_read;
39
40         to_read = size * count;
41         
42         if (!OPEN4READING(iop))
43         {
44                 __set_errno (EINVAL);
45                 return 0;
46         }
47
48         if (!__validfp (iop) )
49         {
50                 __set_errno (EINVAL);
51                 return 0;
52         }
53         if (feof (iop) || ferror (iop))
54                 return 0;
55
56         if (vptr == NULL || to_read == 0)
57                 return 0;
58
59
60         while(iop->_cnt > 0 && to_read > 0 ) {
61                 to_read--;
62                 *ptr++ = getc(iop);
63         } 
64
65         // if the buffer is dirty it will have to be written now
66         // otherwise the file pointer won't match anymore.
67   
68         fflush(iop);
69
70          // check to see if this will work with in combination with ungetc
71          
72         n_read =  _read(fileno(iop), ptr, to_read);
73         if ( n_read != -1 )
74                 to_read -= n_read;
75
76         // the file buffer is empty and there is no read ahead information anymore.
77
78         iop->_flag &= ~_IOAHEAD;
79         
80         return count- (to_read/size);
81
82 #endif
83