fread.c revision 35129
1227825Stheraven/*-
2227825Stheraven * Copyright (c) 1990, 1993
3227825Stheraven *	The Regents of the University of California.  All rights reserved.
4227825Stheraven *
5227825Stheraven * This code is derived from software contributed to Berkeley by
6227825Stheraven * Chris Torek.
7227825Stheraven *
8227825Stheraven * Redistribution and use in source and binary forms, with or without
9227825Stheraven * modification, are permitted provided that the following conditions
10227825Stheraven * are met:
11227825Stheraven * 1. Redistributions of source code must retain the above copyright
12227825Stheraven *    notice, this list of conditions and the following disclaimer.
13227825Stheraven * 2. Redistributions in binary form must reproduce the above copyright
14227825Stheraven *    notice, this list of conditions and the following disclaimer in the
15227825Stheraven *    documentation and/or other materials provided with the distribution.
16227825Stheraven * 3. All advertising materials mentioning features or use of this software
17227825Stheraven *    must display the following acknowledgement:
18227825Stheraven *	This product includes software developed by the University of
19227825Stheraven *	California, Berkeley and its contributors.
20227825Stheraven * 4. Neither the name of the University nor the names of its contributors
21227825Stheraven *    may be used to endorse or promote products derived from this software
22227825Stheraven *    without specific prior written permission.
23227825Stheraven *
24227825Stheraven * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25227825Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26227825Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27227825Stheraven * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28227825Stheraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29227825Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30227825Stheraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31227825Stheraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32227825Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33227825Stheraven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34227825Stheraven * SUCH DAMAGE.
35227825Stheraven */
36227825Stheraven
37227825Stheraven#if defined(LIBC_SCCS) && !defined(lint)
38227825Stheraven#if 0
39227825Stheravenstatic char sccsid[] = "@(#)fread.c	8.2 (Berkeley) 12/11/93";
40227825Stheraven#endif
41227825Stheravenstatic const char rcsid[] =
42227825Stheraven		"$Id: fread.c,v 1.5 1997/02/22 15:02:03 peter Exp $";
43227825Stheraven#endif /* LIBC_SCCS and not lint */
44227825Stheraven
45227825Stheraven#include <stdio.h>
46227825Stheraven#include <string.h>
47227825Stheraven#include "local.h"
48227825Stheraven#include "libc_private.h"
49227825Stheraven
50227825Stheravensize_t
51227825Stheravenfread(buf, size, count, fp)
52227825Stheraven	void *buf;
53227825Stheraven	size_t size, count;
54227825Stheraven	register FILE *fp;
55227825Stheraven{
56227825Stheraven	register size_t resid;
57227825Stheraven	register char *p;
58227825Stheraven	register int r;
59227825Stheraven	size_t total;
60227825Stheraven
61227825Stheraven	/*
62227825Stheraven	 * The ANSI standard requires a return value of 0 for a count
63227825Stheraven	 * or a size of 0.  Peculiarily, it imposes no such requirements
64227825Stheraven	 * on fwrite; it only requires fread to be broken.
65227825Stheraven	 */
66227825Stheraven	if ((resid = count * size) == 0)
67227825Stheraven		return (0);
68227825Stheraven	FLOCKFILE(fp);
69227825Stheraven	if (fp->_r < 0)
70227825Stheraven		fp->_r = 0;
71227825Stheraven	total = resid;
72227825Stheraven	p = buf;
73227825Stheraven	while (resid > (r = fp->_r)) {
74227825Stheraven		(void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
75227825Stheraven		fp->_p += r;
76227825Stheraven		/* fp->_r = 0 ... done in __srefill */
77227825Stheraven		p += r;
78227825Stheraven		resid -= r;
79227825Stheraven		if (__srefill(fp)) {
80227825Stheraven			/* no more input: return partial result */
81227825Stheraven			FUNLOCKFILE(fp);
82227825Stheraven			return ((total - resid) / size);
83227825Stheraven		}
84227825Stheraven	}
85227825Stheraven	(void)memcpy((void *)p, (void *)fp->_p, resid);
86227825Stheraven	fp->_r -= resid;
87227825Stheraven	fp->_p += resid;
88227825Stheraven	FUNLOCKFILE(fp);
89227825Stheraven	return (count);
90227825Stheraven}
91227825Stheraven