Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / fread.c
blob8664dc3e5ee25fa15ab3d145696aee1be50cffa3
1 /*
2 * Copyright (c) 1990, 2007 The Regents of the University of California.
3 * All rights reserved.
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.
19 FUNCTION
20 <<fread>>, <<fread_unlocked>>---read array elements from a file
22 INDEX
23 fread
24 INDEX
25 fread_unlocked
26 INDEX
27 _fread_r
28 INDEX
29 _fread_unlocked_r
31 SYNOPSIS
32 #include <stdio.h>
33 size_t fread(void *restrict <[buf]>, size_t <[size]>, size_t <[count]>,
34 FILE *restrict <[fp]>);
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]>);
41 #include <stdio.h>
42 size_t _fread_r(struct _reent *<[ptr]>, void *restrict <[buf]>,
43 size_t <[size]>, size_t <[count]>, FILE *restrict <[fp]>);
45 #include <stdio.h>
46 size_t _fread_unlocked_r(struct _reent *<[ptr]>, void *restrict <[buf]>,
47 size_t <[size]>, size_t <[count]>, FILE *restrict <[fp]>);
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.
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]>.
70 RETURNS
71 The result of <<fread>> is the number of elements it succeeded in
72 reading.
74 PORTABILITY
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>>.
83 #include <_ansi.h>
84 #include <stdio.h>
85 #include <string.h>
86 #include <malloc.h>
87 #include "local.h"
89 #ifdef __IMPL_UNLOCKED__
90 #define _fread_r _fread_unlocked_r
91 #define fread fread_unlocked
92 #endif
94 #ifdef __SCLE
95 static size_t
96 crlf_r (struct _reent * ptr,
97 FILE * fp,
98 char * buf,
99 size_t count,
100 int eof)
102 int r;
103 char *s, *d, *e;
105 if (count == 0)
106 return 0;
108 e = buf + count;
109 for (s=d=buf; s<e-1; s++)
111 if (*s == '\r' && s[1] == '\n')
112 s++;
113 *d++ = *s;
115 if (s < e)
117 if (*s == '\r')
119 int c = __sgetc_raw_r (ptr, fp);
120 if (c == '\n')
121 *s = '\n';
122 else
123 ungetc (c, fp);
125 *d++ = *s++;
129 while (d < e)
131 r = _getc_r (ptr, fp);
132 if (r == EOF)
133 return count - (e-d);
134 *d++ = r;
137 return count;
141 #endif
143 size_t
144 _fread_r (struct _reent * ptr,
145 void *__restrict buf,
146 size_t size,
147 size_t count,
148 FILE * __restrict fp)
150 register size_t resid;
151 register char *p;
152 register int r;
153 size_t total;
155 if ((resid = count * size) == 0)
156 return 0;
158 CHECK_INIT(ptr, fp);
160 _newlib_flockfile_start (fp);
161 if (ORIENT (fp, -1) != -1)
163 count = 0;
164 goto ret;
166 if (fp->_r < 0)
167 fp->_r = 0;
168 total = resid;
169 p = buf;
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);
179 fp->_p += copy_size;
180 fp->_r -= copy_size;
181 p += copy_size;
182 resid -= copy_size;
184 /* If still more data needed, free any allocated ungetc buffer. */
185 if (HASUB (fp) && resid > 0)
186 FREEUB (ptr, fp);
188 /* Finally read directly into user's buffer if needed. */
189 while (resid > 0)
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_r (ptr, 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)
210 #ifdef __SCLE
211 if (fp->_flags & __SCLE)
213 _newlib_flockfile_exit (fp);
214 return crlf_r (ptr, fp, buf, total-resid, 1) / size;
216 #endif
217 _newlib_flockfile_exit (fp);
218 return (total - resid) / size;
222 else
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);
228 fp->_p += r;
229 /* fp->_r = 0 ... done in __srefill */
230 p += r;
231 resid -= r;
232 if (__srefill_r (ptr, fp))
234 /* no more input: return partial result */
235 #ifdef __SCLE
236 if (fp->_flags & __SCLE)
238 _newlib_flockfile_exit (fp);
239 return crlf_r (ptr, fp, buf, total-resid, 1) / size;
241 #endif
242 _newlib_flockfile_exit (fp);
243 return (total - resid) / size;
246 (void) memcpy ((void *) p, (void *) fp->_p, resid);
247 fp->_r -= resid;
248 fp->_p += resid;
251 /* Perform any CR/LF clean-up if necessary. */
252 #ifdef __SCLE
253 if (fp->_flags & __SCLE)
255 _newlib_flockfile_exit (fp);
256 return crlf_r(ptr, fp, buf, total, 0) / size;
258 #endif
259 ret:
260 _newlib_flockfile_end (fp);
261 return count;
264 #ifndef _REENT_ONLY
265 size_t
266 fread (void *__restrict buf,
267 size_t size,
268 size_t count,
269 FILE *__restrict fp)
271 return _fread_r (_REENT, buf, size, count, fp);
273 #endif