diff options
author | H. Peter Anvin <hpa@zytor.com> | 2016-03-08 12:14:55 -0800 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2016-03-08 12:20:02 -0800 |
commit | 3ef4f00d5a14000b91851f4c5e4ca5e701550528 (patch) | |
tree | 1b13efa1992915f017c7e3437e19015ba74ad3b6 /stdlib | |
parent | 7ab5595347d70ec9da736a0cedc9675a63356da3 (diff) | |
download | nasm-3ef4f00d5a14000b91851f4c5e4ca5e701550528.tar.gz |
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 <hpa@zytor.com>
Diffstat (limited to 'stdlib')
-rw-r--r-- | stdlib/snprintf.c | 29 | ||||
-rw-r--r-- | stdlib/strlcpy.c | 51 | ||||
-rw-r--r-- | stdlib/vsnprintf.c | 50 |
3 files changed, 130 insertions, 0 deletions
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 <stdio.h> +#include <stdlib.h> +#include <stdarg.h> + +#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 <Todd.Miller@courtesan.com> + * + * 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 <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> + +#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 |