portability: take over strlcpy

cute-signatures
Ariadne Conill 2021-12-14 14:09:15 -06:00 committed by Timo Teräs
parent 03a5e6d9b9
commit 2c3cef8787
5 changed files with 18 additions and 17 deletions

View File

@ -6,6 +6,7 @@ libportability_src = []
check_functions = [ check_functions = [
['memrchr', 'memrchr.c', 'NEED_MEMRCHR', 'string.h'], ['memrchr', 'memrchr.c', 'NEED_MEMRCHR', 'string.h'],
['strlcpy', 'strlcpy.c', 'NEED_STRLCPY', 'string.h'],
] ]

View File

@ -3,3 +3,7 @@
#ifdef NEED_MEMRCHR #ifdef NEED_MEMRCHR
extern void *memrchr(const void *m, int c, size_t n); extern void *memrchr(const void *m, int c, size_t n);
#endif #endif
#ifdef NEED_STRLCPY
size_t strlcpy(char *dst, const char *src, size_t size);
#endif

13
portability/strlcpy.c Normal file
View File

@ -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;
}

View File

@ -127,8 +127,4 @@ void apk_blob_pull_base64(apk_blob_t *b, apk_blob_t to);
void apk_blob_pull_hexdump(apk_blob_t *b, apk_blob_t to); void apk_blob_pull_hexdump(apk_blob_t *b, apk_blob_t to);
int apk_blob_pull_blob_match(apk_blob_t *b, apk_blob_t match); int apk_blob_pull_blob_match(apk_blob_t *b, apk_blob_t match);
#if defined(__GLIBC__) && !defined(__UCLIBC__)
extern size_t strlcpy(char *dest, const char *src, size_t size);
#endif
#endif #endif

View File

@ -676,16 +676,3 @@ void apk_blob_pull_base64(apk_blob_t *b, apk_blob_t to)
err: err:
*b = APK_BLOB_NULL; *b = APK_BLOB_NULL;
} }
#if defined(__GLIBC__) && !defined(__UCLIBC__)
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;
}
#endif