1/*	$OpenBSD: fread.c,v 1.6 2005/08/08 08:05:36 espie Exp $ */
2/*-
3 * Copyright (c) 1990, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <stdio.h>
35#include <string.h>
36#include <errno.h>
37#include "local.h"
38
39static int
40lflush(FILE *fp)
41{
42
43    if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR))
44        return (__sflush(fp));
45    return (0);
46}
47
48size_t
49fread(void *buf, size_t size, size_t count, FILE *fp)
50{
51    size_t resid;
52    char *p;
53    int r;
54    size_t total;
55
56    /*
57     * The ANSI standard requires a return value of 0 for a count
58     * or a size of 0.  Peculiarily, it imposes no such requirements
59     * on fwrite; it only requires fread to be broken.
60     */
61    if ((resid = count * size) == 0)
62        return (0);
63    if (fp->_r < 0)
64        fp->_r = 0;
65    total = resid;
66    p = buf;
67
68#if 1  /* BIONIC: optimize unbuffered reads */
69    if (fp->_flags & __SNBF && fp->_ur == 0)
70    {
71        /* the following comes mainly from __srefill(), with slight
72         * modifications
73         */
74
75        /* make sure stdio is set up */
76        if (!__sdidinit)
77            __sinit();
78
79        fp->_r = 0;     /* largely a convenience for callers */
80
81        /* SysV does not make this test; take it out for compatibility */
82        if (fp->_flags & __SEOF)
83            return (EOF);
84
85        /* if not already reading, have to be reading and writing */
86        if ((fp->_flags & __SRD) == 0) {
87            if ((fp->_flags & __SRW) == 0) {
88                errno = EBADF;
89                fp->_flags |= __SERR;
90                return (EOF);
91            }
92            /* switch to reading */
93            if (fp->_flags & __SWR) {
94                if (__sflush(fp))
95                    return (EOF);
96                fp->_flags &= ~__SWR;
97                fp->_w = 0;
98                fp->_lbfsize = 0;
99            }
100            fp->_flags |= __SRD;
101        } else {
102            /*
103             * We were reading.  If there is an ungetc buffer,
104             * we must have been reading from that.  Drop it,
105             * restoring the previous buffer (if any).  If there
106             * is anything in that buffer, return.
107             */
108            if (HASUB(fp)) {
109                FREEUB(fp);
110            }
111        }
112
113        /*
114         * Before reading from a line buffered or unbuffered file,
115         * flush all line buffered output files, per the ANSI C
116         * standard.
117         */
118
119        if (fp->_flags & (__SLBF|__SNBF))
120            (void) _fwalk(lflush);
121
122        while (resid > 0) {
123            int   len = (*fp->_read)(fp->_cookie, p, resid );
124            fp->_flags &= ~__SMOD;
125            if (len <= 0) {
126                if (len == 0)
127                    fp->_flags |= __SEOF;
128                else {
129                    fp->_flags |= __SERR;
130                }
131                return ((total - resid) / size);
132            }
133            p     += len;
134            resid -= len;
135        }
136        return (count);
137    }
138    else
139#endif
140    {
141        while (resid > (size_t)(r = fp->_r)) {
142            (void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
143            fp->_p += r;
144            /* fp->_r = 0 ... done in __srefill */
145            p += r;
146            resid -= r;
147            if (__srefill(fp)) {
148                /* no more input: return partial result */
149                return ((total - resid) / size);
150            }
151        }
152    }
153
154    (void)memcpy((void *)p, (void *)fp->_p, resid);
155    fp->_r -= resid;
156    fp->_p += resid;
157    return (count);
158}
159