[hackers] [scc] [libc] Add skeleton of vfprintf() || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Sat, 4 Mar 2017 07:23:31 +0100 (CET)

commit ed368eb12822027ed7c15bcfdf4eae457373ca33
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Sat Mar 4 07:19:52 2017 +0100
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Sat Mar 4 07:19:52 2017 +0100

    [libc] Add skeleton of vfprintf()
    
    This is a first version of a minimal vfprintf. This version only
    supports %d, %c, %o, %x and %s that is good enough to help us
    to debug other routines.

diff --git a/libc/src/Makefile b/libc/src/Makefile
index 53facc2..8399312 100644
--- a/libc/src/Makefile
+++ b/libc/src/Makefile
_AT_@ -10,7 +10,8 @@ LIBCOBJ = assert.o strcpy.o strcmp.o strlen.o strchr.o \
           isalnum.o isalpha.o isascii.o isblank.o iscntrl.o isdigit.o \
           isgraph.o islower.o isprint.o ispunct.o isspace.o isupper.o \
           isxdigit.o toupper.o tolower.o ctype.o setlocale.o \
- localeconv.o atoi.o atexit.o exit.o
+ localeconv.o atoi.o atexit.o exit.o \
+ printf.o fprintf.o vfprintf.o
 
 all: libc.a
 
diff --git a/libc/src/fprintf.c b/libc/src/fprintf.c
new file mode 100644
index 0000000..c61af45
--- /dev/null
+++ b/libc/src/fprintf.c
_AT_@ -0,0 +1,17 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <stdarg.h>
+#include <stdio.h>
+#undef fprintf
+
+int
+fprintf(FILE * restrict fp, const char * restrict fmt, ...)
+{
+ va_list va;
+ int cnt;
+
+ va_start(va, fmt);
+ cnt = vfprintf(fp, fmt, va);
+ va_end(va);
+ return cnt;
+}
diff --git a/libc/src/printf.c b/libc/src/printf.c
new file mode 100644
index 0000000..0e7f4ea
--- /dev/null
+++ b/libc/src/printf.c
_AT_@ -0,0 +1,17 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <stdarg.h>
+#include <stdio.h>
+#undef printf
+
+int
+printf(const char * restrict fmt, ...)
+{
+ int cnt;
+ va_list va;
+
+ va_start(va, fmt);
+ cnt = vfprintf(stdin, fmt, va);
+ va_end(va);
+ return cnt;
+}
diff --git a/libc/src/vfprintf.c b/libc/src/vfprintf.c
new file mode 100644
index 0000000..9e6bc6a
--- /dev/null
+++ b/libc/src/vfprintf.c
_AT_@ -0,0 +1,76 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <stdarg.h>
+#include <stdio.h>
+#undef vfprintf
+
+static int
+printn2(FILE * restrict fp, unsigned n, int base)
+{
+ unsigned t;
+ int cnt = 0;
+ static char digits[] = "0123456789ABCDEF";
+
+ if ((t = n / base) != 0)
+ cnt += printn2(fp, t, base);
+ putc(digits[n % base], fp);
+ return cnt + 1;
+}
+
+static int
+printn(FILE * restrict fp, int n, int b, int sign)
+{
+ int cnt = 0;
+
+ if (sign && n < 0) {
+ n = -n;
+ putc('-', fp);
+ ++cnt;
+ }
+ cnt += printn2(fp, n, b);
+ return cnt;
+}
+
+int
+vfprintf(FILE * restrict fp, const char *fmt, va_list va)
+{
+ int c, base, sign, cnt;
+ char *s;
+
+ while (( c = *fmt++) != '\0') {
+ if (c == '%') {
+ sign = 0;
+ switch (*fmt++) {
+ case '%':
+ c = '%';
+ break;
+ case 'c':
+ c = va_arg(va, int);
+ break;
+ case 'o':
+ base = 8;
+ goto numeric;
+ case 'd':
+ sign = 1;
+ base = 10;
+ goto numeric;
+ case 'x':
+ base = 16;
+ numeric:
+ c = va_arg(va, int);
+ cnt += printn(fp, c, base, sign);
+ continue;
+ case 's':
+ s = va_arg(va, char *);
+ while ((c = *s++) != '\0')
+ putc(c, fp);
+ /* passthrou */
+ default:
+ continue;
+ }
+ }
+ putc(c, fp);
+ ++cnt;
+ }
+ return cnt;
+}
Received on Sat Mar 04 2017 - 07:23:31 CET

This archive was generated by hypermail 2.3.0 : Sat Mar 04 2017 - 07:24:19 CET