You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
926 B
43 lines
926 B
|
16 years ago
|
Index: gl/lib/xstrndup.c
|
||
|
|
--- gl/lib/xstrndup.c.orig 2009-11-02 20:09:57 +0100
|
||
|
|
+++ gl/lib/xstrndup.c 2009-11-27 22:49:55 +0100
|
||
|
|
@@ -23,13 +23,37 @@
|
||
|
|
#include <string.h>
|
||
|
|
#include "xalloc.h"
|
||
|
|
|
||
|
|
+static size_t
|
||
|
|
+my_strnlen(const char *s, size_t maxlen)
|
||
|
|
+{
|
||
|
|
+ size_t len;
|
||
|
|
+ for (len = 0; len < maxlen; len++, s++) {
|
||
|
|
+ if (!*s)
|
||
|
|
+ break;
|
||
|
|
+ }
|
||
|
|
+ return (len);
|
||
|
|
+}
|
||
|
|
+
|
||
|
|
+static char *
|
||
|
|
+my_strndup (char const *s, size_t n)
|
||
|
|
+{
|
||
|
|
+ size_t len = my_strnlen (s, n);
|
||
|
|
+ char *new = malloc (len + 1);
|
||
|
|
+
|
||
|
|
+ if (new == NULL)
|
||
|
|
+ return NULL;
|
||
|
|
+
|
||
|
|
+ new[len] = '\0';
|
||
|
|
+ return memcpy (new, s, len);
|
||
|
|
+}
|
||
|
|
+
|
||
|
|
/* Return a newly allocated copy of at most N bytes of STRING.
|
||
|
|
In other words, return a copy of the initial segment of length N of
|
||
|
|
STRING. */
|
||
|
|
char *
|
||
|
|
xstrndup (const char *string, size_t n)
|
||
|
|
{
|
||
|
|
- char *s = strndup (string, n);
|
||
|
|
+ char *s = my_strndup (string, n);
|
||
|
|
if (! s)
|
||
|
|
xalloc_die ();
|
||
|
|
return s;
|