summaryrefslogtreecommitdiff
path: root/lib/libc/stdio/fgets.c
diff options
context:
space:
mode:
authorJordan K. Hubbard <jkh@FreeBSD.org>1996-06-03 05:12:29 +0000
committerJordan K. Hubbard <jkh@FreeBSD.org>1996-06-03 05:12:29 +0000
commitaccd7f182e87d2a1d578214211ee7ded32753803 (patch)
tree90de65019704d8a469505b67d00c11e813d480e0 /lib/libc/stdio/fgets.c
parentc3287ad65e82f73d993c7537d1fc5a7a16199449 (diff)
Notes
Diffstat (limited to 'lib/libc/stdio/fgets.c')
-rw-r--r--lib/libc/stdio/fgets.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/lib/libc/stdio/fgets.c b/lib/libc/stdio/fgets.c
index 09f68772818d..186a1a1136ad 100644
--- a/lib/libc/stdio/fgets.c
+++ b/lib/libc/stdio/fgets.c
@@ -40,6 +40,11 @@ static char sccsid[] = "@(#)fgets.c 8.2 (Berkeley) 12/22/93";
#include <stdio.h>
#include <string.h>
+#include "local.h"
+#ifdef _THREAD_SAFE
+#include <pthread.h>
+#include "pthread_private.h"
+#endif
/*
* Read at most n-1 characters from the given file.
@@ -49,16 +54,19 @@ static char sccsid[] = "@(#)fgets.c 8.2 (Berkeley) 12/22/93";
char *
fgets(buf, n, fp)
char *buf;
- register size_t n;
+ register int n;
register FILE *fp;
{
register size_t len;
register char *s;
register unsigned char *p, *t;
- if (n == 0) /* sanity check */
+ if (n <= 0) /* sanity check */
return (NULL);
+#ifdef _THREAD_SAFE
+ _thread_flockfile(fp,__FILE__,__LINE__);
+#endif
s = buf;
n--; /* leave space for NUL */
while (n != 0) {
@@ -68,8 +76,12 @@ fgets(buf, n, fp)
if ((len = fp->_r) <= 0) {
if (__srefill(fp)) {
/* EOF/error: stop with partial or no line */
- if (s == buf)
+ if (s == buf) {
+#ifdef _THREAD_SAFE
+ _thread_funlockfile(fp);
+#endif
return (NULL);
+ }
break;
}
len = fp->_r;
@@ -91,6 +103,9 @@ fgets(buf, n, fp)
fp->_p = t;
(void)memcpy((void *)s, (void *)p, len);
s[len] = 0;
+#ifdef _THREAD_SAFE
+ _thread_funlockfile(fp);
+#endif
return (buf);
}
fp->_r -= len;
@@ -100,5 +115,8 @@ fgets(buf, n, fp)
n -= len;
}
*s = 0;
+#ifdef _THREAD_SAFE
+ _thread_funlockfile(fp);
+#endif
return (buf);
}