[U-Boot] [PATCH] lib/string: added strndup

This patch adds optional support for strndup.
Signed-off-by: Grant Erickson marathon96@gmail.com --- include/linux/string.h | 3 +++ lib/string.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/include/linux/string.h b/include/linux/string.h index 6239039..5668290 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -59,6 +59,9 @@ extern __kernel_size_t strnlen(const char *,__kernel_size_t); #ifndef __HAVE_ARCH_STRDUP extern char * strdup(const char *); #endif +#ifndef __HAVE_ARCH_STRNDUP +extern char * strndup(const char *,__kernel_size_t); +#endif #ifndef __HAVE_ARCH_STRSWAB extern char * strswab(const char *); #endif diff --git a/lib/string.c b/lib/string.c index 2c4f0ec..5fb6693 100644 --- a/lib/string.c +++ b/lib/string.c @@ -260,6 +260,27 @@ char * strdup(const char *s) } #endif
+#ifndef __HAVE_ARCH_STRNDUP +char * strndup(const char *s, size_t n) +{ + size_t len; + char *new; + + len = strnlen(s, n); + + if ((s == NULL) || + ((new = malloc (len + 1)) == NULL) ) { + return NULL; + } + + strncpy (new, s, len); + + new[len] = '\0'; + + return new; +} +#endif + #ifndef __HAVE_ARCH_STRSPN /** * strspn - Calculate the length of the initial substring of @s which only

Dear Grant Erickson,
In message 1324577816-26198-1-git-send-email-marathon96@gmail.com you wrote:
This patch adds optional support for strndup.
Signed-off-by: Grant Erickson marathon96@gmail.com
include/linux/string.h | 3 +++ lib/string.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 0 deletions(-)
What is this good for? There are no users for this function (neither in mainline nor in any of the patches you have submitted).
Best regards,
Wolfgang Denk

On Dec 22, 2011, at 1:08 PM, Wolfgang Denk wrote:
In message 1324577816-26198-1-git-send-email-marathon96@gmail.com you wrote:
This patch adds optional support for strndup.
Signed-off-by: Grant Erickson marathon96@gmail.com
include/linux/string.h | 3 +++ lib/string.c | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 0 deletions(-)
What is this good for? There are no users for this function (neither in mainline nor in any of the patches you have submitted).
Wolfgang:
I have a new board addition coming along shortly that leverages this.
Best,
Grant Erickson

On Thursday 22 December 2011 13:16:56 Grant Erickson wrote:
+#ifndef __HAVE_ARCH_STRNDUP +extern char * strndup(const char *,__kernel_size_t); +#endif
no space after that first "*", and add a space after the ","
since your definition uses "size_t", then use that rather than __kernel_size_t
--- a/lib/string.c +++ b/lib/string.c
+#ifndef __HAVE_ARCH_STRNDUP +char * strndup(const char *s, size_t n)
no space after that first "*"
- if ((s == NULL) ||
((new = malloc (len + 1)) == NULL) ) {
return NULL;
- }
please split this up such as: if (s == NULL) return s;
new = malloc(len + 1); if (new == NULL) return new;
- strncpy (new, s, len);
no space before that "(" -mike

Dear Mike Frysinger,
In message 201201051821.35774.vapier@gentoo.org you wrote:
- if ((s == NULL) ||
((new = malloc (len + 1)) == NULL) ) {
return NULL;
- }
please split this up such as:
I'm OK with the splitting, but...
if (s == NULL) return s;
new = malloc(len + 1); if (new == NULL) return new;
... in both cases, a "return NULL" is much easier to parse for the human and identical for the compiler, so that should be used.
Best regards,
Wolfgang Denk

On Thursday 05 January 2012 18:25:06 Wolfgang Denk wrote:
Mike Frysinger wrote:
- if ((s == NULL) ||
((new = malloc (len + 1)) == NULL) ) {
return NULL;
- }
please split this up such as:
I'm OK with the splitting, but...
if (s == NULL) return s;
new = malloc(len + 1); if (new == NULL) return new;
... in both cases, a "return NULL" is much easier to parse for the human and identical for the compiler, so that should be used.
sure, np -mike
participants (3)
-
Grant Erickson
-
Mike Frysinger
-
Wolfgang Denk