aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2003-01-07 05:21:24 +0000
committerH. Peter Anvin <hpa@zytor.com>2003-01-07 05:21:24 +0000
commitd55a12d59a7f5318a69a90f75f49a0e91cd08b3a (patch)
treed5210f65c55dfe92e82a9035673365c17c081a77
parentc81f84914c27722423ea4fc336bdea7f7e437331 (diff)
downloadklibc-d55a12d59a7f5318a69a90f75f49a0e91cd08b3a.tar.gz
Add [f]getc() and fgets()klibc-0.72
-rw-r--r--include/stdio.h4
-rw-r--r--klibc/Makefile3
-rw-r--r--klibc/fgetc.c20
-rw-r--r--klibc/fgets.c33
-rw-r--r--klibc/include/stdio.h4
5 files changed, 63 insertions, 1 deletions
diff --git a/include/stdio.h b/include/stdio.h
index d51e969c4fb92..5e621af5f18bc 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -70,6 +70,10 @@ __extern int fputc(int, FILE *);
#define putc(c,f) fputc((c),(f))
#define putchar(c) fputc((c),stdout)
+__extern int fgetc(FILE *);
+__extern char * fgets(char *, int, FILE *);
+#define getc(f) fgetc(f)
+
__extern size_t _fread(void *, size_t, FILE *);
__extern size_t _fwrite(const void *, size_t, FILE *);
diff --git a/klibc/Makefile b/klibc/Makefile
index 81fe510a4d564..6f00342072047 100644
--- a/klibc/Makefile
+++ b/klibc/Makefile
@@ -19,7 +19,8 @@ LIBOBJS = vsnprintf.o snprintf.o vsprintf.o sprintf.o \
execl.o execle.o execv.o execvpe.o execvp.o execlp.o execlpe.o \
fork.o wait.o wait3.o waitpid.o setpgrp.o \
printf.o vprintf.o fprintf.o vfprintf.o perror.o \
- fopen.o fread.o fread2.o fwrite.o fwrite2.o fputc.o fputs.o puts.o \
+ fopen.o fread.o fread2.o fgetc.o fgets.o \
+ fwrite.o fwrite2.o fputc.o fputs.o puts.o \
sleep.o usleep.o raise.o abort.o assert.o alarm.o pause.o \
__signal.o signal.o bsd_signal.o siglist.o siglongjmp.o \
sigaction.o sigpending.o sigprocmask.o sigsuspend.o \
diff --git a/klibc/fgetc.c b/klibc/fgetc.c
new file mode 100644
index 0000000000000..83eee16f61621
--- /dev/null
+++ b/klibc/fgetc.c
@@ -0,0 +1,20 @@
+/*
+ * fgetc.c
+ *
+ * Extremely slow fgetc implementation, using _fread(). If people
+ * actually need character-oriented input to be fast, we may actually
+ * have to implement buffering. Sigh.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+
+int fgetc(FILE *f)
+{
+ unsigned char ch;
+
+ return (_fread(&ch, 1, f) == 1) ? (int)ch : EOF;
+}
+
diff --git a/klibc/fgets.c b/klibc/fgets.c
new file mode 100644
index 0000000000000..88a145a63f065
--- /dev/null
+++ b/klibc/fgets.c
@@ -0,0 +1,33 @@
+/*
+ * fgets.c
+ *
+ * This will be very slow due to the implementation of getc(),
+ * but we can't afford to drain characters we don't need from
+ * the input.
+ */
+
+#include <stdio.h>
+
+char *fgets(char *s, int n, FILE *f)
+{
+ int ch;
+ char *p = s;
+
+ while ( n > 1 ) {
+ ch = getc(f);
+ if ( ch == EOF ) {
+ *p = '\0';
+ return NULL;
+ }
+ *p++ = ch;
+ if ( ch == '\n' )
+ break;
+ }
+ if ( n )
+ *p = '\0';
+
+ return s;
+}
+
+
+
diff --git a/klibc/include/stdio.h b/klibc/include/stdio.h
index d51e969c4fb92..5e621af5f18bc 100644
--- a/klibc/include/stdio.h
+++ b/klibc/include/stdio.h
@@ -70,6 +70,10 @@ __extern int fputc(int, FILE *);
#define putc(c,f) fputc((c),(f))
#define putchar(c) fputc((c),stdout)
+__extern int fgetc(FILE *);
+__extern char * fgets(char *, int, FILE *);
+#define getc(f) fgetc(f)
+
__extern size_t _fread(void *, size_t, FILE *);
__extern size_t _fwrite(const void *, size_t, FILE *);