aboutsummaryrefslogtreecommitdiff
path: root/sys/libc/fgets.c
diff options
context:
space:
mode:
authorJoe Hunkeler <jhunkeler@gmail.com>2015-08-11 16:51:37 -0400
committerJoe Hunkeler <jhunkeler@gmail.com>2015-08-11 16:51:37 -0400
commit40e5a5811c6ffce9b0974e93cdd927cbcf60c157 (patch)
tree4464880c571602d54f6ae114729bf62a89518057 /sys/libc/fgets.c
downloadiraf-osx-40e5a5811c6ffce9b0974e93cdd927cbcf60c157.tar.gz
Repatch (from linux) of OSX IRAF
Diffstat (limited to 'sys/libc/fgets.c')
-rw-r--r--sys/libc/fgets.c43
1 files changed, 43 insertions, 0 deletions
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 <iraf.h>
+
+
+/* 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);
+ }
+}