2 * Copyright (c) 1990, 2007 The Regents of the University of California.
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.
20 <<fread>>, <<fread_unlocked>>---read array elements from a file
33 size_t fread(void *restrict <[buf]>, size_t <[size]>, size_t <[count]>,
34 FILE *restrict <[fp]>);
38 size_t fread_unlocked(void *restrict <[buf]>, size_t <[size]>, size_t <[count]>,
39 FILE *restrict <[fp]>);
42 size_t _fread_r(struct _reent *<[ptr]>, void *restrict <[buf]>,
43 size_t <[size]>, size_t <[count]>, FILE *restrict <[fp]>);
46 size_t _fread_unlocked_r(struct _reent *<[ptr]>, void *restrict <[buf]>,
47 size_t <[size]>, size_t <[count]>, FILE *restrict <[fp]>);
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.
55 <<fread>> also advances the file position indicator (if any) for
56 <[fp]> by the number of @emph{characters} actually read.
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>>.
67 <<_fread_r>> and <<_fread_unlocked_r>> are simply reentrant versions of the
68 above that take an additional reentrant structure pointer argument: <[ptr]>.
71 The result of <<fread>> is the number of elements it succeeded in
75 ANSI C requires <<fread>>.
77 <<fread_unlocked>> is a BSD extension also provided by GNU libc.
79 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
80 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
89 #ifdef __IMPL_UNLOCKED__
90 #define _fread_r _fread_unlocked_r
91 #define fread fread_unlocked
96 crlf_r (struct _reent
* ptr
,
109 for (s
=d
=buf
; s
<e
-1; s
++)
111 if (*s
== '\r' && s
[1] == '\n')
119 int c
= __sgetc_raw_r (ptr
, fp
);
131 r
= _getc_r (ptr
, fp
);
133 return count
- (e
-d
);
144 _fread_r (struct _reent
* ptr
,
145 void *__restrict buf
,
148 FILE * __restrict fp
)
150 register size_t resid
;
155 if ((resid
= count
* size
) == 0)
160 _newlib_flockfile_start (fp
);
161 if (ORIENT (fp
, -1) != -1)
171 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
173 /* Optimize unbuffered I/O. */
174 if (fp
->_flags
& __SNBF
)
176 /* First copy any available characters from ungetc buffer. */
177 int copy_size
= resid
> fp
->_r
? fp
->_r
: resid
;
178 (void) memcpy ((void *) p
, (void *) fp
->_p
, (size_t) copy_size
);
184 /* If still more data needed, free any allocated ungetc buffer. */
185 if (HASUB (fp
) && resid
> 0)
188 /* Finally read directly into user's buffer if needed. */
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_r (ptr
, fp
);
201 /* restore fp buffering back to original state */
202 fp
->_bf
._base
= old_base
;
203 fp
->_bf
._size
= old_size
;
211 if (fp
->_flags
& __SCLE
)
213 _newlib_flockfile_exit (fp
);
214 return crlf_r (ptr
, fp
, buf
, total
-resid
, 1) / size
;
217 _newlib_flockfile_exit (fp
);
218 return (total
- resid
) / size
;
223 #endif /* !PREFER_SIZE_OVER_SPEED && !__OPTIMIZE_SIZE__ */
225 while (resid
> (r
= fp
->_r
))
227 (void) memcpy ((void *) p
, (void *) fp
->_p
, (size_t) r
);
229 /* fp->_r = 0 ... done in __srefill */
232 if (__srefill_r (ptr
, fp
))
234 /* no more input: return partial result */
236 if (fp
->_flags
& __SCLE
)
238 _newlib_flockfile_exit (fp
);
239 return crlf_r (ptr
, fp
, buf
, total
-resid
, 1) / size
;
242 _newlib_flockfile_exit (fp
);
243 return (total
- resid
) / size
;
246 (void) memcpy ((void *) p
, (void *) fp
->_p
, resid
);
251 /* Perform any CR/LF clean-up if necessary. */
253 if (fp
->_flags
& __SCLE
)
255 _newlib_flockfile_exit (fp
);
256 return crlf_r(ptr
, fp
, buf
, total
, 0) / size
;
260 _newlib_flockfile_end (fp
);
266 fread (void *__restrict buf
,
271 return _fread_r (_REENT
, buf
, size
, count
, fp
);