Commit 6adcb5f3d2b5770995114521b4a6504d1f0cad03

Vicent Marti 2011-05-17T15:04:18

utils: Implement git__strndup

diff --git a/src/util.h b/src/util.h
index 6724e8d..ff6e67f 100644
--- a/src/util.h
+++ b/src/util.h
@@ -34,6 +34,25 @@ GIT_INLINE(char *) git__strdup(const char *str)
 	return ptr;
 }
 
+GIT_INLINE(char *) git__strndup(const char *str, size_t n)
+{
+	size_t length;
+	char *ptr;
+
+	length = strlen(str);
+	if (n < length)
+		length = n;
+
+	ptr = malloc(length + 1);
+	if (!ptr)
+		git__throw(GIT_ENOMEM, "Out of memory. Failed to duplicate string");
+
+	memcpy(ptr, str, length);
+	ptr[length] = 0;
+
+	return ptr;
+}
+
 GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
 {
 	void *new_ptr = realloc(ptr, size);