summaryrefslogtreecommitdiff
path: root/src/util.h
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2011-05-17 15:04:18 +0300
committerVicent Marti <tanoku@gmail.com>2011-05-17 15:11:19 +0300
commit6adcb5f3d2b5770995114521b4a6504d1f0cad03 (patch)
tree2977fb37576b37bd2559744ddd6cb35d5f6ae040 /src/util.h
parentb3d94069b4c03374bee482df4ea20eed022743dc (diff)
downloadlibgit2-6adcb5f3d2b5770995114521b4a6504d1f0cad03.tar.gz
utils: Implement git__strndup
Diffstat (limited to 'src/util.h')
-rw-r--r--src/util.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h
index 6724e8d41..ff6e67f8d 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);