|
1 /* FREAD.C |
|
2 * |
|
3 * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies). |
|
4 * All rights reserved. |
|
5 */ |
|
6 |
|
7 /* |
|
8 * Copyright (c) 1990 The Regents of the University of California. |
|
9 * All rights reserved. |
|
10 * |
|
11 * Redistribution and use in source and binary forms are permitted |
|
12 * provided that the above copyright notice and this paragraph are |
|
13 * duplicated in all such forms and that any documentation, |
|
14 * advertising materials, and other materials related to such |
|
15 * distribution and use acknowledge that the software was developed |
|
16 * by the University of California, Berkeley. The name of the |
|
17 * University may not be used to endorse or promote products derived |
|
18 * from this software without specific prior written permission. |
|
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
|
20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
|
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
|
22 */ |
|
23 |
|
24 /* |
|
25 FUNCTION |
|
26 <<fread>>---read array elements from a file |
|
27 |
|
28 INDEX |
|
29 fread |
|
30 |
|
31 ANSI_SYNOPSIS |
|
32 #include <stdio.h> |
|
33 size_t fread(void *<[buf]>, size_t <[size]>, size_t <[count]>, |
|
34 FILE *<[fp]>); |
|
35 |
|
36 TRAD_SYNOPSIS |
|
37 #include <stdio.h> |
|
38 size_t fread(<[buf]>, <[size]>, <[count]>, <[fp]>) |
|
39 char *<[buf]>; |
|
40 size_t <[size]>; |
|
41 size_t <[count]>; |
|
42 FILE *<[fp]>; |
|
43 |
|
44 DESCRIPTION |
|
45 <<fread>> attempts to copy, from the file or stream identified by |
|
46 <[fp]>, <[count]> elements (each of size <[size]>) into memory, |
|
47 starting at <[buf]>. <<fread>> may copy fewer elements than |
|
48 <[count]> if an error, or end of file, intervenes. |
|
49 |
|
50 <<fread>> also advances the file position indicator (if any) for |
|
51 <[fp]> by the number of @emph{characters} actually read. |
|
52 |
|
53 RETURNS |
|
54 The result of <<fread>> is the number of elements it succeeded in |
|
55 reading. |
|
56 |
|
57 PORTABILITY |
|
58 ANSI C requires <<fread>>. |
|
59 |
|
60 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>, |
|
61 <<lseek>>, <<read>>, <<sbrk>>, <<write>>. |
|
62 */ |
|
63 |
|
64 #include <stdio.h> |
|
65 #include <string.h> |
|
66 #include "LOCAL.H" |
|
67 |
|
68 /** |
|
69 Read block of data from a stream. |
|
70 Read count number of items each one with a size of size bytes from the stream |
|
71 and stores it in the specified buffer. |
|
72 Stream's postion indicator is increased by the number of bytes readed. |
|
73 Total amount of bytes read is (size x count). |
|
74 @return The total number of items readed is returned. |
|
75 @param buf Pointer to the destination structure with a minimum size of (size*count) bytes. |
|
76 @param size Size in bytes of each item to be read. |
|
77 @param count Number of items, each one with a size of size bytes. |
|
78 @param fp pointer to an open file. |
|
79 */ |
|
80 EXPORT_C size_t |
|
81 fread (void *buf, size_t size, size_t count, FILE * fp) |
|
82 { |
|
83 register size_t resid; |
|
84 register char *p; |
|
85 register int r; |
|
86 size_t total; |
|
87 |
|
88 if ((resid = count * size) == 0) |
|
89 return 0; |
|
90 if (fp->_r < 0) |
|
91 fp->_r = 0; |
|
92 total = resid; |
|
93 p = (char*)buf; |
|
94 while ((int)resid > (r = fp->_r)) |
|
95 { |
|
96 (void) memcpy ((void *) p, (void *) fp->_p, (size_t) r); |
|
97 fp->_p += r; |
|
98 /* fp->_r = 0 ... done in __srefill */ |
|
99 p += r; |
|
100 resid -= r; |
|
101 if (__srefill (fp)) |
|
102 { |
|
103 /* no more input: return partial result */ |
|
104 return (total - resid) / size; |
|
105 } |
|
106 } |
|
107 (void) memcpy ((void *) p, (void *) fp->_p, resid); |
|
108 fp->_r -= resid; |
|
109 fp->_p += resid; |
|
110 return count; |
|
111 } |