--format=pretty: avoid calculating expensive expansions twice
[git/git.git] / git-compat-util.h
CommitLineData
4050c0df
JH
1#ifndef GIT_COMPAT_UTIL_H
2#define GIT_COMPAT_UTIL_H
3
b97e9116
MW
4#define _FILE_OFFSET_BITS 64
5
8f1d2e6f
JH
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
13
b4f2a6ac
JH
14#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
15
8723f216
NP
16#ifdef __GNUC__
17#define TYPEOF(x) (__typeof__(x))
18#else
19#define TYPEOF(x)
20#endif
21
22#define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
23
cf606e3d
AW
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)
26
95ca1c6c 27#if !defined(__APPLE__) && !defined(__FreeBSD__)
85023577
JH
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 */
c902c9a6 30#endif
fb952206
JR
31#define _ALL_SOURCE 1
32#define _GNU_SOURCE 1
33#define _BSD_SOURCE 1
85023577 34
4050c0df
JH
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>
4050c0df
JH
46#include <sys/types.h>
47#include <dirent.h>
85023577
JH
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 <assert.h>
56#include <regex.h>
57#include <netinet/in.h>
58#include <netinet/tcp.h>
59#include <arpa/inet.h>
60#include <netdb.h>
61#include <pwd.h>
007e2ba6 62#include <inttypes.h>
41b20017
RJ
63#if defined(__CYGWIN__)
64#undef _XOPEN_SOURCE
65#include <grp.h>
66#define _XOPEN_SOURCE 600
67#else
fb952206 68#undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */
85023577 69#include <grp.h>
fb952206 70#define _ALL_SOURCE 1
41b20017 71#endif
85023577
JH
72
73#ifndef NO_ICONV
74#include <iconv.h>
75#endif
4050c0df 76
d0c2449f
JH
77/* On most systems <limits.h> would have given us this, but
78 * not on some systems (e.g. GNU/Hurd).
79 */
80#ifndef PATH_MAX
81#define PATH_MAX 4096
82#endif
83
c4001d92
SP
84#ifndef PRIuMAX
85#define PRIuMAX "llu"
86#endif
87
4050c0df
JH
88#ifdef __GNUC__
89#define NORETURN __attribute__((__noreturn__))
90#else
91#define NORETURN
92#ifndef __attribute__
93#define __attribute__(x)
94#endif
95#endif
96
97/* General helper functions */
98extern void usage(const char *err) NORETURN;
99extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
100extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
46efd2d9 101extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
4050c0df 102
39a3f5ea
PB
103extern void set_usage_routine(void (*routine)(const char *err) NORETURN);
104extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
105extern void set_error_routine(void (*routine)(const char *err, va_list params));
fa39b6b5 106extern void set_warn_routine(void (*routine)(const char *warn, va_list params));
39a3f5ea 107
4050c0df
JH
108#ifdef NO_MMAP
109
110#ifndef PROT_READ
111#define PROT_READ 1
112#define PROT_WRITE 2
113#define MAP_PRIVATE 1
114#define MAP_FAILED ((void*)-1)
115#endif
116
d6779124
SP
117#define mmap git_mmap
118#define munmap git_munmap
119extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
120extern int git_munmap(void *start, size_t length);
4050c0df 121
5faaf246 122/* This value must be multiple of (pagesize * 2) */
8c82534d
SP
123#define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
124
4050c0df
JH
125#else /* NO_MMAP */
126
127#include <sys/mman.h>
5faaf246
JH
128
129/* This value must be multiple of (pagesize * 2) */
22bac0ea
SP
130#define DEFAULT_PACKED_GIT_WINDOW_SIZE \
131 (sizeof(void*) >= 8 \
132 ? 1 * 1024 * 1024 * 1024 \
133 : 32 * 1024 * 1024)
4050c0df
JH
134
135#endif /* NO_MMAP */
136
22bac0ea 137#define DEFAULT_PACKED_GIT_LIMIT \
ecaebf4a 138 ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
8c82534d 139
6900679c
SH
140#ifdef NO_PREAD
141#define pread git_pread
142extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
143#endif
144
4050c0df
JH
145#ifdef NO_SETENV
146#define setenv gitsetenv
147extern int gitsetenv(const char *, const char *, int);
148#endif
149
ca5bb5d5
SP
150#ifdef NO_MKDTEMP
151#define mkdtemp gitmkdtemp
152extern char *gitmkdtemp(char *);
153#endif
154
731043fd
JR
155#ifdef NO_UNSETENV
156#define unsetenv gitunsetenv
157extern void gitunsetenv(const char *);
158#endif
159
4050c0df
JH
160#ifdef NO_STRCASESTR
161#define strcasestr gitstrcasestr
162extern char *gitstrcasestr(const char *haystack, const char *needle);
163#endif
164
817151e6
PE
165#ifdef NO_STRLCPY
166#define strlcpy gitstrlcpy
167extern size_t gitstrlcpy(char *, const char *, size_t);
168#endif
169
bc6b4f52
JR
170#ifdef NO_STRTOUMAX
171#define strtoumax gitstrtoumax
172extern uintmax_t gitstrtoumax(const char *, char **, int);
173#endif
174
fa0c87c3
AR
175#ifdef NO_HSTRERROR
176#define hstrerror githstrerror
177extern const char *githstrerror(int herror);
178#endif
179
b21b9f1d
RS
180#ifdef NO_MEMMEM
181#define memmem gitmemmem
182void *gitmemmem(const void *haystack, size_t haystacklen,
183 const void *needle, size_t needlelen);
184#endif
185
659c69cf
RS
186#ifdef NO_STRCHRNUL
187#define strchrnul gitstrchrnul
188char *gitstrchrnul(const char *s, int c);
189#endif
190
d1efefa4 191extern void release_pack_memory(size_t, int);
97bfeb34 192
9befac47
SP
193static inline char* xstrdup(const char *str)
194{
195 char *ret = strdup(str);
97bfeb34 196 if (!ret) {
d1efefa4 197 release_pack_memory(strlen(str) + 1, -1);
97bfeb34
SP
198 ret = strdup(str);
199 if (!ret)
200 die("Out of memory, strdup failed");
201 }
9befac47
SP
202 return ret;
203}
204
4050c0df
JH
205static inline void *xmalloc(size_t size)
206{
207 void *ret = malloc(size);
4e7a2ecc
JH
208 if (!ret && !size)
209 ret = malloc(1);
97bfeb34 210 if (!ret) {
d1efefa4 211 release_pack_memory(size, -1);
97bfeb34
SP
212 ret = malloc(size);
213 if (!ret && !size)
214 ret = malloc(1);
215 if (!ret)
216 die("Out of memory, malloc failed");
217 }
aa5481c1
JH
218#ifdef XMALLOC_POISON
219 memset(ret, 0xA5, size);
220#endif
4050c0df
JH
221 return ret;
222}
223
68d3025a 224static inline void *xmemdupz(const void *data, size_t len)
5094102e 225{
68d3025a
PH
226 char *p = xmalloc(len + 1);
227 memcpy(p, data, len);
5094102e
DB
228 p[len] = '\0';
229 return p;
230}
231
68d3025a
PH
232static inline char *xstrndup(const char *str, size_t len)
233{
234 char *p = memchr(str, '\0', len);
235 return xmemdupz(str, p ? p - str : len);
236}
237
4050c0df
JH
238static inline void *xrealloc(void *ptr, size_t size)
239{
240 void *ret = realloc(ptr, size);
4e7a2ecc
JH
241 if (!ret && !size)
242 ret = realloc(ptr, 1);
97bfeb34 243 if (!ret) {
d1efefa4 244 release_pack_memory(size, -1);
97bfeb34
SP
245 ret = realloc(ptr, size);
246 if (!ret && !size)
247 ret = realloc(ptr, 1);
248 if (!ret)
249 die("Out of memory, realloc failed");
250 }
4050c0df
JH
251 return ret;
252}
253
254static inline void *xcalloc(size_t nmemb, size_t size)
255{
256 void *ret = calloc(nmemb, size);
4e7a2ecc
JH
257 if (!ret && (!nmemb || !size))
258 ret = calloc(1, 1);
97bfeb34 259 if (!ret) {
d1efefa4 260 release_pack_memory(nmemb * size, -1);
97bfeb34
SP
261 ret = calloc(nmemb, size);
262 if (!ret && (!nmemb || !size))
263 ret = calloc(1, 1);
264 if (!ret)
265 die("Out of memory, calloc failed");
266 }
4050c0df
JH
267 return ret;
268}
269
c4712e45
SP
270static inline void *xmmap(void *start, size_t length,
271 int prot, int flags, int fd, off_t offset)
272{
273 void *ret = mmap(start, length, prot, flags, fd, offset);
274 if (ret == MAP_FAILED) {
9130ac1e
LT
275 if (!length)
276 return NULL;
d1efefa4 277 release_pack_memory(length, fd);
c4712e45
SP
278 ret = mmap(start, length, prot, flags, fd, offset);
279 if (ret == MAP_FAILED)
280 die("Out of memory? mmap failed: %s", strerror(errno));
281 }
282 return ret;
283}
284
1c15afb9
JH
285static inline ssize_t xread(int fd, void *buf, size_t len)
286{
287 ssize_t nr;
288 while (1) {
289 nr = read(fd, buf, len);
290 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
291 continue;
292 return nr;
293 }
294}
295
296static inline ssize_t xwrite(int fd, const void *buf, size_t len)
297{
298 ssize_t nr;
299 while (1) {
300 nr = write(fd, buf, len);
301 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
302 continue;
303 return nr;
304 }
305}
306
f5788250
JM
307static inline int xdup(int fd)
308{
309 int ret = dup(fd);
310 if (ret < 0)
311 die("dup failed: %s", strerror(errno));
312 return ret;
313}
314
315static inline FILE *xfdopen(int fd, const char *mode)
316{
317 FILE *stream = fdopen(fd, mode);
318 if (stream == NULL)
319 die("Out of memory? fdopen failed: %s", strerror(errno));
320 return stream;
321}
322
f21a47b2
LFC
323static inline int xmkstemp(char *template)
324{
325 int fd;
326
327 fd = mkstemp(template);
328 if (fd < 0)
329 die("Unable to create temporary file: %s", strerror(errno));
330 return fd;
331}
332
dc49cd76
SP
333static inline size_t xsize_t(off_t len)
334{
335 return (size_t)len;
336}
337
5bb1cda5 338static inline int has_extension(const char *filename, const char *ext)
83a2b841 339{
5bb1cda5
RS
340 size_t len = strlen(filename);
341 size_t extlen = strlen(ext);
83a2b841
RS
342 return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
343}
344
4050c0df
JH
345/* Sane ctype - no locale, and works with signed chars */
346#undef isspace
347#undef isdigit
348#undef isalpha
349#undef isalnum
350#undef tolower
351#undef toupper
352extern unsigned char sane_ctype[256];
353#define GIT_SPACE 0x01
354#define GIT_DIGIT 0x02
355#define GIT_ALPHA 0x04
356#define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
357#define isspace(x) sane_istest(x,GIT_SPACE)
358#define isdigit(x) sane_istest(x,GIT_DIGIT)
359#define isalpha(x) sane_istest(x,GIT_ALPHA)
360#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
361#define tolower(x) sane_case((unsigned char)(x), 0x20)
362#define toupper(x) sane_case((unsigned char)(x), 0)
363
364static inline int sane_case(int x, int high)
365{
366 if (sane_istest(x, GIT_ALPHA))
367 x = (x & ~0x20) | high;
368 return x;
369}
370
cff0302c
JH
371static inline int prefixcmp(const char *str, const char *prefix)
372{
373 return strncmp(str, prefix, strlen(prefix));
374}
375
6aead43d
JM
376static inline int strtoul_ui(char const *s, int base, unsigned int *result)
377{
378 unsigned long ul;
379 char *p;
380
381 errno = 0;
382 ul = strtoul(s, &p, base);
383 if (errno || *p || p == s || (unsigned int) ul != ul)
384 return -1;
385 *result = ul;
386 return 0;
387}
388
7791ecbc
JH
389static inline int strtol_i(char const *s, int base, int *result)
390{
391 long ul;
392 char *p;
393
394 errno = 0;
395 ul = strtol(s, &p, base);
396 if (errno || *p || p == s || (int) ul != ul)
397 return -1;
398 *result = ul;
399 return 0;
400}
401
4050c0df 402#endif