OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libc/] [stdio/] [fread.c] - Blame information for rev 148

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/*
2
 * Copyright (c) 1990, 2007 The Regents of the University of California.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms are permitted
6
 * provided that the above copyright notice and this paragraph are
7
 * duplicated in all such forms and that any documentation,
8
 * advertising materials, and other materials related to such
9
 * distribution and use acknowledge that the software was developed
10
 * by the University of California, Berkeley.  The name of the
11
 * University may not be used to endorse or promote products derived
12
 * from this software without specific prior written permission.
13
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16
 */
17
 
18
/*
19
FUNCTION
20
<<fread>>---read array elements from a file
21
 
22
INDEX
23
        fread
24
INDEX
25
        _fread_r
26
 
27
ANSI_SYNOPSIS
28
        #include <stdio.h>
29
        size_t fread(void *<[buf]>, size_t <[size]>, size_t <[count]>,
30
                     FILE *<[fp]>);
31
 
32
        #include <stdio.h>
33
        size_t _fread_r(struct _reent *<[ptr]>, void *<[buf]>,
34
                        size_t <[size]>, size_t <[count]>, 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
        #include <stdio.h>
45
        size_t _fread_r(<[ptr]>, <[buf]>, <[size]>, <[count]>, <[fp]>)
46
        struct _reent *<[ptr]>;
47
        char *<[buf]>;
48
        size_t <[size]>;
49
        size_t <[count]>;
50
        FILE *<[fp]>;
51
 
52
DESCRIPTION
53
<<fread>> attempts to copy, from the file or stream identified by
54
<[fp]>, <[count]> elements (each of size <[size]>) into memory,
55
starting at <[buf]>.   <<fread>> may copy fewer elements than
56
<[count]> if an error, or end of file, intervenes.
57
 
58
<<fread>> also advances the file position indicator (if any) for
59
<[fp]> by the number of @emph{characters} actually read.
60
 
61
<<_fread_r>> is simply the reentrant version of <<fread>> that
62
takes an additional reentrant structure pointer argument: <[ptr]>.
63
 
64
RETURNS
65
The result of <<fread>> is the number of elements it succeeded in
66
reading.
67
 
68
PORTABILITY
69
ANSI C requires <<fread>>.
70
 
71
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
72
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
73
*/
74
 
75
#include <_ansi.h>
76
#include <stdio.h>
77
#include <string.h>
78
#include <malloc.h>
79
#include "local.h"
80
 
81
#ifdef __SCLE
82
static size_t
83
_DEFUN(crlf_r, (ptr, fp, buf, count, eof),
84
       struct _reent * ptr _AND
85
       FILE * fp _AND
86
       char * buf _AND
87
       size_t count _AND
88
       int eof)
89
{
90
  int r;
91
  char *s, *d, *e;
92
 
93
  if (count == 0)
94
    return 0;
95
 
96
  e = buf + count;
97
  for (s=d=buf; s<e-1; s++)
98
    {
99
      if (*s == '\r' && s[1] == '\n')
100
        s++;
101
      *d++ = *s;
102
    }
103
  if (s < e)
104
    {
105
      if (*s == '\r')
106
        {
107
          int c = __sgetc_raw_r (ptr, fp);
108
          if (c == '\n')
109
            *s = '\n';
110
          else
111
            ungetc (c, fp);
112
        }
113
      *d++ = *s++;
114
    }
115
 
116
 
117
  while (d < e)
118
    {
119
      r = _getc_r (ptr, fp);
120
      if (r == EOF)
121
        return count - (e-d);
122
      *d++ = r;
123
    }
124
 
125
  return count;
126
 
127
}
128
 
129
#endif
130
 
131
size_t
132
_DEFUN(_fread_r, (ptr, buf, size, count, fp),
133
       struct _reent * ptr _AND
134
       _PTR buf _AND
135
       size_t size _AND
136
       size_t count _AND
137
       FILE * fp)
138
{
139
  register size_t resid;
140
  register char *p;
141
  register int r;
142
  size_t total;
143
 
144
  if ((resid = count * size) == 0)
145
    return 0;
146
 
147
  CHECK_INIT(ptr, fp);
148
 
149
  _flockfile (fp);
150
  ORIENT (fp, -1);
151
  if (fp->_r < 0)
152
    fp->_r = 0;
153
  total = resid;
154
  p = buf;
155
 
156
#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
157
 
158
  /* Optimize unbuffered I/O.  */
159
  if (fp->_flags & __SNBF)
160
    {
161
      /* First copy any available characters from ungetc buffer.  */
162
      int copy_size = resid > fp->_r ? fp->_r : resid;
163
      _CAST_VOID memcpy ((_PTR) p, (_PTR) fp->_p, (size_t) copy_size);
164
      fp->_p += copy_size;
165
      fp->_r -= copy_size;
166
      p += copy_size;
167
      resid -= copy_size;
168
 
169
      /* If still more data needed, free any allocated ungetc buffer.  */
170
      if (HASUB (fp) && resid > 0)
171
        FREEUB (ptr, fp);
172
 
173
      /* Finally read directly into user's buffer if needed.  */
174
      while (resid > 0)
175
        {
176
          int rc = 0;
177
          /* save fp buffering state */
178
          void *old_base = fp->_bf._base;
179
          void * old_p = fp->_p;
180
          int old_size = fp->_bf._size;
181
          /* allow __refill to use user's buffer */
182
          fp->_bf._base = p;
183
          fp->_bf._size = resid;
184
          fp->_p = p;
185
          rc = __srefill_r (ptr, fp);
186
          /* restore fp buffering back to original state */
187
          fp->_bf._base = old_base;
188
          fp->_bf._size = old_size;
189
          fp->_p = old_p;
190
          resid -= fp->_r;
191
          p += fp->_r;
192
          fp->_r = 0;
193
          if (rc)
194
            {
195
#ifdef __SCLE
196
              if (fp->_flags & __SCLE)
197
                {
198
                  _funlockfile (fp);
199
                  return crlf_r (ptr, fp, buf, total-resid, 1) / size;
200
                }
201
#endif
202
              _funlockfile (fp);
203
              return (total - resid) / size;
204
            }
205
        }
206
    }
207
  else
208
#endif /* !PREFER_SIZE_OVER_SPEED && !__OPTIMIZE_SIZE__ */
209
    {
210
      while (resid > (r = fp->_r))
211
        {
212
          _CAST_VOID memcpy ((_PTR) p, (_PTR) fp->_p, (size_t) r);
213
          fp->_p += r;
214
          /* fp->_r = 0 ... done in __srefill */
215
          p += r;
216
          resid -= r;
217
          if (__srefill_r (ptr, fp))
218
            {
219
              /* no more input: return partial result */
220
#ifdef __SCLE
221
              if (fp->_flags & __SCLE)
222
                {
223
                  _funlockfile (fp);
224
                  return crlf_r (ptr, fp, buf, total-resid, 1) / size;
225
                }
226
#endif
227
              _funlockfile (fp);
228
              return (total - resid) / size;
229
            }
230
        }
231
      _CAST_VOID memcpy ((_PTR) p, (_PTR) fp->_p, resid);
232
      fp->_r -= resid;
233
      fp->_p += resid;
234
    }
235
 
236
  /* Perform any CR/LF clean-up if necessary.  */
237
#ifdef __SCLE
238
  if (fp->_flags & __SCLE)
239
    {
240
      _funlockfile (fp);
241
      return crlf_r(ptr, fp, buf, total, 0) / size;
242
    }
243
#endif
244
  _funlockfile (fp);
245
  return count;
246
}
247
 
248
#ifndef _REENT_ONLY
249
size_t
250
_DEFUN(fread, (buf, size, count, fp),
251
       _PTR buf _AND
252
       size_t size _AND
253
       size_t count _AND
254
       FILE * fp)
255
{
256
   return _fread_r (_REENT, buf, size, count, fp);
257
}
258
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.