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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [fread.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1010 ivang
/*
2
 * Copyright (c) 1990 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
 
25
ANSI_SYNOPSIS
26
        #include <stdio.h>
27
        size_t fread(void *<[buf]>, size_t <[size]>, size_t <[count]>,
28
                     FILE *<[fp]>);
29
 
30
TRAD_SYNOPSIS
31
        #include <stdio.h>
32
        size_t fread(<[buf]>, <[size]>, <[count]>, <[fp]>)
33
        char *<[buf]>;
34
        size_t <[size]>;
35
        size_t <[count]>;
36
        FILE *<[fp]>;
37
 
38
DESCRIPTION
39
<<fread>> attempts to copy, from the file or stream identified by
40
<[fp]>, <[count]> elements (each of size <[size]>) into memory,
41
starting at <[buf]>.   <<fread>> may copy fewer elements than
42
<[count]> if an error, or end of file, intervenes.
43
 
44
<<fread>> also advances the file position indicator (if any) for
45
<[fp]> by the number of @emph{characters} actually read.
46
 
47
RETURNS
48
The result of <<fread>> is the number of elements it succeeded in
49
reading.
50
 
51
PORTABILITY
52
ANSI C requires <<fread>>.
53
 
54
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
55
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
56
*/
57
 
58
#include <stdio.h>
59
#include <string.h>
60
#include "local.h"
61
 
62
#ifdef __SCLE
63
static size_t
64
_DEFUN (crlf, (fp, buf, count, eof),
65
      FILE * fp _AND
66
      char * buf _AND
67
      size_t count _AND
68
      int eof)
69
{
70
  int newcount = 0, r;
71
  char *s, *d, *e;
72
 
73
  if (count == 0)
74
    return 0;
75
 
76
  e = buf + count;
77
  for (s=d=buf; s<e-1; s++)
78
    {
79
      if (*s == '\r' && s[1] == '\n')
80
      s++;
81
      *d++ = *s;
82
    }
83
  if (s < e)
84
    {
85
      if (*s == '\r')
86
      {
87
        int c = __sgetc_raw(fp);
88
        if (c == '\n')
89
          *s = '\n';
90
        else
91
          ungetc(c, fp);
92
      }
93
      *d++ = *s++;
94
    }
95
 
96
 
97
  while (d < e)
98
    {
99
      r = getc(fp);
100
      if (r == EOF)
101
      return count - (e-d);
102
      *d++ = r;
103
    }
104
 
105
  return count;
106
 
107
}
108
 
109
#endif
110
 
111
size_t
112
_DEFUN (fread, (buf, size, count, fp),
113
        _PTR buf _AND
114
        size_t size _AND
115
        size_t count _AND
116
        FILE * fp)
117
{
118
  register size_t resid;
119
  register char *p;
120
  register int r;
121
  size_t total;
122
 
123
  if ((resid = count * size) == 0)
124
    return 0;
125
  if (fp->_r < 0)
126
    fp->_r = 0;
127
  total = resid;
128
  p = buf;
129
 
130
  while (resid > (r = fp->_r))
131
    {
132
      (void) memcpy ((void *) p, (void *) fp->_p, (size_t) r);
133
      fp->_p += r;
134
      /* fp->_r = 0 ... done in __srefill */
135
      p += r;
136
      resid -= r;
137
      if (__srefill (fp))
138
        {
139
          /* no more input: return partial result */
140
#ifdef __SCLE
141
        if (fp->_flags & __SCLE)
142
            return crlf(fp, buf, total-resid, 1) / size;
143
#endif
144
          return (total - resid) / size;
145
        }
146
    }
147
  (void) memcpy ((void *) p, (void *) fp->_p, resid);
148
  fp->_r -= resid;
149
  fp->_p += resid;
150
#ifdef __SCLE
151
  if (fp->_flags & __SCLE)
152
    return crlf(fp, buf, total, 0) / size;
153
#endif
154
  return count;
155
}

powered by: WebSVN 2.1.0

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