summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <abaumann@yahoo.com>2009-03-10 17:15:28 +0100
committerAndreas Baumann <abaumann@yahoo.com>2009-03-10 17:15:28 +0100
commit0de4a082d0f8a156b5aa40719c13e9fe44d2787d (patch)
tree5275d618ec64958b05e88c989c13ad9a4a559ccf
parenta6b606fd903e4c7dd188fe6a8dd35ed583e54cbf (diff)
downloadwolfbones-0de4a082d0f8a156b5aa40719c13e9fe44d2787d.tar.gz
wolfbones-0de4a082d0f8a156b5aa40719c13e9fe44d2787d.tar.bz2
cleanup in stdio.h with snprintf (on Linux works at least), also added testing for snprintf
-rw-r--r--include/wolf/port/stdio.h70
-rw-r--r--src/GNUmakefile6
-rw-r--r--src/port/snprintf.c21
-rw-r--r--src/port/snprintf.h40
-rw-r--r--src/port/stdio.c25
-rw-r--r--src/port/stdio.h18
-rw-r--r--tests/port/GNUmakefile5
-rw-r--r--tests/port/test_snprintf.c38
8 files changed, 160 insertions, 63 deletions
diff --git a/include/wolf/port/stdio.h b/include/wolf/port/stdio.h
new file mode 100644
index 0000000..bcf8d29
--- /dev/null
+++ b/include/wolf/port/stdio.h
@@ -0,0 +1,70 @@
+/*
+ Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef WOLF_STDIO_H
+#define WOLF_STDIO_H
+
+/**
+ * @addtogroup wolf_port Porting
+ * @{
+ */
+
+/**
+ * @file stdio.h
+ * @brief nivelate things found in stdio.h, mostly snprintf's
+ * @author Andreas Baumann <abaumann@yahoo.com>
+ */
+
+#include "port/sys.h"
+
+#include <stdio.h>
+
+#if defined HAVE_STDARG_H
+#include <stdarg.h> /* for va_list */
+#endif
+
+#if defined HAVE_SYS_TYPES_H
+#include <sys/types.h> /* for size_t */
+#endif
+
+/* TODO: check which features we need in snprintf, make sure the platform one supports it,
+ * otherwise we take the stub */
+/* TODO: also clean up mess here with autoconf relicts! */
+#if defined _WIN32
+/* secure snprintf on Wndows is called differently */
+#define snprintf sprintf_s
+#else
+
+#if !defined HAVE_SNPRINTF || defined WOLF_TEST_SNPRINTF
+extern int rpl_snprintf( char *str, size_t size, const char *format, ... );
+#endif /* !defined HAVE_SNPRINTF || defined TEST_SNPRINTF */
+#if !defined HAVE_SNPRINTF
+#define snprintf( s1, s2, n ) rpl_snprintf( s1, s2, n )
+#endif /* !defined HAVE_SNPRINTF */
+
+#if !defined HAVE_VSNPRINTF || defined WOLF_TEST_VSNPRINTF
+extern int rpl_vsnprintf( char *str, size_t size, const char *format, va_list args );
+#endif /* !defined HAVE_VSNPRINTF || defined TEST_VSNPRINTF */
+#if !defined HAVE_VSNPRINTF
+#define vsnprintf rpl_vsnprintf
+#endif /* !defined HAVE_VSNPRINTF */
+
+#endif /* defined _WIN32 */
+
+/** @} */ /* @addtogroup wolf_port */
+
+#endif /* ifndef WOLF_STDIO_H */
diff --git a/src/GNUmakefile b/src/GNUmakefile
index 8ab01d4..5d9417d 100644
--- a/src/GNUmakefile
+++ b/src/GNUmakefile
@@ -11,7 +11,7 @@ LIBS = libwolf.a
PORT_OBJS = \
port/string.o \
port/unistd.o \
- port/snprintf.o \
+ port/stdio.o \
port/time.o
COMMON_OBJS = \
@@ -32,8 +32,8 @@ local_all: $(LIBS)
-include $(TOPDIR)/makefiles/gmake/sub.mk
# snprintf has some quirks, compile relaxed
-port/snprintf.o : port/snprintf.c port/snprintf.h
- $(CC) -c -o $@ $(INCLUDE_DIRS) $<
+#port/snprintf.o : port/snprintf.c $(TOPDIR)/include/wolf/port/snprintf.h
+# $(CC) -c -o $@ $(INCLUDE_DIRS) $<
libwolf.a: $(OBJS)
$(AR) cr $@ $?
diff --git a/src/port/snprintf.c b/src/port/snprintf.c
index 6062154..f5731b0 100644
--- a/src/port/snprintf.c
+++ b/src/port/snprintf.c
@@ -165,6 +165,7 @@
*/
#include "port/snprintf.h"
+#include "port/stdio.h"
#if HAVE_CONFIG_H
#include <config.h>
@@ -283,7 +284,7 @@
#define vasprintf rpl_vasprintf
#endif /* TEST_SNPRINTF */
-#if !defined( HAVE_SNPRINTF ) || !defined( HAVE_VSNPRINTF ) || !defined( HAVE_ASPRINTF ) || !defined( HAVE_VASPRINTF )
+#if !defined( HAVE_SNPRINTF ) || !defined( HAVE_VSNPRINTF ) || !defined( HAVE_ASPRINTF ) || !defined( HAVE_VASPRINTF ) || defined( WOLF_TEST_SNPRINTF ) || defined( WOLF_TEST_VSNPRINTF )
#include <stdio.h> /* For NULL, size_t, vsnprintf(3), and vasprintf(3). */
#ifdef VA_START
#undef VA_START
@@ -301,7 +302,9 @@
#define VA_SHIFT(ap, value, type) value = va_arg(ap, type)
#endif /* HAVE_STDARG_H */
-#if !defined HAVE_VASPRINTF || !HAVE_VASPRINTF
+#if defined NOT_USED
+
+#if !defined HAVE_VASPRINTF || HAVE_VASPRINTF
#if HAVE_STDLIB_H
#include <stdlib.h> /* For malloc(3). */
#endif /* HAVE_STDLIB_H */
@@ -327,7 +330,9 @@ static void *mymemcpy(void *, void *, size_t);
#endif /* HAVE_VA_COPY */
#endif /* !HAVE_VASPRINTF */
-#if !defined HAVE_VSNPRINTF
+#endif
+
+#if !defined HAVE_VSNPRINTF ||defined WOLF_TEST_VSNPRINTF
#include <errno.h> /* For ERANGE and errno. */
#include <limits.h> /* For *_MAX. */
#if HAVE_INTTYPES_H
@@ -1481,6 +1486,8 @@ mypow10(int exponent)
}
#endif /* !HAVE_VSNPRINTF */
+#if defined NOT_USED
+
#if !defined HAVE_VASPRINTF || !HAVE_VASPRINTF
#if defined NEED_MYMEMCPY && NEED_MYMEMCPY
void *
@@ -1512,7 +1519,9 @@ rpl_vasprintf(char **ret, const char *format, va_list ap)
}
#endif /* !HAVE_VASPRINTF */
-#if !defined HAVE_SNPRINTF
+#endif
+
+#if !defined HAVE_SNPRINTF || defined WOLF_TEST_SNPRINTF
#if HAVE_STDARG_H
int
rpl_snprintf(char *str, size_t size, const char *format, ...)
@@ -1539,6 +1548,8 @@ rpl_snprintf(va_alist) va_dcl
}
#endif /* !HAVE_SNPRINTF */
+#if defined NOT_USED
+
#if !defined HAVE_ASPRINTF || !HAVE_ASPRINTF
#if HAVE_STDARG_H
int
@@ -1563,6 +1574,8 @@ rpl_asprintf(va_alist) va_dcl
return len;
}
#endif /* !HAVE_ASPRINTF */
+#endif
+
#else /* Dummy declaration to avoid empty translation unit warnings. */
int main(void);
#endif /* !HAVE_SNPRINTF || !HAVE_VSNPRINTF || !HAVE_ASPRINTF || [...] */
diff --git a/src/port/snprintf.h b/src/port/snprintf.h
index 4e06c8b..0ffffe2 100644
--- a/src/port/snprintf.h
+++ b/src/port/snprintf.h
@@ -13,6 +13,9 @@
#ifndef HAVE_STDARG_H
#define HAVE_STDARG_H 1
#endif
+#ifndef HAVE_SYS_TYPES_H
+#define HAVE_SYS_TYPES_H 1
+#endif
#ifndef HAVE_STDDEF_H
#define HAVE_STDDEF_H 1
#endif
@@ -34,41 +37,4 @@
#include <sys/types.h> /* for size_t */
#endif
-#if !defined HAVE_VSNPRINTF
-#define vsnprintf rpl_vsnprintf
-#if HAVE_STDARG_H
-extern int rpl_vsnprintf(char *str, size_t size, const char *format, va_list args);
-#else
- #error va_list required for vsnprintf!
-#endif /* HAVE_STDARG_H */
-#endif /* !defined HAVE_VSNPRINTF */
-
-#if !defined HAVE_SNPRINTF
-#define snprintf rpl_snprintf
-#if HAVE_STDARG_H
-extern int
-rpl_snprintf(char *str, size_t size, const char *format, ...);
-#else
-extern int
-rpl_snprintf(va_alist) va_dcl;
-#endif /* HAVE_STDARG_H */
-#endif /* ! defined HAVE_SNPRINTF */
-
-#if !defined HAVE_VASPRINTF
-#define vasprintf rpl_vasprintf
-extern int
-rpl_vasprintf(char **ret, const char *format, va_list ap);
-#endif /* ! defined HAVE_VASPRINTF */
-
-#if !defined HAVE_ASPRINTF
-#define asprintf rpl_asprintf
-#if HAVE_STDARG_H
-extern int
-rpl_asprintf(char **ret, const char *format, ...);
-#else
-extern int
-rpl_asprintf(va_alist) va_dcl;
-#endif /* HAVE_STDARG_H */
-#endif /* ! defined HAVE_ASPRINTF */
-
#endif /* ifndef __SNPRINTF_H */
diff --git a/src/port/stdio.c b/src/port/stdio.c
new file mode 100644
index 0000000..d7a4d3f
--- /dev/null
+++ b/src/port/stdio.c
@@ -0,0 +1,25 @@
+/*
+ Copyright (C) 2008 Andreas Baumann <abaumann@yahoo.com>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "port/stdio.h"
+
+/**
+ * C99 snprintf and vsnprintf
+ * (from http://www.jhweiss.de/software/snprintf.html)
+ */
+
+#include "port/snprintf.c"
diff --git a/src/port/stdio.h b/src/port/stdio.h
deleted file mode 100644
index 8f09201..0000000
--- a/src/port/stdio.h
+++ /dev/null
@@ -1,18 +0,0 @@
-#ifndef __STDIO_H
-#define __STDIO_H
-
-#include "port/sys.h"
-
-#include <stdio.h>
-
-/* TODO: check which features we need in snprintf, make sure the platform one supports it,
- * otherwise we take the stub */
-/* TODO: also clean up mess here with autoconf relicts! */
-#if defined _WIN32
-/* secure snprintf on Wndows is called differently */
-#define snprintf sprintf_s
-#else
-#include "port/snprintf.h"
-#endif
-
-#endif /* ifndef __STDIO_H */
diff --git a/tests/port/GNUmakefile b/tests/port/GNUmakefile
index dd28702..af58e2f 100644
--- a/tests/port/GNUmakefile
+++ b/tests/port/GNUmakefile
@@ -11,7 +11,8 @@ TEST_BINS = \
test_strerror_r$(EXE) \
test_strcasecmp$(EXE) \
test_strncasecmp$(EXE) \
- test_localtime_r$(EXE)
+ test_localtime_r$(EXE) \
+ test_snprintf$(EXE)
-include $(TOPDIR)/makefiles/gmake/sub.mk
@@ -32,3 +33,5 @@ local_test:
@./test_strncasecmp >/dev/null
@echo "Testing localtime_r.."
@./test_localtime_r >/dev/null
+ @echo "Testing snprintf.."
+ @./test_snprintf > /dev/null
diff --git a/tests/port/test_snprintf.c b/tests/port/test_snprintf.c
new file mode 100644
index 0000000..a9ddb45
--- /dev/null
+++ b/tests/port/test_snprintf.c
@@ -0,0 +1,38 @@
+#include "port/sys.h"
+
+#define WOLF_TEST_SNPRINTF
+#include "port/stdio.c" /* for snprintf */
+
+#include "port/string.h" /* for strcmp */
+#include <stdlib.h> /* for exit, EXIT_SUCCESS, free */
+
+int main( void ) {
+ char buf1[255];
+ int res1;
+ char buf2[255];
+ int res2;
+ char buf3[255];
+ int res3;
+ char small_buf1[10];
+ int small_res1;
+ char small_buf2[10];
+ int small_res2;
+
+ /* test if we get the same things for the most important format strings */
+ res1 = rpl_snprintf( buf1, 255, "test with %s %d %lu", "test", 12, 234443334L );
+ res2 = snprintf( buf2, 255, "test with %s %d %lu", "test", 12, 234443334L );
+ res3 = snprintf( buf3, 255, "test with %s %d %lu", "test", 12, 234443334L );
+ if( res1 != res2 || res1 != res3 || res1 != 27 ) return EXIT_FAILURE;
+ if( strcmp( buf1, buf2 ) != 0 ||
+ strcmp( buf1, buf2 ) != 0 ) return EXIT_FAILURE;
+
+ /* most important, does the overflow protection work */
+ small_res1 = rpl_snprintf( small_buf1, 10, "%s", "12345678901234" );
+ small_res2 = rpl_snprintf( small_buf2, 10, "%s", "12345678901234" );
+ if( small_res1 != small_res2 || small_res1 != 14 ) return EXIT_FAILURE;
+ if( small_buf1[9] != '\0' || small_buf2[9] != '\0' ) return EXIT_FAILURE;
+ if( strcmp( small_buf1, small_buf2 ) != 0 ) return EXIT_FAILURE;
+ if( strcmp( small_buf1, "123456789" ) != 0 ) return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
+}