OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [stdio/] [findfp.c] - Diff between revs 207 and 345

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

Rev 207 Rev 345
/*
/*
 * 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.
 */
 */
/* No user fns here.  Pesch 15apr92. */
/* No user fns here.  Pesch 15apr92. */
 
 
#include <_ansi.h>
#include <_ansi.h>
#include <reent.h>
#include <reent.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <errno.h>
#include <errno.h>
#include <string.h>
#include <string.h>
#include <fcntl.h>
#include <fcntl.h>
#include <sys/lock.h>
#include <sys/lock.h>
#include "local.h"
#include "local.h"
 
 
#ifdef _REENT_SMALL
#ifdef _REENT_SMALL
const struct __sFILE_fake __sf_fake_stdin =
const struct __sFILE_fake __sf_fake_stdin =
    {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
    {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
const struct __sFILE_fake __sf_fake_stdout =
const struct __sFILE_fake __sf_fake_stdout =
    {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
    {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
const struct __sFILE_fake __sf_fake_stderr =
const struct __sFILE_fake __sf_fake_stderr =
    {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
    {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
#endif
#endif
 
 
static _VOID
static _VOID
_DEFUN(std, (ptr, flags, file, data),
_DEFUN(std, (ptr, flags, file, data),
            FILE *ptr _AND
            FILE *ptr _AND
            int flags _AND
            int flags _AND
            int file  _AND
            int file  _AND
            struct _reent *data)
            struct _reent *data)
{
{
  ptr->_p = 0;
  ptr->_p = 0;
  ptr->_r = 0;
  ptr->_r = 0;
  ptr->_w = 0;
  ptr->_w = 0;
  ptr->_flags = flags;
  ptr->_flags = flags;
  ptr->_flags2 = 0;
  ptr->_flags2 = 0;
  ptr->_file = file;
  ptr->_file = file;
  ptr->_bf._base = 0;
  ptr->_bf._base = 0;
  ptr->_bf._size = 0;
  ptr->_bf._size = 0;
  ptr->_lbfsize = 0;
  ptr->_lbfsize = 0;
  memset (&ptr->_mbstate, 0, sizeof (_mbstate_t));
  memset (&ptr->_mbstate, 0, sizeof (_mbstate_t));
  ptr->_cookie = ptr;
  ptr->_cookie = ptr;
  ptr->_read = __sread;
  ptr->_read = __sread;
#ifndef __LARGE64_FILES
#ifndef __LARGE64_FILES
  ptr->_write = __swrite;
  ptr->_write = __swrite;
#else /* __LARGE64_FILES */
#else /* __LARGE64_FILES */
  ptr->_write = __swrite64;
  ptr->_write = __swrite64;
  ptr->_seek64 = __sseek64;
  ptr->_seek64 = __sseek64;
  ptr->_flags |= __SL64;
  ptr->_flags |= __SL64;
#endif /* __LARGE64_FILES */
#endif /* __LARGE64_FILES */
  ptr->_seek = __sseek;
  ptr->_seek = __sseek;
  ptr->_close = __sclose;
  ptr->_close = __sclose;
#if !defined(__SINGLE_THREAD__) && !defined(_REENT_SMALL)
#if !defined(__SINGLE_THREAD__) && !defined(_REENT_SMALL)
  __lock_init_recursive (ptr->_lock);
  __lock_init_recursive (ptr->_lock);
  /*
  /*
   * #else
   * #else
   * lock is already initialized in __sfp
   * lock is already initialized in __sfp
   */
   */
#endif
#endif
 
 
#ifdef __SCLE
#ifdef __SCLE
  if (__stextmode (ptr->_file))
  if (__stextmode (ptr->_file))
    ptr->_flags |= __SCLE;
    ptr->_flags |= __SCLE;
#endif
#endif
}
}
 
 
struct _glue *
struct _glue *
_DEFUN(__sfmoreglue, (d, n),
_DEFUN(__sfmoreglue, (d, n),
       struct _reent *d _AND
       struct _reent *d _AND
       register int n)
       register int n)
{
{
  struct _glue *g;
  struct _glue *g;
  FILE *p;
  FILE *p;
 
 
  g = (struct _glue *) _malloc_r (d, sizeof (*g) + n * sizeof (FILE));
  g = (struct _glue *) _malloc_r (d, sizeof (*g) + n * sizeof (FILE));
  if (g == NULL)
  if (g == NULL)
    return NULL;
    return NULL;
  p = (FILE *) (g + 1);
  p = (FILE *) (g + 1);
  g->_next = NULL;
  g->_next = NULL;
  g->_niobs = n;
  g->_niobs = n;
  g->_iobs = p;
  g->_iobs = p;
  memset (p, 0, n * sizeof (FILE));
  memset (p, 0, n * sizeof (FILE));
  return g;
  return g;
}
}
 
 
/*
/*
 * Find a free FILE for fopen et al.
 * Find a free FILE for fopen et al.
 */
 */
 
 
FILE *
FILE *
_DEFUN(__sfp, (d),
_DEFUN(__sfp, (d),
       struct _reent *d)
       struct _reent *d)
{
{
  FILE *fp;
  FILE *fp;
  int n;
  int n;
  struct _glue *g;
  struct _glue *g;
 
 
  __sfp_lock_acquire ();
  __sfp_lock_acquire ();
 
 
  if (!_GLOBAL_REENT->__sdidinit)
  if (!_GLOBAL_REENT->__sdidinit)
    __sinit (_GLOBAL_REENT);
    __sinit (_GLOBAL_REENT);
  for (g = &_GLOBAL_REENT->__sglue;; g = g->_next)
  for (g = &_GLOBAL_REENT->__sglue;; g = g->_next)
    {
    {
      for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
      for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
        if (fp->_flags == 0)
        if (fp->_flags == 0)
          goto found;
          goto found;
      if (g->_next == NULL &&
      if (g->_next == NULL &&
          (g->_next = __sfmoreglue (d, NDYNAMIC)) == NULL)
          (g->_next = __sfmoreglue (d, NDYNAMIC)) == NULL)
        break;
        break;
    }
    }
  __sfp_lock_release ();
  __sfp_lock_release ();
  d->_errno = ENOMEM;
  d->_errno = ENOMEM;
  return NULL;
  return NULL;
 
 
found:
found:
  fp->_file = -1;               /* no file */
  fp->_file = -1;               /* no file */
  fp->_flags = 1;               /* reserve this slot; caller sets real flags */
  fp->_flags = 1;               /* reserve this slot; caller sets real flags */
  fp->_flags2 = 0;
  fp->_flags2 = 0;
#ifndef __SINGLE_THREAD__
#ifndef __SINGLE_THREAD__
  __lock_init_recursive (fp->_lock);
  __lock_init_recursive (fp->_lock);
#endif
#endif
  __sfp_lock_release ();
  __sfp_lock_release ();
 
 
  fp->_p = NULL;                /* no current pointer */
  fp->_p = NULL;                /* no current pointer */
  fp->_w = 0;                    /* nothing to read or write */
  fp->_w = 0;                    /* nothing to read or write */
  fp->_r = 0;
  fp->_r = 0;
  fp->_bf._base = NULL;         /* no buffer */
  fp->_bf._base = NULL;         /* no buffer */
  fp->_bf._size = 0;
  fp->_bf._size = 0;
  fp->_lbfsize = 0;              /* not line buffered */
  fp->_lbfsize = 0;              /* not line buffered */
  memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
  memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
  /* fp->_cookie = <any>; */    /* caller sets cookie, _read/_write etc */
  /* fp->_cookie = <any>; */    /* caller sets cookie, _read/_write etc */
  fp->_ub._base = NULL;         /* no ungetc buffer */
  fp->_ub._base = NULL;         /* no ungetc buffer */
  fp->_ub._size = 0;
  fp->_ub._size = 0;
  fp->_lb._base = NULL;         /* no line buffer */
  fp->_lb._base = NULL;         /* no line buffer */
  fp->_lb._size = 0;
  fp->_lb._size = 0;
 
 
  return fp;
  return fp;
}
}
 
 
/*
/*
 * exit() calls _cleanup() through *__cleanup, set whenever we
 * exit() calls _cleanup() through *__cleanup, set whenever we
 * open or buffer a file.  This chicanery is done so that programs
 * open or buffer a file.  This chicanery is done so that programs
 * that do not use stdio need not link it all in.
 * that do not use stdio need not link it all in.
 *
 *
 * The name `_cleanup' is, alas, fairly well known outside stdio.
 * The name `_cleanup' is, alas, fairly well known outside stdio.
 */
 */
 
 
_VOID
_VOID
_DEFUN(_cleanup_r, (ptr),
_DEFUN(_cleanup_r, (ptr),
       struct _reent *ptr)
       struct _reent *ptr)
{
{
  _CAST_VOID _fwalk(ptr, fclose);
  _CAST_VOID _fwalk(ptr, fclose);
  /* _CAST_VOID _fwalk (ptr, fflush); */        /* `cheating' */
  /* _CAST_VOID _fwalk (ptr, fflush); */        /* `cheating' */
}
}
 
 
#ifndef _REENT_ONLY
#ifndef _REENT_ONLY
_VOID
_VOID
_DEFUN_VOID(_cleanup)
_DEFUN_VOID(_cleanup)
{
{
  _cleanup_r (_GLOBAL_REENT);
  _cleanup_r (_GLOBAL_REENT);
}
}
#endif
#endif
 
 
/*
/*
 * __sinit() is called whenever stdio's internal variables must be set up.
 * __sinit() is called whenever stdio's internal variables must be set up.
 */
 */
 
 
_VOID
_VOID
_DEFUN(__sinit, (s),
_DEFUN(__sinit, (s),
       struct _reent *s)
       struct _reent *s)
{
{
  __sinit_lock_acquire ();
  __sinit_lock_acquire ();
 
 
  if (s->__sdidinit)
  if (s->__sdidinit)
    {
    {
      __sinit_lock_release ();
      __sinit_lock_release ();
      return;
      return;
    }
    }
 
 
  /* make sure we clean up on exit */
  /* make sure we clean up on exit */
  s->__cleanup = _cleanup_r;    /* conservative */
  s->__cleanup = _cleanup_r;    /* conservative */
  s->__sdidinit = 1;
  s->__sdidinit = 1;
 
 
  s->__sglue._next = NULL;
  s->__sglue._next = NULL;
#ifndef _REENT_SMALL
#ifndef _REENT_SMALL
  s->__sglue._niobs = 3;
  s->__sglue._niobs = 3;
  s->__sglue._iobs = &s->__sf[0];
  s->__sglue._iobs = &s->__sf[0];
#else
#else
  s->__sglue._niobs = 0;
  s->__sglue._niobs = 0;
  s->__sglue._iobs = NULL;
  s->__sglue._iobs = NULL;
  s->_stdin = __sfp(s);
  s->_stdin = __sfp(s);
  s->_stdout = __sfp(s);
  s->_stdout = __sfp(s);
  s->_stderr = __sfp(s);
  s->_stderr = __sfp(s);
#endif
#endif
 
 
  std (s->_stdin,  __SRD, 0, s);
  std (s->_stdin,  __SRD, 0, s);
 
 
  /* On platforms that have true file system I/O, we can verify
  /* On platforms that have true file system I/O, we can verify
     whether stdout is an interactive terminal or not, as part of
     whether stdout is an interactive terminal or not, as part of
     __smakebuf on first use of the stream.  For all other platforms,
     __smakebuf on first use of the stream.  For all other platforms,
     we will default to line buffered mode here.  Technically, POSIX
     we will default to line buffered mode here.  Technically, POSIX
     requires both stdin and stdout to be line-buffered, but tradition
     requires both stdin and stdout to be line-buffered, but tradition
     leaves stdin alone on systems without fcntl.  */
     leaves stdin alone on systems without fcntl.  */
#ifdef HAVE_FCNTL
#ifdef HAVE_FCNTL
  std (s->_stdout, __SWR, 1, s);
  std (s->_stdout, __SWR, 1, s);
#else
#else
  std (s->_stdout, __SWR | __SLBF, 1, s);
  std (s->_stdout, __SWR | __SLBF, 1, s);
#endif
#endif
 
 
  /* POSIX requires stderr to be opened for reading and writing, even
  /* POSIX requires stderr to be opened for reading and writing, even
     when the underlying fd 2 is write-only.  */
     when the underlying fd 2 is write-only.  */
  std (s->_stderr, __SRW | __SNBF, 2, s);
  std (s->_stderr, __SRW | __SNBF, 2, s);
 
 
  __sinit_lock_release ();
  __sinit_lock_release ();
}
}
 
 
#ifndef __SINGLE_THREAD__
#ifndef __SINGLE_THREAD__
 
 
__LOCK_INIT_RECURSIVE(static, __sfp_lock);
__LOCK_INIT_RECURSIVE(static, __sfp_lock);
__LOCK_INIT_RECURSIVE(static, __sinit_lock);
__LOCK_INIT_RECURSIVE(static, __sinit_lock);
 
 
_VOID
_VOID
_DEFUN_VOID(__sfp_lock_acquire)
_DEFUN_VOID(__sfp_lock_acquire)
{
{
  __lock_acquire_recursive (__sfp_lock);
  __lock_acquire_recursive (__sfp_lock);
}
}
 
 
_VOID
_VOID
_DEFUN_VOID(__sfp_lock_release)
_DEFUN_VOID(__sfp_lock_release)
{
{
  __lock_release_recursive (__sfp_lock);
  __lock_release_recursive (__sfp_lock);
}
}
 
 
_VOID
_VOID
_DEFUN_VOID(__sinit_lock_acquire)
_DEFUN_VOID(__sinit_lock_acquire)
{
{
  __lock_acquire_recursive (__sinit_lock);
  __lock_acquire_recursive (__sinit_lock);
}
}
 
 
_VOID
_VOID
_DEFUN_VOID(__sinit_lock_release)
_DEFUN_VOID(__sinit_lock_release)
{
{
  __lock_release_recursive (__sinit_lock);
  __lock_release_recursive (__sinit_lock);
}
}
 
 
/* Walkable file locking routine.  */
/* Walkable file locking routine.  */
static int
static int
_DEFUN(__fp_lock, (ptr),
_DEFUN(__fp_lock, (ptr),
       FILE * ptr)
       FILE * ptr)
{
{
  _flockfile (ptr);
  _flockfile (ptr);
 
 
  return 0;
  return 0;
}
}
 
 
/* Walkable file unlocking routine.  */
/* Walkable file unlocking routine.  */
static int
static int
_DEFUN(__fp_unlock, (ptr),
_DEFUN(__fp_unlock, (ptr),
       FILE * ptr)
       FILE * ptr)
{
{
  _funlockfile (ptr);
  _funlockfile (ptr);
 
 
  return 0;
  return 0;
}
}
 
 
_VOID
_VOID
_DEFUN_VOID(__fp_lock_all)
_DEFUN_VOID(__fp_lock_all)
{
{
  __sfp_lock_acquire ();
  __sfp_lock_acquire ();
 
 
  _CAST_VOID _fwalk (_REENT, __fp_lock);
  _CAST_VOID _fwalk (_REENT, __fp_lock);
}
}
 
 
_VOID
_VOID
_DEFUN_VOID(__fp_unlock_all)
_DEFUN_VOID(__fp_unlock_all)
{
{
  _CAST_VOID _fwalk (_REENT, __fp_unlock);
  _CAST_VOID _fwalk (_REENT, __fp_unlock);
 
 
  __sfp_lock_release ();
  __sfp_lock_release ();
}
}
#endif
#endif
 
 

powered by: WebSVN 2.1.0

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