Hash :
10aa3fa7
Author :
Date :
2009-06-04T17:14:35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#define GIT__NO_HIDE_MALLOC
#include "common.h"
#include <stdarg.h>
#include <stdio.h>
void *git__malloc(size_t n)
{
void *r = malloc(n);
if (!r)
return git_ptr_error(GIT_ENOMEM);
return r;
}
void *git__calloc(size_t a, size_t b)
{
void *r = calloc(a, b);
if (!r)
return git_ptr_error(GIT_ENOMEM);
return r;
}
char *git__strdup(const char *s)
{
char *r = strdup(s);
if (!s)
return git_ptr_error(GIT_ENOMEM);
return r;
}
int git__fmt(char *buf, size_t buf_sz, const char *fmt, ...)
{
va_list va;
int r;
va_start(va, fmt);
r = vsnprintf(buf, buf_sz, fmt, va);
va_end(va);
if (r < 0 || r >= buf_sz)
return GIT_ERROR;
return r;
}
int git__prefixcmp(const char *str, const char *prefix)
{
for (;;) {
char p = *(prefix++), s;
if (!p)
return 0;
if ((s = *(str++)) != p)
return s - p;
}
}
int git__suffixcmp(const char *str, const char *suffix)
{
size_t a = strlen(str);
size_t b = strlen(suffix);
if (a < b)
return -1;
return strcmp(str + (a - b), suffix);
}
int git__dirname(char *dir, size_t n, char *path)
{
char *s;
size_t len;
assert(dir && n > 1);
if (!path || !*path || (s = strrchr(path, '/')) == NULL) {
strcpy(dir, ".");
return 1;
}
if (s == path) { /* "/[aaa]" */
strcpy(dir, "/");
return 1;
}
if ((len = s - path) >= n)
return GIT_ERROR;
memcpy(dir, path, len);
dir[len] = '\0';
return len;
}
int git__basename(char *base, size_t n, char *path)
{
char *s;
size_t len;
assert(base && n > 1);
if (!path || !*path) {
strcpy(base, ".");
return 1;
}
len = strlen(path);
if ((s = strrchr(path, '/')) == NULL) {
if (len >= n)
return GIT_ERROR;
strcpy(base, path);
return len;
}
if (s == path && len == 1) { /* "/" */
strcpy(base, "/");
return 1;
}
len -= s - path;
if (len >= n)
return GIT_ERROR;
memcpy(base, s+1, len);
base[len] = '\0';
return len;
}