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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [newlib/] [newlib/] [libc/] [stdio/] [fread.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
/*
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
size_t
63
_DEFUN (fread, (buf, size, count, fp),
64
        _PTR buf _AND
65
        size_t size _AND
66
        size_t count _AND
67
        FILE * fp)
68
{
69
  register size_t resid;
70
  register char *p;
71
  register int r;
72
  size_t total;
73
 
74
  if ((resid = count * size) == 0)
75
    return 0;
76
  if (fp->_r < 0)
77
    fp->_r = 0;
78
  total = resid;
79
  p = buf;
80
  while (resid > (r = fp->_r))
81
    {
82
      (void) memcpy ((void *) p, (void *) fp->_p, (size_t) r);
83
      fp->_p += r;
84
      /* fp->_r = 0 ... done in __srefill */
85
      p += r;
86
      resid -= r;
87
      if (__srefill (fp))
88
        {
89
          /* no more input: return partial result */
90
          return (total - resid) / size;
91
        }
92
    }
93
  (void) memcpy ((void *) p, (void *) fp->_p, resid);
94
  fp->_r -= resid;
95
  fp->_p += resid;
96
  return count;
97
}

powered by: WebSVN 2.1.0

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