]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
utils: Implement strlcpy() and strlcat()
authorPhil Sutter <phil@nwl.cc>
Fri, 1 Sep 2017 16:52:51 +0000 (18:52 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Fri, 1 Sep 2017 19:10:54 +0000 (12:10 -0700)
By making use of strncpy(), both implementations are really simple so
there is no need to add libbsd as additional dependency.

Signed-off-by: Phil Sutter <phil@nwl.cc>
include/utils.h
lib/utils.c

index 7a3b3fd24058f8546ac76f611be0599d38677edd..f161588b0be2f789f873c415c5f1f1de6f58b4d5 100644 (file)
@@ -251,4 +251,7 @@ int make_path(const char *path, mode_t mode);
 char *find_cgroup2_mount(void);
 int get_command_name(const char *pid, char *comm, size_t len);
 
+size_t strlcpy(char *dst, const char *src, size_t size);
+size_t strlcat(char *dst, const char *src, size_t size);
+
 #endif /* __UTILS_H__ */
index 9143ed22848707ac628ef44472a42410c6786b12..330ab073c2068266706de901969ff9bb20fc97ce 100644 (file)
@@ -1230,3 +1230,22 @@ int get_real_family(int rtm_type, int rtm_family)
 
        return rtm_family;
 }
+
+size_t strlcpy(char *dst, const char *src, size_t size)
+{
+       if (size) {
+               strncpy(dst, src, size - 1);
+               dst[size - 1] = '\0';
+       }
+       return strlen(src);
+}
+
+size_t strlcat(char *dst, const char *src, size_t size)
+{
+       size_t dlen = strlen(dst);
+
+       if (dlen > size)
+               return dlen + strlen(src);
+
+       return dlen + strlcpy(dst + dlen, src, size - dlen);
+}