1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
14 #include <sys/param.h>
15 #include <netinet/in.h>
16 #include <sys/types.h>
20 #define NORETURN __attribute__((__noreturn__))
24 #define __attribute__(x)
28 /* General helper functions */
29 extern void usage(const char *err) NORETURN;
30 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
31 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
39 #define MAP_FAILED ((void*)-1)
42 #define mmap gitfakemmap
43 #define munmap gitfakemunmap
44 extern void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset);
45 extern int gitfakemunmap(void *start, size_t length);
54 #define setenv gitsetenv
55 extern int gitsetenv(const char *, const char *, int);
59 #define strcasestr gitstrcasestr
60 extern char *gitstrcasestr(const char *haystack, const char *needle);
63 static inline void *xmalloc(size_t size)
65 void *ret = malloc(size);
67 die("Out of memory, malloc failed");
71 static inline void *xrealloc(void *ptr, size_t size)
73 void *ret = realloc(ptr, size);
75 die("Out of memory, realloc failed");
79 static inline void *xcalloc(size_t nmemb, size_t size)
81 void *ret = calloc(nmemb, size);
83 die("Out of memory, calloc failed");
87 static inline ssize_t xread(int fd, void *buf, size_t len)
91 nr = read(fd, buf, len);
92 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
98 static inline ssize_t xwrite(int fd, const void *buf, size_t len)
102 nr = write(fd, buf, len);
103 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
109 /* Sane ctype - no locale, and works with signed chars */
116 extern unsigned char sane_ctype[256];
117 #define GIT_SPACE 0x01
118 #define GIT_DIGIT 0x02
119 #define GIT_ALPHA 0x04
120 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
121 #define isspace(x) sane_istest(x,GIT_SPACE)
122 #define isdigit(x) sane_istest(x,GIT_DIGIT)
123 #define isalpha(x) sane_istest(x,GIT_ALPHA)
124 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
125 #define tolower(x) sane_case((unsigned char)(x), 0x20)
126 #define toupper(x) sane_case((unsigned char)(x), 0)
128 static inline int sane_case(int x, int high)
130 if (sane_istest(x, GIT_ALPHA))
131 x = (x & ~0x20) | high;
136 #define MAXPATHLEN 256