whiterose

linux unikernel
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/whiterose

commit f173072e30e3e5cc8d0e88d3c27dea2677073d10
parent 6444a22edfeefb03117fff6281b55d1f0089d441
Author: Louis Solofrizzo <lsolofrizzo@online.net>
Date:   Wed, 24 Apr 2019 08:58:59 +0200

ukl: add puts, putchar and putc

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>

Diffstat:
Minclude/ukl/stdio.h | 3+++
Mukl/stdio/Makefile | 2+-
Mukl/stdio/__internals.c | 12++++++++++++
Mukl/stdio/fputs.c | 8++++++++
Mukl/stdio/internals.h | 1+
Aukl/stdio/putc.c | 19+++++++++++++++++++
6 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/include/ukl/stdio.h b/include/ukl/stdio.h @@ -27,5 +27,8 @@ int fputs(const char *, FILE *); int fclose(FILE *); int fflush(FILE *); int fseek(FILE *, long, int); +int putc(int c, FILE *f); +int putc_unlocked(int c, FILE *f); +int putchar(int c); #endif /* STDIO_H */ diff --git a/ukl/stdio/Makefile b/ukl/stdio/Makefile @@ -1 +1 @@ -obj-y := fopen.o __internals.o stdin.o stdout.o stderr.o fwrite.o fputs.o fread.o fclose.o fflush.o fseek.o +obj-y := fopen.o __internals.o stdin.o stdout.o stderr.o fwrite.o fputs.o fread.o fclose.o fflush.o fseek.o putc.o diff --git a/ukl/stdio/__internals.c b/ukl/stdio/__internals.c @@ -177,3 +177,15 @@ int __toread(FILE *f) f->rpos = f->rend = f->buf + f->buf_size; return (f->flags & F_EOF) ? EOF : 0; } + +int __overflow(FILE *f, int _c) +{ + unsigned char c = _c; + if (!f->wend && __towrite(f)) + return EOF; + if (f->wpos != f->wend && c != f->lbf) + return *f->wpos++ = c; + if (f->write(f, &c, 1) != 1) + return EOF; + return c; +} diff --git a/ukl/stdio/fputs.c b/ukl/stdio/fputs.c @@ -6,5 +6,13 @@ int fputs(const char *s, FILE *f) { size_t l = strlen(s); + return (fwrite(s, 1, l, f) == l) - 1; } + +int puts(const char *s) +{ + int r; + r = -(fputs(s, stdout) < 0 || putc_unlocked('\n', stdout) < 0); + return r; +} diff --git a/ukl/stdio/internals.h b/ukl/stdio/internals.h @@ -51,5 +51,6 @@ off_t __stdio_seek(FILE *f, off_t off, int whence); size_t __fwritex(const unsigned char *s, size_t l, FILE *f); int __towrite(FILE *f); int __toread(FILE *f); +int __overflow(FILE *f, int _c); #endif /* STDIO_INTERNALS_H */ diff --git a/ukl/stdio/putc.c b/ukl/stdio/putc.c @@ -0,0 +1,19 @@ +#include <stdio.h> +#include "internals.h" + +int putc_unlocked(int c, FILE *f) +{ + return putc(c, f); +} + +int putchar(int c) +{ + return putc(c, stdout); +} + +int putc(int c, FILE *f) +{ + return (((unsigned char)(c) != (f)->lbf && (f)->wpos != (f)->wend)) ? + *(f)->wpos++ = (unsigned char)(c) : + __overflow((f), (unsigned char)(c)); +}