* Copyright (c) 1990 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid
[] = "@(#)fgets.c 5.5 (Berkeley) %G%";
#endif /* LIBC_SCCS and not lint */
* Read at most n-1 characters from the given file.
* Stop when a newline has been read, or the count runs out.
* Return first argument, or NULL if no characters were read.
register unsigned char *p
, *t
;
if (n
< 2) /* sanity check */
n
--; /* leave space for NUL */
* If the buffer is empty, refill it.
if ((len
= fp
->_r
) <= 0) {
/* EOF/error: stop with partial or no line */
* Scan through at most n bytes of the current buffer,
* looking for '\n'. If found, copy up to and including
* newline, and stop. Otherwise, copy entire chunk
t
= memchr((void *)p
, '\n', len
);
(void)memcpy((void *)s
, (void *)p
, len
);
(void)memcpy((void *)s
, (void *)p
, len
);
} while ((n
-= len
) != 0);