X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=git-compat-util.h;h=7b29d1b905c618a50704a7b2c38041f71c942e71;hb=5410a02ab9e6a1987147724f8ea65e6a077b3832;hp=362e040f52cf8df17811ae95e46f0b60d2b6f071;hpb=a6954452ecf757523b31d6eaaf7e00c7a2d91e46;p=git.git diff --git a/git-compat-util.h b/git-compat-util.h index 362e040f5..7b29d1b90 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -147,6 +147,11 @@ extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset); extern int gitsetenv(const char *, const char *, int); #endif +#ifdef NO_MKDTEMP +#define mkdtemp gitmkdtemp +extern char *gitmkdtemp(char *); +#endif + #ifdef NO_UNSETENV #define unsetenv gitunsetenv extern void gitunsetenv(const char *); @@ -172,6 +177,12 @@ extern uintmax_t gitstrtoumax(const char *, char **, int); extern const char *githstrerror(int herror); #endif +#ifdef NO_MEMMEM +#define memmem gitmemmem +void *gitmemmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen); +#endif + extern void release_pack_memory(size_t, int); static inline char* xstrdup(const char *str) @@ -205,19 +216,20 @@ static inline void *xmalloc(size_t size) return ret; } -static inline char *xstrndup(const char *str, size_t len) +static inline void *xmemdupz(const void *data, size_t len) { - char *p; - - p = memchr(str, '\0', len); - if (p) - len = p - str; - p = xmalloc(len + 1); - memcpy(p, str, len); + char *p = xmalloc(len + 1); + memcpy(p, data, len); p[len] = '\0'; return p; } +static inline char *xstrndup(const char *str, size_t len) +{ + char *p = memchr(str, '\0', len); + return xmemdupz(str, p ? p - str : len); +} + static inline void *xrealloc(void *ptr, size_t size) { void *ret = realloc(ptr, size); @@ -303,6 +315,16 @@ static inline FILE *xfdopen(int fd, const char *mode) return stream; } +static inline int xmkstemp(char *template) +{ + int fd; + + fd = mkstemp(template); + if (fd < 0) + die("Unable to create temporary file: %s", strerror(errno)); + return fd; +} + static inline size_t xsize_t(off_t len) { return (size_t)len; @@ -359,4 +381,17 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result) return 0; } +static inline int strtol_i(char const *s, int base, int *result) +{ + long ul; + char *p; + + errno = 0; + ul = strtol(s, &p, base); + if (errno || *p || p == s || (int) ul != ul) + return -1; + *result = ul; + return 0; +} + #endif