From bb2a1b3e73a1b1cebe49f9a6aaaf8670ce6af673 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sat, 20 Jul 2013 14:10:06 +0200 Subject: [PATCH] Add dprintf(3) and vdprintf(3). --- libc/Makefile | 2 ++ libc/include/stdio.h | 8 ++++---- libc/stdio/dprintf.cpp | 35 +++++++++++++++++++++++++++++++++++ libc/stdio/vdprintf.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 libc/stdio/dprintf.cpp create mode 100644 libc/stdio/vdprintf.cpp diff --git a/libc/Makefile b/libc/Makefile index 72a902e1..c7843348 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -37,6 +37,7 @@ signal/sigemptyset.o \ signal/sigfillset.o \ signal/sigismember.o \ stdio/clearerr.o \ +stdio/dprintf.o \ stdio/fbufsize.o \ stdio/fclose.o \ stdio/fdeletefile.o \ @@ -84,6 +85,7 @@ stdio/snprintf.o \ stdio/sprintf.o \ stdio/sscanf.o \ stdio/ungetc.o \ +stdio/vdprintf.o \ stdio/vfscanf.o \ stdio/vsnprintf.o \ stdio/vsprintf.o \ diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 56c0e89b..493e6093 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -22,8 +22,8 @@ *******************************************************************************/ -#ifndef _STDIO_H -#define _STDIO_H 1 +#ifndef INCLUDE_STDIO_H +#define INCLUDE_STDIO_H #include #include @@ -72,6 +72,7 @@ extern FILE* stderr; #define stderr stderr extern void clearerr(FILE* stream); +extern int dprintf(int fildes, const char* __restrict format, ...); extern int fclose(FILE* stream); extern FILE* fdopen(int fildes, const char* mode); extern int feof(FILE* stream); @@ -123,6 +124,7 @@ extern int scanf(const char* __restrict format, ...); extern int sscanf(const char* __restrict s, const char* __restrict format, ...); extern FILE* tmpfile(void); extern int ungetc(int c, FILE* stream); +extern int vdprintf(int fildes, const char* __restrict format, __gnuc_va_list ap); extern int vfprintf(FILE* __restrict stream, const char* __restrict format, __gnuc_va_list ap); extern int vfscanf(FILE* __restrict stream, const char* __restrict format, __gnuc_va_list arg); extern int vprintf(const char* __restrict format, __gnuc_va_list ap); @@ -140,12 +142,10 @@ extern char* tmpnam(char* s); extern char* ctermid(char* s); extern FILE *fmemopen(void* __restrict buf, size_t size, const char* __restrict mode); extern FILE* open_memstream(char** bufp, size_t* sizep); -extern int dprintf(int fildes, const char* __restrict format, ...); extern int getchar_unlocked(void); extern int getc_unlocked(FILE* stream); extern int putchar_unlocked(int c); extern int putc_unlocked(int c, FILE* steam); -extern int vdprintf(int fildes, const char* __restrict format, __gnuc_va_list ap); #if __POSIX_OBSOLETE <= 200801 extern char* tempnam(const char* dir, const char* pfx); diff --git a/libc/stdio/dprintf.cpp b/libc/stdio/dprintf.cpp new file mode 100644 index 00000000..4b406d4f --- /dev/null +++ b/libc/stdio/dprintf.cpp @@ -0,0 +1,35 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + stdio/dprintf.cpp + Prints a formatted string to a file descriptor. + +*******************************************************************************/ + +#include +#include + +extern "C" int dprintf(int fd, const char* restrict format, ...) +{ + va_list list; + va_start(list, format); + int result = vdprintf(fd, format, list); + va_end(list); + return result; +} diff --git a/libc/stdio/vdprintf.cpp b/libc/stdio/vdprintf.cpp new file mode 100644 index 00000000..14d3f8f0 --- /dev/null +++ b/libc/stdio/vdprintf.cpp @@ -0,0 +1,38 @@ +/******************************************************************************* + + Copyright(C) Jonas 'Sortie' Termansen 2013. + + This file is part of the Sortix C Library. + + The Sortix C Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or (at your + option) any later version. + + The Sortix C Library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with the Sortix C Library. If not, see . + + stdio/vdprintf.cpp + Prints a formatted string to a file descriptor. + +*******************************************************************************/ + +#include +#include +#include +#include + +static size_t write_callback(void* user, const char* string, size_t stringlen) +{ + return writeall((int) (uintptr_t) user, string, stringlen * sizeof(char)); +} + +extern "C" int vdprintf(int fd, const char* restrict format, va_list list) +{ + return vprintf_callback(write_callback, (void*) (uintptr_t) fd, format, list); +}