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 | |
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.
-rw-r--r-- | Makefile.in | 14 | ||||
-rw-r--r-- | Mkfiles/msvc.mak | 2 | ||||
-rw-r--r-- | Mkfiles/openwcom.mak | 4 | ||||
-rw-r--r-- | Mkfiles/owlinux.mak | 4 | ||||
-rw-r--r-- | compiler.h | 21 | ||||
-rw-r--r-- | configure.in | 9 | ||||
-rw-r--r-- | lib/snprintf.c | 24 | ||||
-rw-r--r-- | lib/vsnprintf.c | 49 | ||||
-rw-r--r-- | nasmlib.c | 2 |
9 files changed, 112 insertions, 17 deletions
diff --git a/Makefile.in b/Makefile.in index d41d455..dd21a10 100644 --- a/Makefile.in +++ b/Makefile.in @@ -23,6 +23,8 @@ LDFLAGS = @LDFLAGS@ LIBS = @LIBS@ PERL = perl -I$(srcdir)/perllib +XOBJS = @XOBJS@ + INSTALL = @INSTALL@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ INSTALL_DATA = @INSTALL_DATA@ @@ -70,11 +72,11 @@ NDISASM = ndisasm.$(O) disasm.$(O) sync.$(O) nasmlib.$(O) insnsd.$(O) all: nasm$(X) ndisasm$(X) nasm.man ndisasm.man cd rdoff && $(MAKE) all -nasm$(X): $(NASM) - $(CC) $(LDFLAGS) -o nasm$(X) $(NASM) $(LIBS) +nasm$(X): $(NASM) $(XOBJS) + $(CC) $(LDFLAGS) -o nasm$(X) $(NASM) $(XOBJS) $(LIBS) -ndisasm$(X): $(NDISASM) - $(CC) $(LDFLAGS) -o ndisasm$(X) $(NDISASM) $(LIBS) +ndisasm$(X): $(NDISASM) $(XOBJS) + $(CC) $(LDFLAGS) -o ndisasm$(X) $(NDISASM) $(XOBJS) $(LIBS) # These source files are automagically generated from a single # instruction-table file by a Perl script. They're distributed, @@ -214,7 +216,7 @@ splint: # alldeps: perlreq $(PERL) mkdep.pl -M Makefile.in Mkfiles/*.mak -- \ - . output + . output lib ./config.status #-- Magic hints to mkdep.pl --# @@ -242,6 +244,8 @@ insnsd.$(O): insnsd.c compiler.h config.h insns.h insnsi.h nasm.h nasmlib.h \ insnsn.$(O): insnsn.c labels.$(O): labels.c compiler.h config.h hashtbl.h insnsi.h nasm.h \ nasmlib.h regs.h version.h +lib/snprintf.$(O): lib/snprintf.c compiler.h config.h nasmlib.h +lib/vsnprintf.$(O): lib/vsnprintf.c compiler.h config.h nasmlib.h listing.$(O): listing.c compiler.h config.h insnsi.h listing.h nasm.h \ nasmlib.h regs.h version.h macros.$(O): macros.c diff --git a/Mkfiles/msvc.mak b/Mkfiles/msvc.mak index d0ab870..66cd981 100644 --- a/Mkfiles/msvc.mak +++ b/Mkfiles/msvc.mak @@ -192,6 +192,8 @@ insnsd.$(O): insnsd.c compiler.h insns.h insnsi.h nasm.h nasmlib.h regs.h \ insnsn.$(O): insnsn.c labels.$(O): labels.c compiler.h hashtbl.h insnsi.h nasm.h nasmlib.h regs.h \ version.h +lib/snprintf.$(O): lib/snprintf.c compiler.h nasmlib.h +lib/vsnprintf.$(O): lib/vsnprintf.c compiler.h nasmlib.h listing.$(O): listing.c compiler.h insnsi.h listing.h nasm.h nasmlib.h \ regs.h version.h macros.$(O): macros.c diff --git a/Mkfiles/openwcom.mak b/Mkfiles/openwcom.mak index 382dc05..955f4f4 100644 --- a/Mkfiles/openwcom.mak +++ b/Mkfiles/openwcom.mak @@ -16,7 +16,7 @@ mandir = $(prefix)\man CC = wcl386 CFLAGS = -3 -bcl=$(TARGET) -ox -wx -ze -fpi BUILD_CFLAGS = $(CFLAGS) # -I$(srcdir)/inttypes -INTERNAL_CFLAGS = -I$(srcdir) -I. +INTERNAL_CFLAGS = -I$(srcdir) -I. -DHAVE_SNPRINTF -DHAVE_VSNPRINTF ALL_CFLAGS = $(BUILD_CFLAGS) $(INTERNAL_CFLAGS) LD = $(CC) LDFLAGS = $(ALL_CFLAGS) @@ -217,6 +217,8 @@ insnsd.$(O): insnsd.c compiler.h insns.h insnsi.h nasm.h nasmlib.h regs.h & insnsn.$(O): insnsn.c labels.$(O): labels.c compiler.h hashtbl.h insnsi.h nasm.h nasmlib.h regs.h & version.h +lib\snprintf.$(O): lib\snprintf.c compiler.h nasmlib.h +lib\vsnprintf.$(O): lib\vsnprintf.c compiler.h nasmlib.h listing.$(O): listing.c compiler.h insnsi.h listing.h nasm.h nasmlib.h & regs.h version.h macros.$(O): macros.c diff --git a/Mkfiles/owlinux.mak b/Mkfiles/owlinux.mak index 6745eb6..f25e039 100644 --- a/Mkfiles/owlinux.mak +++ b/Mkfiles/owlinux.mak @@ -14,7 +14,7 @@ mandir = $(prefix)/man CC = wcl386 CFLAGS = -3 -bcl=$(TARGET) -ox -wx -ze -fpi BUILD_CFLAGS = $(CFLAGS) # -I$(srcdir)/inttypes -INTERNAL_CFLAGS = -I$(srcdir) -I. +INTERNAL_CFLAGS = -I$(srcdir) -I. -DHAVE_SNPRINTF -DHAVE_VSNPRINTF ALL_CFLAGS = $(BUILD_CFLAGS) $(INTERNAL_CFLAGS) LD = $(CC) LDFLAGS = $(ALL_CFLAGS) @@ -210,6 +210,8 @@ insnsd.$(O): insnsd.c compiler.h insns.h insnsi.h nasm.h nasmlib.h regs.h \ insnsn.$(O): insnsn.c labels.$(O): labels.c compiler.h hashtbl.h insnsi.h nasm.h nasmlib.h regs.h \ version.h +lib/snprintf.$(O): lib/snprintf.c compiler.h nasmlib.h +lib/vsnprintf.$(O): lib/vsnprintf.c compiler.h nasmlib.h listing.$(O): listing.c compiler.h insnsi.h listing.h nasm.h nasmlib.h \ regs.h version.h macros.$(O): macros.c @@ -38,13 +38,24 @@ #endif /* Some versions of MSVC have these only with underscores in front */ - -#if !defined(HAVE_SNPRINTF) && defined(HAVE__SNPRINTF) -# define snprintf _snprintf +#include <stdio.h> +#include <stddef.h> +#include <stdarg.h> + +#ifndef HAVE_SNPRINTF +# ifdef HAVE__SNPRINTF +# define snprintf _snprintf +# else +int snprintf(char *, size_t, const char *, ...); +# endif #endif -#if !defined(HAVE_VSNPRINTF) && defined(HAVE__VSNPRINTF) -# define vsnprintf _vsnprintf +#ifndef HAVE_VSNPRINTF +# ifdef HAVE__VSNPRINT +# define vsnprintf _vsnprintf +# else +int vsnprintf(char *, size_t, const char *, va_list); +# endif #endif #endif /* COMPILER_H */ diff --git a/configure.in b/configure.in index a52b620..3def6cb 100644 --- a/configure.in +++ b/configure.in @@ -87,6 +87,7 @@ AC_C_CONST AC_TYPE_SIZE_T dnl Checks for library functions. +AC_SUBST_FILE(XOBJS) AC_CHECK_FUNCS(strcspn, , AC_MSG_ERROR([NASM requires ANSI C (specifically, "strcspn")])) @@ -95,15 +96,15 @@ AC_CHECK_FUNCS(strspn, , AC_MSG_ERROR([NASM requires ANSI C (specifically, "strspn")])) missing=true -AC_CHECK_FUNCS([snprintf _snprintf], missing=false) +AC_CHECK_FUNCS([vsnprintf _vsnprintf], missing=false) if $missing; then - AC_MSG_ERROR([NASM requires ISO C99 (specifically, "snprintf")]) + XOBJS="$XOBJS lib/vsnprintf.o" fi missing=true -AC_CHECK_FUNCS([vsnprintf _vsnprintf], missing=false) +AC_CHECK_FUNCS([snprintf _snprintf], missing=false) if $missing; then - AC_MSG_ERROR([NASM requires ISO C99 (specifically, "vsnprintf")]) + XOBJS="$XOBJS lib/snprintf.o" fi AC_CHECK_FUNCS(getuid) 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; +} @@ -17,7 +17,7 @@ #include "insns.h" int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */ -static efunc nasm_malloc_error; +efunc nasm_malloc_error; /* Exported for the benefit of vsnprintf.c */ #ifdef LOGALLOC static FILE *logfp; |