Message ID | 20240209093619.2553-1-dkirjanov@suse.de (mailing list archive) |
---|---|
State | Rejected |
Delegated to: | Stephen Hemminger |
Headers | show |
Series | [iproute2,1/2] lib: utils: introduce scnprintf | expand |
Context | Check | Description |
---|---|---|
netdev/tree_selection | success | Not a local patch |
On Fri, 9 Feb 2024 04:36:18 -0500 Denis Kirjanov <kirjanov@gmail.com> wrote: > The function is similar to the standard snprintf but > returns the number of characters actually written to @buf > argument excluding the trailing '\0' > > Signed-off-by: Denis Kirjanov <dkirjanov@suse.de> > --- I don't understand, why not use snprintf in ifstat? None of the cases in patch 2 care about the return value length.
On 2/9/24 19:33, Stephen Hemminger wrote: > On Fri, 9 Feb 2024 04:36:18 -0500 > Denis Kirjanov <kirjanov@gmail.com> wrote: > >> The function is similar to the standard snprintf but >> returns the number of characters actually written to @buf >> argument excluding the trailing '\0' >> >> Signed-off-by: Denis Kirjanov <dkirjanov@suse.de> >> --- > > I don't understand, why not use snprintf in ifstat? > None of the cases in patch 2 care about the return value length. Hi Stephen, My intention is just use one safe version of the string formatting function. I'm going to convert other places as well.
diff --git a/include/utils.h b/include/utils.h index 9ba129b8..5b65edc5 100644 --- a/include/utils.h +++ b/include/utils.h @@ -393,4 +393,5 @@ int proto_a2n(unsigned short *id, const char *buf, const char *proto_n2a(unsigned short id, char *buf, int len, const struct proto *proto_tb, size_t tb_len); +int scnprintf(char * buf, size_t size, const char * fmt, ...); #endif /* __UTILS_H__ */ diff --git a/lib/utils.c b/lib/utils.c index 6c1c1a8d..752da66f 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -7,6 +7,7 @@ #include <stdio.h> #include <stdlib.h> +#include <stdarg.h> #include <math.h> #include <unistd.h> #include <fcntl.h> @@ -2003,3 +2004,16 @@ int proto_a2n(unsigned short *id, const char *buf, return 0; } + +int scnprintf(char * buf, size_t size, const char * fmt, ...) +{ + ssize_t ssize = size; + va_list args; + int i; + + va_start(args, fmt); + i = vsnprintf(buf, size, fmt, args); + va_end(args); + + return (i >= ssize) ? (ssize - 1) : i; +}
The function is similar to the standard snprintf but returns the number of characters actually written to @buf argument excluding the trailing '\0' Signed-off-by: Denis Kirjanov <dkirjanov@suse.de> --- include/utils.h | 1 + lib/utils.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+)