netinet/in.h: add IPPROTO_MH
[uclibc-ng.git] / libc / stdio / _READ.c
bloba3c1413851f01381fa5b8069ccec7423cc9cae3d
1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
6 */
8 #include "_stdio.h"
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.
17 * Returns the number of bytes read, even in EOF and error cases.
19 * Notes:
20 * Calling with bufsize == 0 is NOT permitted (unlike __stdio_WRITE).
21 * NOT THREADSAFE! Assumes stream already locked if necessary.
24 size_t attribute_hidden __stdio_READ(register FILE *stream,
25 unsigned char *buf, size_t bufsize)
27 ssize_t rv = 0;
29 __STDIO_STREAM_VALIDATE(stream);
30 assert(stream->__filedes >= -2);
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);
36 if (!__FEOF_UNLOCKED(stream)) {
37 if (bufsize > SSIZE_MAX) {
38 bufsize = SSIZE_MAX;
41 /* RETRY: */
42 if ((rv = __READ(stream, (char *) buf, bufsize)) <= 0) {
43 if (rv == 0) {
44 __STDIO_STREAM_SET_EOF(stream);
45 } else {
46 /* if (errno == EINTR) goto RETRY; */
47 __STDIO_STREAM_SET_ERROR(stream);
48 rv = 0;
50 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
51 } else {
52 assert(rv <= bufsize);
53 if (rv > bufsize) { /* Read more than bufsize! */
54 abort();
56 #endif
60 return rv;