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>
30 #define NORETURN __attribute__((__noreturn__))
34 #define __attribute__(x)
38 /* General helper functions */
39 extern void usage(const char *err
) NORETURN
;
40 extern void die(const char *err
, ...) NORETURN
__attribute__((format (printf
, 1, 2)));
41 extern int error(const char *err
, ...) __attribute__((format (printf
, 1, 2)));
49 #define MAP_FAILED ((void*)-1)
52 #define mmap gitfakemmap
53 #define munmap gitfakemunmap
54 extern void *gitfakemmap(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t offset
);
55 extern int gitfakemunmap(void *start
, size_t length
);
64 #define setenv gitsetenv
65 extern int gitsetenv(const char *, const char *, int);
69 #define unsetenv gitunsetenv
70 extern void gitunsetenv(const char *);
74 #define strcasestr gitstrcasestr
75 extern char *gitstrcasestr(const char *haystack
, const char *needle
);
78 static inline void *xmalloc(size_t size
)
80 void *ret
= malloc(size
);
84 die("Out of memory, malloc failed");
88 static inline void *xrealloc(void *ptr
, size_t size
)
90 void *ret
= realloc(ptr
, size
);
92 ret
= realloc(ptr
, 1);
94 die("Out of memory, realloc failed");
98 static inline void *xcalloc(size_t nmemb
, size_t size
)
100 void *ret
= calloc(nmemb
, size
);
101 if (!ret
&& (!nmemb
|| !size
))
104 die("Out of memory, calloc failed");
108 static inline ssize_t
xread(int fd
, void *buf
, size_t len
)
112 nr
= read(fd
, buf
, len
);
113 if ((nr
< 0) && (errno
== EAGAIN
|| errno
== EINTR
))
119 static inline ssize_t
xwrite(int fd
, const void *buf
, size_t len
)
123 nr
= write(fd
, buf
, len
);
124 if ((nr
< 0) && (errno
== EAGAIN
|| errno
== EINTR
))
130 /* Sane ctype - no locale, and works with signed chars */
137 extern unsigned char sane_ctype
[256];
138 #define GIT_SPACE 0x01
139 #define GIT_DIGIT 0x02
140 #define GIT_ALPHA 0x04
141 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
142 #define isspace(x) sane_istest(x,GIT_SPACE)
143 #define isdigit(x) sane_istest(x,GIT_DIGIT)
144 #define isalpha(x) sane_istest(x,GIT_ALPHA)
145 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
146 #define tolower(x) sane_case((unsigned char)(x), 0x20)
147 #define toupper(x) sane_case((unsigned char)(x), 0)
149 static inline int sane_case(int x
, int high
)
151 if (sane_istest(x
, GIT_ALPHA
))
152 x
= (x
& ~0x20) | high
;
157 #define MAXPATHLEN 256