newlib/newlib/libc/stdio/findfp.c

367 lines
8.5 KiB
C
Raw Normal View History

2000-02-17 20:39:52 +01:00
/*
* 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.
*/
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com> * libc/stdio/asprintf.c libc/stdio/clearerr.c, libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c, libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c, libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c, libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c, libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c, libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c, libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c, libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c, libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c, libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c, libc/stdio/getchar.c libc/stdio/getchar_u.c, libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c, libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h, libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c, libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c, libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c, libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c, libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c, libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c, libc/stdio/setlinebuf.c libc/stdio/setvbuf.c, libc/stdio/siprintf.c libc/stdio/snprintf.c, libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c, libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c, libc/stdio/vasprintf.c libc/stdio/vfieeefp.h, libc/stdio/vfprintf.c libc/stdio/vfscanf.c, libc/stdio/vprintf.c libc/stdio/vscanf.c, libc/stdio/vsnprintf.c libc/stdio/vsprintf.c, libc/stdio/vsscanf.c libc/stdio/wbuf.c, libc/stdio/wsetup.c: Perform minor formatting changes. Move copyright notices to top of file, ensure that <_ansi.h> is included, be consistent with open parentheses, use _DEFUN macro, include "local.h" where needed, and remove various compiler warnings.
2004-04-23 22:01:55 +02:00
/* No user fns here. Pesch 15apr92. */
2000-02-17 20:39:52 +01:00
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com> * libc/stdio/asprintf.c libc/stdio/clearerr.c, libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c, libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c, libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c, libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c, libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c, libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c, libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c, libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c, libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c, libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c, libc/stdio/getchar.c libc/stdio/getchar_u.c, libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c, libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h, libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c, libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c, libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c, libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c, libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c, libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c, libc/stdio/setlinebuf.c libc/stdio/setvbuf.c, libc/stdio/siprintf.c libc/stdio/snprintf.c, libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c, libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c, libc/stdio/vasprintf.c libc/stdio/vfieeefp.h, libc/stdio/vfprintf.c libc/stdio/vfscanf.c, libc/stdio/vprintf.c libc/stdio/vscanf.c, libc/stdio/vsnprintf.c libc/stdio/vsprintf.c, libc/stdio/vsscanf.c libc/stdio/wbuf.c, libc/stdio/wsetup.c: Perform minor formatting changes. Move copyright notices to top of file, ensure that <_ansi.h> is included, be consistent with open parentheses, use _DEFUN macro, include "local.h" where needed, and remove various compiler warnings.
2004-04-23 22:01:55 +02:00
#include <_ansi.h>
#include <reent.h>
2000-02-17 20:39:52 +01:00
#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"
#if defined(_REENT_SMALL) && !defined(_REENT_GLOBAL_STDIO_STREAMS)
2006-09-26 23:22:19 +02:00
const struct __sFILE_fake __sf_fake_stdin =
{_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
const struct __sFILE_fake __sf_fake_stdout =
{_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
const struct __sFILE_fake __sf_fake_stderr =
{_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL};
#endif
#ifdef _REENT_GLOBAL_STDIO_STREAMS
__FILE __sf[3];
#endif
#if (defined (__OPTIMIZE_SIZE__) || defined (PREFER_SIZE_OVER_SPEED))
_NOINLINE_STATIC void
#else
static void
#endif
std (FILE *ptr,
int flags,
int file)
2000-02-17 20:39:52 +01:00
{
ptr->_p = 0;
ptr->_r = 0;
ptr->_w = 0;
ptr->_flags = flags;
ptr->_flags2 = 0;
2000-02-17 20:39:52 +01:00
ptr->_file = file;
ptr->_bf._base = 0;
ptr->_bf._size = 0;
ptr->_lbfsize = 0;
2008-12-10 Corinna Vinschen <corinna@vinschen.de> Implement basic wide char stdio functionality, based on FreeBSD. * libc/include/stdio.h (__SORD): Define. (__SWID): Define. * libc/include/wchar.h: Add declarations for new wide char functions. (getwc): Define as macro. (getwchar): Ditto. (putwc): Ditto. (putwchar): Ditto. * libc/include/sys/reent.h (struct __sFILE): Add _mbstate member. (struct __sFILE64): Ditto. * libc/stdio/Makefile.am (ELIX_4_SOURCES): Add fgetwc.c, fgetws.c, fputwc.c, fputws.c, fwide.c, getwc.c, getwchar.c, putwc.c, putwchar.c and ungetwc.c. (CHEWOUT_FILES): Add fgetwc.def, fgetws.def, fputwc.def, fputws.def, fwide.def, getwc.def, getwchar.def, putwc.def, putwchar.def and ungetwc.def. Add header dependency rules for the new files. * libc/stdio/Makefile.in: Regenerate. * libc/stdio/fgetwc.c: New file, implementing fgetwc and _fgetwc_r. * libc/stdio/fgetws.c: New file, implementing fgetws and _fgetws_r. * libc/stdio/findfp.c (std): Initialize FILE's _mbstate member. (__sfmoreglue): Ditto. * libc/stdio/fputs.c (_fputs_r): Set stream orientation. * libc/stdio/fputwc.c: New file, implementing fputwc and _fputwc_r. * libc/stdio/fputws.c: New file, implementing fputws and _fputws_r. * libc/stdio/fread.c (_fread_r): Set stream orientation. * libc/stdio/freopen.c (_freopen_r): Reset stream orientation. Reset _mbstate. * libc/stdio/fseek.c (_fseek_r): Reset _mbstate. * libc/stdio/fwide.c: New file, implementing fwide and _fwide_r. * libc/stdio/fwrite.c (_fwrite_r): Set stream orientation. * libc/stdio/getwc.c: New file, implementing getwc and _getwc_r. * libc/stdio/getwchar.c: New file, implementing getwchar and _getwchar_r. * libc/stdio/local.h (ORIENT): New macro. * libc/stdio/puts.c (_puts_r): Set stream orientation. * libc/stdio/putwc.c: New file, implementing putwc and _putwc_r. * libc/stdio/putwchar.c: New file, implementing putwchar and _putwchar_r. * libc/stdio/refill.c (__srefill_r): Set stream orientation. * libc/stdio/stdio.tex: Add documentation for new functions. * libc/stdio/ungetc.c (_ungetc_r): Set stream orientation. * libc/stdio/ungetwc.c: New file, implementing ungetwc and _ungetwc_r. * libc/stdio/vfscanf.c (__SVFSCANF_R): Set stream orientation. * libc/stdio/wbuf.c (__swbuf_r): Ditto.
2008-12-11 00:43:12 +01:00
memset (&ptr->_mbstate, 0, sizeof (_mbstate_t));
2000-02-17 20:39:52 +01:00
ptr->_cookie = ptr;
ptr->_read = __sread;
#ifndef __LARGE64_FILES
2000-02-17 20:39:52 +01:00
ptr->_write = __swrite;
#else /* __LARGE64_FILES */
ptr->_write = __swrite64;
ptr->_seek64 = __sseek64;
ptr->_flags |= __SL64;
#endif /* __LARGE64_FILES */
2000-02-17 20:39:52 +01:00
ptr->_seek = __sseek;
#ifdef _STDIO_CLOSE_PER_REENT_STD_STREAMS
2000-02-17 20:39:52 +01:00
ptr->_close = __sclose;
#else /* _STDIO_CLOSE_STD_STREAMS */
ptr->_close = NULL;
#endif /* _STDIO_CLOSE_STD_STREAMS */
#if !defined(__SINGLE_THREAD__) && !(defined(_REENT_SMALL) && !defined(_REENT_GLOBAL_STDIO_STREAMS))
__lock_init_recursive (ptr->_lock);
2004-03-25 23:29:18 +01:00
/*
* #else
* lock is already initialized in __sfp
*/
#endif
#ifdef __SCLE
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com> * libc/stdio/asprintf.c libc/stdio/clearerr.c, libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c, libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c, libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c, libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c, libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c, libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c, libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c, libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c, libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c, libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c, libc/stdio/getchar.c libc/stdio/getchar_u.c, libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c, libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h, libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c, libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c, libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c, libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c, libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c, libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c, libc/stdio/setlinebuf.c libc/stdio/setvbuf.c, libc/stdio/siprintf.c libc/stdio/snprintf.c, libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c, libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c, libc/stdio/vasprintf.c libc/stdio/vfieeefp.h, libc/stdio/vfprintf.c libc/stdio/vfscanf.c, libc/stdio/vprintf.c libc/stdio/vscanf.c, libc/stdio/vsnprintf.c libc/stdio/vsprintf.c, libc/stdio/vsscanf.c libc/stdio/wbuf.c, libc/stdio/wsetup.c: Perform minor formatting changes. Move copyright notices to top of file, ensure that <_ansi.h> is included, be consistent with open parentheses, use _DEFUN macro, include "local.h" where needed, and remove various compiler warnings.
2004-04-23 22:01:55 +02:00
if (__stextmode (ptr->_file))
ptr->_flags |= __SCLE;
#endif
2000-02-17 20:39:52 +01:00
}
static inline void
stdin_init(FILE *ptr)
{
std (ptr, __SRD, 0);
}
static inline void
stdout_init(FILE *ptr)
{
/* On platforms that have true file system I/O, we can verify
whether stdout is an interactive terminal or not, as part of
__smakebuf on first use of the stream. For all other platforms,
we will default to line buffered mode here. Technically, POSIX
requires both stdin and stdout to be line-buffered, but tradition
leaves stdin alone on systems without fcntl. */
#ifdef HAVE_FCNTL
std (ptr, __SWR, 1);
#else
std (ptr, __SWR | __SLBF, 1);
#endif
}
static inline void
stderr_init(FILE *ptr)
{
/* POSIX requires stderr to be opened for reading and writing, even
when the underlying fd 2 is write-only. */
std (ptr, __SRW | __SNBF, 2);
}
struct glue_with_file {
struct _glue glue;
FILE file;
};
2000-02-17 20:39:52 +01:00
struct _glue *
__sfmoreglue (struct _reent *d,
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com> * libc/stdio/asprintf.c libc/stdio/clearerr.c, libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c, libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c, libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c, libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c, libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c, libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c, libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c, libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c, libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c, libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c, libc/stdio/getchar.c libc/stdio/getchar_u.c, libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c, libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h, libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c, libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c, libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c, libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c, libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c, libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c, libc/stdio/setlinebuf.c libc/stdio/setvbuf.c, libc/stdio/siprintf.c libc/stdio/snprintf.c, libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c, libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c, libc/stdio/vasprintf.c libc/stdio/vfieeefp.h, libc/stdio/vfprintf.c libc/stdio/vfscanf.c, libc/stdio/vprintf.c libc/stdio/vscanf.c, libc/stdio/vsnprintf.c libc/stdio/vsprintf.c, libc/stdio/vsscanf.c libc/stdio/wbuf.c, libc/stdio/wsetup.c: Perform minor formatting changes. Move copyright notices to top of file, ensure that <_ansi.h> is included, be consistent with open parentheses, use _DEFUN macro, include "local.h" where needed, and remove various compiler warnings.
2004-04-23 22:01:55 +02:00
register int n)
2000-02-17 20:39:52 +01:00
{
struct glue_with_file *g;
2000-02-17 20:39:52 +01:00
g = (struct glue_with_file *)
_malloc_r (d, sizeof (*g) + (n - 1) * sizeof (FILE));
2000-02-17 20:39:52 +01:00
if (g == NULL)
return NULL;
g->glue._next = NULL;
g->glue._niobs = n;
g->glue._iobs = &g->file;
memset (&g->file, 0, n * sizeof (FILE));
return &g->glue;
2000-02-17 20:39:52 +01:00
}
/*
* Find a free FILE for fopen et al.
*/
FILE *
__sfp (struct _reent *d)
2000-02-17 20:39:52 +01:00
{
FILE *fp;
int n;
struct _glue *g;
_newlib_sfp_lock_start ();
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;
}
_newlib_sfp_lock_exit ();
2000-02-17 20:39:52 +01:00
d->_errno = ENOMEM;
return NULL;
found:
2004-03-25 23:29:18 +01:00
fp->_file = -1; /* no file */
2000-02-17 20:39:52 +01:00
fp->_flags = 1; /* reserve this slot; caller sets real flags */
fp->_flags2 = 0;
#ifndef __SINGLE_THREAD__
__lock_init_recursive (fp->_lock);
#endif
_newlib_sfp_lock_end ();
2004-03-25 23:29:18 +01:00
2000-02-17 20:39:52 +01:00
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 */
2008-12-10 Corinna Vinschen <corinna@vinschen.de> Implement basic wide char stdio functionality, based on FreeBSD. * libc/include/stdio.h (__SORD): Define. (__SWID): Define. * libc/include/wchar.h: Add declarations for new wide char functions. (getwc): Define as macro. (getwchar): Ditto. (putwc): Ditto. (putwchar): Ditto. * libc/include/sys/reent.h (struct __sFILE): Add _mbstate member. (struct __sFILE64): Ditto. * libc/stdio/Makefile.am (ELIX_4_SOURCES): Add fgetwc.c, fgetws.c, fputwc.c, fputws.c, fwide.c, getwc.c, getwchar.c, putwc.c, putwchar.c and ungetwc.c. (CHEWOUT_FILES): Add fgetwc.def, fgetws.def, fputwc.def, fputws.def, fwide.def, getwc.def, getwchar.def, putwc.def, putwchar.def and ungetwc.def. Add header dependency rules for the new files. * libc/stdio/Makefile.in: Regenerate. * libc/stdio/fgetwc.c: New file, implementing fgetwc and _fgetwc_r. * libc/stdio/fgetws.c: New file, implementing fgetws and _fgetws_r. * libc/stdio/findfp.c (std): Initialize FILE's _mbstate member. (__sfmoreglue): Ditto. * libc/stdio/fputs.c (_fputs_r): Set stream orientation. * libc/stdio/fputwc.c: New file, implementing fputwc and _fputwc_r. * libc/stdio/fputws.c: New file, implementing fputws and _fputws_r. * libc/stdio/fread.c (_fread_r): Set stream orientation. * libc/stdio/freopen.c (_freopen_r): Reset stream orientation. Reset _mbstate. * libc/stdio/fseek.c (_fseek_r): Reset _mbstate. * libc/stdio/fwide.c: New file, implementing fwide and _fwide_r. * libc/stdio/fwrite.c (_fwrite_r): Set stream orientation. * libc/stdio/getwc.c: New file, implementing getwc and _getwc_r. * libc/stdio/getwchar.c: New file, implementing getwchar and _getwchar_r. * libc/stdio/local.h (ORIENT): New macro. * libc/stdio/puts.c (_puts_r): Set stream orientation. * libc/stdio/putwc.c: New file, implementing putwc and _putwc_r. * libc/stdio/putwchar.c: New file, implementing putwchar and _putwchar_r. * libc/stdio/refill.c (__srefill_r): Set stream orientation. * libc/stdio/stdio.tex: Add documentation for new functions. * libc/stdio/ungetc.c (_ungetc_r): Set stream orientation. * libc/stdio/ungetwc.c: New file, implementing ungetwc and _ungetwc_r. * libc/stdio/vfscanf.c (__SVFSCANF_R): Set stream orientation. * libc/stdio/wbuf.c (__swbuf_r): Ditto.
2008-12-11 00:43:12 +01:00
memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
2000-02-17 20:39:52 +01:00
/* 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 (struct _reent *ptr)
2000-02-17 20:39:52 +01:00
{
int (*cleanup_func) (struct _reent *, FILE *);
#ifdef _STDIO_BSD_SEMANTICS
/* BSD and Glibc systems only flush streams which have been written to
at exit time. Calling flush rather than close for speed, as on
the aforementioned systems. */
cleanup_func = __sflushw_r;
#else
/* Otherwise close files and flush read streams, too.
Note we call flush directly if "--enable-lite-exit" is in effect. */
#ifdef _LITE_EXIT
cleanup_func = _fflush_r;
#else
cleanup_func = _fclose_r;
#endif
#endif
#ifdef _REENT_GLOBAL_STDIO_STREAMS
if (ptr->_stdin != &__sf[0])
(*cleanup_func) (ptr, ptr->_stdin);
if (ptr->_stdout != &__sf[1])
(*cleanup_func) (ptr, ptr->_stdout);
if (ptr->_stderr != &__sf[2])
(*cleanup_func) (ptr, ptr->_stderr);
#endif
(void) _fwalk_reent (ptr, cleanup_func);
2000-02-17 20:39:52 +01:00
}
#ifndef _REENT_ONLY
void
_cleanup (void)
2000-02-17 20:39:52 +01:00
{
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 (struct _reent *s)
2000-02-17 20:39:52 +01:00
{
__sinit_lock_acquire ();
if (s->__sdidinit)
{
__sinit_lock_release ();
return;
}
2000-02-17 20:39:52 +01:00
/* make sure we clean up on exit */
s->__cleanup = _cleanup_r; /* conservative */
s->__sglue._next = NULL;
#ifndef _REENT_SMALL
# ifndef _REENT_GLOBAL_STDIO_STREAMS
s->__sglue._niobs = 3;
s->__sglue._iobs = &s->__sf[0];
# endif /* _REENT_GLOBAL_STDIO_STREAMS */
#else
s->__sglue._niobs = 0;
s->__sglue._iobs = NULL;
/* Avoid infinite recursion when calling __sfp for _GLOBAL_REENT. The
problem is that __sfp checks for _GLOBAL_REENT->__sdidinit and calls
__sinit if it's 0. */
if (s == _GLOBAL_REENT)
s->__sdidinit = 1;
# ifndef _REENT_GLOBAL_STDIO_STREAMS
s->_stdin = __sfp(s);
s->_stdout = __sfp(s);
s->_stderr = __sfp(s);
# else /* _REENT_GLOBAL_STDIO_STREAMS */
s->_stdin = &__sf[0];
s->_stdout = &__sf[1];
s->_stderr = &__sf[2];
# endif /* _REENT_GLOBAL_STDIO_STREAMS */
#endif
#ifdef _REENT_GLOBAL_STDIO_STREAMS
if (__sf[0]._cookie == NULL) {
_GLOBAL_REENT->__sglue._niobs = 3;
_GLOBAL_REENT->__sglue._iobs = &__sf[0];
stdin_init (&__sf[0]);
stdout_init (&__sf[1]);
stderr_init (&__sf[2]);
}
#else /* _REENT_GLOBAL_STDIO_STREAMS */
stdin_init (s->_stdin);
stdout_init (s->_stdout);
stderr_init (s->_stderr);
#endif /* _REENT_GLOBAL_STDIO_STREAMS */
2000-02-17 20:39:52 +01:00
s->__sdidinit = 1;
__sinit_lock_release ();
2000-02-17 20:39:52 +01:00
}
#ifndef __SINGLE_THREAD__
__LOCK_INIT_RECURSIVE(static, __sfp_recursive_mutex);
__LOCK_INIT_RECURSIVE(static, __sinit_recursive_mutex);
2004-03-25 23:29:18 +01:00
void
__sfp_lock_acquire (void)
2004-03-25 23:29:18 +01:00
{
__lock_acquire_recursive (__sfp_recursive_mutex);
2004-03-25 23:29:18 +01:00
}
void
__sfp_lock_release (void)
2004-03-25 23:29:18 +01:00
{
__lock_release_recursive (__sfp_recursive_mutex);
2004-03-25 23:29:18 +01:00
}
void
__sinit_lock_acquire (void)
{
__lock_acquire_recursive (__sinit_recursive_mutex);
}
void
__sinit_lock_release (void)
{
__lock_release_recursive (__sinit_recursive_mutex);
}
/* Walkable file locking routine. */
static int
__fp_lock (FILE * ptr)
{
if (!(ptr->_flags2 & __SNLK))
_flockfile (ptr);
return 0;
}
/* Walkable file unlocking routine. */
static int
__fp_unlock (FILE * ptr)
{
if (!(ptr->_flags2 & __SNLK))
_funlockfile (ptr);
return 0;
}
void
__fp_lock_all (void)
{
__sfp_lock_acquire ();
(void) _fwalk (_REENT, __fp_lock);
}
void
__fp_unlock_all (void)
{
(void) _fwalk (_REENT, __fp_unlock);
__sfp_lock_release ();
}
#endif