newlib/newlib/libc/stdio/findfp.c

183 lines
4.2 KiB
C
Raw Normal View History

2000-02-17 20:39:52 +01:00
/* No user fns here. Pesch 15apr92. */
/*
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/lock.h>
2000-02-17 20:39:52 +01:00
#include "local.h"
static void
std (ptr, flags, file, data)
FILE *ptr;
int flags;
int file;
2000-02-17 20:39:52 +01:00
struct _reent *data;
{
ptr->_p = 0;
ptr->_r = 0;
ptr->_w = 0;
ptr->_flags = flags;
ptr->_file = file;
ptr->_bf._base = 0;
ptr->_bf._size = 0;
ptr->_lbfsize = 0;
ptr->_cookie = ptr;
ptr->_read = __sread;
ptr->_write = __swrite;
ptr->_seek = __sseek;
ptr->_close = __sclose;
#ifndef __SINGLE_THREAD__
__lock_init_recursive (*(_LOCK_RECURSIVE_T *)&ptr->_lock);
#endif
#ifdef __SCLE
if (__stextmode(ptr->_file))
ptr->_flags |= __SCLE;
#endif
2000-02-17 20:39:52 +01:00
}
struct _glue *
__sfmoreglue (d, n)
struct _reent *d;
register int n;
{
struct _glue *g;
FILE *p;
g = (struct _glue *) _malloc_r (d, sizeof (*g) + n * sizeof (FILE));
if (g == NULL)
return NULL;
p = (FILE *) (g + 1);
g->_next = NULL;
g->_niobs = n;
g->_iobs = p;
memset (p, 0, n * sizeof (FILE));
return g;
}
/*
* Find a free FILE for fopen et al.
*/
FILE *
__sfp (d)
struct _reent *d;
{
FILE *fp;
int n;
struct _glue *g;
2003-08-22 Jeff Johnston <jjohnstn@redhat.com> * libc/include/sys/reent.h: Add _GLOBAL_REENT macro. * libc/stdio: Globally remove/replace all references to fp->_data. Replace with _REENT or _GLOBAL_REENT where appropriate. * libc/stdio/asprintf.c: Ditto. * libc/stdio/fclose.c: Ditto. * libc/stdio/fvwrite.c: Ditto. * libc/stdio/makebuf.c: Ditto. * libc/stdio/refill.c: Ditto. * libc/stdio/local.h: Ditto. * libc/stdio/setvbuf.c: Ditto. * libc/stdio/sscanf.c: Ditto. * libc/stdio/stdio.c: Ditto. * libc/stdio/ungetc.c: Ditto. * libc/stdio/vfscanf.c: Ditto. * libc/stdio/vsscanf.c: Ditto. * libc/stdio/fopen.c: Ditto. Also use _fseek_r in _fopen_r. * libc/stdio/vasprintf.c: Ditto. Also call _vfprintf_r directly. * libc/stdio/vsnprintf.c: Ditto. * libc/stdio/vsprintf.c: Ditto. * libc/stdio/fcloseall.c(fcloseall): Use _GLOBAL_REENT macro instead of _REENT to walk file list. * libc/stdio/fflush.c: Ditto. * libc/stdio/fgetpos.c: Add reentrant version and have regular version call reentrant version with _REENT argument. * libc/stdio/fsetpos.c: Ditto. * libc/stdio/fseek.c: Ditto. * libc/stdio/fseeko.c: Ditto. * libc/stdio/ftell.c: Ditto. * libc/stdio/ftello.c: Ditto. * libc/stdio/freopen.c: Ditto. * libc/stdio/findfp.c: Use _GLOBAL_REENT pointer when adding new files to chain. Also use _GLOBAL_REENT pointer for cleaning up. * libc/stdio/fiprintf.c: Reformatted to minimize duplicate code. * libc/stdio/siprintf.c: Ditto. * libc/stdio/iprintf.c: Ditto. * libc/stdio/fprintf.c: Ditto. * libc/stdio/printf.c: Ditto. * libc/stdio/snprintf.c: Call _vfprintf_r directly. * libc/stdio/sprintf.c: Ditto. * libc/stdio/vprintf.c: Ditto. Also add _REENT_ONLY check. * libc/stdio/rewind.c: Call _fseek_r directly. * libc/stdio/tmpfile.c: Call _fopen_r and _remove_r directly. * libc/stdio/vfprintf.c (_VFPRINTF_R): Change _r routines to use data pointer. (get_arg): Add extra struct _reent pointer argument. * libc/stdio64/fgetpos64.c: Add _r versions, remove any reference to fp->_data. * libc/stdio64/fopen64.c: Ditto. * libc/stdio64/freopen64.c: Ditto. * libc/stdio64/fsetpos64.c: Ditto. * libc/stdio64/ftello64.c: Ditto. * libc/stdio64/local64.h: Ditto. * libc/stdio64/stdio64.c: Ditto. * libc/stdio64/fseeko64.c: Ditto plus use _fstat_r instead of _fstat64_r for the meantime.
2003-08-22 20:52:25 +02:00
if (!_GLOBAL_REENT->__sdidinit)
__sinit (_GLOBAL_REENT);
for (g = &_GLOBAL_REENT->__sglue;; g = g->_next)
2000-02-17 20:39:52 +01:00
{
for (fp = g->_iobs, n = g->_niobs; --n >= 0; fp++)
if (fp->_flags == 0)
goto found;
if (g->_next == NULL &&
(g->_next = __sfmoreglue (d, NDYNAMIC)) == NULL)
break;
}
d->_errno = ENOMEM;
return NULL;
found:
fp->_flags = 1; /* reserve this slot; caller sets real flags */
fp->_p = NULL; /* no current pointer */
fp->_w = 0; /* nothing to read or write */
fp->_r = 0;
fp->_bf._base = NULL; /* no buffer */
fp->_bf._size = 0;
fp->_lbfsize = 0; /* not line buffered */
fp->_file = -1; /* no file */
/* fp->_cookie = <any>; */ /* caller sets cookie, _read/_write etc */
fp->_ub._base = NULL; /* no ungetc buffer */
fp->_ub._size = 0;
fp->_lb._base = NULL; /* no line buffer */
fp->_lb._size = 0;
2000-02-17 20:39:52 +01:00
return fp;
}
/*
* exit() calls _cleanup() through *__cleanup, set whenever we
* open or buffer a file. This chicanery is done so that programs
* that do not use stdio need not link it all in.
*
* The name `_cleanup' is, alas, fairly well known outside stdio.
*/
void
_cleanup_r (ptr)
struct _reent *ptr;
{
/* (void) _fwalk(fclose); */
(void) _fwalk (ptr, fflush); /* `cheating' */
}
#ifndef _REENT_ONLY
void
_cleanup ()
{
2003-08-22 Jeff Johnston <jjohnstn@redhat.com> * libc/include/sys/reent.h: Add _GLOBAL_REENT macro. * libc/stdio: Globally remove/replace all references to fp->_data. Replace with _REENT or _GLOBAL_REENT where appropriate. * libc/stdio/asprintf.c: Ditto. * libc/stdio/fclose.c: Ditto. * libc/stdio/fvwrite.c: Ditto. * libc/stdio/makebuf.c: Ditto. * libc/stdio/refill.c: Ditto. * libc/stdio/local.h: Ditto. * libc/stdio/setvbuf.c: Ditto. * libc/stdio/sscanf.c: Ditto. * libc/stdio/stdio.c: Ditto. * libc/stdio/ungetc.c: Ditto. * libc/stdio/vfscanf.c: Ditto. * libc/stdio/vsscanf.c: Ditto. * libc/stdio/fopen.c: Ditto. Also use _fseek_r in _fopen_r. * libc/stdio/vasprintf.c: Ditto. Also call _vfprintf_r directly. * libc/stdio/vsnprintf.c: Ditto. * libc/stdio/vsprintf.c: Ditto. * libc/stdio/fcloseall.c(fcloseall): Use _GLOBAL_REENT macro instead of _REENT to walk file list. * libc/stdio/fflush.c: Ditto. * libc/stdio/fgetpos.c: Add reentrant version and have regular version call reentrant version with _REENT argument. * libc/stdio/fsetpos.c: Ditto. * libc/stdio/fseek.c: Ditto. * libc/stdio/fseeko.c: Ditto. * libc/stdio/ftell.c: Ditto. * libc/stdio/ftello.c: Ditto. * libc/stdio/freopen.c: Ditto. * libc/stdio/findfp.c: Use _GLOBAL_REENT pointer when adding new files to chain. Also use _GLOBAL_REENT pointer for cleaning up. * libc/stdio/fiprintf.c: Reformatted to minimize duplicate code. * libc/stdio/siprintf.c: Ditto. * libc/stdio/iprintf.c: Ditto. * libc/stdio/fprintf.c: Ditto. * libc/stdio/printf.c: Ditto. * libc/stdio/snprintf.c: Call _vfprintf_r directly. * libc/stdio/sprintf.c: Ditto. * libc/stdio/vprintf.c: Ditto. Also add _REENT_ONLY check. * libc/stdio/rewind.c: Call _fseek_r directly. * libc/stdio/tmpfile.c: Call _fopen_r and _remove_r directly. * libc/stdio/vfprintf.c (_VFPRINTF_R): Change _r routines to use data pointer. (get_arg): Add extra struct _reent pointer argument. * libc/stdio64/fgetpos64.c: Add _r versions, remove any reference to fp->_data. * libc/stdio64/fopen64.c: Ditto. * libc/stdio64/freopen64.c: Ditto. * libc/stdio64/fsetpos64.c: Ditto. * libc/stdio64/ftello64.c: Ditto. * libc/stdio64/local64.h: Ditto. * libc/stdio64/stdio64.c: Ditto. * libc/stdio64/fseeko64.c: Ditto plus use _fstat_r instead of _fstat64_r for the meantime.
2003-08-22 20:52:25 +02:00
_cleanup_r (_GLOBAL_REENT);
2000-02-17 20:39:52 +01:00
}
#endif
/*
* __sinit() is called whenever stdio's internal variables must be set up.
*/
void
__sinit (s)
struct _reent *s;
{
/* make sure we clean up on exit */
s->__cleanup = _cleanup_r; /* conservative */
s->__sdidinit = 1;
s->__sglue._next = NULL;
#ifndef _REENT_SMALL
s->__sglue._niobs = 3;
s->__sglue._iobs = &s->__sf[0];
#else
s->__sglue._niobs = 0;
s->__sglue._iobs = NULL;
s->_stdin = __sfp(s);
s->_stdout = __sfp(s);
s->_stderr = __sfp(s);
#endif
std (s->_stdin, __SRD, 0, s);
/* on platforms that have true file system I/O, we can verify whether stdout
is an interactive terminal or not. For all other platforms, we will
default to line buffered mode here. */
#ifdef HAVE_FCNTL
std (s->_stdout, __SWR, 1, s);
#else
std (s->_stdout, __SWR | __SLBF, 1, s);
#endif
std (s->_stderr, __SWR | __SNBF, 2, s);
2000-02-17 20:39:52 +01:00
}