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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [fread.c] - Diff between revs 1010 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 1010 Rev 1765
/*
/*
 * Copyright (c) 1990 The Regents of the University of California.
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 * All rights reserved.
 *
 *
 * Redistribution and use in source and binary forms are permitted
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * distribution and use acknowledge that the software was developed
 * by the University of California, Berkeley.  The name of the
 * by the University of California, Berkeley.  The name of the
 * University may not be used to endorse or promote products derived
 * University may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 * from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
 */
 
 
/*
/*
FUNCTION
FUNCTION
<<fread>>---read array elements from a file
<<fread>>---read array elements from a file
 
 
INDEX
INDEX
        fread
        fread
 
 
ANSI_SYNOPSIS
ANSI_SYNOPSIS
        #include <stdio.h>
        #include <stdio.h>
        size_t fread(void *<[buf]>, size_t <[size]>, size_t <[count]>,
        size_t fread(void *<[buf]>, size_t <[size]>, size_t <[count]>,
                     FILE *<[fp]>);
                     FILE *<[fp]>);
 
 
TRAD_SYNOPSIS
TRAD_SYNOPSIS
        #include <stdio.h>
        #include <stdio.h>
        size_t fread(<[buf]>, <[size]>, <[count]>, <[fp]>)
        size_t fread(<[buf]>, <[size]>, <[count]>, <[fp]>)
        char *<[buf]>;
        char *<[buf]>;
        size_t <[size]>;
        size_t <[size]>;
        size_t <[count]>;
        size_t <[count]>;
        FILE *<[fp]>;
        FILE *<[fp]>;
 
 
DESCRIPTION
DESCRIPTION
<<fread>> attempts to copy, from the file or stream identified by
<<fread>> attempts to copy, from the file or stream identified by
<[fp]>, <[count]> elements (each of size <[size]>) into memory,
<[fp]>, <[count]> elements (each of size <[size]>) into memory,
starting at <[buf]>.   <<fread>> may copy fewer elements than
starting at <[buf]>.   <<fread>> may copy fewer elements than
<[count]> if an error, or end of file, intervenes.
<[count]> if an error, or end of file, intervenes.
 
 
<<fread>> also advances the file position indicator (if any) for
<<fread>> also advances the file position indicator (if any) for
<[fp]> by the number of @emph{characters} actually read.
<[fp]> by the number of @emph{characters} actually read.
 
 
RETURNS
RETURNS
The result of <<fread>> is the number of elements it succeeded in
The result of <<fread>> is the number of elements it succeeded in
reading.
reading.
 
 
PORTABILITY
PORTABILITY
ANSI C requires <<fread>>.
ANSI C requires <<fread>>.
 
 
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
*/
*/
 
 
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include "local.h"
#include "local.h"
 
 
#ifdef __SCLE
#ifdef __SCLE
static size_t
static size_t
_DEFUN (crlf, (fp, buf, count, eof),
_DEFUN (crlf, (fp, buf, count, eof),
      FILE * fp _AND
      FILE * fp _AND
      char * buf _AND
      char * buf _AND
      size_t count _AND
      size_t count _AND
      int eof)
      int eof)
{
{
  int newcount = 0, r;
  int newcount = 0, r;
  char *s, *d, *e;
  char *s, *d, *e;
 
 
  if (count == 0)
  if (count == 0)
    return 0;
    return 0;
 
 
  e = buf + count;
  e = buf + count;
  for (s=d=buf; s<e-1; s++)
  for (s=d=buf; s<e-1; s++)
    {
    {
      if (*s == '\r' && s[1] == '\n')
      if (*s == '\r' && s[1] == '\n')
      s++;
      s++;
      *d++ = *s;
      *d++ = *s;
    }
    }
  if (s < e)
  if (s < e)
    {
    {
      if (*s == '\r')
      if (*s == '\r')
      {
      {
        int c = __sgetc_raw(fp);
        int c = __sgetc_raw(fp);
        if (c == '\n')
        if (c == '\n')
          *s = '\n';
          *s = '\n';
        else
        else
          ungetc(c, fp);
          ungetc(c, fp);
      }
      }
      *d++ = *s++;
      *d++ = *s++;
    }
    }
 
 
 
 
  while (d < e)
  while (d < e)
    {
    {
      r = getc(fp);
      r = getc(fp);
      if (r == EOF)
      if (r == EOF)
      return count - (e-d);
      return count - (e-d);
      *d++ = r;
      *d++ = r;
    }
    }
 
 
  return count;
  return count;
 
 
}
}
 
 
#endif
#endif
 
 
size_t
size_t
_DEFUN (fread, (buf, size, count, fp),
_DEFUN (fread, (buf, size, count, fp),
        _PTR buf _AND
        _PTR buf _AND
        size_t size _AND
        size_t size _AND
        size_t count _AND
        size_t count _AND
        FILE * fp)
        FILE * fp)
{
{
  register size_t resid;
  register size_t resid;
  register char *p;
  register char *p;
  register int r;
  register int r;
  size_t total;
  size_t total;
 
 
  if ((resid = count * size) == 0)
  if ((resid = count * size) == 0)
    return 0;
    return 0;
  if (fp->_r < 0)
  if (fp->_r < 0)
    fp->_r = 0;
    fp->_r = 0;
  total = resid;
  total = resid;
  p = buf;
  p = buf;
 
 
  while (resid > (r = fp->_r))
  while (resid > (r = fp->_r))
    {
    {
      (void) memcpy ((void *) p, (void *) fp->_p, (size_t) r);
      (void) memcpy ((void *) p, (void *) fp->_p, (size_t) r);
      fp->_p += r;
      fp->_p += r;
      /* fp->_r = 0 ... done in __srefill */
      /* fp->_r = 0 ... done in __srefill */
      p += r;
      p += r;
      resid -= r;
      resid -= r;
      if (__srefill (fp))
      if (__srefill (fp))
        {
        {
          /* no more input: return partial result */
          /* no more input: return partial result */
#ifdef __SCLE
#ifdef __SCLE
        if (fp->_flags & __SCLE)
        if (fp->_flags & __SCLE)
            return crlf(fp, buf, total-resid, 1) / size;
            return crlf(fp, buf, total-resid, 1) / size;
#endif
#endif
          return (total - resid) / size;
          return (total - resid) / size;
        }
        }
    }
    }
  (void) memcpy ((void *) p, (void *) fp->_p, resid);
  (void) memcpy ((void *) p, (void *) fp->_p, resid);
  fp->_r -= resid;
  fp->_r -= resid;
  fp->_p += resid;
  fp->_p += resid;
#ifdef __SCLE
#ifdef __SCLE
  if (fp->_flags & __SCLE)
  if (fp->_flags & __SCLE)
    return crlf(fp, buf, total, 0) / size;
    return crlf(fp, buf, total, 0) / size;
#endif
#endif
  return count;
  return count;
}
}
 
 

powered by: WebSVN 2.1.0

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