mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] string.h: Implement strndup
@ 2017-12-20 10:36 Uwe Kleine-König
  2018-01-05 14:48 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Uwe Kleine-König @ 2017-12-20 10:36 UTC (permalink / raw)
  To: barebox

I implemented this while trying to import mmc-utils into barebox. While
I didn't finish this import this function might still be useful for
someone?!

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 include/linux/string.h |  3 +++
 lib/string.c           | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 5df8c50e579a..ed4eeb5519e6 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -80,6 +80,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 *, size_t);
+#endif
 #ifndef __HAVE_ARCH_STRSWAB
 extern char * strswab(const char *);
 #endif
diff --git a/lib/string.c b/lib/string.c
index 1d491c9c2fb5..f588933e8191 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -323,6 +323,26 @@ char * strdup(const char *s)
 #endif
 EXPORT_SYMBOL(strdup);
 
+#ifndef __HAVE_ARCH_STRNDUP
+char *strndup(const char *s, size_t n)
+{
+	char *new;
+	size_t len = strnlen(s, n);
+
+	if ((s == NULL) ||
+	    ((new = malloc(len + 1)) == NULL)) {
+		return NULL;
+	}
+
+	memcpy(new, s, len);
+	new[len] = '\0';
+
+	return new;
+}
+
+#endif
+EXPORT_SYMBOL(strndup);
+
 #ifndef __HAVE_ARCH_STRSPN
 /**
  * strspn - Calculate the length of the initial substring of @s which only
-- 
2.11.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] string.h: Implement strndup
  2017-12-20 10:36 [PATCH] string.h: Implement strndup Uwe Kleine-König
@ 2018-01-05 14:48 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2018-01-05 14:48 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox

On Wed, Dec 20, 2017 at 11:36:44AM +0100, Uwe Kleine-König wrote:
> I implemented this while trying to import mmc-utils into barebox. While
> I didn't finish this import this function might still be useful for
> someone?!
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

I find it useful, so:

Applied, thanks

Sascha

> ---
>  include/linux/string.h |  3 +++
>  lib/string.c           | 20 ++++++++++++++++++++
>  2 files changed, 23 insertions(+)
> 
> diff --git a/include/linux/string.h b/include/linux/string.h
> index 5df8c50e579a..ed4eeb5519e6 100644
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -80,6 +80,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 *, size_t);
> +#endif
>  #ifndef __HAVE_ARCH_STRSWAB
>  extern char * strswab(const char *);
>  #endif
> diff --git a/lib/string.c b/lib/string.c
> index 1d491c9c2fb5..f588933e8191 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -323,6 +323,26 @@ char * strdup(const char *s)
>  #endif
>  EXPORT_SYMBOL(strdup);
>  
> +#ifndef __HAVE_ARCH_STRNDUP
> +char *strndup(const char *s, size_t n)
> +{
> +	char *new;
> +	size_t len = strnlen(s, n);
> +
> +	if ((s == NULL) ||
> +	    ((new = malloc(len + 1)) == NULL)) {
> +		return NULL;
> +	}
> +
> +	memcpy(new, s, len);
> +	new[len] = '\0';
> +
> +	return new;
> +}
> +
> +#endif
> +EXPORT_SYMBOL(strndup);
> +
>  #ifndef __HAVE_ARCH_STRSPN
>  /**
>   * strspn - Calculate the length of the initial substring of @s which only
> -- 
> 2.11.0
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-01-05 14:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-20 10:36 [PATCH] string.h: Implement strndup Uwe Kleine-König
2018-01-05 14:48 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox