Update.
[platform/upstream/glibc.git] / stdio / fread.c
1 /* Copyright (C) 1991, 92, 95, 96, 97, 98 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
19 #include <errno.h>
20 #include <stdio.h>
21 #include <string.h>
22
23
24 #define default_func    __default_room_functions.__input
25
26 /* Read NMEMB chunks of SIZE bytes each from STREAM into P.  */
27 size_t
28 fread (p, size, nmemb, stream)
29      void *p;
30      size_t size;
31      size_t nmemb;
32      register FILE *stream;
33 {
34   register char *ptr = (char *) p;
35   register size_t to_read = size * nmemb;
36   size_t bytes = to_read;
37
38   if (!__validfp (stream) || !stream->__mode.__read)
39     {
40       __set_errno (EINVAL);
41       return 0;
42     }
43   if (feof (stream) || ferror (stream))
44     return 0;
45   if (p == NULL || to_read == 0)
46     return 0;
47
48   if (!stream->__seen || stream->__buffer == NULL || stream->__pushed_back)
49     {
50       /* This stream has never been seen before, or it has a character
51          pushed back.  Call __fillbf to deal with those cases.  Life will
52          be simpler after this call.  */
53       int c = __fillbf (stream);
54       if (c == EOF)
55         return 0;
56       *ptr++ = c;
57       if (--to_read == 0)
58         return 1;
59     }
60
61  read_from_buffer:;
62   if (stream->__bufp < stream->__get_limit)
63     {
64       /* First off, empty out the buffer.  */
65       register size_t copy = stream->__get_limit - stream->__bufp;
66       if (copy > to_read)
67         copy = to_read;
68       to_read -= copy;
69       if (copy > 20)
70         memcpy((void *) ptr, (void *) stream->__bufp, copy);
71       else
72         {
73           register size_t i;
74           for (i = 0; i < copy; ++i)
75             ptr[i] = stream->__bufp[i];
76         }
77       stream->__bufp += copy;
78       if (to_read == 0)
79         return nmemb;
80       ptr += copy;
81     }
82
83   /* Reading directly into the user's buffer doesn't help when
84      using a user-specified input buffer filling/expanding function,
85      so we don't do it in that case.  */
86   if (to_read >= stream->__bufsize &&
87       stream->__room_funcs.__input == default_func &&
88       stream->__offset == stream->__target)
89     {
90       /* Read directly into the user's buffer.  */
91       if (stream->__io_funcs.__read != NULL)
92         while (to_read > 0)
93           {
94             register int count;
95             count = (*stream->__io_funcs.__read) (stream->__cookie,
96                                                   ptr, to_read);
97             if (count > 0)
98               {
99                 to_read -= count;
100                 if (stream->__offset != -1)
101                   {
102                     stream->__offset += count;
103                     stream->__target += count;
104                   }
105                 ptr += count;
106               }
107             else if (count == 0)
108               {
109                 stream->__eof = 1;
110                 break;
111               }
112             else
113               {
114                 stream->__error = 1;
115                 break;
116               }
117           }
118       else
119         stream->__eof = 1;
120     }
121   else
122     {
123       int c = __fillbf (stream);
124       if (c == EOF)
125         return (bytes - to_read) / size;
126       *ptr++ = (char) c;
127       --to_read;
128       if (to_read > 0)
129         goto read_from_buffer;
130     }
131
132   return (bytes - to_read) / size;
133 }
134
135 weak_alias (fread, fread_unlocked)