From 3ef4f00d5a14000b91851f4c5e4ca5e701550528 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Tue, 8 Mar 2016 12:14:55 -0800 Subject: lib: split library into stdlib and nasmlib; header handling fixes Split lib/ into nasmlib/ (for nasm-specific functions) and stdlib/ (for replacements for C library functions which may be missing.) Rename the ersatz inttypes.h to nasmint.h so we can use a simple test in compiler.h instead of dealing with include path magic. Remove tests in configure.in for ancient missing functions (which will break the build anyway.) Signed-off-by: H. Peter Anvin --- stdlib/snprintf.c | 29 +++++++++++++++++++++++++++++ stdlib/strlcpy.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ stdlib/vsnprintf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 stdlib/snprintf.c create mode 100644 stdlib/strlcpy.c create mode 100644 stdlib/vsnprintf.c (limited to 'stdlib') diff --git a/stdlib/snprintf.c b/stdlib/snprintf.c new file mode 100644 index 00000000..95bfc314 --- /dev/null +++ b/stdlib/snprintf.c @@ -0,0 +1,29 @@ +/* + * snprintf() + * + * Implement snprintf() in terms of vsnprintf() + */ + +#include "compiler.h" + +#include +#include +#include + +#include "nasmlib.h" + +#if !defined(HAVE_SNPRINTF) && !defined(HAVE__SNPRINTF) + +int snprintf(char *str, size_t size, const char *format, ...) +{ + va_list ap; + int rv; + + va_start(ap, format); + rv = vsnprintf(str, size, format, ap); + va_end(ap); + + return rv; +} + +#endif diff --git a/stdlib/strlcpy.c b/stdlib/strlcpy.c new file mode 100644 index 00000000..22d9ccfb --- /dev/null +++ b/stdlib/strlcpy.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 1998 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "compiler.h" + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +#ifndef HAVE_STRLCPY + +size_t strlcpy(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} + +#endif diff --git a/stdlib/vsnprintf.c b/stdlib/vsnprintf.c new file mode 100644 index 00000000..b8fd082a --- /dev/null +++ b/stdlib/vsnprintf.c @@ -0,0 +1,50 @@ +/* + * vsnprintf() + * + * Poor substitute for a real vsnprintf() function for systems + * that don't have them... + */ + +#include "compiler.h" + +#include +#include +#include +#include + +#include "nasmlib.h" + +#if !defined(HAVE_VSNPRINTF) && !defined(HAVE__VSNPRINTF) + +#define BUFFER_SIZE 65536 /* Bigger than any string we might print... */ + +static char snprintf_buffer[BUFFER_SIZE]; + +int vsnprintf(char *str, size_t size, const char *format, va_list ap) +{ + int rv, bytes; + + if (size > BUFFER_SIZE) { + nasm_panic(ERR_NOFILE, + "vsnprintf: size (%d) > BUFFER_SIZE (%d)", + size, BUFFER_SIZE); + size = BUFFER_SIZE; + } + + rv = vsprintf(snprintf_buffer, format, ap); + if (rv >= BUFFER_SIZE) + nasm_panic(ERR_NOFILE, "vsnprintf buffer overflow"); + + if (size > 0) { + if ((size_t)rv < size-1) + bytes = rv; + else + bytes = size-1; + memcpy(str, snprintf_buffer, bytes); + str[bytes] = '\0'; + } + + return rv; +} + +#endif -- cgit v1.2.1