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   if (ORIENT (fp, -1) != -1)
163     {
164       count = 0;
165       goto ret;
166     }
167   if (fp->_r < 0)
168     fp->_r = 0;
169   total = resid;
170   p = buf;
171 
172 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
173 
174   /* Optimize unbuffered I/O.  */
175   if (fp->_flags & __SNBF)
176     {
177       /* First copy any available characters from ungetc buffer.  */
178       int copy_size = resid > (size_t) fp->_r ? fp->_r : (int) resid;
179       (void) memcpy ((void *) p, (void *) fp->_p, (size_t) copy_size);
180       fp->_p += copy_size;
181       fp->_r -= copy_size;
182       p += copy_size;
183       resid -= copy_size;
184 
185       /* If still more data needed, free any allocated ungetc buffer.  */
186       if (HASUB (fp) && resid > 0)
187 	FREEUB (ptr, fp);
188 
189       /* Finally read directly into user's buffer if needed.  */
190       while (resid > 0)
191 	{
192 	  int rc = 0;
193 	  /* save fp buffering state */
194 	  void *old_base = fp->_bf._base;
195 	  void * old_p = fp->_p;
196 	  int old_size = fp->_bf._size;
197 	  /* allow __refill to use user's buffer */
198 	  fp->_bf._base = (unsigned char *) p;
199 	  fp->_bf._size = resid;
200 	  fp->_p = (unsigned char *) p;
201 	  rc = _srefill ( fp);
202 	  /* restore fp buffering back to original state */
203 	  fp->_bf._base = old_base;
204 	  fp->_bf._size = old_size;
205 	  fp->_p = old_p;
206 	  resid -= fp->_r;
207 	  p += fp->_r;
208 	  fp->_r = 0;
209 	  if (rc)
210 	    {
211 #ifdef __SCLE
212               if (fp->_flags & __SCLE)
213 	        {
214 	          _newlib_flockfile_exit (fp);
215 	          return crlf_r (ptr, fp, buf, total-resid, 1) / size;
216 	        }
217 #endif
218 	      _newlib_flockfile_exit (fp);
219 	      return (total - resid) / size;
220 	    }
221 	}
222     }
223   else
224 #endif /* !PREFER_SIZE_OVER_SPEED && !__OPTIMIZE_SIZE__ */
225     {
226         while (resid > (size_t) (r = fp->_r))
227 	{
228 	  (void) memcpy ((void *) p, (void *) fp->_p, (size_t) r);
229 	  fp->_p += r;
230 	  /* fp->_r = 0 ... done in __srefill */
231 	  p += r;
232 	  resid -= r;
233 	  if (_srefill ( fp))
234 	    {
235 	      /* no more input: return partial result */
236 #ifdef __SCLE
237 	      if (fp->_flags & __SCLE)
238 		{
239 		  _newlib_flockfile_exit (fp);
240 		  return crlf_r (ptr, fp, buf, total-resid, 1) / size;
241 		}
242 #endif
243 	      _newlib_flockfile_exit (fp);
244 	      return (total - resid) / size;
245 	    }
246 	}
247       (void) memcpy ((void *) p, (void *) fp->_p, resid);
248       fp->_r -= resid;
249       fp->_p += resid;
250     }
251 
252   /* Perform any CR/LF clean-up if necessary.  */
253 #ifdef __SCLE
254   if (fp->_flags & __SCLE)
255     {
256       _newlib_flockfile_exit (fp);
257       return crlf_r(ptr, fp, buf, total, 0) / size;
258     }
259 #endif
260 ret:
261   _newlib_flockfile_end (fp);
262   return count;
263 }
264