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)));
43 extern void set_usage_routine(void (*routine
)(const char *err
) NORETURN
);
44 extern void set_die_routine(void (*routine
)(const char *err
, va_list params
) NORETURN
);
45 extern void set_error_routine(void (*routine
)(const char *err
, va_list params
));
53 #define MAP_FAILED ((void*)-1)
56 #define mmap gitfakemmap
57 #define munmap gitfakemunmap
58 extern void *gitfakemmap(void *start
, size_t length
, int prot
, int flags
, int fd
, off_t offset
);
59 extern int gitfakemunmap(void *start
, size_t length
);
68 #define setenv gitsetenv
69 extern int gitsetenv(const char *, const char *, int);
73 #define unsetenv gitunsetenv
74 extern void gitunsetenv(const char *);
78 #define strcasestr gitstrcasestr
79 extern char *gitstrcasestr(const char *haystack
, const char *needle
);
83 #define strlcpy gitstrlcpy
84 extern size_t gitstrlcpy(char *, const char *, size_t);
87 static inline void *xmalloc(size_t size
)
89 void *ret
= malloc(size
);
93 die("Out of memory, malloc failed");
95 memset(ret
, 0xA5, size
);
100 static inline void *xrealloc(void *ptr
, size_t size
)
102 void *ret
= realloc(ptr
, size
);
104 ret
= realloc(ptr
, 1);
106 die("Out of memory, realloc failed");
110 static inline void *xcalloc(size_t nmemb
, size_t size
)
112 void *ret
= calloc(nmemb
, size
);
113 if (!ret
&& (!nmemb
|| !size
))
116 die("Out of memory, calloc failed");
120 static inline ssize_t
xread(int fd
, void *buf
, size_t len
)
124 nr
= read(fd
, buf
, len
);
125 if ((nr
< 0) && (errno
== EAGAIN
|| errno
== EINTR
))
131 static inline ssize_t
xwrite(int fd
, const void *buf
, size_t len
)
135 nr
= write(fd
, buf
, len
);
136 if ((nr
< 0) && (errno
== EAGAIN
|| errno
== EINTR
))
142 static inline int has_extension(const char *filename
, int len
, const char *ext
)
144 int extlen
= strlen(ext
);
145 return len
> extlen
&& !memcmp(filename
+ len
- extlen
, ext
, extlen
);
148 /* Sane ctype - no locale, and works with signed chars */
155 extern unsigned char sane_ctype
[256];
156 #define GIT_SPACE 0x01
157 #define GIT_DIGIT 0x02
158 #define GIT_ALPHA 0x04
159 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
160 #define isspace(x) sane_istest(x,GIT_SPACE)
161 #define isdigit(x) sane_istest(x,GIT_DIGIT)
162 #define isalpha(x) sane_istest(x,GIT_ALPHA)
163 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
164 #define tolower(x) sane_case((unsigned char)(x), 0x20)
165 #define toupper(x) sane_case((unsigned char)(x), 0)
167 static inline int sane_case(int x
, int high
)
169 if (sane_istest(x
, GIT_ALPHA
))
170 x
= (x
& ~0x20) | high
;
175 #define MAXPATHLEN 256