2
0
mirror of https://github.com/openvswitch/ovs synced 2025-10-15 14:17:18 +00:00

stdio: New module, initially to provide working [v]snprintf() on Windows.

This should transparently define snprintf() and vsnprintf() wrappers for
use on Windows.

CC: Saurabh Shah <ssaurabh@vmware.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
This commit is contained in:
Ben Pfaff
2013-12-24 09:18:42 -08:00
parent 554764d1aa
commit cde1c287b1
7 changed files with 120 additions and 3 deletions

View File

@@ -91,8 +91,8 @@ OVS_CHECK_ATOMIC_ALWAYS_LOCK_FREE(8)
OVS_CHECK_POSIX_AIO
OVS_CHECK_PTHREAD_SET_NAME
OVS_CHECK_INCLUDE_NEXT([string.h])
AC_CONFIG_FILES([lib/string.h])
OVS_CHECK_INCLUDE_NEXT([stdio.h string.h])
AC_CONFIG_FILES([lib/stdio.h lib/string.h])
OVS_ENABLE_OPTION([-Wall])
OVS_ENABLE_OPTION([-Wextra])

1
lib/.gitignore vendored
View File

@@ -5,6 +5,7 @@
/coverage-counters.c
/ofp-errors.inc
/ofp-msgs.inc
/stdio.h
/string.h
/vswitch-idl.c
/vswitch-idl.h

View File

@@ -196,6 +196,7 @@ lib_libopenvswitch_la_SOURCES = \
lib/stream-unix.c \
lib/stream.c \
lib/stream.h \
lib/stdio.c \
lib/string.c \
lib/svec.c \
lib/svec.h \
@@ -234,7 +235,9 @@ lib_libopenvswitch_la_SOURCES = \
lib/vswitch-idl.h \
lib/vtep-idl.c \
lib/vtep-idl.h
EXTRA_DIST += lib/string.h.in
EXTRA_DIST += \
lib/stdio.h.in \
lib/string.h.in
nodist_lib_libopenvswitch_la_SOURCES = \
lib/dirs.c

48
lib/stdio.c Normal file
View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include <stdio.h>
#ifdef _WIN32
#undef snprintf
#undef vsnprintf
int
ovs_snprintf(char *s, size_t n, const char *format, ... )
{
va_list args;
int len;
va_start(args, format);
len = ovs_vsnprintf(s, n, format, args);
va_end(args);
return len;
}
int
ovs_vsnprintf(char *s, size_t n, const char *format, va_list args)
{
int needed = _vscprintf(format, args);
if (s && n) {
vsnprintf(s, n, format, args);
s[n - 1] = '\0';
}
return needed;
}
#endif /* _WIN32 */

43
lib/stdio.h.in Normal file
View File

@@ -0,0 +1,43 @@
/*
* Copyright (c) 2013 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#@INCLUDE_NEXT@ @NEXT_STDIO_H@
#if !defined STDIO_H_WRAPPER
#define STDIO_H_WRAPPER 1
#ifdef _WIN32
#include <stdarg.h>
#include <stddef.h>
/* Windows libc has defective snprintf() and vsnprintf():
*
* - They return -1 if the output won't fit.
*
* - They don't null-terminate the output if it won't fit.
*
* We need working versions so here we define substitutes. */
#undef snprintf
#define snprintf ovs_snprintf
int ovs_snprintf(char *, size_t, const char *, ...);
#undef vsnprintf
#define vsnprintf ovs_vsnprintf
int ovs_vsnprintf(char *, size_t, const char *, va_list);
#endif /* _WIN32 */
#endif /* stdio.h wrapper */

View File

@@ -181,3 +181,7 @@ AT_CHECK([sed 's/.*: //
])
AT_CLEANUP
AT_SETUP([snprintf])
AT_CHECK([test-util snprintf])
AT_CLEANUP

View File

@@ -1013,6 +1013,23 @@ test_ovs_scan(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
ovs_assert(sscanf("0x12-3]xyz", "%[^-a-f]", str));
ovs_assert(!strcmp(str, "0x12"));
}
static void
test_snprintf(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
{
char s[16];
ovs_assert(snprintf(s, 4, "abcde") == 5);
ovs_assert(!strcmp(s, "abc"));
ovs_assert(snprintf(s, 5, "abcde") == 5);
ovs_assert(!strcmp(s, "abcd"));
ovs_assert(snprintf(s, 6, "abcde") == 5);
ovs_assert(!strcmp(s, "abcde"));
ovs_assert(snprintf(NULL, 0, "abcde") == 5);
}
static const struct command commands[] = {
{"ctz", 0, 0, test_ctz},
@@ -1028,6 +1045,7 @@ static const struct command commands[] = {
{"follow-symlinks", 1, INT_MAX, test_follow_symlinks},
{"assert", 0, 0, test_assert},
{"ovs_scan", 0, 0, test_ovs_scan},
{"snprintf", 0, 0, test_snprintf},
{NULL, 0, 0, NULL},
};