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