]> git.ipfire.org Git - thirdparty/git.git/blobdiff - git-compat-util.h
Make xstrndup common
[thirdparty/git.git] / git-compat-util.h
index e3cf3703bbb896067f4c2d5b5e1f3ce898d8b6fc..bd93b6257885d56fda7268ae88ea82f721a6707f 100644 (file)
@@ -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);