Move #include <sys/select.h> and <sys/ioctl.h> to git-compat-util.h.
[git/trast.git] / git-compat-util.h
blobe83195bde5ce202bd20b1d1a5dc95fa3f54ed6a1
1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
4 #define _FILE_OFFSET_BITS 64
6 #ifndef FLEX_ARRAY
7 #if defined(__GNUC__) && (__GNUC__ < 3)
8 #define FLEX_ARRAY 0
9 #else
10 #define FLEX_ARRAY /* empty */
11 #endif
12 #endif
14 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
16 #ifdef __GNUC__
17 #define TYPEOF(x) (__typeof__(x))
18 #else
19 #define TYPEOF(x)
20 #endif
22 #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
24 /* Approximation of the length of the decimal representation of this type. */
25 #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
27 #if !defined(__APPLE__) && !defined(__FreeBSD__)
28 #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
29 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
30 #endif
31 #define _ALL_SOURCE 1
32 #define _GNU_SOURCE 1
33 #define _BSD_SOURCE 1
35 #include <unistd.h>
36 #include <stdio.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <stddef.h>
40 #include <stdlib.h>
41 #include <stdarg.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <limits.h>
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <dirent.h>
48 #include <sys/time.h>
49 #include <time.h>
50 #include <signal.h>
51 #include <sys/wait.h>
52 #include <fnmatch.h>
53 #include <sys/poll.h>
54 #include <sys/socket.h>
55 #include <sys/ioctl.h>
56 #include <sys/select.h>
57 #include <assert.h>
58 #include <regex.h>
59 #include <netinet/in.h>
60 #include <netinet/tcp.h>
61 #include <arpa/inet.h>
62 #include <netdb.h>
63 #include <pwd.h>
64 #include <inttypes.h>
65 #if defined(__CYGWIN__)
66 #undef _XOPEN_SOURCE
67 #include <grp.h>
68 #define _XOPEN_SOURCE 600
69 #else
70 #undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */
71 #include <grp.h>
72 #define _ALL_SOURCE 1
73 #endif
75 #ifndef NO_ICONV
76 #include <iconv.h>
77 #endif
79 /* On most systems <limits.h> would have given us this, but
80 * not on some systems (e.g. GNU/Hurd).
82 #ifndef PATH_MAX
83 #define PATH_MAX 4096
84 #endif
86 #ifndef PRIuMAX
87 #define PRIuMAX "llu"
88 #endif
90 #ifdef __GNUC__
91 #define NORETURN __attribute__((__noreturn__))
92 #else
93 #define NORETURN
94 #ifndef __attribute__
95 #define __attribute__(x)
96 #endif
97 #endif
99 /* General helper functions */
100 extern void usage(const char *err) NORETURN;
101 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
102 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
103 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
105 extern void set_usage_routine(void (*routine)(const char *err) NORETURN);
106 extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
107 extern void set_error_routine(void (*routine)(const char *err, va_list params));
108 extern void set_warn_routine(void (*routine)(const char *warn, va_list params));
110 #ifdef NO_MMAP
112 #ifndef PROT_READ
113 #define PROT_READ 1
114 #define PROT_WRITE 2
115 #define MAP_PRIVATE 1
116 #define MAP_FAILED ((void*)-1)
117 #endif
119 #define mmap git_mmap
120 #define munmap git_munmap
121 extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
122 extern int git_munmap(void *start, size_t length);
124 /* This value must be multiple of (pagesize * 2) */
125 #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
127 #else /* NO_MMAP */
129 #include <sys/mman.h>
131 /* This value must be multiple of (pagesize * 2) */
132 #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
133 (sizeof(void*) >= 8 \
134 ? 1 * 1024 * 1024 * 1024 \
135 : 32 * 1024 * 1024)
137 #endif /* NO_MMAP */
139 #define DEFAULT_PACKED_GIT_LIMIT \
140 ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
142 #ifdef NO_PREAD
143 #define pread git_pread
144 extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
145 #endif
147 #ifdef NO_SETENV
148 #define setenv gitsetenv
149 extern int gitsetenv(const char *, const char *, int);
150 #endif
152 #ifdef NO_MKDTEMP
153 #define mkdtemp gitmkdtemp
154 extern char *gitmkdtemp(char *);
155 #endif
157 #ifdef NO_UNSETENV
158 #define unsetenv gitunsetenv
159 extern void gitunsetenv(const char *);
160 #endif
162 #ifdef NO_STRCASESTR
163 #define strcasestr gitstrcasestr
164 extern char *gitstrcasestr(const char *haystack, const char *needle);
165 #endif
167 #ifdef NO_STRLCPY
168 #define strlcpy gitstrlcpy
169 extern size_t gitstrlcpy(char *, const char *, size_t);
170 #endif
172 #ifdef NO_STRTOUMAX
173 #define strtoumax gitstrtoumax
174 extern uintmax_t gitstrtoumax(const char *, char **, int);
175 #endif
177 #ifdef NO_HSTRERROR
178 #define hstrerror githstrerror
179 extern const char *githstrerror(int herror);
180 #endif
182 #ifdef NO_MEMMEM
183 #define memmem gitmemmem
184 void *gitmemmem(const void *haystack, size_t haystacklen,
185 const void *needle, size_t needlelen);
186 #endif
188 #ifdef __GLIBC_PREREQ
189 #if __GLIBC_PREREQ(2, 1)
190 #define HAVE_STRCHRNUL
191 #endif
192 #endif
194 #ifndef HAVE_STRCHRNUL
195 #define strchrnul gitstrchrnul
196 static inline char *gitstrchrnul(const char *s, int c)
198 while (*s && *s != c)
199 s++;
200 return (char *)s;
202 #endif
204 extern void release_pack_memory(size_t, int);
206 static inline char* xstrdup(const char *str)
208 char *ret = strdup(str);
209 if (!ret) {
210 release_pack_memory(strlen(str) + 1, -1);
211 ret = strdup(str);
212 if (!ret)
213 die("Out of memory, strdup failed");
215 return ret;
218 static inline void *xmalloc(size_t size)
220 void *ret = malloc(size);
221 if (!ret && !size)
222 ret = malloc(1);
223 if (!ret) {
224 release_pack_memory(size, -1);
225 ret = malloc(size);
226 if (!ret && !size)
227 ret = malloc(1);
228 if (!ret)
229 die("Out of memory, malloc failed");
231 #ifdef XMALLOC_POISON
232 memset(ret, 0xA5, size);
233 #endif
234 return ret;
237 static inline void *xmemdupz(const void *data, size_t len)
239 char *p = xmalloc(len + 1);
240 memcpy(p, data, len);
241 p[len] = '\0';
242 return p;
245 static inline char *xstrndup(const char *str, size_t len)
247 char *p = memchr(str, '\0', len);
248 return xmemdupz(str, p ? p - str : len);
251 static inline void *xrealloc(void *ptr, size_t size)
253 void *ret = realloc(ptr, size);
254 if (!ret && !size)
255 ret = realloc(ptr, 1);
256 if (!ret) {
257 release_pack_memory(size, -1);
258 ret = realloc(ptr, size);
259 if (!ret && !size)
260 ret = realloc(ptr, 1);
261 if (!ret)
262 die("Out of memory, realloc failed");
264 return ret;
267 static inline void *xcalloc(size_t nmemb, size_t size)
269 void *ret = calloc(nmemb, size);
270 if (!ret && (!nmemb || !size))
271 ret = calloc(1, 1);
272 if (!ret) {
273 release_pack_memory(nmemb * size, -1);
274 ret = calloc(nmemb, size);
275 if (!ret && (!nmemb || !size))
276 ret = calloc(1, 1);
277 if (!ret)
278 die("Out of memory, calloc failed");
280 return ret;
283 static inline void *xmmap(void *start, size_t length,
284 int prot, int flags, int fd, off_t offset)
286 void *ret = mmap(start, length, prot, flags, fd, offset);
287 if (ret == MAP_FAILED) {
288 if (!length)
289 return NULL;
290 release_pack_memory(length, fd);
291 ret = mmap(start, length, prot, flags, fd, offset);
292 if (ret == MAP_FAILED)
293 die("Out of memory? mmap failed: %s", strerror(errno));
295 return ret;
298 static inline ssize_t xread(int fd, void *buf, size_t len)
300 ssize_t nr;
301 while (1) {
302 nr = read(fd, buf, len);
303 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
304 continue;
305 return nr;
309 static inline ssize_t xwrite(int fd, const void *buf, size_t len)
311 ssize_t nr;
312 while (1) {
313 nr = write(fd, buf, len);
314 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
315 continue;
316 return nr;
320 static inline int xdup(int fd)
322 int ret = dup(fd);
323 if (ret < 0)
324 die("dup failed: %s", strerror(errno));
325 return ret;
328 static inline FILE *xfdopen(int fd, const char *mode)
330 FILE *stream = fdopen(fd, mode);
331 if (stream == NULL)
332 die("Out of memory? fdopen failed: %s", strerror(errno));
333 return stream;
336 static inline int xmkstemp(char *template)
338 int fd;
340 fd = mkstemp(template);
341 if (fd < 0)
342 die("Unable to create temporary file: %s", strerror(errno));
343 return fd;
346 static inline size_t xsize_t(off_t len)
348 return (size_t)len;
351 static inline int has_extension(const char *filename, const char *ext)
353 size_t len = strlen(filename);
354 size_t extlen = strlen(ext);
355 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
358 /* Sane ctype - no locale, and works with signed chars */
359 #undef isspace
360 #undef isdigit
361 #undef isalpha
362 #undef isalnum
363 #undef tolower
364 #undef toupper
365 extern unsigned char sane_ctype[256];
366 #define GIT_SPACE 0x01
367 #define GIT_DIGIT 0x02
368 #define GIT_ALPHA 0x04
369 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
370 #define isspace(x) sane_istest(x,GIT_SPACE)
371 #define isdigit(x) sane_istest(x,GIT_DIGIT)
372 #define isalpha(x) sane_istest(x,GIT_ALPHA)
373 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
374 #define tolower(x) sane_case((unsigned char)(x), 0x20)
375 #define toupper(x) sane_case((unsigned char)(x), 0)
377 static inline int sane_case(int x, int high)
379 if (sane_istest(x, GIT_ALPHA))
380 x = (x & ~0x20) | high;
381 return x;
384 static inline int prefixcmp(const char *str, const char *prefix)
386 return strncmp(str, prefix, strlen(prefix));
389 static inline int strtoul_ui(char const *s, int base, unsigned int *result)
391 unsigned long ul;
392 char *p;
394 errno = 0;
395 ul = strtoul(s, &p, base);
396 if (errno || *p || p == s || (unsigned int) ul != ul)
397 return -1;
398 *result = ul;
399 return 0;
402 static inline int strtol_i(char const *s, int base, int *result)
404 long ul;
405 char *p;
407 errno = 0;
408 ul = strtol(s, &p, base);
409 if (errno || *p || p == s || (int) ul != ul)
410 return -1;
411 *result = ul;
412 return 0;
415 #endif