commit 26d544e443cbc7965ae5450a84d3125e56d354c4
parent 93dae2f7825bdc8456eb9be48facae05a39a6b8f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 4 Dec 2017 13:16:03 +0000
[lib/c] Add gets() and fgets()
Diffstat:
3 files changed, 37 insertions(+), 0 deletions(-)
diff --git a/lib/c/src/Makefile b/lib/c/src/Makefile
@@ -11,6 +11,7 @@ OBJ = assert.o strcpy.o strcmp.o strlen.o strchr.o \
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
diff --git a/lib/c/src/fgets.c b/lib/c/src/fgets.c
@@ -0,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;
+}
diff --git a/lib/c/src/gets.c b/lib/c/src/gets.c
@@ -0,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;
+}