Thomas Heijligen has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/70115 )
Change subject: libpayload/string: add strndup() function ......................................................................
libpayload/string: add strndup() function
Change-Id: Ie509e49f21fb537692704ac6527efa09649164e3 Signed-off-by: Thomas Heijligen src@posteo.de --- M payloads/libpayload/include/string.h M payloads/libpayload/libc/string.c 2 files changed, 32 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/15/70115/1
diff --git a/payloads/libpayload/include/string.h b/payloads/libpayload/include/string.h index 393881d..e87dea5 100644 --- a/payloads/libpayload/include/string.h +++ b/payloads/libpayload/include/string.h @@ -59,6 +59,7 @@ char *strchr(const char *s, int c); char *strrchr(const char *s, int c); char *strdup(const char *s); +char *strndup(const char *s, size_t size); char *strstr(const char *h, const char *n); char *strsep(char **stringp, const char *delim); size_t strspn(const char *s, const char *a); diff --git a/payloads/libpayload/libc/string.c b/payloads/libpayload/libc/string.c index 46c3c01..d1594f7 100644 --- a/payloads/libpayload/libc/string.c +++ b/payloads/libpayload/libc/string.c @@ -322,6 +322,27 @@ }
/** + * Duplicate a string with a max length of size + * + * @param s The string to duplicate. + * @param size The max length of the string + * @return A pointer to the copy of the original string. + */ +char *strndup(const char *s, size_t size) +{ + size_t n = strlen(s); + n = n < size ? n : size; + char *p = malloc(n + 1); + + if (p != NULL) { + strncpy(p, s, n); + p[n] = 0; + } + return p; +} + + +/** * Find a substring within a string. * * @param h The haystack string.