Commit | Line | Data |
---|---|---|
411867e7 KB |
1 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Chris Torek. | |
7 | * | |
8 | * %sccs.include.redist.c% | |
586c39b1 DF |
9 | */ |
10 | ||
2ce81398 | 11 | #if defined(LIBC_SCCS) && !defined(lint) |
411867e7 KB |
12 | static char sccsid[] = "@(#)fread.c 5.3 (Berkeley) %G%"; |
13 | #endif /* LIBC_SCCS and not lint */ | |
586c39b1 | 14 | |
411867e7 KB |
15 | #include <stdio.h> |
16 | #include <string.h> | |
637f6ac3 | 17 | |
411867e7 KB |
18 | fread(buf, size, count, fp) |
19 | void *buf; | |
20 | size_t size, count; | |
21 | register FILE *fp; | |
637f6ac3 | 22 | { |
411867e7 KB |
23 | register size_t resid; |
24 | register char *p; | |
25 | register int r; | |
26 | size_t total; | |
637f6ac3 | 27 | |
411867e7 KB |
28 | if ((resid = count * size) == 0) |
29 | return (count); | |
30 | if (fp->_r < 0) | |
31 | fp->_r = 0; | |
32 | total = resid; | |
33 | p = buf; | |
34 | while (resid > (r = fp->_r)) { | |
35 | (void) memcpy((void *)p, (void *)fp->_p, (size_t)r); | |
36 | fp->_p += r; | |
37 | /* fp->_r = 0 ... done in __srefill */ | |
38 | p += r; | |
39 | resid -= r; | |
40 | if (__srefill(fp)) { | |
41 | /* no more input: return partial result */ | |
42 | return ((total - resid) / size); | |
637f6ac3 MK |
43 | } |
44 | } | |
411867e7 KB |
45 | (void) memcpy((void *)p, (void *)fp->_p, resid); |
46 | fp->_r -= resid; | |
47 | fp->_p += resid; | |
48 | return (count); | |
637f6ac3 | 49 | } |