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 strcasestr gitstrcasestr
68 extern char *gitstrcasestr(const char *haystack, const char *needle);
71 static inline void *xmalloc(size_t size)
73 void *ret = malloc(size);
77 die("Out of memory, malloc failed");
81 static inline void *xrealloc(void *ptr, size_t size)
83 void *ret = realloc(ptr, size);
85 ret = realloc(ptr, 1);
87 die("Out of memory, realloc failed");
91 static inline void *xcalloc(size_t nmemb, size_t size)
93 void *ret = calloc(nmemb, size);
94 if (!ret && (!nmemb || !size))
97 die("Out of memory, calloc failed");
101 static inline ssize_t xread(int fd, void *buf, size_t len)
105 nr = read(fd, buf, len);
106 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
112 static inline ssize_t xwrite(int fd, const void *buf, size_t len)
116 nr = write(fd, buf, len);
117 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
123 /* Sane ctype - no locale, and works with signed chars */
130 extern unsigned char sane_ctype[256];
131 #define GIT_SPACE 0x01
132 #define GIT_DIGIT 0x02
133 #define GIT_ALPHA 0x04
134 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
135 #define isspace(x) sane_istest(x,GIT_SPACE)
136 #define isdigit(x) sane_istest(x,GIT_DIGIT)
137 #define isalpha(x) sane_istest(x,GIT_ALPHA)
138 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
139 #define tolower(x) sane_case((unsigned char)(x), 0x20)
140 #define toupper(x) sane_case((unsigned char)(x), 0)
142 static inline int sane_case(int x, int high)
144 if (sane_istest(x, GIT_ALPHA))
145 x = (x & ~0x20) | high;
150 #define MAXPATHLEN 256