glibc/stdio/fgets.c

122 lines
2.9 KiB
C
Raw Normal View History

1992-03-18 00:51:35 +00:00
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
1991-12-02 21:35:38 +00:00
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */
#include <ansidecl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
1992-03-18 00:51:35 +00:00
/* Reads characters from STREAM into S, until either a newline character
is read, N - 1 characters have been read, or EOF is seen. Returns
the newline, unlike gets. Finishes by appending a null character and
returning S. If EOF is seen before any characters have been written
to S, the function returns NULL without appending the null character.
If there is a file error, always return NULL. */
1991-12-02 21:35:38 +00:00
char *
DEFUN(fgets, (s, n, stream), char *s AND size_t n AND register FILE *stream)
{
register char *p = s;
1992-03-18 00:51:35 +00:00
if (!__validfp(stream) || s == NULL || n == 0)
1991-12-02 21:35:38 +00:00
{
errno = EINVAL;
return NULL;
}
1992-03-18 00:51:35 +00:00
if (ferror (stream))
1991-12-02 21:35:38 +00:00
return NULL;
if (stream->__buffer == NULL && stream->__userbuf)
{
/* Unbuffered stream. Not much optimization to do. */
1992-03-18 00:51:35 +00:00
register int c = 0;
while (--n > 0 && (c = getc (stream)) != EOF)
1991-12-02 21:35:38 +00:00
if ((*p++ = c) == '\n')
break;
1992-03-25 06:34:45 +00:00
if (c == EOF && (p == s || ferror (stream)))
1992-03-18 00:51:35 +00:00
return NULL;
1991-12-02 21:35:38 +00:00
*p = '\0';
return s;
}
1992-03-18 00:51:35 +00:00
/* Leave space for the null. */
--n;
if (n > 0 &&
1992-03-25 06:34:45 +00:00
(!stream->__seen || stream->__buffer == NULL || stream->__pushed_back))
1991-12-02 21:35:38 +00:00
{
/* Do one with getc to allocate a buffer. */
int c = getc (stream);
if (c == EOF)
return NULL;
*p++ = c;
if (c == '\n')
{
*p = '\0';
return s;
}
1992-03-18 00:51:35 +00:00
else
--n;
1991-12-02 21:35:38 +00:00
}
while (n > 0)
{
size_t i;
char *found;
i = stream->__get_limit - stream->__bufp;
if (i == 0)
{
/* Refill the buffer. */
1992-03-18 00:51:35 +00:00
int c = __fillbf (stream);
1991-12-02 21:35:38 +00:00
if (c == EOF)
1992-03-18 00:51:35 +00:00
break;
1991-12-02 21:35:38 +00:00
*p++ = c;
1992-04-01 07:07:03 +00:00
--n;
1991-12-02 21:35:38 +00:00
if (c == '\n')
{
*p = '\0';
return s;
}
1992-03-18 00:51:35 +00:00
i = stream->__get_limit - stream->__bufp;
1991-12-02 21:35:38 +00:00
}
if (i > n)
i = n;
1992-03-18 00:51:35 +00:00
found = (char *) __memccpy ((PTR) p, stream->__bufp, '\n', i);
1991-12-02 21:35:38 +00:00
if (found != NULL)
{
stream->__bufp += found - p;
p = found;
break;
}
stream->__bufp += i;
n -= i;
p += i;
}
1992-03-23 22:38:19 +00:00
if (p == s)
return NULL;
*p = '\0';
1992-03-18 00:51:35 +00:00
return ferror (stream) ? NULL : s;
1991-12-02 21:35:38 +00:00
}