diff options
author | Anas Nashif <anashif@tizendev.org> | 2012-05-23 08:22:02 +0900 |
---|---|---|
committer | Gerrit Code Review <gerrit2@localhost> | 2012-05-23 08:22:02 +0900 |
commit | b9fae45f49f97914a89b763028f5dbf8b70a9cbc (patch) | |
tree | a76f72f9a6d751f31456309e3cfdf3022b6c5546 /lib | |
parent | ecc629773ccf1eaae669dce3d2b017ed476ce911 (diff) | |
parent | b9fad1ab2ad3bd87bff05c4688c978d582ada438 (diff) | |
download | nasm-b9fae45f49f97914a89b763028f5dbf8b70a9cbc.tar.gz nasm-b9fae45f49f97914a89b763028f5dbf8b70a9cbc.tar.bz2 nasm-b9fae45f49f97914a89b763028f5dbf8b70a9cbc.zip |
Merge " Upstream version 2.08rc7"
Diffstat (limited to 'lib')
-rw-r--r-- | lib/snprintf.c | 25 | ||||
-rw-r--r-- | lib/strlcpy.c | 53 | ||||
-rw-r--r-- | lib/vsnprintf.c | 49 |
3 files changed, 127 insertions, 0 deletions
diff --git a/lib/snprintf.c b/lib/snprintf.c new file mode 100644 index 0000000..01734dc --- /dev/null +++ b/lib/snprintf.c @@ -0,0 +1,25 @@ +/* + * snprintf() + * + * Implement snprintf() in terms of vsnprintf() + */ + +#include "compiler.h" + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> + +#include "nasmlib.h" + +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; +} diff --git a/lib/strlcpy.c b/lib/strlcpy.c new file mode 100644 index 0000000..4335359 --- /dev/null +++ b/lib/strlcpy.c @@ -0,0 +1,53 @@ +/* + * 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 <sys/types.h> +#include <string.h> +#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/lib/vsnprintf.c b/lib/vsnprintf.c new file mode 100644 index 0000000..f5ae2fe --- /dev/null +++ b/lib/vsnprintf.c @@ -0,0 +1,49 @@ +/* + * 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" + +#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_malloc_error(ERR_PANIC|ERR_NOFILE, + "snprintf: size (%d) > BUFFER_SIZE (%d)", + size, BUFFER_SIZE); + size = BUFFER_SIZE; + } + + rv = vsprintf(snprintf_buffer, format, ap); + if (rv >= BUFFER_SIZE) { + nasm_malloc_error(ERR_PANIC|ERR_NOFILE, + "snprintf 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; +} |