diff --git a/project/common.mk b/project/common.mk index 453b6bf..ef5b62f 100644 --- a/project/common.mk +++ b/project/common.mk @@ -2,6 +2,7 @@ API_SRCS = \ INTERNAL_SRCS = \ src/internal/nolibc/toksvc_compiler.c \ + src/internal/toksvc_dprintf_impl.c \ src/internal/toksvc_nolibc_impl.c \ src/internal/toksvc_ntaio_impl.c \ diff --git a/project/headers.mk b/project/headers.mk index 19d613e..80fde47 100644 --- a/project/headers.mk +++ b/project/headers.mk @@ -11,6 +11,7 @@ INTERNAL_HEADERS = \ $(SOURCE_DIR)/src/internal/nolibc/stdlib.h \ $(SOURCE_DIR)/src/internal/nolibc/string.h \ $(SOURCE_DIR)/src/internal/nolibc/unistd.h \ + $(SOURCE_DIR)/src/internal/toksvc_dprintf_impl.h \ $(SOURCE_DIR)/src/internal/toksvc_nolibc_impl.h \ ALL_HEADERS = $(API_HEADERS) $(INTERNAL_HEADERS) diff --git a/src/internal/toksvc_dprintf_impl.c b/src/internal/toksvc_dprintf_impl.c new file mode 100644 index 0000000..f74ef35 --- /dev/null +++ b/src/internal/toksvc_dprintf_impl.c @@ -0,0 +1,67 @@ +/*********************************************************/ +/* toksvc: a framework-native token broker service */ +/* Copyright (C) 2020 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.TOKSVC. */ +/*********************************************************/ + +#include +#include + +#include +#include "toksvc_driver_impl.h" +#include "toksvc_nolibc_impl.h" + +#include +#include +#include + +int toks_dprintf(int fd, const char * fmt, ...) +{ + int ret; + int cnt; + int size; + va_list ap; + char * ch; + char * buf; + char chbuf[2048]; + + va_start(ap,fmt); + + size = sizeof(chbuf); + buf = ((cnt = ntapi->vsnprintf(chbuf,size,fmt,ap)) < size) + ? chbuf : calloc(1, cnt + 1); + + va_end(ap); + + if (buf == chbuf) { + (void)0; + + } else if (buf) { + va_start(ap,fmt); + ntapi->vsprintf(buf,fmt,ap); + va_end(ap); + + } else { + return -1; + } + + ret = 0; + ch = buf; + + for (; cnt && ret>=0; ) { + ret = toks_write(fd,ch,cnt); + + while ((ret == NT_STATUS_ALERTED) || (ret == NT_STATUS_USER_APC)) + ret = toks_write(fd,ch,cnt); + + ch += ret; + cnt -= ret; + } + + ret = (ret < 0) ? -1 : ch - buf; + + if (buf != chbuf) + free(buf); + + return ret; +} diff --git a/src/internal/toksvc_dprintf_impl.h b/src/internal/toksvc_dprintf_impl.h new file mode 100644 index 0000000..7a2ce06 --- /dev/null +++ b/src/internal/toksvc_dprintf_impl.h @@ -0,0 +1,10 @@ +#ifndef TOKSVC_DPRINTF_IMPL_H +#define TOKSVC_DPRINTF_IMPL_H + +#ifdef ARGV_DRIVER +#define argv_dprintf toks_dprintf +#endif + +int toks_dprintf(int fd, const char * fmt, ...); + +#endif