614583e56adecb3217d18bd27a2f5b873ae0d55e
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 #if !defined(__APPLE__) && !defined(__FreeBSD__)
15 #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
16 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
17 #endif
18 #define _GNU_SOURCE
19 #define _BSD_SOURCE
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stddef.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <inttypes.h>
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <dirent.h>
35 #include <sys/time.h>
36 #include <time.h>
37 #include <signal.h>
38 #include <sys/wait.h>
39 #include <fnmatch.h>
40 #include <sys/poll.h>
41 #include <sys/socket.h>
42 #include <assert.h>
43 #include <regex.h>
44 #include <netinet/in.h>
45 #include <netinet/tcp.h>
46 #include <arpa/inet.h>
47 #include <netdb.h>
48 #include <pwd.h>
49 #include <grp.h>
51 #ifndef NO_ICONV
52 #include <iconv.h>
53 #endif
55 /* On most systems <limits.h> would have given us this, but
56 * not on some systems (e.g. GNU/Hurd).
57 */
58 #ifndef PATH_MAX
59 #define PATH_MAX 4096
60 #endif
62 #ifdef __GNUC__
63 #define NORETURN __attribute__((__noreturn__))
64 #else
65 #define NORETURN
66 #ifndef __attribute__
67 #define __attribute__(x)
68 #endif
69 #endif
71 /* General helper functions */
72 extern void usage(const char *err) NORETURN;
73 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
74 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
75 extern void warn(const char *err, ...) __attribute__((format (printf, 1, 2)));
77 extern void set_usage_routine(void (*routine)(const char *err) NORETURN);
78 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
79 extern void set_error_routine(void (*routine)(const char *err, va_list params));
80 extern void set_warn_routine(void (*routine)(const char *warn, va_list params));
82 #ifdef NO_MMAP
84 #ifndef PROT_READ
85 #define PROT_READ 1
86 #define PROT_WRITE 2
87 #define MAP_PRIVATE 1
88 #define MAP_FAILED ((void*)-1)
89 #endif
91 #define mmap git_mmap
92 #define munmap git_munmap
93 extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
94 extern int git_munmap(void *start, size_t length);
96 #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
98 #else /* NO_MMAP */
100 #include <sys/mman.h>
101 #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
102 (sizeof(void*) >= 8 \
103 ? 1 * 1024 * 1024 * 1024 \
104 : 32 * 1024 * 1024)
106 #endif /* NO_MMAP */
108 #define DEFAULT_PACKED_GIT_LIMIT \
109 ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
111 #ifdef NO_PREAD
112 #define pread git_pread
113 extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
114 #endif
116 #ifdef NO_SETENV
117 #define setenv gitsetenv
118 extern int gitsetenv(const char *, const char *, int);
119 #endif
121 #ifdef NO_UNSETENV
122 #define unsetenv gitunsetenv
123 extern void gitunsetenv(const char *);
124 #endif
126 #ifdef NO_STRCASESTR
127 #define strcasestr gitstrcasestr
128 extern char *gitstrcasestr(const char *haystack, const char *needle);
129 #endif
131 #ifdef NO_STRLCPY
132 #define strlcpy gitstrlcpy
133 extern size_t gitstrlcpy(char *, const char *, size_t);
134 #endif
136 extern void release_pack_memory(size_t);
138 static inline char* xstrdup(const char *str)
139 {
140 char *ret = strdup(str);
141 if (!ret) {
142 release_pack_memory(strlen(str) + 1);
143 ret = strdup(str);
144 if (!ret)
145 die("Out of memory, strdup failed");
146 }
147 return ret;
148 }
150 static inline void *xmalloc(size_t size)
151 {
152 void *ret = malloc(size);
153 if (!ret && !size)
154 ret = malloc(1);
155 if (!ret) {
156 release_pack_memory(size);
157 ret = malloc(size);
158 if (!ret && !size)
159 ret = malloc(1);
160 if (!ret)
161 die("Out of memory, malloc failed");
162 }
163 #ifdef XMALLOC_POISON
164 memset(ret, 0xA5, size);
165 #endif
166 return ret;
167 }
169 static inline void *xrealloc(void *ptr, size_t size)
170 {
171 void *ret = realloc(ptr, size);
172 if (!ret && !size)
173 ret = realloc(ptr, 1);
174 if (!ret) {
175 release_pack_memory(size);
176 ret = realloc(ptr, size);
177 if (!ret && !size)
178 ret = realloc(ptr, 1);
179 if (!ret)
180 die("Out of memory, realloc failed");
181 }
182 return ret;
183 }
185 static inline void *xcalloc(size_t nmemb, size_t size)
186 {
187 void *ret = calloc(nmemb, size);
188 if (!ret && (!nmemb || !size))
189 ret = calloc(1, 1);
190 if (!ret) {
191 release_pack_memory(nmemb * size);
192 ret = calloc(nmemb, size);
193 if (!ret && (!nmemb || !size))
194 ret = calloc(1, 1);
195 if (!ret)
196 die("Out of memory, calloc failed");
197 }
198 return ret;
199 }
201 static inline void *xmmap(void *start, size_t length,
202 int prot, int flags, int fd, off_t offset)
203 {
204 void *ret = mmap(start, length, prot, flags, fd, offset);
205 if (ret == MAP_FAILED) {
206 if (!length)
207 return NULL;
208 release_pack_memory(length);
209 ret = mmap(start, length, prot, flags, fd, offset);
210 if (ret == MAP_FAILED)
211 die("Out of memory? mmap failed: %s", strerror(errno));
212 }
213 return ret;
214 }
216 static inline ssize_t xread(int fd, void *buf, size_t len)
217 {
218 ssize_t nr;
219 while (1) {
220 nr = read(fd, buf, len);
221 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
222 continue;
223 return nr;
224 }
225 }
227 static inline ssize_t xwrite(int fd, const void *buf, size_t len)
228 {
229 ssize_t nr;
230 while (1) {
231 nr = write(fd, buf, len);
232 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
233 continue;
234 return nr;
235 }
236 }
238 static inline int has_extension(const char *filename, const char *ext)
239 {
240 size_t len = strlen(filename);
241 size_t extlen = strlen(ext);
242 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
243 }
245 /* Sane ctype - no locale, and works with signed chars */
246 #undef isspace
247 #undef isdigit
248 #undef isalpha
249 #undef isalnum
250 #undef tolower
251 #undef toupper
252 extern unsigned char sane_ctype[256];
253 #define GIT_SPACE 0x01
254 #define GIT_DIGIT 0x02
255 #define GIT_ALPHA 0x04
256 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
257 #define isspace(x) sane_istest(x,GIT_SPACE)
258 #define isdigit(x) sane_istest(x,GIT_DIGIT)
259 #define isalpha(x) sane_istest(x,GIT_ALPHA)
260 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
261 #define tolower(x) sane_case((unsigned char)(x), 0x20)
262 #define toupper(x) sane_case((unsigned char)(x), 0)
264 static inline int sane_case(int x, int high)
265 {
266 if (sane_istest(x, GIT_ALPHA))
267 x = (x & ~0x20) | high;
268 return x;
269 }
271 #endif