diff options
Diffstat (limited to 'portability')
-rw-r--r-- | portability/meson.build | 1 | ||||
-rw-r--r-- | portability/string.h | 4 | ||||
-rw-r--r-- | portability/strlcpy.c | 13 |
3 files changed, 18 insertions, 0 deletions
diff --git a/portability/meson.build b/portability/meson.build index 51e4aee..3a680fe 100644 --- a/portability/meson.build +++ b/portability/meson.build @@ -6,6 +6,7 @@ libportability_src = [] check_functions = [ ['memrchr', 'memrchr.c', 'NEED_MEMRCHR', 'string.h'], + ['strlcpy', 'strlcpy.c', 'NEED_STRLCPY', 'string.h'], ] diff --git a/portability/string.h b/portability/string.h index 0b8bb71..688d75b 100644 --- a/portability/string.h +++ b/portability/string.h @@ -3,3 +3,7 @@ #ifdef NEED_MEMRCHR extern void *memrchr(const void *m, int c, size_t n); #endif + +#ifdef NEED_STRLCPY +size_t strlcpy(char *dst, const char *src, size_t size); +#endif diff --git a/portability/strlcpy.c b/portability/strlcpy.c new file mode 100644 index 0000000..6ce46e3 --- /dev/null +++ b/portability/strlcpy.c @@ -0,0 +1,13 @@ +#include <stddef.h> +#include <string.h> + +size_t strlcpy(char *dst, const char *src, size_t size) +{ + size_t ret = strlen(src), len; + if (!size) return ret; + len = ret; + if (len >= size) len = size - 1; + memcpy(dst, src, len); + dst[len] = 0; + return ret; +} |