repo-config: fix printing of bool
[git/jrn.git] / git-compat-util.h
blobb3d4cf532e5728e6b962d0d65789f6e8e4bd7fe5
1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
4 #ifndef FLEX_ARRAY
5 #if defined(__GNUC__) && (__GNUC__ < 3)
6 #define FLEX_ARRAY 0
7 #else
8 #define FLEX_ARRAY /* empty */
9 #endif
10 #endif
12 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <stdarg.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <sys/param.h>
25 #include <netinet/in.h>
26 #include <sys/types.h>
27 #include <dirent.h>
29 #ifdef __GNUC__
30 #define NORETURN __attribute__((__noreturn__))
31 #else
32 #define NORETURN
33 #ifndef __attribute__
34 #define __attribute__(x)
35 #endif
36 #endif
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));
47 #ifdef NO_MMAP
49 #ifndef PROT_READ
50 #define PROT_READ 1
51 #define PROT_WRITE 2
52 #define MAP_PRIVATE 1
53 #define MAP_FAILED ((void*)-1)
54 #endif
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);
61 #else /* NO_MMAP */
63 #include <sys/mman.h>
65 #endif /* NO_MMAP */
67 #ifdef NO_SETENV
68 #define setenv gitsetenv
69 extern int gitsetenv(const char *, const char *, int);
70 #endif
72 #ifdef NO_UNSETENV
73 #define unsetenv gitunsetenv
74 extern void gitunsetenv(const char *);
75 #endif
77 #ifdef NO_STRCASESTR
78 #define strcasestr gitstrcasestr
79 extern char *gitstrcasestr(const char *haystack, const char *needle);
80 #endif
82 static inline void *xmalloc(size_t size)
84 void *ret = malloc(size);
85 if (!ret && !size)
86 ret = malloc(1);
87 if (!ret)
88 die("Out of memory, malloc failed");
89 return ret;
92 static inline void *xrealloc(void *ptr, size_t size)
94 void *ret = realloc(ptr, size);
95 if (!ret && !size)
96 ret = realloc(ptr, 1);
97 if (!ret)
98 die("Out of memory, realloc failed");
99 return ret;
102 static inline void *xcalloc(size_t nmemb, size_t size)
104 void *ret = calloc(nmemb, size);
105 if (!ret && (!nmemb || !size))
106 ret = calloc(1, 1);
107 if (!ret)
108 die("Out of memory, calloc failed");
109 return ret;
112 static inline ssize_t xread(int fd, void *buf, size_t len)
114 ssize_t nr;
115 while (1) {
116 nr = read(fd, buf, len);
117 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
118 continue;
119 return nr;
123 static inline ssize_t xwrite(int fd, const void *buf, size_t len)
125 ssize_t nr;
126 while (1) {
127 nr = write(fd, buf, len);
128 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
129 continue;
130 return nr;
134 /* Sane ctype - no locale, and works with signed chars */
135 #undef isspace
136 #undef isdigit
137 #undef isalpha
138 #undef isalnum
139 #undef tolower
140 #undef toupper
141 extern unsigned char sane_ctype[256];
142 #define GIT_SPACE 0x01
143 #define GIT_DIGIT 0x02
144 #define GIT_ALPHA 0x04
145 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
146 #define isspace(x) sane_istest(x,GIT_SPACE)
147 #define isdigit(x) sane_istest(x,GIT_DIGIT)
148 #define isalpha(x) sane_istest(x,GIT_ALPHA)
149 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
150 #define tolower(x) sane_case((unsigned char)(x), 0x20)
151 #define toupper(x) sane_case((unsigned char)(x), 0)
153 static inline int sane_case(int x, int high)
155 if (sane_istest(x, GIT_ALPHA))
156 x = (x & ~0x20) | high;
157 return x;
160 #ifndef MAXPATHLEN
161 #define MAXPATHLEN 256
162 #endif
163 #endif