|
|
99be8f |
From 3bcdea42e7402e79a914fe3cbefdcc1caa89464c Mon Sep 17 00:00:00 2001
|
|
|
99be8f |
From: Andrea Claudi <aclaudi@redhat.com>
|
|
|
99be8f |
Date: Mon, 29 Apr 2019 20:08:08 +0200
|
|
|
99be8f |
Subject: [PATCH] utils: Implement strlcpy() and strlcat()
|
|
|
99be8f |
|
|
|
99be8f |
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1465646
|
|
|
99be8f |
Upstream Status: iproute2.git commit 8d15e012a3227
|
|
|
99be8f |
|
|
|
99be8f |
commit 8d15e012a3227d79295cd95582bb6d8a6f0bdc92
|
|
|
99be8f |
Author: Phil Sutter <phil@nwl.cc>
|
|
|
99be8f |
Date: Fri Sep 1 18:52:51 2017 +0200
|
|
|
99be8f |
|
|
|
99be8f |
utils: Implement strlcpy() and strlcat()
|
|
|
99be8f |
|
|
|
99be8f |
By making use of strncpy(), both implementations are really simple so
|
|
|
99be8f |
there is no need to add libbsd as additional dependency.
|
|
|
99be8f |
|
|
|
99be8f |
Signed-off-by: Phil Sutter <phil@nwl.cc>
|
|
|
99be8f |
---
|
|
|
99be8f |
include/utils.h | 3 +++
|
|
|
99be8f |
lib/utils.c | 19 +++++++++++++++++++
|
|
|
99be8f |
2 files changed, 22 insertions(+)
|
|
|
99be8f |
|
|
|
99be8f |
diff --git a/include/utils.h b/include/utils.h
|
|
|
99be8f |
index d707a9dacdb85..d596a6fc10574 100644
|
|
|
99be8f |
--- a/include/utils.h
|
|
|
99be8f |
+++ b/include/utils.h
|
|
|
99be8f |
@@ -264,4 +264,7 @@ int make_path(const char *path, mode_t mode);
|
|
|
99be8f |
char *find_cgroup2_mount(void);
|
|
|
99be8f |
int get_command_name(const char *pid, char *comm, size_t len);
|
|
|
99be8f |
|
|
|
99be8f |
+size_t strlcpy(char *dst, const char *src, size_t size);
|
|
|
99be8f |
+size_t strlcat(char *dst, const char *src, size_t size);
|
|
|
99be8f |
+
|
|
|
99be8f |
#endif /* __UTILS_H__ */
|
|
|
99be8f |
diff --git a/lib/utils.c b/lib/utils.c
|
|
|
99be8f |
index fc9c575ba0c7d..c9ba2f332c2a7 100644
|
|
|
99be8f |
--- a/lib/utils.c
|
|
|
99be8f |
+++ b/lib/utils.c
|
|
|
99be8f |
@@ -1228,3 +1228,22 @@ int get_real_family(int rtm_type, int rtm_family)
|
|
|
99be8f |
|
|
|
99be8f |
return rtm_family;
|
|
|
99be8f |
}
|
|
|
99be8f |
+
|
|
|
99be8f |
+size_t strlcpy(char *dst, const char *src, size_t size)
|
|
|
99be8f |
+{
|
|
|
99be8f |
+ if (size) {
|
|
|
99be8f |
+ strncpy(dst, src, size - 1);
|
|
|
99be8f |
+ dst[size - 1] = '\0';
|
|
|
99be8f |
+ }
|
|
|
99be8f |
+ return strlen(src);
|
|
|
99be8f |
+}
|
|
|
99be8f |
+
|
|
|
99be8f |
+size_t strlcat(char *dst, const char *src, size_t size)
|
|
|
99be8f |
+{
|
|
|
99be8f |
+ size_t dlen = strlen(dst);
|
|
|
99be8f |
+
|
|
|
99be8f |
+ if (dlen > size)
|
|
|
99be8f |
+ return dlen + strlen(src);
|
|
|
99be8f |
+
|
|
|
99be8f |
+ return dlen + strlcpy(dst + dlen, src, size - dlen);
|
|
|
99be8f |
+}
|
|
|
99be8f |
--
|
|
|
d30c09 |
2.21.0
|
|
|
99be8f |
|