OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / stdio / fgets.c
1 /* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7
8 #include "_stdio.h"
9
10 libc_hidden_proto(fgets_unlocked)
11
12 #ifdef __DO_UNLOCKED
13
14 libc_hidden_proto(__fgetc_unlocked)
15
16 char *fgets_unlocked(char *__restrict s, int n,
17                                            register FILE * __restrict stream)
18 {
19         register char *p;
20         int c;
21
22         __STDIO_STREAM_VALIDATE(stream);
23
24 #ifdef __UCLIBC_MJN3_ONLY__
25 #warning CONSIDER: What should fgets do if n <= 0?
26 #endif /* __UCLIBC_MJN3_ONLY__ */
27         /* Should we assert here?  Or set errno?  Or just fail... */
28         if (n <= 0) {
29 /*              __set_errno(EINVAL); */
30                 goto ERROR;
31         }
32
33         p = s;
34
35         while (--n) {
36                 if (__STDIO_STREAM_CAN_USE_BUFFER_GET(stream)) {
37                         if ((*p++ = __STDIO_STREAM_BUFFER_GET(stream)) == '\n') {
38                                 break;
39                         }
40                 } else {
41                         if ((c = __fgetc_unlocked(stream)) == EOF) {
42                                 if (__FERROR_UNLOCKED(stream)) {
43                                         goto ERROR;
44                                 }
45                                 break;
46                         }
47                         if ((*p++ = c) == '\n') {
48                                 break;
49                         }
50                 }
51         }
52
53 #ifdef __UCLIBC_MJN3_ONLY__
54 #warning CONSIDER: If n==1 and not at EOF, should fgets return an empty string?
55 #endif /* __UCLIBC_MJN3_ONLY__ */
56         if (p > s) {
57                 *p = 0;
58                 return s;
59         }
60
61  ERROR:
62         return NULL;
63 }
64 libc_hidden_def(fgets_unlocked)
65
66 #ifndef __UCLIBC_HAS_THREADS__
67 strong_alias(fgets_unlocked,fgets)
68 libc_hidden_proto(fgets)
69 libc_hidden_def(fgets)
70 #endif
71
72 #elif defined __UCLIBC_HAS_THREADS__
73
74 char *fgets(char *__restrict s, int n,
75                         register FILE * __restrict stream)
76 {
77         char *retval;
78         __STDIO_AUTO_THREADLOCK_VAR;
79
80         __STDIO_AUTO_THREADLOCK(stream);
81
82         retval = fgets_unlocked(s, n, stream);
83
84         __STDIO_AUTO_THREADUNLOCK(stream);
85
86         return retval;
87 }
88 libc_hidden_proto(fgets)
89 libc_hidden_def(fgets)
90
91 #endif