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 <_ansi.h>
85 #include <stdio.h>
86 #include <string.h>
87 #include <malloc.h>
88 #include "local.h"
89 
90 #ifdef __IMPL_UNLOCKED__
91 #define _fread_r _fread_unlocked_r
92 #define fread fread_unlocked
93 #endif
94 
95 #ifdef __SCLE
96 static size_t
crlf_r(struct _reent * ptr,FILE * fp,char * buf,size_t count,int eof)97 crlf_r (struct _reent * ptr,
98        FILE * fp,
99        char * buf,
100        size_t count,
101        int eof)
102 {
103   int r;
104   char *s, *d, *e;
105 
106   if (count == 0)
107     return 0;
108 
109   e = buf + count;
110   for (s=d=buf; s<e-1; s++)
111     {
112       if (*s == '\r' && s[1] == '\n')
113         s++;
114       *d++ = *s;
115     }
116   if (s < e)
117     {
118       if (*s == '\r')
119         {
120           int c = _sgetc_raw ( fp);
121           if (c == '\n')
122             *s = '\n';
123           else
124             ungetc (c, fp);
125         }
126       *d++ = *s++;
127     }
128 
129 
130   while (d < e)
131     {
132       r = getc ( fp);
133       if (r == EOF)
134         return count - (e-d);
135       *d++ = r;
136     }
137 
138   return count;
139 
140 }
141 
142 #endif
143 
144 size_t
fread(void * __restrict buf,size_t size,size_t count,FILE * __restrict fp)145 fread (
146        void *__restrict buf,
147        size_t size,
148        size_t count,
149        FILE * __restrict fp)
150 {
151   register size_t resid;
152   register char *p;
153   register int r;
154   size_t total;
155 
156   if ((resid = count * size) == 0)
157     return 0;
158 
159   CHECK_INIT(ptr, fp);
160 
161   _newlib_flockfile_start (fp);
162   ORIENT (fp, -1);
163   if (fp->_r < 0)
164     fp->_r = 0;
165   total = resid;
166   p = buf;
167 
168 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
169 
170   /* Optimize unbuffered I/O.  */
171   if (fp->_flags & __SNBF)
172     {
173       /* First copy any available characters from ungetc buffer.  */
174       int copy_size = resid > (size_t) fp->_r ? fp->_r : (int) resid;
175       (void) memcpy ((void *) p, (void *) fp->_p, (size_t) copy_size);
176       fp->_p += copy_size;
177       fp->_r -= copy_size;
178       p += copy_size;
179       resid -= copy_size;
180 
181       /* If still more data needed, free any allocated ungetc buffer.  */
182       if (HASUB (fp) && resid > 0)
183 	FREEUB (ptr, fp);
184 
185       /* Finally read directly into user's buffer if needed.  */
186       while (resid > 0)
187 	{
188 	  int rc = 0;
189 	  /* save fp buffering state */
190 	  void *old_base = fp->_bf._base;
191 	  void * old_p = fp->_p;
192 	  int old_size = fp->_bf._size;
193 	  /* allow __refill to use user's buffer */
194 	  fp->_bf._base = (unsigned char *) p;
195 	  fp->_bf._size = resid;
196 	  fp->_p = (unsigned char *) p;
197 	  rc = _srefill ( fp);
198 	  /* restore fp buffering back to original state */
199 	  fp->_bf._base = old_base;
200 	  fp->_bf._size = old_size;
201 	  fp->_p = old_p;
202 	  resid -= fp->_r;
203 	  p += fp->_r;
204 	  fp->_r = 0;
205 	  if (rc)
206 	    {
207 #ifdef __SCLE
208               if (fp->_flags & __SCLE)
209 	        {
210 	          _newlib_flockfile_exit (fp);
211 	          return crlf_r (ptr, fp, buf, total-resid, 1) / size;
212 	        }
213 #endif
214 	      _newlib_flockfile_exit (fp);
215 	      return (total - resid) / size;
216 	    }
217 	}
218     }
219   else
220 #endif /* !PREFER_SIZE_OVER_SPEED && !__OPTIMIZE_SIZE__ */
221     {
222         while (resid > (size_t) (r = fp->_r))
223 	{
224 	  (void) memcpy ((void *) p, (void *) fp->_p, (size_t) r);
225 	  fp->_p += r;
226 	  /* fp->_r = 0 ... done in __srefill */
227 	  p += r;
228 	  resid -= r;
229 	  if (_srefill ( fp))
230 	    {
231 	      /* no more input: return partial result */
232 #ifdef __SCLE
233 	      if (fp->_flags & __SCLE)
234 		{
235 		  _newlib_flockfile_exit (fp);
236 		  return crlf_r (ptr, fp, buf, total-resid, 1) / size;
237 		}
238 #endif
239 	      _newlib_flockfile_exit (fp);
240 	      return (total - resid) / size;
241 	    }
242 	}
243       (void) memcpy ((void *) p, (void *) fp->_p, resid);
244       fp->_r -= resid;
245       fp->_p += resid;
246     }
247 
248   /* Perform any CR/LF clean-up if necessary.  */
249 #ifdef __SCLE
250   if (fp->_flags & __SCLE)
251     {
252       _newlib_flockfile_exit (fp);
253       return crlf_r(ptr, fp, buf, total, 0) / size;
254     }
255 #endif
256   _newlib_flockfile_end (fp);
257   return count;
258 }
259