1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
5 #if defined(__GNUC__) && (__GNUC__ < 3)
8 #define FLEX_ARRAY /* empty */
22 #include <sys/param.h>
23 #include <netinet/in.h>
24 #include <sys/types.h>
28 #define NORETURN __attribute__((__noreturn__))
32 #define __attribute__(x)
36 /* General helper functions */
37 extern void usage(const char *err) NORETURN;
38 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
39 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
47 #define MAP_FAILED ((void*)-1)
50 #define mmap gitfakemmap
51 #define munmap gitfakemunmap
52 extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
53 extern int gitfakemunmap(void *start, size_t length);
62 #define setenv gitsetenv
63 extern int gitsetenv(const char *, const char *, int);
67 #define unsetenv gitunsetenv
68 extern void gitunsetenv(const char *);
72 #define strcasestr gitstrcasestr
73 extern char *gitstrcasestr(const char *haystack, const char *needle);
76 static inline void *xmalloc(size_t size)
78 void *ret = malloc(size);
82 die("Out of memory, malloc failed");
86 static inline void *xrealloc(void *ptr, size_t size)
88 void *ret = realloc(ptr, size);
90 ret = realloc(ptr, 1);
92 die("Out of memory, realloc failed");
96 static inline void *xcalloc(size_t nmemb, size_t size)
98 void *ret = calloc(nmemb, size);
99 if (!ret && (!nmemb || !size))
102 die("Out of memory, calloc failed");
106 static inline ssize_t xread(int fd, void *buf, size_t len)
110 nr = read(fd, buf, len);
111 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
117 static inline ssize_t xwrite(int fd, const void *buf, size_t len)
121 nr = write(fd, buf, len);
122 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
128 /* Sane ctype - no locale, and works with signed chars */
135 extern unsigned char sane_ctype[256];
136 #define GIT_SPACE 0x01
137 #define GIT_DIGIT 0x02
138 #define GIT_ALPHA 0x04
139 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
140 #define isspace(x) sane_istest(x,GIT_SPACE)
141 #define isdigit(x) sane_istest(x,GIT_DIGIT)
142 #define isalpha(x) sane_istest(x,GIT_ALPHA)
143 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
144 #define tolower(x) sane_case((unsigned char)(x), 0x20)
145 #define toupper(x) sane_case((unsigned char)(x), 0)
147 static inline int sane_case(int x, int high)
149 if (sane_istest(x, GIT_ALPHA))
150 x = (x & ~0x20) | high;
155 #define MAXPATHLEN 256