shithub: scc

Download patch

ref: 37e4ca9b85b68ca0441c18c2f4fbf24c866e38cb
parent: b589353e707d07bf1412c206e9e5cfa876e57400
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Dec 4 08:16:03 EST 2017

[lib/c] Add gets() and fgets()

--- a/lib/c/src/Makefile
+++ b/lib/c/src/Makefile
@@ -11,6 +11,7 @@
       isxdigit.o toupper.o tolower.o ctype.o setlocale.o \
       localeconv.o atoi.o atol.o atoll.o atexit.o exit.o \
       printf.o fprintf.o vfprintf.o \
+      fgets.o gets.o \
       realloc.o calloc.o malloc.o
 
 all: $(ARCH)-libc.a
--- /dev/null
+++ b/lib/c/src/fgets.c
@@ -1,0 +1,19 @@
+#include <stdio.h>
+#undef fgets
+
+char *
+fgets(char *s, int n, FILE *fp)
+{
+	int ch;
+	char *t = s;
+
+	while (--n > 0 && (ch = getc(fp)) != EOF) {
+		if ((*t++ = ch) == '\n')
+			break;
+	}
+	if (ch == EOF && s == t)
+		return NULL;
+	*t = '\0';
+
+	return s;
+}
--- /dev/null
+++ b/lib/c/src/gets.c
@@ -1,0 +1,17 @@
+#include <stdio.h>
+#undef gets
+
+char *
+gets(char *s)
+{
+	int ch;
+	char *t = s;
+
+	while ((ch = getc(stdin)) != EOF && ch != '\n')
+		*t++ = ch;
+	if (ch == EOF && s == t)
+		return NULL;
+	*t = '\0';
+
+	return s;
+}