netinet/in.h: add IPPROTO_MH
[uclibc-ng.git] / libc / stdio / fgets.c
blob0a6d31e5c2cc55349b18f6712d2fd38ea3e02c78
1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
6 */
8 #include "_stdio.h"
11 #ifdef __DO_UNLOCKED
14 char *fgets_unlocked(char *__restrict s, int n,
15 register FILE * __restrict stream)
17 register char *p;
18 int c;
20 __STDIO_STREAM_VALIDATE(stream);
22 /* Should we assert here? Or set errno? Or just fail... */
23 if (n <= 0) {
24 /* __set_errno(EINVAL); */
25 goto ERROR;
28 p = s;
30 while (--n) {
31 if (__STDIO_STREAM_CAN_USE_BUFFER_GET(stream)) {
32 if ((*p++ = __STDIO_STREAM_BUFFER_GET(stream)) == '\n') {
33 break;
35 } else {
36 if ((c = __fgetc_unlocked(stream)) == EOF) {
37 if (__FERROR_UNLOCKED(stream)) {
38 goto ERROR;
40 break;
42 if ((*p++ = c) == '\n') {
43 break;
48 if (p > s) {
49 *p = 0;
50 return s;
53 ERROR:
54 return NULL;
56 libc_hidden_def(fgets_unlocked)
58 #ifndef __UCLIBC_HAS_THREADS__
59 strong_alias(fgets_unlocked,fgets)
60 libc_hidden_def(fgets)
61 #endif
63 #elif defined __UCLIBC_HAS_THREADS__
65 char *fgets(char *__restrict s, int n,
66 register FILE * __restrict stream)
68 char *retval;
69 __STDIO_AUTO_THREADLOCK_VAR;
71 __STDIO_AUTO_THREADLOCK(stream);
73 retval = fgets_unlocked(s, n, stream);
75 __STDIO_AUTO_THREADUNLOCK(stream);
77 return retval;
79 libc_hidden_def(fgets)
81 #endif