diff options
author | H. Peter Anvin <hpa@zytor.com> | 2007-09-28 10:50:20 -0700 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2007-09-28 10:50:20 -0700 |
commit | 304b60556397464143c6fc24edb856b03fc9781a (patch) | |
tree | 230a24f6dc769b2e54088cebe34db0a7829bf681 /lib | |
parent | bb2018587b4e14a170a648d08dcdd74dc995d8ce (diff) | |
download | nasm-304b60556397464143c6fc24edb856b03fc9781a.tar.gz nasm-304b60556397464143c6fc24edb856b03fc9781a.tar.bz2 nasm-304b60556397464143c6fc24edb856b03fc9781a.zip |
Add substitutes for snprintf() and vsnprintf()
To deal with fools^Wpeople trying to keep really old systems alive,
create a proper framework for substitution functions, and make it
possible to deal with the lack of snprintf/vsnprintf in particular.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/snprintf.c | 24 | ||||
-rw-r--r-- | lib/vsnprintf.c | 49 |
2 files changed, 73 insertions, 0 deletions
diff --git a/lib/snprintf.c b/lib/snprintf.c new file mode 100644 index 0000000..f56a492 --- /dev/null +++ b/lib/snprintf.c @@ -0,0 +1,24 @@ +/* + * snprintf() + * + * Implement snprintf() in terms of vsnprintf() + */ + +#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/vsnprintf.c b/lib/vsnprintf.c new file mode 100644 index 0000000..b2b19d9 --- /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 <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> + +#include "nasmlib.h" + +extern efunc nasm_malloc_error; + +#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 (rv < (int)size-1) + bytes = rv; + else + bytes = size-1; + + if (size > 0) { + memcpy(str, snprintf_buffer, bytes); + str[bytes] = '\0'; + } + + return rv; +} |