From: Saurabh Shah <ssaur...@vmware.com> So that snprintf() & vsnprintf() on windows have C99 like semantics.
Signed-off-by: Saurabh Shah <ssaur...@vmware.com> --- lib/command-line.c | 4 ++-- lib/dynamic-string.c | 4 ++-- lib/json.c | 4 ++-- lib/match.c | 2 +- lib/netdev-vport.c | 2 +- lib/odp-util.c | 2 +- lib/ofp-print.c | 4 ++-- lib/ofp-util.c | 6 +++--- lib/sflow_agent.c | 4 ++-- lib/signals.c | 2 +- lib/socket-util.c | 6 +++--- lib/stream-unix.c | 2 +- lib/util.c | 41 ++++++++++++++++++++++++++++++++++++++--- lib/util.h | 2 ++ lib/vconn.c | 2 +- lib/vlandev.c | 2 +- ovsdb/file.c | 2 +- ovsdb/jsonrpc-server.c | 2 +- ovsdb/log.c | 2 +- tests/test-sflow.c | 4 ++-- tests/test-stp.c | 2 +- 21 files changed, 69 insertions(+), 32 deletions(-) diff --git a/lib/command-line.c b/lib/command-line.c index 805e51b..46182af 100644 --- a/lib/command-line.c +++ b/lib/command-line.c @@ -169,9 +169,9 @@ proctitle_set(const char *format, ...) } va_start(args, format); - n = snprintf(argv_start, argv_size, "%s: ", program_name); + n = ovs_snprintf(argv_start, argv_size, "%s: ", program_name); if (n < argv_size) { - n += vsnprintf(argv_start + n, argv_size - n, format, args); + n += ovs_vsnprintf(argv_start + n, argv_size - n, format, args); } if (n >= argv_size) { /* The name is too long, so add an ellipsis at the end. */ diff --git a/lib/dynamic-string.c b/lib/dynamic-string.c index 914af64..dcc1379 100644 --- a/lib/dynamic-string.c +++ b/lib/dynamic-string.c @@ -152,7 +152,7 @@ ds_put_format_valist(struct ds *ds, const char *format, va_list args_) va_copy(args, args_); available = ds->string ? ds->allocated - ds->length + 1 : 0; - needed = vsnprintf(&ds->string[ds->length], available, format, args); + needed = ovs_vsnprintf(&ds->string[ds->length], available, format, args); va_end(args); if (needed < available) { @@ -162,7 +162,7 @@ ds_put_format_valist(struct ds *ds, const char *format, va_list args_) va_copy(args, args_); available = ds->allocated - ds->length + 1; - needed = vsnprintf(&ds->string[ds->length], available, format, args); + needed = ovs_vsnprintf(&ds->string[ds->length], available, format, args); va_end(args); ovs_assert(needed < available); diff --git a/lib/json.c b/lib/json.c index db0e09e..28c8099 100644 --- a/lib/json.c +++ b/lib/json.c @@ -1740,10 +1740,10 @@ json_serialized_length(const struct json *json) return json_array_serialized_length(&json->u.array); case JSON_INTEGER: - return snprintf(NULL, 0, "%lld", json->u.integer); + return ovs_snprintf(NULL, 0, "%lld", json->u.integer); case JSON_REAL: - return snprintf(NULL, 0, "%.*g", DBL_DIG, json->u.real); + return ovs_snprintf(NULL, 0, "%.*g", DBL_DIG, json->u.real); case JSON_STRING: return json_string_serialized_length(json->u.string); diff --git a/lib/match.c b/lib/match.c index cc18a6a..964dcd9 100644 --- a/lib/match.c +++ b/lib/match.c @@ -921,7 +921,7 @@ match_format(const struct match *match, struct ds *s, unsigned int priority) for (i = 0; i < FLOW_N_REGS; i++) { #define REGNAME_LEN 20 char regname[REGNAME_LEN]; - if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) { + if (ovs_snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) { strcpy(regname, "reg?"); } format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]); diff --git a/lib/netdev-vport.c b/lib/netdev-vport.c index 165c1c6..be9140f 100644 --- a/lib/netdev-vport.c +++ b/lib/netdev-vport.c @@ -147,7 +147,7 @@ netdev_vport_get_dpif_port(const struct netdev *netdev, */ BUILD_ASSERT(NETDEV_VPORT_NAME_BUFSIZE >= IFNAMSIZ); ovs_assert(strlen(type) + 10 < IFNAMSIZ); - snprintf(namebuf, bufsize, "%s_sys_%d", type, + ovs_snprintf(namebuf, bufsize, "%s_sys_%d", type, ntohs(vport->tnl_cfg.dst_port)); return namebuf; } else { diff --git a/lib/odp-util.c b/lib/odp-util.c index f44c7d4..d70c1b0 100644 --- a/lib/odp-util.c +++ b/lib/odp-util.c @@ -120,7 +120,7 @@ ovs_key_attr_to_string(enum ovs_key_attr attr, char *namebuf, size_t bufsize) case __OVS_KEY_ATTR_MAX: default: - snprintf(namebuf, bufsize, "key%u", (unsigned int) attr); + ovs_snprintf(namebuf, bufsize, "key%u", (unsigned int) attr); return namebuf; } } diff --git a/lib/ofp-print.c b/lib/ofp-print.c index 4c89b36..c61cbee 100644 --- a/lib/ofp-print.c +++ b/lib/ofp-print.c @@ -907,7 +907,7 @@ ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason, case OFPRR_METER_DELETE: return "meter_delete"; default: - snprintf(reasonbuf, bufsize, "%d", (int) reason); + ovs_snprintf(reasonbuf, bufsize, "%d", (int) reason); return reasonbuf; } } @@ -2061,7 +2061,7 @@ ofp_port_reason_to_string(enum ofp_port_reason reason, return "modify"; default: - snprintf(reasonbuf, bufsize, "%d", (int) reason); + ovs_snprintf(reasonbuf, bufsize, "%d", (int) reason); return reasonbuf; } } diff --git a/lib/ofp-util.c b/lib/ofp-util.c index a0a372f..d537652 100644 --- a/lib/ofp-util.c +++ b/lib/ofp-util.c @@ -3444,7 +3444,7 @@ ofputil_packet_in_reason_to_string(enum ofp_packet_in_reason reason, case OFPR_N_REASONS: default: - snprintf(reasonbuf, bufsize, "%d", (int) reason); + ovs_snprintf(reasonbuf, bufsize, "%d", (int) reason); return reasonbuf; } } @@ -5077,7 +5077,7 @@ ofputil_port_to_string(ofp_port_t port, #undef OFPUTIL_NAMED_PORT default: - snprintf(namebuf, bufsize, "%"PRIu16, port); + ovs_snprintf(namebuf, bufsize, "%"PRIu16, port); break; } } @@ -5136,7 +5136,7 @@ ofputil_group_to_string(uint32_t group_id, break; default: - snprintf(namebuf, bufsize, "%"PRIu32, group_id); + ovs_snprintf(namebuf, bufsize, "%"PRIu32, group_id); break; } } diff --git a/lib/sflow_agent.c b/lib/sflow_agent.c index 817420d..abc15b6 100644 --- a/lib/sflow_agent.c +++ b/lib/sflow_agent.c @@ -454,7 +454,7 @@ void sfl_agent_resetReceiver(SFLAgent *agent, SFLReceiver *receiver) void sfl_agent_error(SFLAgent *agent, char *modName, char *msg) { char errm[MAX_ERRMSG_LEN]; - snprintf(errm, sizeof errm, "sfl_agent_error: %s: %s\n", modName, msg); + ovs_snprintf(errm, sizeof errm, "sfl_agent_error: %s: %s\n", modName, msg); if(agent->errorFn) (*agent->errorFn)(agent->magic, agent, errm); else { fprintf(stderr, "%s\n", errm); @@ -470,7 +470,7 @@ void sfl_agent_error(SFLAgent *agent, char *modName, char *msg) void sfl_agent_sysError(SFLAgent *agent, char *modName, char *msg) { char errm[MAX_ERRMSG_LEN]; - snprintf(errm, sizeof errm, "sfl_agent_sysError: %s: %s (errno = %d - %s)\n", modName, msg, errno, ovs_strerror(errno)); + ovs_snprintf(errm, sizeof errm, "sfl_agent_sysError: %s: %s (errno = %d - %s)\n", modName, msg, errno, ovs_strerror(errno)); if(agent->errorFn) (*agent->errorFn)(agent->magic, agent, errm); else { fprintf(stderr, "%s\n", errm); diff --git a/lib/signals.c b/lib/signals.c index 27da5d6..bb82e7a 100644 --- a/lib/signals.c +++ b/lib/signals.c @@ -118,7 +118,7 @@ signal_name(int signum, char *namebuf, size_t bufsize) } #endif - snprintf(namebuf, bufsize, "signal %d", signum); + ovs_snprintf(namebuf, bufsize, "signal %d", signum); return namebuf; } diff --git a/lib/socket-util.c b/lib/socket-util.c index bb48ade..95bc70a 100644 --- a/lib/socket-util.c +++ b/lib/socket-util.c @@ -374,7 +374,7 @@ shorten_name_via_proc(const char *name, char short_name[MAX_UN_LEN + 1], free(dir); base = base_name(name); - len = snprintf(short_name, MAX_UN_LEN + 1, + len = ovs_snprintf(short_name, MAX_UN_LEN + 1, "/proc/self/fd/%d/%s", dirfd, base); free(base); @@ -415,7 +415,7 @@ shorten_name_via_symlink(const char *name, char short_name[MAX_UN_LEN + 1], for (i = 0; i < 1000; i++) { int len; - len = snprintf(linkname, MAX_UN_LEN + 1, + len = ovs_snprintf(linkname, MAX_UN_LEN + 1, "%s/ovs-un-c-%"PRIu32, tmpdir, random_uint32()); error = (len < 0 || len > MAX_UN_LEN ? ENAMETOOLONG : symlink(dir, linkname) ? errno @@ -430,7 +430,7 @@ shorten_name_via_symlink(const char *name, char short_name[MAX_UN_LEN + 1], fatal_signal_add_file_to_unlink(linkname); - len = snprintf(short_name, MAX_UN_LEN + 1, "%s/%s", linkname, base); + len = ovs_snprintf(short_name, MAX_UN_LEN + 1, "%s/%s", linkname, base); if (len < 0 || len > MAX_UN_LEN) { fatal_signal_unlink_file_now(linkname); error = ENAMETOOLONG; diff --git a/lib/stream-unix.c b/lib/stream-unix.c index e4b7e77..fd75083 100644 --- a/lib/stream-unix.c +++ b/lib/stream-unix.c @@ -113,7 +113,7 @@ punix_accept(int fd, const struct sockaddr *sa, size_t sa_len, char name[128]; if (name_len > 0) { - snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path); + ovs_snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path); } else { strcpy(name, "unix"); } diff --git a/lib/util.c b/lib/util.c index 984ab45..4c95cdb 100644 --- a/lib/util.c +++ b/lib/util.c @@ -155,11 +155,11 @@ xvasprintf(const char *format, va_list args) char *s; va_copy(args2, args); - needed = vsnprintf(NULL, 0, format, args); + needed = ovs_vsnprintf(NULL, 0, format, args); s = xmalloc(needed + 1); - vsnprintf(s, needed + 1, format, args2); + ovs_vsnprintf(s, needed + 1, format, args2); va_end(args2); return s; @@ -346,7 +346,7 @@ ovs_strerror(int error) * is too short). We don't check the actual failure reason because * POSIX requires strerror_r() to return the error but old glibc * (before 2.13) returns -1 and sets errno. */ - snprintf(buffer, BUFSIZE, "Unknown error %d", error); + ovs_snprintf(buffer, BUFSIZE, "Unknown error %d", error); } #endif @@ -502,6 +502,41 @@ ovs_hex_dump(FILE *stream, const void *buf_, size_t size, } } +int +ovs_vsnprintf(char * s, size_t n, const char * format, va_list arg) +{ +#ifndef _WIN32 + return vsnprintf(s, n ,format, arg); +#else + int needed = _vscprintf(format, arg); + if (needed < 0) { + return needed; + } + + if (s) { + (void) vsnprintf(s, n, format, arg); + if (needed >= n) { + s[n-1] = '\0'; + } + } else { + needed = -1; + errno = -EINVAL; + } + return needed; +#endif +} + +int +ovs_snprintf(char *s, size_t n, const char * format, ... ) +{ + int len = 0; + va_list arg; + va_start(arg, format); + len = ovs_vsnprintf(s, n, format, arg); + va_end(arg); + return len; +} + bool str_to_int(const char *s, int base, int *i) { diff --git a/lib/util.h b/lib/util.h index 8886a54..f1d8bb5 100644 --- a/lib/util.h +++ b/lib/util.h @@ -273,6 +273,8 @@ void ovs_error_valist(int err_no, const char *format, va_list) const char *ovs_retval_to_string(int); const char *ovs_strerror(int); void ovs_hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii); +int ovs_snprintf(char * s, size_t n, const char * format, ...); +int ovs_vsnprintf(char * s, size_t n, const char * format, va_list arg); bool str_to_int(const char *, int base, int *); bool str_to_long(const char *, int base, long *); diff --git a/lib/vconn.c b/lib/vconn.c index f0549d5..934d6b8 100644 --- a/lib/vconn.c +++ b/lib/vconn.c @@ -512,7 +512,7 @@ vcs_send_error(struct vconn *vconn) local_s = version_bitmap_to_string(vconn->allowed_versions); peer_s = version_bitmap_to_string(vconn->peer_versions); - snprintf(s, sizeof s, "We support %s, you support %s, no common versions.", + ovs_snprintf(s, sizeof s, "We support %s, you support %s, no common versions.", local_s, peer_s); free(peer_s); free(local_s); diff --git a/lib/vlandev.c b/lib/vlandev.c index b793f77..8e9c799 100644 --- a/lib/vlandev.c +++ b/lib/vlandev.c @@ -315,7 +315,7 @@ vlandev_dummy_add(const char *real_dev, int vid) { char name[IFNAMSIZ]; - if (snprintf(name, sizeof name, "%s.%d", real_dev, vid) >= sizeof name) { + if (ovs_snprintf(name, sizeof name, "%s.%d", real_dev, vid) >= sizeof name) { return ENAMETOOLONG; } return vlandev_add__(name, real_dev, vid); diff --git a/ovsdb/file.c b/ovsdb/file.c index 7c8ac6f..6af9d18 100644 --- a/ovsdb/file.c +++ b/ovsdb/file.c @@ -750,7 +750,7 @@ ovsdb_file_txn_add_row(struct ovsdb_file_txn *ftxn, } /* Add row to transaction for this table. */ - snprintf(uuid, sizeof uuid, + ovs_snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old))); json_object_put(ftxn->table_json, uuid, row); } diff --git a/ovsdb/jsonrpc-server.c b/ovsdb/jsonrpc-server.c index 4758442..aae8a21 100644 --- a/ovsdb/jsonrpc-server.c +++ b/ovsdb/jsonrpc-server.c @@ -1511,7 +1511,7 @@ ovsdb_jsonrpc_monitor_change_cb(const struct ovsdb_row *old, } /* Add JSON row to JSON table. */ - snprintf(uuid, sizeof uuid, + ovs_snprintf(uuid, sizeof uuid, UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old))); json_object_put(aux->table_json, uuid, row_json); diff --git a/ovsdb/log.c b/ovsdb/log.c index 807b983..b941522 100644 --- a/ovsdb/log.c +++ b/ovsdb/log.c @@ -356,7 +356,7 @@ ovsdb_log_write(struct ovsdb_log *file, struct json *json) /* Compose header. */ sha1_bytes(json_string, length, sha1); - snprintf(header, sizeof header, "%s%"PRIuSIZE" "SHA1_FMT"\n", + ovs_snprintf(header, sizeof header, "%s%"PRIuSIZE" "SHA1_FMT"\n", magic, length, SHA1_ARGS(sha1)); /* Write. */ diff --git a/tests/test-sflow.c b/tests/test-sflow.c index cba01b9..1e89158 100644 --- a/tests/test-sflow.c +++ b/tests/test-sflow.c @@ -325,14 +325,14 @@ process_datagram(struct sflow_xdr *x) /* Store the agent address as a string. */ if (x->agentAddr.type == SFLOW_ADDRTYPE_IP6) { - snprintf(x->agentIPStr, SFLOW_MAX_AGENTIP_STRLEN, + ovs_snprintf(x->agentIPStr, SFLOW_MAX_AGENTIP_STRLEN, "%04x:%04x:%04x:%04x", x->agentAddr.a.ip6[0], x->agentAddr.a.ip6[1], x->agentAddr.a.ip6[2], x->agentAddr.a.ip6[3]); } else { - snprintf(x->agentIPStr, SFLOW_MAX_AGENTIP_STRLEN, + ovs_snprintf(x->agentIPStr, SFLOW_MAX_AGENTIP_STRLEN, IP_FMT, IP_ARGS(x->agentAddr.a.ip4)); } diff --git a/tests/test-stp.c b/tests/test-stp.c index be1b395..e9e9688 100644 --- a/tests/test-stp.c +++ b/tests/test-stp.c @@ -118,7 +118,7 @@ new_bridge(struct test_case *tc, int id) char name[16]; b->tc = tc; b->id = id; - snprintf(name, sizeof name, "stp%x", id); + ovs_snprintf(name, sizeof name, "stp%x", id); b->stp = stp_create(name, id, send_bpdu, b); assert(tc->n_bridges < ARRAY_SIZE(tc->bridges)); b->n_ports = 0; -- 1.7.9.5 _______________________________________________ dev mailing list dev@openvswitch.org http://openvswitch.org/mailman/listinfo/dev