1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
2 *
3 * GNU Library General Public License (LGPL) version 2 or later.
4 *
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
6 */
7
8 #include "_stdio.h"
9
10
11 /* Given a reading stream without its end-of-file indicator set and
12 * with no buffered input or ungots, read at most 'bufsize' bytes
13 * into 'buf' (which may be the stream's __bufstart).
14 * If a read error occurs, set the stream's error indicator.
15 * If EOF is encountered, set the stream's end-of-file indicator.
16 *
17 * Returns the number of bytes read, even in EOF and error cases.
18 *
19 * Notes:
20 * Calling with bufsize == 0 is NOT permitted (unlike __stdio_WRITE).
21 * NOT THREADSAFE! Assumes stream already locked if necessary.
22 */
23
__stdio_READ(register FILE * stream,unsigned char * buf,size_t bufsize)24 size_t attribute_hidden __stdio_READ(register FILE *stream,
25 unsigned char *buf, size_t bufsize)
26 {
27 ssize_t rv = 0;
28
29 __STDIO_STREAM_VALIDATE(stream);
30 assert(stream->__filedes >= -1);
31 assert(__STDIO_STREAM_IS_READING(stream));
32 assert(!__STDIO_STREAM_BUFFER_RAVAIL(stream)); /* Buffer must be empty. */
33 assert(!(stream->__modeflags & __FLAG_UNGOT));
34 assert(bufsize);
35
36 if (!__FEOF_UNLOCKED(stream)) {
37 if (bufsize > SSIZE_MAX) {
38 bufsize = SSIZE_MAX;
39 }
40
41 #ifdef __UCLIBC_MJN3_ONLY__
42 #warning EINTR?
43 #endif
44 /* RETRY: */
45 if ((rv = __READ(stream, (char *) buf, bufsize)) <= 0) {
46 if (rv == 0) {
47 __STDIO_STREAM_SET_EOF(stream);
48 } else {
49 /* if (errno == EINTR) goto RETRY; */
50 __STDIO_STREAM_SET_ERROR(stream);
51 rv = 0;
52 }
53 #ifdef __UCLIBC_MJN3_ONLY__
54 #warning TODO: Make custom stream read return check optional.
55 #endif
56 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
57 } else {
58 assert(rv <= bufsize);
59 if (rv > bufsize) { /* Read more than bufsize! */
60 abort();
61 }
62 #endif
63 }
64 }
65
66 return rv;
67 }
68