diff options
author | Daniel Barkalow <barkalow@iabervon.org> | 2007-05-02 22:49:41 -0400 |
---|---|---|
committer | Junio C Hamano <junkio@cox.net> | 2007-05-03 22:28:55 -0700 |
commit | 5094102e13640d7559a02fd7c77b44dc9254cbbb (patch) | |
tree | c1d19c522edf8c5e4a5eb49442509d937b02c995 /git-compat-util.h | |
parent | cdda666201710dcf50d7ebee804aac65fdec32fd (diff) | |
download | git-5094102e13640d7559a02fd7c77b44dc9254cbbb.tar.gz git-5094102e13640d7559a02fd7c77b44dc9254cbbb.tar.xz |
Make xstrndup common
This also improves the implementation to match how strndup is
specified (by GNU): if the length given is longer than the string,
only the string's length is allocated and copied, but the string need
not be null-terminated if it is at least as long as the given length.
Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'git-compat-util.h')
-rw-r--r-- | git-compat-util.h | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/git-compat-util.h b/git-compat-util.h index e3cf3703b..bd93b6257 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -189,6 +189,19 @@ static inline void *xmalloc(size_t size) return ret; } +static inline char *xstrndup(const char *str, size_t len) +{ + char *p; + + p = memchr(str, '\0', len); + if (p) + len = p - str; + p = xmalloc(len + 1); + memcpy(p, str, len); + p[len] = '\0'; + return p; +} + static inline void *xrealloc(void *ptr, size_t size) { void *ret = realloc(ptr, size); |