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

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc2/] [newlib/] [libc/] [stdio/] [fgets.c] - Diff between revs 207 and 520

Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 520
/*
/*
 * 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
<<fgets>>---get character string from a file or stream
<<fgets>>---get character string from a file or stream
 
 
INDEX
INDEX
        fgets
        fgets
INDEX
INDEX
        _fgets_r
        _fgets_r
 
 
ANSI_SYNOPSIS
ANSI_SYNOPSIS
        #include <stdio.h>
        #include <stdio.h>
        char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>);
        char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>);
 
 
        #include <stdio.h>
        #include <stdio.h>
        char *_fgets_r(struct _reent *<[ptr]>, char *<[buf]>, int <[n]>, FILE *<[fp]>);
        char *_fgets_r(struct _reent *<[ptr]>, char *<[buf]>, int <[n]>, FILE *<[fp]>);
 
 
TRAD_SYNOPSIS
TRAD_SYNOPSIS
        #include <stdio.h>
        #include <stdio.h>
        char *fgets(<[buf]>,<[n]>,<[fp]>)
        char *fgets(<[buf]>,<[n]>,<[fp]>)
        char *<[buf]>;
        char *<[buf]>;
        int <[n]>;
        int <[n]>;
        FILE *<[fp]>;
        FILE *<[fp]>;
 
 
        #include <stdio.h>
        #include <stdio.h>
        char *_fgets_r(<[ptr]>, <[buf]>,<[n]>,<[fp]>)
        char *_fgets_r(<[ptr]>, <[buf]>,<[n]>,<[fp]>)
        struct _reent *<[ptr]>;
        struct _reent *<[ptr]>;
        char *<[buf]>;
        char *<[buf]>;
        int <[n]>;
        int <[n]>;
        FILE *<[fp]>;
        FILE *<[fp]>;
 
 
DESCRIPTION
DESCRIPTION
        Reads at most <[n-1]> characters from <[fp]> until a newline
        Reads at most <[n-1]> characters from <[fp]> until a newline
        is found. The characters including to the newline are stored
        is found. The characters including to the newline are stored
        in <[buf]>. The buffer is terminated with a 0.
        in <[buf]>. The buffer is terminated with a 0.
 
 
        The <<_fgets_r>> function is simply the reentrant version of
        The <<_fgets_r>> function is simply the reentrant version of
        <<fgets>> and is passed an additional reentrancy structure
        <<fgets>> and is passed an additional reentrancy structure
        pointer: <[ptr]>.
        pointer: <[ptr]>.
 
 
RETURNS
RETURNS
        <<fgets>> returns the buffer passed to it, with the data
        <<fgets>> returns the buffer passed to it, with the data
        filled in. If end of file occurs with some data already
        filled in. If end of file occurs with some data already
        accumulated, the data is returned with no other indication. If
        accumulated, the data is returned with no other indication. If
        no data are read, NULL is returned instead.
        no data are read, NULL is returned instead.
 
 
PORTABILITY
PORTABILITY
        <<fgets>> should replace all uses of <<gets>>. Note however
        <<fgets>> should replace all uses of <<gets>>. Note however
        that <<fgets>> returns all of the data, while <<gets>> removes
        that <<fgets>> returns all of the data, while <<gets>> removes
        the trailing newline (with no indication that it has done so.)
        the trailing newline (with no indication that it has done so.)
 
 
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
*/
*/
 
 
#include <_ansi.h>
#include <_ansi.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include "local.h"
#include "local.h"
 
 
/*
/*
 * Read at most n-1 characters from the given file.
 * Read at most n-1 characters from the given file.
 * Stop when a newline has been read, or the count runs out.
 * Stop when a newline has been read, or the count runs out.
 * Return first argument, or NULL if no characters were read.
 * Return first argument, or NULL if no characters were read.
 */
 */
 
 
char *
char *
_DEFUN(_fgets_r, (ptr, buf, n, fp),
_DEFUN(_fgets_r, (ptr, buf, n, fp),
       struct _reent * ptr _AND
       struct _reent * ptr _AND
       char *buf _AND
       char *buf _AND
       int n     _AND
       int n     _AND
       FILE * fp)
       FILE * fp)
{
{
  size_t len;
  size_t len;
  char *s;
  char *s;
  unsigned char *p, *t;
  unsigned char *p, *t;
 
 
  if (n < 2)                    /* sanity check */
  if (n < 2)                    /* sanity check */
    return 0;
    return 0;
 
 
  s = buf;
  s = buf;
 
 
  CHECK_INIT(ptr, fp);
  CHECK_INIT(ptr, fp);
 
 
  __sfp_lock_acquire ();
  __sfp_lock_acquire ();
  _flockfile (fp);
  _flockfile (fp);
#ifdef __SCLE
#ifdef __SCLE
  if (fp->_flags & __SCLE)
  if (fp->_flags & __SCLE)
    {
    {
      int c;
      int c;
      /* Sorry, have to do it the slow way */
      /* Sorry, have to do it the slow way */
      while (--n > 0 && (c = __sgetc_r (ptr, fp)) != EOF)
      while (--n > 0 && (c = __sgetc_r (ptr, fp)) != EOF)
        {
        {
          *s++ = c;
          *s++ = c;
          if (c == '\n')
          if (c == '\n')
            break;
            break;
        }
        }
      if (c == EOF && s == buf)
      if (c == EOF && s == buf)
        {
        {
          _funlockfile (fp);
          _funlockfile (fp);
          __sfp_lock_release ();
          __sfp_lock_release ();
          return NULL;
          return NULL;
        }
        }
      *s = 0;
      *s = 0;
      _funlockfile (fp);
      _funlockfile (fp);
      __sfp_lock_release ();
      __sfp_lock_release ();
      return buf;
      return buf;
    }
    }
#endif
#endif
 
 
  n--;                          /* leave space for NUL */
  n--;                          /* leave space for NUL */
  do
  do
    {
    {
      /*
      /*
       * If the buffer is empty, refill it.
       * If the buffer is empty, refill it.
       */
       */
      if ((len = fp->_r) <= 0)
      if ((len = fp->_r) <= 0)
        {
        {
          if (__srefill_r (ptr, fp))
          if (__srefill_r (ptr, fp))
            {
            {
              /* EOF: stop with partial or no line */
              /* EOF: stop with partial or no line */
              if (s == buf)
              if (s == buf)
                {
                {
                  _funlockfile (fp);
                  _funlockfile (fp);
                  __sfp_lock_release ();
                  __sfp_lock_release ();
                  return 0;
                  return 0;
                }
                }
              break;
              break;
            }
            }
          len = fp->_r;
          len = fp->_r;
        }
        }
      p = fp->_p;
      p = fp->_p;
 
 
      /*
      /*
       * Scan through at most n bytes of the current buffer,
       * Scan through at most n bytes of the current buffer,
       * looking for '\n'.  If found, copy up to and including
       * looking for '\n'.  If found, copy up to and including
       * newline, and stop.  Otherwise, copy entire chunk
       * newline, and stop.  Otherwise, copy entire chunk
       * and loop.
       * and loop.
       */
       */
      if (len > n)
      if (len > n)
        len = n;
        len = n;
      t = (unsigned char *) memchr ((_PTR) p, '\n', len);
      t = (unsigned char *) memchr ((_PTR) p, '\n', len);
      if (t != 0)
      if (t != 0)
        {
        {
          len = ++t - p;
          len = ++t - p;
          fp->_r -= len;
          fp->_r -= len;
          fp->_p = t;
          fp->_p = t;
          _CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
          _CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
          s[len] = 0;
          s[len] = 0;
          _funlockfile (fp);
          _funlockfile (fp);
          __sfp_lock_release ();
          __sfp_lock_release ();
          return (buf);
          return (buf);
        }
        }
      fp->_r -= len;
      fp->_r -= len;
      fp->_p += len;
      fp->_p += len;
      _CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
      _CAST_VOID memcpy ((_PTR) s, (_PTR) p, len);
      s += len;
      s += len;
    }
    }
  while ((n -= len) != 0);
  while ((n -= len) != 0);
  *s = 0;
  *s = 0;
  _funlockfile (fp);
  _funlockfile (fp);
  __sfp_lock_release ();
  __sfp_lock_release ();
  return buf;
  return buf;
}
}
 
 
#ifndef _REENT_ONLY
#ifndef _REENT_ONLY
 
 
char *
char *
_DEFUN(fgets, (buf, n, fp),
_DEFUN(fgets, (buf, n, fp),
       char *buf _AND
       char *buf _AND
       int n     _AND
       int n     _AND
       FILE * fp)
       FILE * fp)
{
{
  return _fgets_r (_REENT, buf, n, fp);
  return _fgets_r (_REENT, buf, n, fp);
}
}
 
 
#endif /* !_REENT_ONLY */
#endif /* !_REENT_ONLY */
 
 

powered by: WebSVN 2.1.0

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