From: Peter Johnson Date: Thu, 20 Jun 2002 05:15:59 +0000 (-0000) Subject: Add xstrndup() to easily create a 0-terminated duplicate of a X-Git-Tag: v0.2.0~201 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f78505a6a5487e9e18a849d4d5ba7a1206e6203c;p=yasm Add xstrndup() to easily create a 0-terminated duplicate of a non-zero-terminated source string. svn path=/trunk/yasm/; revision=649 --- diff --git a/libyasm/util.h b/libyasm/util.h index a4fc61e7..05d8d782 100644 --- a/libyasm/util.h +++ b/libyasm/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); diff --git a/libyasm/xstrdup.c b/libyasm/xstrdup.c index 77cccba0..42685ea1 100644 --- a/libyasm/xstrdup.c +++ b/libyasm/xstrdup.c @@ -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/src/util.h b/src/util.h index a4fc61e7..05d8d782 100644 --- a/src/util.h +++ b/src/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); diff --git a/src/xstrdup.c b/src/xstrdup.c index 77cccba0..42685ea1 100644 --- a/src/xstrdup.c +++ b/src/xstrdup.c @@ -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 a4fc61e7..05d8d782 100644 --- 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);