netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH iproute2 1/2] lib: utils: introduce scnprintf
@ 2024-02-09  9:36 Denis Kirjanov
  2024-02-09  9:36 ` [PATCH iproute2 2/2] ifstat: convert string formatting calls to scnprintf Denis Kirjanov
  2024-02-09 16:33 ` [PATCH iproute2 1/2] lib: utils: introduce scnprintf Stephen Hemminger
  0 siblings, 2 replies; 4+ messages in thread
From: Denis Kirjanov @ 2024-02-09  9:36 UTC (permalink / raw)
  To: stephen, dsahern; +Cc: netdev, Denis Kirjanov

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(+)

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;
+}
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-02-10  8:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-09  9:36 [PATCH iproute2 1/2] lib: utils: introduce scnprintf Denis Kirjanov
2024-02-09  9:36 ` [PATCH iproute2 2/2] ifstat: convert string formatting calls to scnprintf Denis Kirjanov
2024-02-09 16:33 ` [PATCH iproute2 1/2] lib: utils: introduce scnprintf Stephen Hemminger
2024-02-10  8:37   ` Denis Kirjanov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).