1 /* Copyright (C) 1991, 92, 95, 96, 97, 98 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
23 /* Reads characters from STREAM into S, until either a newline character
24 is read, N - 1 characters have been read, or EOF is seen. Returns
25 the newline, unlike gets. Finishes by appending a null character and
26 returning S. If EOF is seen before any characters have been written
27 to S, the function returns NULL without appending the null character.
28 If there is a file error, always return NULL. */
37 if (!__validfp (stream
) || s
== NULL
|| n
<= 0)
46 if (stream
->__buffer
== NULL
&& stream
->__userbuf
)
48 /* Unbuffered stream. Not much optimization to do. */
50 while (--n
> 0 && (c
= getc (stream
)) != EOF
)
51 if ((*p
++ = c
) == '\n')
53 if (c
== EOF
&& (p
== s
|| ferror (stream
)))
59 /* Leave space for the null. */
63 (!stream
->__seen
|| stream
->__buffer
== NULL
|| stream
->__pushed_back
))
65 /* Do one with getc to allocate a buffer. */
66 int c
= getc (stream
);
84 i
= stream
->__get_limit
- stream
->__bufp
;
87 /* Refill the buffer. */
88 int c
= __fillbf (stream
);
98 i
= stream
->__get_limit
- stream
->__bufp
;
104 found
= (char *) __memccpy ((void *) p
, stream
->__bufp
, '\n', i
);
108 stream
->__bufp
+= found
- p
;
122 return ferror (stream
) ? NULL
: s
;
125 weak_alias (fgets
, fgets_unlocked
)