1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
5 #if defined(__GNUC__) && (__GNUC__ < 3)
8 #define FLEX_ARRAY /* empty */
12 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
24 #include <sys/param.h>
25 #include <netinet/in.h>
26 #include <sys/types.h>
29 /* On most systems <limits.h> would have given us this, but
30 * not on some systems (e.g. GNU/Hurd).
37 #define NORETURN __attribute__((__noreturn__))
41 #define __attribute__(x)
45 /* General helper functions */
46 extern void usage(const char *err) NORETURN;
47 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
48 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
50 extern void set_usage_routine(void (*routine)(const char *err) NORETURN);
51 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
52 extern void set_error_routine(void (*routine)(const char *err, va_list params));
60 #define MAP_FAILED ((void*)-1)
63 #define mmap gitfakemmap
64 #define munmap gitfakemunmap
65 extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
66 extern int gitfakemunmap(void *start, size_t length);
75 #define setenv gitsetenv
76 extern int gitsetenv(const char *, const char *, int);
80 #define unsetenv gitunsetenv
81 extern void gitunsetenv(const char *);
85 #define strcasestr gitstrcasestr
86 extern char *gitstrcasestr(const char *haystack, const char *needle);
90 #define strlcpy gitstrlcpy
91 extern size_t gitstrlcpy(char *, const char *, size_t);
94 static inline char* xstrdup(const char *str)
96 char *ret = strdup(str);
98 die("Out of memory, strdup failed");
102 static inline void *xmalloc(size_t size)
104 void *ret = malloc(size);
108 die("Out of memory, malloc failed");
109 #ifdef XMALLOC_POISON
110 memset(ret, 0xA5, size);
115 static inline void *xrealloc(void *ptr, size_t size)
117 void *ret = realloc(ptr, size);
119 ret = realloc(ptr, 1);
121 die("Out of memory, realloc failed");
125 static inline void *xcalloc(size_t nmemb, size_t size)
127 void *ret = calloc(nmemb, size);
128 if (!ret && (!nmemb || !size))
131 die("Out of memory, calloc failed");
135 static inline ssize_t xread(int fd, void *buf, size_t len)
139 nr = read(fd, buf, len);
140 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
146 static inline ssize_t xwrite(int fd, const void *buf, size_t len)
150 nr = write(fd, buf, len);
151 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
157 static inline int has_extension(const char *filename, const char *ext)
159 size_t len = strlen(filename);
160 size_t extlen = strlen(ext);
161 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
164 /* Sane ctype - no locale, and works with signed chars */
171 extern unsigned char sane_ctype[256];
172 #define GIT_SPACE 0x01
173 #define GIT_DIGIT 0x02
174 #define GIT_ALPHA 0x04
175 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
176 #define isspace(x) sane_istest(x,GIT_SPACE)
177 #define isdigit(x) sane_istest(x,GIT_DIGIT)
178 #define isalpha(x) sane_istest(x,GIT_ALPHA)
179 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
180 #define tolower(x) sane_case((unsigned char)(x), 0x20)
181 #define toupper(x) sane_case((unsigned char)(x), 0)
183 static inline int sane_case(int x, int high)
185 if (sane_istest(x, GIT_ALPHA))
186 x = (x & ~0x20) | high;