1 /*
2  * Copyright (c) 1990, 2007 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * and/or other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 /*
19 FUNCTION
20 <<fread>>, <<fread_unlocked>>---read array elements from a file
21 
22 INDEX
23 	fread
24 INDEX
25 	fread_unlocked
26 INDEX
27 	_fread_r
28 INDEX
29 	_fread_unlocked_r
30 
31 SYNOPSIS
32 	#include <stdio.h>
33 	size_t fread(void *restrict <[buf]>, size_t <[size]>, size_t <[count]>,
34 		     FILE *restrict <[fp]>);
35 
36 	#define _BSD_SOURCE
37 	#include <stdio.h>
38 	size_t fread_unlocked(void *restrict <[buf]>, size_t <[size]>, size_t <[count]>,
39 		     FILE *restrict <[fp]>);
40 
41 	#include <stdio.h>
42 	size_t fread( void *restrict <[buf]>,
43 	                size_t <[size]>, size_t <[count]>, FILE *restrict <[fp]>);
44 
45 	#include <stdio.h>
46 	size_t fread_unlocked( void *restrict <[buf]>,
47 	                size_t <[size]>, size_t <[count]>, FILE *restrict <[fp]>);
48 
49 DESCRIPTION
50 <<fread>> attempts to copy, from the file or stream identified by
51 <[fp]>, <[count]> elements (each of size <[size]>) into memory,
52 starting at <[buf]>.   <<fread>> may copy fewer elements than
53 <[count]> if an error, or end of file, intervenes.
54 
55 <<fread>> also advances the file position indicator (if any) for
56 <[fp]> by the number of @emph{characters} actually read.
57 
58 <<fread_unlocked>> is a non-thread-safe version of <<fread>>.
59 <<fread_unlocked>> may only safely be used within a scope
60 protected by flockfile() (or ftrylockfile()) and funlockfile().  This
61 function may safely be used in a multi-threaded program if and only
62 if they are called while the invoking thread owns the (FILE *)
63 object, as is the case after a successful call to the flockfile() or
64 ftrylockfile() functions.  If threads are disabled, then
65 <<fread_unlocked>> is equivalent to <<fread>>.
66 
67 <<_fread_r>> and <<_fread_unlocked_r>> are simply reentrant versions of the
68 above that take an additional reentrant structure pointer argument: <[ptr]>.
69 
70 RETURNS
71 The result of <<fread>> is the number of elements it succeeded in
72 reading.
73 
74 PORTABILITY
75 ANSI C requires <<fread>>.
76 
77 <<fread_unlocked>> is a BSD extension also provided by GNU libc.
78 
79 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
80 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
81 */
82 
83 #define _DEFAULT_SOURCE
84 #include <stdio.h>
85 #include <string.h>
86 #include <malloc.h>
87 #include "local.h"
88 
89 #ifdef __IMPL_UNLOCKED__
90 #define _fread_r _fread_unlocked_r
91 #define fread fread_unlocked
92 #endif
93 
94 #ifdef __SCLE
95 static size_t
crlf_r(struct _reent * ptr,FILE * fp,char * buf,size_t count,int eof)96 crlf_r (struct _reent * ptr,
97        FILE * fp,
98        char * buf,
99        size_t count,
100        int eof)
101 {
102   int r;
103   char *s, *d, *e;
104 
105   if (count == 0)
106     return 0;
107 
108   e = buf + count;
109   for (s=d=buf; s<e-1; s++)
110     {
111       if (*s == '\r' && s[1] == '\n')
112         s++;
113       *d++ = *s;
114     }
115   if (s < e)
116     {
117       if (*s == '\r')
118         {
119           int c = _sgetc_raw ( fp);
120           if (c == '\n')
121             *s = '\n';
122           else
123             ungetc (c, fp);
124         }
125       *d++ = *s++;
126     }
127 
128 
129   while (d < e)
130     {
131       r = getc ( fp);
132       if (r == EOF)
133         return count - (e-d);
134       *d++ = r;
135     }
136 
137   return count;
138 
139 }
140 
141 #endif
142 
143 size_t
fread(void * __restrict buf,size_t size,size_t count,FILE * __restrict fp)144 fread (
145        void *__restrict buf,
146        size_t size,
147        size_t count,
148        FILE * __restrict fp)
149 {
150   register size_t resid;
151   register char *p;
152   register int r;
153   size_t total;
154 
155   if ((resid = count * size) == 0)
156     return 0;
157 
158   CHECK_INIT(ptr, fp);
159 
160   _newlib_flockfile_start (fp);
161   if (ORIENT (fp, -1) != -1)
162     {
163       count = 0;
164       goto ret;
165     }
166   if (fp->_r < 0)
167     fp->_r = 0;
168   total = resid;
169   p = buf;
170 
171 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
172 
173   /* Optimize unbuffered I/O.  */
174   if (fp->_flags & __SNBF)
175     {
176       /* First copy any available characters from ungetc buffer.  */
177       int copy_size = resid > (size_t) fp->_r ? fp->_r : (int) resid;
178       (void) memcpy ((void *) p, (void *) fp->_p, (size_t) copy_size);
179       fp->_p += copy_size;
180       fp->_r -= copy_size;
181       p += copy_size;
182       resid -= copy_size;
183 
184       /* If still more data needed, free any allocated ungetc buffer.  */
185       if (HASUB (fp) && resid > 0)
186 	FREEUB (ptr, fp);
187 
188       /* Finally read directly into user's buffer if needed.  */
189       while (resid > 0)
190 	{
191 	  int rc = 0;
192 	  /* save fp buffering state */
193 	  void *old_base = fp->_bf._base;
194 	  void * old_p = fp->_p;
195 	  int old_size = fp->_bf._size;
196 	  /* allow __refill to use user's buffer */
197 	  fp->_bf._base = (unsigned char *) p;
198 	  fp->_bf._size = resid;
199 	  fp->_p = (unsigned char *) p;
200 	  rc = _srefill ( fp);
201 	  /* restore fp buffering back to original state */
202 	  fp->_bf._base = old_base;
203 	  fp->_bf._size = old_size;
204 	  fp->_p = old_p;
205 	  resid -= fp->_r;
206 	  p += fp->_r;
207 	  fp->_r = 0;
208 	  if (rc)
209 	    {
210 #ifdef __SCLE
211               if (fp->_flags & __SCLE)
212 	        {
213 	          _newlib_flockfile_exit (fp);
214 	          return crlf_r (ptr, fp, buf, total-resid, 1) / size;
215 	        }
216 #endif
217 	      _newlib_flockfile_exit (fp);
218 	      return (total - resid) / size;
219 	    }
220 	}
221     }
222   else
223 #endif /* !PREFER_SIZE_OVER_SPEED && !__OPTIMIZE_SIZE__ */
224     {
225         while (resid > (size_t) (r = fp->_r))
226 	{
227 	  (void) memcpy ((void *) p, (void *) fp->_p, (size_t) r);
228 	  fp->_p += r;
229 	  /* fp->_r = 0 ... done in __srefill */
230 	  p += r;
231 	  resid -= r;
232 	  if (_srefill ( fp))
233 	    {
234 	      /* no more input: return partial result */
235 #ifdef __SCLE
236 	      if (fp->_flags & __SCLE)
237 		{
238 		  _newlib_flockfile_exit (fp);
239 		  return crlf_r (ptr, fp, buf, total-resid, 1) / size;
240 		}
241 #endif
242 	      _newlib_flockfile_exit (fp);
243 	      return (total - resid) / size;
244 	    }
245 	}
246       (void) memcpy ((void *) p, (void *) fp->_p, resid);
247       fp->_r -= resid;
248       fp->_p += resid;
249     }
250 
251   /* Perform any CR/LF clean-up if necessary.  */
252 #ifdef __SCLE
253   if (fp->_flags & __SCLE)
254     {
255       _newlib_flockfile_exit (fp);
256       return crlf_r(ptr, fp, buf, total, 0) / size;
257     }
258 #endif
259 ret:
260   _newlib_flockfile_end (fp);
261   return count;
262 }
263