X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=git-compat-util.h;h=7b29d1b905c618a50704a7b2c38041f71c942e71;hb=5410a02ab9e6a1987147724f8ea65e6a077b3832;hp=6bd8987b2774774fbbd3747a2b571b66ad78727b;hpb=8e29f903eb366b2d2e846dc35ec4510d2cb263ad;p=git.git diff --git a/git-compat-util.h b/git-compat-util.h index 6bd8987b2..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 *); @@ -167,6 +172,17 @@ extern size_t gitstrlcpy(char *, const char *, size_t); extern uintmax_t gitstrtoumax(const char *, char **, int); #endif +#ifdef NO_HSTRERROR +#define hstrerror githstrerror +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) @@ -200,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); @@ -282,6 +299,32 @@ static inline ssize_t xwrite(int fd, const void *buf, size_t len) } } +static inline int xdup(int fd) +{ + int ret = dup(fd); + if (ret < 0) + die("dup failed: %s", strerror(errno)); + return ret; +} + +static inline FILE *xfdopen(int fd, const char *mode) +{ + FILE *stream = fdopen(fd, mode); + if (stream == NULL) + die("Out of memory? fdopen failed: %s", strerror(errno)); + 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; @@ -338,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