libc: Add (v)fprintf and make (v)printf use that
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Let's free libc of console_write!
This commit is contained in:
parent
6a6a56a8b4
commit
cb1ef3e404
@ -34,5 +34,5 @@ int main()
|
||||
stdout = fopen("/dev/console", "w");
|
||||
stderr = fopen("/dev/console", "w");
|
||||
|
||||
fputs("Hello, world!", stderr);
|
||||
fprintf(stderr, "init is running as PID %d\n", getpid());
|
||||
}
|
||||
|
@ -84,8 +84,11 @@ extern "C"
|
||||
|
||||
void setbuf(FILE*, char*);
|
||||
|
||||
int fprintf(FILE*, const char*, ...);
|
||||
int vfprintf(FILE*, const char*, va_list);
|
||||
/* Write formatted output to a file. */
|
||||
int fprintf(FILE* stream, const char* format, ...);
|
||||
|
||||
/* Write formatted output to a file. */
|
||||
int vfprintf(FILE* stream, const char* format, va_list ap);
|
||||
|
||||
/* Write formatted output into a buffer. */
|
||||
int sprintf(char* buf, const char* format, ...);
|
||||
@ -99,15 +102,15 @@ extern "C"
|
||||
/* Write up to max bytes of formatted output into a buffer. */
|
||||
int vsnprintf(char*, size_t, const char*, va_list);
|
||||
|
||||
/* Write formatted output to standard output. */
|
||||
int vprintf(const char*, va_list ap);
|
||||
|
||||
/* Write formatted output to standard output. */
|
||||
int printf(const char*, ...);
|
||||
|
||||
/* Write a string followed by a newline to standard output. */
|
||||
int puts(const char* s);
|
||||
|
||||
/* Output a message to the console. */
|
||||
int console_write(const char* msg, size_t size);
|
||||
|
||||
/* Write an error message to standard error. */
|
||||
void perror(const char* s);
|
||||
|
||||
|
@ -30,12 +30,6 @@ static int fopen_parse_mode(const char* mode)
|
||||
|
||||
extern "C"
|
||||
{
|
||||
int console_write(const char* str, size_t size)
|
||||
{
|
||||
long rc = syscall(SYS_console_write, str, size);
|
||||
__errno_return(rc, int);
|
||||
}
|
||||
|
||||
FILE* fopen(const char* path, const char* mode)
|
||||
{
|
||||
int flags;
|
||||
@ -231,19 +225,46 @@ extern "C"
|
||||
return rc;
|
||||
}
|
||||
|
||||
int vfprintf(FILE* stream, const char* format, va_list ap)
|
||||
{
|
||||
usize rc = cstyle_format(
|
||||
format,
|
||||
[](char c, void* f) -> Result<void> {
|
||||
int rc = fputc(c, (FILE*)f);
|
||||
if (rc == EOF) return err(errno);
|
||||
return {};
|
||||
},
|
||||
stream, ap)
|
||||
.value_or(-1);
|
||||
|
||||
if (rc == (usize)-1) return -1;
|
||||
|
||||
return (int)rc;
|
||||
}
|
||||
|
||||
int fprintf(FILE* stream, const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
int rc = vfprintf(stream, format, ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int vprintf(const char* format, va_list ap)
|
||||
{
|
||||
return vfprintf(stdout, format, ap);
|
||||
}
|
||||
|
||||
int printf(const char* format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
int rc = (int)cstyle_format(
|
||||
format,
|
||||
[](char c, void*) -> Result<void> {
|
||||
console_write(&c, 1);
|
||||
return {};
|
||||
},
|
||||
nullptr, ap)
|
||||
.value();
|
||||
int rc = vfprintf(stdout, format, ap);
|
||||
|
||||
va_end(ap);
|
||||
|
||||
@ -252,30 +273,22 @@ extern "C"
|
||||
|
||||
int puts(const char* s)
|
||||
{
|
||||
if (console_write(s, strlen(s)) < 0) return -1;
|
||||
if (console_write("\n", 1) < 0) return -1;
|
||||
if (fputs(s, stdout) < 0) return -1;
|
||||
if (putchar('\n') == EOF) return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void perror(const char* s)
|
||||
{
|
||||
// FIXME: Output to stderr when available.
|
||||
int err = errno;
|
||||
if (s && *s) printf("%s: ", s);
|
||||
printf("%s\n", strerror(err));
|
||||
if (s && *s) fprintf(stderr, "%s: ", s);
|
||||
fprintf(stderr, "%s\n", strerror(err));
|
||||
}
|
||||
}
|
||||
|
||||
void debug_log_impl(const char* format, va_list ap)
|
||||
{
|
||||
cstyle_format(
|
||||
format,
|
||||
[](char c, void*) -> Result<void> {
|
||||
console_write(&c, 1);
|
||||
return {};
|
||||
},
|
||||
nullptr, ap)
|
||||
.value();
|
||||
console_write("\n", 1);
|
||||
vfprintf(stderr, format, ap);
|
||||
fputc('\n', stderr);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user