Skip to content

Commit

Permalink
Connect fprintf family to putc
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterymath committed May 20, 2024
1 parent 0c57d75 commit 0c1e64f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
12 changes: 7 additions & 5 deletions mos-platform/common/c/printf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ namespace {

void put(char c, Status *status) {
if (status->i < status->n) {
if (status->s)
status->s[status->i] = c;
if (status->stream)
putc(c, status->stream);
else
putchar(c);
status->s[status->i] = c;
}
++status->i;
}
Expand Down Expand Up @@ -883,6 +883,7 @@ __attribute__((weak)) int vfprintf(FILE *__restrict__ stream,
status.n = SIZE_MAX;
status.i = 0;
status.s = NULL;
status.stream = stream;

va_copy(status.arg, arg);

Expand All @@ -891,8 +892,8 @@ __attribute__((weak)) int vfprintf(FILE *__restrict__ stream,

if ((*format != '%') || ((rc = print(format, &status)) == format)) {
/* No conversion specifier, print verbatim */
putchar(*format);
++format;
if (putc(*format++, stream) == EOF)
return EOF;
status.i++;
} else {
/* Continue parsing after conversion specifier */
Expand All @@ -917,6 +918,7 @@ __attribute__((weak)) int vsnprintf(char *__restrict__ s, size_t n,
status.n = n;
status.i = 0;
status.s = s;
status.stream = NULL;

va_copy(status.arg, arg);

Expand Down
23 changes: 20 additions & 3 deletions mos-platform/common/c/stdio-full.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,26 @@ struct _FILE {
FILE *next; // Pointer to next struct (internal)
};

static FILE serr = {.handle = 2, .status = _IONBF | FWRITE};
static FILE sout = {.handle = 1, .status = _IONBF | FWRITE, .next = &serr};
static FILE sin = {.handle = 0, .status = _IONBF | FREAD, .next = &sout};
// Buffer one-two lines; write() implementations have constant overhead, and
// buffering amortizes it.
static char serr_buf[80];
static char sout_buf[80];
static char sin_buf[80];

static FILE serr = {.handle = 2,
.buffer = serr_buf,
.bufsize = sizeof(serr_buf),
.status = _IONBF | FWRITE};
static FILE sout = {.handle = 1,
.buffer = sout_buf,
.bufsize = sizeof(sout_buf),
.status = _IONBF | FWRITE,
.next = &serr};
static FILE sin = {.handle = 0,
.buffer = sin_buf,
.bufsize = sizeof(sin_buf),
.status = _IONBF | FREAD,
.next = &sout};

FILE *stdin = &sin;
FILE *stdout = &sout;
Expand Down
2 changes: 2 additions & 0 deletions mos-platform/common/c/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -28,6 +29,7 @@ struct Status {
// *sscanf(): source string
size_t width; // specified field width
int prec; // specified field precision
FILE *stream; // *fprintf() / *fscanf() stream
va_list arg; // argument stack
};

Expand Down

0 comments on commit 0c1e64f

Please sign in to comment.