From fa080de7afc95aa1c19a6e6fc0e0708ced2eadc4 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 8 Jul 2015 20:46:52 -0400 Subject: Initial commit --- sys/libc/fgets.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sys/libc/fgets.c (limited to 'sys/libc/fgets.c') diff --git a/sys/libc/fgets.c b/sys/libc/fgets.c new file mode 100644 index 00000000..45c228c6 --- /dev/null +++ b/sys/libc/fgets.c @@ -0,0 +1,43 @@ +/* Copyright(c) 1986 Association of Universities for Research in Astronomy Inc. +*/ + +#define import_spp +#define import_libc +#define import_stdio +#include + + +/* FGETS -- Return a newline delimited string in the user buffer. If the +** buffer fills before newline is seen the string will not be newline +** delimited. +*/ +char * +fgets ( + char *buf, /* user supplied output buffer */ + int maxch, /* max chars out (incl EOS) */ + FILE *fp /* input file */ +) +{ + register int ch = 0, lastch = 0, n = maxch - 1; + register char *op = buf; + + while (--n >= 0 && (ch = getc (fp)) >= 0) { + lastch = ch; + if (ch == '\r') /* handle DOS-style CR-NL */ + continue; + *op++ = ch; + if (ch == '\n') + break; + } + + if (ch == EOF && op == buf) + return ((char *) NULL); + else { +#ifdef ADD_NEWLINE + if (lastch != '\n') /* handle missing NL at EOF */ + *op++ = '\n'; +#endif + *op = EOS; + return (buf); + } +} -- cgit