summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/wolf/log/log.h19
-rw-r--r--include/wolf/port/string.h14
-rw-r--r--src/log/log.c53
-rw-r--r--src/port/string.c43
-rw-r--r--tests/port/GNUmakefile8
-rw-r--r--tests/port/Makefile.W328
-rw-r--r--tests/port/test_strlcat.c29
-rw-r--r--tests/port/test_strlcpy.c26
-rw-r--r--tests/service/testservice.c27
9 files changed, 192 insertions, 35 deletions
diff --git a/include/wolf/log/log.h b/include/wolf/log/log.h
index b9f04b8..3ea748b 100644
--- a/include/wolf/log/log.h
+++ b/include/wolf/log/log.h
@@ -18,6 +18,8 @@
#ifndef WOLF_LOG_H
#define WOLF_LOG_H
+#include <stdarg.h> /* for ap_list */
+
#include "port/stdbool.h"
/**
@@ -286,6 +288,23 @@ void wolf_log_reopenlogtoeventlog( void );
*/
void wolf_log( wolf_log_level_t level, int category_id, int message_id, const char *format, ... );
+/**
+ * Write a formatted log message to the logger (with an ap_list instead
+ * of variable arguments)
+ *
+ * @param level one of wolf_log_level_t
+ * @param category_id a category as defined in messages.h
+ * @param message_id a message id as defined in messages.h
+ * @param format snprintf like format string,
+ * the rest of the parameters given
+ * as defined in the format string
+ * @param ap an ap_list storing the parameters to map
+ */
+void wolf_log_ap( wolf_log_level_t level, int category_id, int message_id, const char *format, va_list ap );
+
+void wolf_log_win32( wolf_log_level_t level, int category_id, int message_id, const char *format, ... );
+void wolf_log_win32_ap( wolf_log_level_t level, int category_id, int message_id, const char *format, va_list ap );
+
#ifdef __cplusplus
}
#endif
diff --git a/include/wolf/port/string.h b/include/wolf/port/string.h
index 39ccd02..0b79f2a 100644
--- a/include/wolf/port/string.h
+++ b/include/wolf/port/string.h
@@ -73,6 +73,20 @@ extern int wolf_port_strncasecmp( const char *s1, const char *s2, size_t n );
#define strncasecmp( s1, s2, n ) wolf_port_strncasecmp( s1, s2, n )
#endif
+#if !defined HAVE_STRLCPY || defined TEST_STRLCPY
+extern size_t wolf_port_strlcpy( char *d, const char *s, size_t bufsize );
+#endif
+#if !defined HAVE_STRLCPY
+#define strlcpy( d, s, bufsize ) wolf_port_strlcpy( d, s, bufsize )
+#endif
+
+#if !defined HAVE_STRLCAT || defined TEST_STRLCAT
+extern size_t wolf_port_strlcat( char *d, const char *s, size_t bufsize );
+#endif
+#if !defined HAVE_STRLCAT
+#define strlcat( d, s, bufsize ) wolf_port_strlcat( d, s, bufsize )
+#endif
+
/** @} */ /* @addtogroup wolf_port */
#endif /* ifndef WOLF_STRING_H */
diff --git a/src/log/log.c b/src/log/log.c
index 28218d2..edbc00d 100644
--- a/src/log/log.c
+++ b/src/log/log.c
@@ -706,12 +706,17 @@ void wolf_log_reopenlogtoeventlog( void ) {
void wolf_log( wolf_log_level_t level, int category_id, int message_id, const char *format, ... ) {
va_list ap;
- char s[1024];
va_start( ap, format );
- vsnprintf( s, 1024, format, ap );
+ wolf_log_ap( level, category_id, message_id, format, ap );
va_end( ap );
-
+}
+
+void wolf_log_ap( wolf_log_level_t level, int category_id, int message_id, const char *format, va_list ap ) {
+ char s[1024];
+
+ vsnprintf( s, 1024, format, ap );
+
if( level <= log_stderr_level )
fprintf( stderr, "%s: %s\n",wolf_log_level_to_str( level ), s );
@@ -746,13 +751,10 @@ void wolf_log( wolf_log_level_t level, int category_id, int message_id, const ch
/* parse C-snprintf-like format string, map the placeholders
* there to normal strings, then add them to a string array
*/
- va_list ap;
int nof_placeholders = compute_nof_placeholders( format );
LPSTR *msg_arr = (LPSTR *)malloc( sizeof( LPCTSTR ) * nof_placeholders );
- va_start( ap, format );
map_placeholders_to_strings( msg_arr, format, ap );
- va_end( ap );
if( !ReportEvent(
event_source, /* event source handle */
@@ -776,3 +778,42 @@ void wolf_log( wolf_log_level_t level, int category_id, int message_id, const ch
}
#endif /* defined HAVE_EVENTLOG */
}
+
+#if defined _WIN32
+
+void wolf_log_win32( wolf_log_level_t level, int category_id, int message_id, const char *format, ... ) {
+ va_list ap;
+
+ va_start( ap, format );
+ wolf_log_win32_ap( level, category_id, message_id, format, ap );
+ va_end( ap );
+}
+
+void wolf_log_win32_ap( wolf_log_level_t level, int category_id, int message_id, const char *format, va_list ap ) {
+ DWORD last_error = GetLastError( );
+ LPVOID buf;
+ DWORD buf_size;
+ DWORD res;
+
+ res = FormatMessage(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM,
+ NULL, /* message is from system */
+ last_error, /* there is a message with that id */
+ 0, /* default language preference */
+ (LPTSTR)&buf, /* buffer allocated internally with LocalAlloc */
+ 0, /* minimum allocation size */
+ NULL ); /* no arguments */
+
+ if( res == 0 ) {
+ /* FormatMessage failed, so log the error just with the
+ * GetLastError() number
+ */
+ char new_format[1024];
+
+
+ wolf_log_ap( level, category_id, message_id, new_format, ap );
+
+ }
+}
+#endif /* defined _WIN32 */
diff --git a/src/port/string.c b/src/port/string.c
index 78c3685..942e688 100644
--- a/src/port/string.c
+++ b/src/port/string.c
@@ -118,3 +118,46 @@ int wolf_port_strncasecmp( const char *s1, const char *s2, size_t n ) {
}
#endif /* !defined HAVE_STRNCASECMP || defined TEST_STRNCASECMP */
+
+#if !defined HAVE_STRLCPY || defined TEST_STRLCPY
+size_t wolf_port_strlcpy( char *d, const char *s, size_t bufsize ) {
+ size_t len;
+ size_t ret;
+
+ if( !d || !s || bufsize <= 0 ) return 0;
+
+ len = strlen( s );
+ ret = len;
+ if( len >= bufsize ) {
+ len = bufsize-1;
+ }
+
+ memcpy( d, s, len );
+ d[len] = 0;
+
+ return ret;
+}
+#endif /* !defined HAVE_STRLCPY || defined TEST_STRLCPY */
+
+#if !defined HAVE_STRLCAT || defined TEST_STRLCAT
+size_t wolf_port_strlcat( char *d, const char *s, size_t bufsize ) {
+ size_t len1;
+ size_t len2;
+ size_t ret;
+
+ if( !d || !s || bufsize <= 0 ) return 0;
+
+ len1 = strlen( d );
+ len2 = strlen( s );
+ ret = len1+len2;
+ if( len1+len2 >= bufsize ) {
+ len2 = bufsize - (len1+1);
+ }
+ if( len2 >= 0 ) {
+ memcpy( d+len1, s, len2 );
+ d[len1+len2] = 0;
+ }
+
+ return ret;
+}
+#endif /* !defined HAVE_STRLCAT || defined TEST_STRLCAT */
diff --git a/tests/port/GNUmakefile b/tests/port/GNUmakefile
index a946ad7..0b0f30c 100644
--- a/tests/port/GNUmakefile
+++ b/tests/port/GNUmakefile
@@ -12,7 +12,9 @@ TEST_BINS = \
test_strcasecmp$(EXE) \
test_strncasecmp$(EXE) \
test_localtime_r$(EXE) \
- test_snprintf$(EXE)
+ test_snprintf$(EXE) \
+ test_strlcpy$(EXE) \
+ test_strlcat$(EXE)
-include $(TOPDIR)/makefiles/gmake/sub.mk
@@ -42,3 +44,7 @@ local_test:
@./test_localtime_r >/dev/null
@echo "Testing snprintf.."
@./test_snprintf > /dev/null
+ @echo "Testing strlcpy.."
+ @./test_strlcpy > /dev/null
+ @echo "Testing strlcat.."
+ @./test_strlcat > /dev/null
diff --git a/tests/port/Makefile.W32 b/tests/port/Makefile.W32
index befea27..958cbff 100644
--- a/tests/port/Makefile.W32
+++ b/tests/port/Makefile.W32
@@ -12,7 +12,9 @@ TEST_BINS = \
test_strcasecmp.exe \
test_strncasecmp.exe \
test_localtime_r.exe \
- test_snprintf.exe
+ test_snprintf.exe \
+ test_strlcpy.exe \
+ test_strlcat.exe
!INCLUDE $(TOPDIR)\makefiles\nmake\sub.mk
@@ -43,3 +45,7 @@ local_test:
@test_localtime_r >NUL 2>NUL
@echo Testing snprintf..
@test_snprintf >NUL 2>NUL
+ @echo Testing strlcpy..
+ @test_strlcpy >NUL 2>NUL
+ @echo Testing strlcat..
+ @test_strlcat >NUL 2>NUL
diff --git a/tests/port/test_strlcat.c b/tests/port/test_strlcat.c
new file mode 100644
index 0000000..402723e
--- /dev/null
+++ b/tests/port/test_strlcat.c
@@ -0,0 +1,29 @@
+#include "port/sys.h"
+
+#define TEST_STRLCAT
+#include "port/string.c" /* for strlcat */
+
+#include <stdlib.h> /* for exit, EXIT_SUCCESS, free */
+
+int main( void ) {
+ char s1[10] = "test";
+ char s2[10] = "test";
+ const char *a = "test";
+
+ wolf_port_strlcat( s1, a, 10 );
+ strlcat( s2, a, 10 );
+ if( strcmp( s1, "testtest" ) != 0 ) return EXIT_FAILURE;
+ if( strcmp( s2, s1 ) != 0 ) return EXIT_FAILURE;
+
+ wolf_port_strlcat( s1, a, 10 );
+ strlcat( s2, a, 10 );
+ if( strcmp( s1, "testtestt" ) != 0 ) return EXIT_FAILURE;
+ if( strcmp( s2, s1 ) != 0 ) return EXIT_FAILURE;
+
+ wolf_port_strlcat( s1, a, 10 );
+ strlcat( s2, a, 10 );
+ if( strcmp( s1, "testtestt" ) != 0 ) return EXIT_FAILURE;
+ if( strcmp( s2, s1 ) != 0 ) return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/port/test_strlcpy.c b/tests/port/test_strlcpy.c
new file mode 100644
index 0000000..d83ed58
--- /dev/null
+++ b/tests/port/test_strlcpy.c
@@ -0,0 +1,26 @@
+#include "port/sys.h"
+
+#define TEST_STRLCPY
+#include "port/string.c" /* for strlcpy */
+
+#include <stdlib.h> /* for exit, EXIT_SUCCESS, free */
+
+int main( void ) {
+ const char *s = "test";
+ char d11[10];
+ char d12[4];
+ char d21[10];
+ char d22[4];
+
+ wolf_port_strlcpy( d11, s, 10 );
+ strlcpy( d21, s, 10 );
+ if( strcmp( d11, s ) != 0 ) return EXIT_FAILURE;
+ if( strcmp( d11, d21 ) != 0 ) return EXIT_FAILURE;
+
+ wolf_port_strlcpy( d12, s, 4 );
+ strlcpy( d22, s, 4 );
+ if( strcmp( d12, "tes" ) != 0 ) return EXIT_FAILURE;
+ if( strcmp( d12, d22 ) != 0 ) return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
+}
diff --git a/tests/service/testservice.c b/tests/service/testservice.c
index 7efa312..76b39b2 100644
--- a/tests/service/testservice.c
+++ b/tests/service/testservice.c
@@ -13,33 +13,6 @@
#define WOLF_MSG_TESTSERVICE_CANT_OPEN_SCM WOLF_MSG_TESTSERVICE_BASE+1
#define WOLF_MSG_TESTSERVICE_REGISTERING_SERVICE WOLF_MSG_TESTSERVICE_BASE+2
-/* TODO: log with GetLastErrror(), FormatStr(), should be a wrapper in wolf/log,
- * the same we should also do for strerror_r
- */
-static void wolf_log_win32( wolf_log_level_t level, int category_id, int message_id, const char *format, ... ) {
- DWORD last_error = GetLastError( );
- LPVOID buf;
- DWORD buf_size;
- DWORD res;
-
- res = FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM,
- NULL, /* message is from system */
- last_error, /* there is a message with that id */
- 0, /* default language preference */
- (LPTSTR)&buf, /* buffer allocated internally with LocalAlloc */
- 0, /* minimum allocation size */
- NULL ); /* no arguments */
- }
-
- if( res != 0 ) {
-
- }
-
- wolf_log(
-}
-
static void wolf_service_register( void ) {
SC_HANDLE scm;