utils: Implement strlcpy() and strlcat()
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>
This commit is contained in:
parent
50f81afd4d
commit
8d15e012a3
|
|
@ -251,4 +251,7 @@ int make_path(const char *path, mode_t mode);
|
||||||
char *find_cgroup2_mount(void);
|
char *find_cgroup2_mount(void);
|
||||||
int get_command_name(const char *pid, char *comm, size_t len);
|
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__ */
|
#endif /* __UTILS_H__ */
|
||||||
|
|
|
||||||
19
lib/utils.c
19
lib/utils.c
|
|
@ -1230,3 +1230,22 @@ int get_real_family(int rtm_type, int rtm_family)
|
||||||
|
|
||||||
return 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);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue