]> granicus.if.org Git - yasm/commitdiff
Add xstrndup() to easily create a 0-terminated duplicate of a
authorPeter Johnson <peter@tortall.net>
Thu, 20 Jun 2002 05:15:59 +0000 (05:15 -0000)
committerPeter Johnson <peter@tortall.net>
Thu, 20 Jun 2002 05:15:59 +0000 (05:15 -0000)
non-zero-terminated source string.

svn path=/trunk/yasm/; revision=649

libyasm/util.h
libyasm/xstrdup.c
src/util.h
src/xstrdup.c
util.h

index a4fc61e74fcb2bd5e4e8f97299033a49fddf88b7..05d8d7828eff667291b98059eb8fbacb202b4df9 100644 (file)
@@ -126,6 +126,7 @@ int strncasecmp(const char *s1, const char *s2, size_t n);
 #else
 /* strdup() implementation with error checking (using xmalloc). */
 /*@only@*/ char *xstrdup(const char *str);
+/*@only@*/ char *xstrndup(const char *str, size_t len);
 
 /* Error-checking memory allocation routines in xmalloc.c. */
 /*@only@*/ /*@out@*/ void *xmalloc(size_t size);
index 77cccba03306d2a74be9452876873dfe30e18980..42685ea1e4e8007b4346c1765e491816a41febc6 100644 (file)
@@ -61,3 +61,14 @@ xstrdup(const char *str)
        return (copy);
 }
 #endif
+
+char *
+xstrndup(const char *str, size_t len)
+{
+       char *copy;
+
+       copy = xmalloc(len+1);
+       memcpy(copy, str, len);
+       copy[len] = '\0';
+       return (copy);
+}
index a4fc61e74fcb2bd5e4e8f97299033a49fddf88b7..05d8d7828eff667291b98059eb8fbacb202b4df9 100644 (file)
@@ -126,6 +126,7 @@ int strncasecmp(const char *s1, const char *s2, size_t n);
 #else
 /* strdup() implementation with error checking (using xmalloc). */
 /*@only@*/ char *xstrdup(const char *str);
+/*@only@*/ char *xstrndup(const char *str, size_t len);
 
 /* Error-checking memory allocation routines in xmalloc.c. */
 /*@only@*/ /*@out@*/ void *xmalloc(size_t size);
index 77cccba03306d2a74be9452876873dfe30e18980..42685ea1e4e8007b4346c1765e491816a41febc6 100644 (file)
@@ -61,3 +61,14 @@ xstrdup(const char *str)
        return (copy);
 }
 #endif
+
+char *
+xstrndup(const char *str, size_t len)
+{
+       char *copy;
+
+       copy = xmalloc(len+1);
+       memcpy(copy, str, len);
+       copy[len] = '\0';
+       return (copy);
+}
diff --git a/util.h b/util.h
index a4fc61e74fcb2bd5e4e8f97299033a49fddf88b7..05d8d7828eff667291b98059eb8fbacb202b4df9 100644 (file)
--- a/util.h
+++ b/util.h
@@ -126,6 +126,7 @@ int strncasecmp(const char *s1, const char *s2, size_t n);
 #else
 /* strdup() implementation with error checking (using xmalloc). */
 /*@only@*/ char *xstrdup(const char *str);
+/*@only@*/ char *xstrndup(const char *str, size_t len);
 
 /* Error-checking memory allocation routines in xmalloc.c. */
 /*@only@*/ /*@out@*/ void *xmalloc(size_t size);