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])
/coverage-counters.c
/ofp-errors.inc
/ofp-msgs.inc
+/stdio.h
/string.h
/vswitch-idl.c
/vswitch-idl.h
lib/stream-unix.c \
lib/stream.c \
lib/stream.h \
+ lib/stdio.c \
lib/string.c \
lib/svec.c \
lib/svec.h \
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
--- /dev/null
+/*
+ * 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 */
--- /dev/null
+/*
+ * 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 */
])
AT_CLEANUP
+
+AT_SETUP([snprintf])
+AT_CHECK([test-util snprintf])
+AT_CLEANUP
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);
+}
\f
static const struct command commands[] = {
{"ctz", 0, 0, test_ctz},
{"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},
};