Skip to contentSkip to conversation

lib/string: added strndup

PATCH New Grant Erickson 3 participants 6 messages Dec 22, 2011 – Jan 6, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/string: added strndup

This patch adds optional support for strndup.

---
 include/linux/string.h |    3 +++
 lib/string.c           |   21 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)
include/linux/string.h +3 -0
index 6239039..5668290 100644
@@ -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
lib/string.c +21 -0
index 2c4f0ec..5fb6693 100644
@@ -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
Signature · 1 line
1.7.7.3
#1 lore raw

PATCH 1/1 lib/string: added strndup

Dear Grant Erickson,

Quoted 7 lines · In message <[email protected]> you wrote:

This patch adds optional support for strndup.

---
 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

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
The complexity of software is an essential property, not an  acciden-
tal  one. Hence, descriptions of a software entity that abstract away
its complexity often abstract away its essence.    - Fred Brooks, Jr.
#2 lore raw

PATCH 1/1 lib/string: added strndup

Quoted 11 lines · Wolfgang Denk
Quoted 7 lines · In message <[email protected]> you wrote:

This patch adds optional support for strndup.

---
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

#3 lore raw

PATCH 1/1 lib/string: added strndup

In reply to Grant Erickson

include/linux/string.h extern __kernel_size_t strnlen(const char *,__kernel_size_t); · line 64

Quoted 3 lines · On Thursday 22 December 2011 13:16:56 Grant Erickson wrote:
include/linux/string.h +3 -0
+#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

lib/string.c char * strdup(const char *s) · line 264

Quoted 5 lines
lib/string.c +2 -0
 
+#ifndef __HAVE_ARCH_STRNDUP
+char * strndup(const char *s, size_t n)

no space after that first "*"

lib/string.c char * strdup(const char *s) · line 274

Quoted 4 lines
lib/string.c +4 -0
+	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;

lib/string.c char * strdup(const char *s) · line 276

Quoted 1 line
lib/string.c +1 -0
+	strncpy (new, s, len);

no space before that "("
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120105/4aee4e34/attachment.pgp>

#4 lore raw

PATCH 1/1 lib/string: added strndup

Dear Mike Frysinger,

Quoted 7 lines · In message <[email protected]> you wrote:

lib/string.c char * strdup(const char *s) · line 274

Quoted 5 lines
lib/string.c +4 -0
+	if ((s == NULL) ||
+		((new = malloc (len + 1)) == NULL) ) {
+		return NULL;
+	}

please split this up such as:

I'm OK with the splitting, but...

Quoted 6 lines
	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

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Good morning. This is the telephone company. Due  to  repairs,  we're
giving  you  advance notice that your service will be cut off indefi-
nitely at ten o'clock. That's two minutes from now.
#5 lore raw

PATCH 1/1 lib/string: added strndup

Quoted 13 lines · On Thursday 05 January 2012 18:25:06 Wolfgang Denk wrote:
Quoted 6 lines · Mike Frysinger

lib/string.c char * strdup(const char *s) · line 274

Quoted 4 lines
lib/string.c +4 -0
+	if ((s == NULL) ||
+		((new = malloc (len + 1)) == NULL) ) {
+		return NULL;
+	}

please split this up such as:

I'm OK with the splitting, but...

Quoted 3 lines
	if (s == NULL)
		return s;
Quoted 6 lines
Quoted 3 lines
	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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120106/369bc232/attachment.pgp>