mirror of
https://source.denx.de/u-boot/u-boot.git
synced 2026-06-13 15:03:58 +03:00
lib/string.c: implement strdup() and strndup() in terms of memdup_nul()
With the addition of memdup_nul(), strdup() and strndup() can be implemented as one-liners. While not required by POSIX or C, do keep the behaviour of gracefully accepting a NULL source and simply return NULL. Reviewed-by: Simon Glass <sjg@chromium.org> Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
This commit is contained in:
committed by
Tom Rini
parent
8c664d2135
commit
ee8be5d4a1
30
lib/string.c
30
lib/string.c
@@ -360,38 +360,12 @@ void *memdup_nul(const void *src, size_t len)
|
||||
|
||||
char * strdup(const char *s)
|
||||
{
|
||||
char *new;
|
||||
|
||||
if ((s == NULL) ||
|
||||
((new = malloc (strlen(s) + 1)) == NULL) ) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy (new, s);
|
||||
return new;
|
||||
return s ? memdup_nul(s, strlen(s)) : NULL;
|
||||
}
|
||||
|
||||
char * strndup(const char *s, size_t n)
|
||||
{
|
||||
size_t len;
|
||||
char *new;
|
||||
|
||||
if (s == NULL)
|
||||
return NULL;
|
||||
|
||||
len = strlen(s);
|
||||
|
||||
if (n < len)
|
||||
len = n;
|
||||
|
||||
new = malloc(len + 1);
|
||||
if (new == NULL)
|
||||
return NULL;
|
||||
|
||||
strncpy(new, s, len);
|
||||
new[len] = '\0';
|
||||
|
||||
return new;
|
||||
return s ? memdup_nul(s, strnlen(s, n)) : NULL;
|
||||
}
|
||||
|
||||
#ifndef __HAVE_ARCH_STRSPN
|
||||
|
||||
Reference in New Issue
Block a user