diff --git a/lib/libc/stdio/fflush.c b/lib/libc/stdio/fflush.c index e315a14fd06e..bd07b958ee21 100644 --- a/lib/libc/stdio/fflush.c +++ b/lib/libc/stdio/fflush.c @@ -1,4 +1,4 @@ -/* $NetBSD: fflush.c,v 1.9 1998/01/19 07:38:42 jtc Exp $ */ +/* $NetBSD: fflush.c,v 1.10 1998/01/22 08:21:45 jtc Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)fflush.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: fflush.c,v 1.9 1998/01/19 07:38:42 jtc Exp $"); +__RCSID("$NetBSD: fflush.c,v 1.10 1998/01/22 08:21:45 jtc Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -50,6 +50,10 @@ __RCSID("$NetBSD: fflush.c,v 1.9 1998/01/19 07:38:42 jtc Exp $"); #include "local.h" #include "reentrant.h" +#ifdef _REENT +extern rwlock_t __sfp_lock; +#endif + /* Flush a single file, or (if fp is NULL) all files. */ int fflush(fp) @@ -57,8 +61,12 @@ fflush(fp) { int r; - if (fp == NULL) - return (_fwalk(__sflush)); + if (fp == NULL) { + rwlock_rdlock(&__sfp_lock); + r = _fwalk(__sflush); + rwlock_unlock(&__sfp_lock); + return r; + } FLOCKFILE(fp); if ((fp->_flags & (__SWR | __SRW)) == 0) { diff --git a/lib/libc/stdio/findfp.c b/lib/libc/stdio/findfp.c index 5d477a21124b..acc93c81524c 100644 --- a/lib/libc/stdio/findfp.c +++ b/lib/libc/stdio/findfp.c @@ -1,4 +1,4 @@ -/* $NetBSD: findfp.c,v 1.7 1997/07/13 20:14:58 christos Exp $ */ +/* $NetBSD: findfp.c,v 1.8 1998/01/22 08:21:47 jtc Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)findfp.c 8.2 (Berkeley) 1/4/94"; #else -__RCSID("$NetBSD: findfp.c,v 1.7 1997/07/13 20:14:58 christos Exp $"); +__RCSID("$NetBSD: findfp.c,v 1.8 1998/01/22 08:21:47 jtc Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -53,6 +53,7 @@ __RCSID("$NetBSD: findfp.c,v 1.7 1997/07/13 20:14:58 christos Exp $"); #include #include "local.h" #include "glue.h" +#include "reentrant.h" int __sdidinit; @@ -76,6 +77,10 @@ struct glue __sglue = { &uglue, 3, __sF }; static struct glue *moreglue __P((int)); void f_prealloc __P((void)); +#ifdef _REENT +rwlock_t __sfp_lock = RWLOCK_INITIALIZER; +#endif + static struct glue * moreglue(n) register int n; @@ -108,6 +113,8 @@ __sfp() if (!__sdidinit) __sinit(); + + rwlock_wrlock(&__sfp_lock); for (g = &__sglue;; g = g->next) { for (fp = g->iobs, n = g->niobs; --n >= 0; fp++) if (fp->_flags == 0) @@ -115,6 +122,7 @@ __sfp() if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL) break; } + rwlock_unlock(&__sfp_lock); return (NULL); found: fp->_flags = 1; /* reserve this slot; caller sets real flags */ @@ -130,6 +138,7 @@ found: fp->_ub._size = 0; fp->_lb._base = NULL; /* no line buffer */ fp->_lb._size = 0; + rwlock_unlock(&__sfp_lock); return (fp); } diff --git a/lib/libc/stdio/refill.c b/lib/libc/stdio/refill.c index aeca280ad291..1e76eda64111 100644 --- a/lib/libc/stdio/refill.c +++ b/lib/libc/stdio/refill.c @@ -1,4 +1,4 @@ -/* $NetBSD: refill.c,v 1.7 1997/07/13 20:15:22 christos Exp $ */ +/* $NetBSD: refill.c,v 1.8 1998/01/22 08:21:48 jtc Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)refill.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: refill.c,v 1.7 1997/07/13 20:15:22 christos Exp $"); +__RCSID("$NetBSD: refill.c,v 1.8 1998/01/22 08:21:48 jtc Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -49,6 +49,11 @@ __RCSID("$NetBSD: refill.c,v 1.7 1997/07/13 20:15:22 christos Exp $"); #include #include #include "local.h" +#include "reentrant.h" + +#ifdef _REENT +extern rwlock_t __sfp_lock; +#endif static int lflush __P((FILE *)); @@ -56,7 +61,6 @@ static int lflush(fp) FILE *fp; { - if ((fp->_flags & (__SLBF|__SWR)) == (__SLBF|__SWR)) return (__sflush(fp)); return (0); @@ -120,8 +124,11 @@ __srefill(fp) * flush all line buffered output files, per the ANSI C * standard. */ - if (fp->_flags & (__SLBF|__SNBF)) + if (fp->_flags & (__SLBF|__SNBF)) { + rwlock_rdlock(&__sfp_lock); (void) _fwalk(lflush); + rwlock_unlock(&__sfp_lock); + } fp->_p = fp->_bf._base; fp->_r = (*fp->_read)(fp->_cookie, (char *)fp->_p, fp->_bf._size); fp->_flags &= ~__SMOD; /* buffer contents are again pristine */