]> git.ipfire.org Git - thirdparty/git.git/blob - git-compat-util.h
untracked cache: guard and disable on system changes
[thirdparty/git.git] / git-compat-util.h
1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
3
4 #define _FILE_OFFSET_BITS 64
5
6 #ifndef FLEX_ARRAY
7 /*
8 * See if our compiler is known to support flexible array members.
9 */
10 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && (!defined(__SUNPRO_C) || (__SUNPRO_C > 0x580))
11 # define FLEX_ARRAY /* empty */
12 #elif defined(__GNUC__)
13 # if (__GNUC__ >= 3)
14 # define FLEX_ARRAY /* empty */
15 # else
16 # define FLEX_ARRAY 0 /* older GNU extension */
17 # endif
18 #endif
19
20 /*
21 * Otherwise, default to safer but a bit wasteful traditional style
22 */
23 #ifndef FLEX_ARRAY
24 # define FLEX_ARRAY 1
25 #endif
26 #endif
27
28 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
29 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
30
31 #define maximum_signed_value_of_type(a) \
32 (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a)))
33
34 #define maximum_unsigned_value_of_type(a) \
35 (UINTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a)))
36
37 /*
38 * Signed integer overflow is undefined in C, so here's a helper macro
39 * to detect if the sum of two integers will overflow.
40 *
41 * Requires: a >= 0, typeof(a) equals typeof(b)
42 */
43 #define signed_add_overflows(a, b) \
44 ((b) > maximum_signed_value_of_type(a) - (a))
45
46 #define unsigned_add_overflows(a, b) \
47 ((b) > maximum_unsigned_value_of_type(a) - (a))
48
49 #ifdef __GNUC__
50 #define TYPEOF(x) (__typeof__(x))
51 #else
52 #define TYPEOF(x)
53 #endif
54
55 #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (bitsizeof(x) - (bits))))
56 #define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
57
58 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
59
60 /* Approximation of the length of the decimal representation of this type. */
61 #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
62
63 #if defined(__sun__)
64 /*
65 * On Solaris, when _XOPEN_EXTENDED is set, its header file
66 * forces the programs to be XPG4v2, defeating any _XOPEN_SOURCE
67 * setting to say we are XPG5 or XPG6. Also on Solaris,
68 * XPG6 programs must be compiled with a c99 compiler, while
69 * non XPG6 programs must be compiled with a pre-c99 compiler.
70 */
71 # if __STDC_VERSION__ - 0 >= 199901L
72 # define _XOPEN_SOURCE 600
73 # else
74 # define _XOPEN_SOURCE 500
75 # endif
76 #elif !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && \
77 !defined(_M_UNIX) && !defined(__sgi) && !defined(__DragonFly__) && \
78 !defined(__TANDEM) && !defined(__QNX__) && !defined(__MirBSD__) && \
79 !defined(__CYGWIN__)
80 #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
81 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
82 #endif
83 #define _ALL_SOURCE 1
84 #define _GNU_SOURCE 1
85 #define _BSD_SOURCE 1
86 #define _DEFAULT_SOURCE 1
87 #define _NETBSD_SOURCE 1
88 #define _SGI_SOURCE 1
89
90 #if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */
91 # if defined (_MSC_VER) && !defined(_WIN32_WINNT)
92 # define _WIN32_WINNT 0x0502
93 # endif
94 #define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */
95 #include <winsock2.h>
96 #include <windows.h>
97 #define GIT_WINDOWS_NATIVE
98 #endif
99
100 #include <unistd.h>
101 #include <stdio.h>
102 #include <sys/stat.h>
103 #include <fcntl.h>
104 #include <stddef.h>
105 #include <stdlib.h>
106 #include <stdarg.h>
107 #include <string.h>
108 #ifdef HAVE_STRINGS_H
109 #include <strings.h> /* for strcasecmp() */
110 #endif
111 #include <errno.h>
112 #include <limits.h>
113 #ifdef NEEDS_SYS_PARAM_H
114 #include <sys/param.h>
115 #endif
116 #include <sys/types.h>
117 #include <dirent.h>
118 #include <sys/time.h>
119 #include <time.h>
120 #include <signal.h>
121 #include <assert.h>
122 #include <regex.h>
123 #include <utime.h>
124 #include <syslog.h>
125 #ifndef NO_SYS_POLL_H
126 #include <sys/poll.h>
127 #else
128 #include <poll.h>
129 #endif
130
131 #if defined(__MINGW32__)
132 /* pull in Windows compatibility stuff */
133 #include "compat/mingw.h"
134 #elif defined(_MSC_VER)
135 #include "compat/msvc.h"
136 #else
137 #include <sys/utsname.h>
138 #include <sys/wait.h>
139 #include <sys/resource.h>
140 #include <sys/socket.h>
141 #include <sys/ioctl.h>
142 #include <termios.h>
143 #ifndef NO_SYS_SELECT_H
144 #include <sys/select.h>
145 #endif
146 #include <netinet/in.h>
147 #include <netinet/tcp.h>
148 #include <arpa/inet.h>
149 #include <netdb.h>
150 #include <pwd.h>
151 #include <sys/un.h>
152 #ifndef NO_INTTYPES_H
153 #include <inttypes.h>
154 #else
155 #include <stdint.h>
156 #endif
157 #ifdef NO_INTPTR_T
158 /*
159 * On I16LP32, ILP32 and LP64 "long" is the save bet, however
160 * on LLP86, IL33LLP64 and P64 it needs to be "long long",
161 * while on IP16 and IP16L32 it is "int" (resp. "short")
162 * Size needs to match (or exceed) 'sizeof(void *)'.
163 * We can't take "long long" here as not everybody has it.
164 */
165 typedef long intptr_t;
166 typedef unsigned long uintptr_t;
167 #endif
168 #undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */
169 #include <grp.h>
170 #define _ALL_SOURCE 1
171 #endif
172
173 /* used on Mac OS X */
174 #ifdef PRECOMPOSE_UNICODE
175 #include "compat/precompose_utf8.h"
176 #else
177 #define precompose_str(in,i_nfd2nfc)
178 #define precompose_argv(c,v)
179 #define probe_utf8_pathname_composition(a,b)
180 #endif
181
182 #ifdef MKDIR_WO_TRAILING_SLASH
183 #define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))
184 extern int compat_mkdir_wo_trailing_slash(const char*, mode_t);
185 #endif
186
187 #ifdef NO_STRUCT_ITIMERVAL
188 struct itimerval {
189 struct timeval it_interval;
190 struct timeval it_value;
191 };
192 #endif
193
194 #ifdef NO_SETITIMER
195 #define setitimer(which,value,ovalue)
196 #endif
197
198 #ifndef NO_LIBGEN_H
199 #include <libgen.h>
200 #else
201 #define basename gitbasename
202 extern char *gitbasename(char *);
203 #endif
204
205 #ifndef NO_ICONV
206 #include <iconv.h>
207 #endif
208
209 #ifndef NO_OPENSSL
210 #ifdef __APPLE__
211 #define __AVAILABILITY_MACROS_USES_AVAILABILITY 0
212 #include <AvailabilityMacros.h>
213 #undef DEPRECATED_ATTRIBUTE
214 #define DEPRECATED_ATTRIBUTE
215 #undef __AVAILABILITY_MACROS_USES_AVAILABILITY
216 #endif
217 #include <openssl/ssl.h>
218 #include <openssl/err.h>
219 #ifdef NO_HMAC_CTX_CLEANUP
220 #define HMAC_CTX_cleanup HMAC_cleanup
221 #endif
222 #endif
223
224 /* On most systems <netdb.h> would have given us this, but
225 * not on some systems (e.g. z/OS).
226 */
227 #ifndef NI_MAXHOST
228 #define NI_MAXHOST 1025
229 #endif
230
231 #ifndef NI_MAXSERV
232 #define NI_MAXSERV 32
233 #endif
234
235 /* On most systems <limits.h> would have given us this, but
236 * not on some systems (e.g. GNU/Hurd).
237 */
238 #ifndef PATH_MAX
239 #define PATH_MAX 4096
240 #endif
241
242 #ifndef PRIuMAX
243 #define PRIuMAX "llu"
244 #endif
245
246 #ifndef PRIu32
247 #define PRIu32 "u"
248 #endif
249
250 #ifndef PRIx32
251 #define PRIx32 "x"
252 #endif
253
254 #ifndef PRIo32
255 #define PRIo32 "o"
256 #endif
257
258 #ifndef PATH_SEP
259 #define PATH_SEP ':'
260 #endif
261
262 #ifdef HAVE_PATHS_H
263 #include <paths.h>
264 #endif
265 #ifndef _PATH_DEFPATH
266 #define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin"
267 #endif
268
269 #ifndef STRIP_EXTENSION
270 #define STRIP_EXTENSION ""
271 #endif
272
273 #ifndef has_dos_drive_prefix
274 static inline int git_has_dos_drive_prefix(const char *path)
275 {
276 return 0;
277 }
278 #define has_dos_drive_prefix git_has_dos_drive_prefix
279 #endif
280
281 #ifndef is_dir_sep
282 static inline int git_is_dir_sep(int c)
283 {
284 return c == '/';
285 }
286 #define is_dir_sep git_is_dir_sep
287 #endif
288
289 #ifndef offset_1st_component
290 static inline int git_offset_1st_component(const char *path)
291 {
292 return is_dir_sep(path[0]);
293 }
294 #define offset_1st_component git_offset_1st_component
295 #endif
296
297 #ifndef find_last_dir_sep
298 static inline char *git_find_last_dir_sep(const char *path)
299 {
300 return strrchr(path, '/');
301 }
302 #define find_last_dir_sep git_find_last_dir_sep
303 #endif
304
305 #if defined(__HP_cc) && (__HP_cc >= 61000)
306 #define NORETURN __attribute__((noreturn))
307 #define NORETURN_PTR
308 #elif defined(__GNUC__) && !defined(NO_NORETURN)
309 #define NORETURN __attribute__((__noreturn__))
310 #define NORETURN_PTR __attribute__((__noreturn__))
311 #elif defined(_MSC_VER)
312 #define NORETURN __declspec(noreturn)
313 #define NORETURN_PTR
314 #else
315 #define NORETURN
316 #define NORETURN_PTR
317 #ifndef __GNUC__
318 #ifndef __attribute__
319 #define __attribute__(x)
320 #endif
321 #endif
322 #endif
323
324 /* The sentinel attribute is valid from gcc version 4.0 */
325 #if defined(__GNUC__) && (__GNUC__ >= 4)
326 #define LAST_ARG_MUST_BE_NULL __attribute__((sentinel))
327 #else
328 #define LAST_ARG_MUST_BE_NULL
329 #endif
330
331 #include "compat/bswap.h"
332
333 #include "wildmatch.h"
334
335 struct strbuf;
336
337 /* General helper functions */
338 extern void vreportf(const char *prefix, const char *err, va_list params);
339 extern void vwritef(int fd, const char *prefix, const char *err, va_list params);
340 extern NORETURN void usage(const char *err);
341 extern NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
342 extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
343 extern NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
344 extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
345 extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
346
347 #ifndef NO_OPENSSL
348 #ifdef APPLE_COMMON_CRYPTO
349 #include "compat/apple-common-crypto.h"
350 #else
351 #include <openssl/evp.h>
352 #include <openssl/hmac.h>
353 #endif /* APPLE_COMMON_CRYPTO */
354 #include <openssl/x509v3.h>
355 #endif /* NO_OPENSSL */
356
357 /*
358 * Let callers be aware of the constant return value; this can help
359 * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
360 * because some compilers may not support variadic macros. Since we're only
361 * trying to help gcc, anyway, it's OK; other compilers will fall back to
362 * using the function as usual.
363 */
364 #if defined(__GNUC__)
365 static inline int const_error(void)
366 {
367 return -1;
368 }
369 #define error(...) (error(__VA_ARGS__), const_error())
370 #endif
371
372 extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
373 extern void set_error_routine(void (*routine)(const char *err, va_list params));
374 extern void set_die_is_recursing_routine(int (*routine)(void));
375
376 extern int starts_with(const char *str, const char *prefix);
377
378 /*
379 * If the string "str" begins with the string found in "prefix", return 1.
380 * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
381 * the string right after the prefix).
382 *
383 * Otherwise, return 0 and leave "out" untouched.
384 *
385 * Examples:
386 *
387 * [extract branch name, fail if not a branch]
388 * if (!skip_prefix(ref, "refs/heads/", &branch)
389 * return -1;
390 *
391 * [skip prefix if present, otherwise use whole string]
392 * skip_prefix(name, "refs/heads/", &name);
393 */
394 static inline int skip_prefix(const char *str, const char *prefix,
395 const char **out)
396 {
397 do {
398 if (!*prefix) {
399 *out = str;
400 return 1;
401 }
402 } while (*str++ == *prefix++);
403 return 0;
404 }
405
406 /*
407 * If buf ends with suffix, return 1 and subtract the length of the suffix
408 * from *len. Otherwise, return 0 and leave *len untouched.
409 */
410 static inline int strip_suffix_mem(const char *buf, size_t *len,
411 const char *suffix)
412 {
413 size_t suflen = strlen(suffix);
414 if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
415 return 0;
416 *len -= suflen;
417 return 1;
418 }
419
420 /*
421 * If str ends with suffix, return 1 and set *len to the size of the string
422 * without the suffix. Otherwise, return 0 and set *len to the size of the
423 * string.
424 *
425 * Note that we do _not_ NUL-terminate str to the new length.
426 */
427 static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
428 {
429 *len = strlen(str);
430 return strip_suffix_mem(str, len, suffix);
431 }
432
433 static inline int ends_with(const char *str, const char *suffix)
434 {
435 size_t len;
436 return strip_suffix(str, suffix, &len);
437 }
438
439 #if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
440
441 #ifndef PROT_READ
442 #define PROT_READ 1
443 #define PROT_WRITE 2
444 #define MAP_PRIVATE 1
445 #endif
446
447 #define mmap git_mmap
448 #define munmap git_munmap
449 extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
450 extern int git_munmap(void *start, size_t length);
451
452 #else /* NO_MMAP || USE_WIN32_MMAP */
453
454 #include <sys/mman.h>
455
456 #endif /* NO_MMAP || USE_WIN32_MMAP */
457
458 #ifdef NO_MMAP
459
460 /* This value must be multiple of (pagesize * 2) */
461 #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
462
463 #else /* NO_MMAP */
464
465 /* This value must be multiple of (pagesize * 2) */
466 #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
467 (sizeof(void*) >= 8 \
468 ? 1 * 1024 * 1024 * 1024 \
469 : 32 * 1024 * 1024)
470
471 #endif /* NO_MMAP */
472
473 #ifndef MAP_FAILED
474 #define MAP_FAILED ((void *)-1)
475 #endif
476
477 #ifdef NO_ST_BLOCKS_IN_STRUCT_STAT
478 #define on_disk_bytes(st) ((st).st_size)
479 #else
480 #define on_disk_bytes(st) ((st).st_blocks * 512)
481 #endif
482
483 #ifdef NEEDS_MODE_TRANSLATION
484 #undef S_IFMT
485 #undef S_IFREG
486 #undef S_IFDIR
487 #undef S_IFLNK
488 #undef S_IFBLK
489 #undef S_IFCHR
490 #undef S_IFIFO
491 #undef S_IFSOCK
492 #define S_IFMT 0170000
493 #define S_IFREG 0100000
494 #define S_IFDIR 0040000
495 #define S_IFLNK 0120000
496 #define S_IFBLK 0060000
497 #define S_IFCHR 0020000
498 #define S_IFIFO 0010000
499 #define S_IFSOCK 0140000
500 #ifdef stat
501 #undef stat
502 #endif
503 #define stat(path, buf) git_stat(path, buf)
504 extern int git_stat(const char *, struct stat *);
505 #ifdef fstat
506 #undef fstat
507 #endif
508 #define fstat(fd, buf) git_fstat(fd, buf)
509 extern int git_fstat(int, struct stat *);
510 #ifdef lstat
511 #undef lstat
512 #endif
513 #define lstat(path, buf) git_lstat(path, buf)
514 extern int git_lstat(const char *, struct stat *);
515 #endif
516
517 #define DEFAULT_PACKED_GIT_LIMIT \
518 ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
519
520 #ifdef NO_PREAD
521 #define pread git_pread
522 extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
523 #endif
524 /*
525 * Forward decl that will remind us if its twin in cache.h changes.
526 * This function is used in compat/pread.c. But we can't include
527 * cache.h there.
528 */
529 extern ssize_t read_in_full(int fd, void *buf, size_t count);
530
531 #ifdef NO_SETENV
532 #define setenv gitsetenv
533 extern int gitsetenv(const char *, const char *, int);
534 #endif
535
536 #ifdef NO_MKDTEMP
537 #define mkdtemp gitmkdtemp
538 extern char *gitmkdtemp(char *);
539 #endif
540
541 #ifdef NO_MKSTEMPS
542 #define mkstemps gitmkstemps
543 extern int gitmkstemps(char *, int);
544 #endif
545
546 #ifdef NO_UNSETENV
547 #define unsetenv gitunsetenv
548 extern void gitunsetenv(const char *);
549 #endif
550
551 #ifdef NO_STRCASESTR
552 #define strcasestr gitstrcasestr
553 extern char *gitstrcasestr(const char *haystack, const char *needle);
554 #endif
555
556 #ifdef NO_STRLCPY
557 #define strlcpy gitstrlcpy
558 extern size_t gitstrlcpy(char *, const char *, size_t);
559 #endif
560
561 #ifdef NO_STRTOUMAX
562 #define strtoumax gitstrtoumax
563 extern uintmax_t gitstrtoumax(const char *, char **, int);
564 #define strtoimax gitstrtoimax
565 extern intmax_t gitstrtoimax(const char *, char **, int);
566 #endif
567
568 #ifdef NO_HSTRERROR
569 #define hstrerror githstrerror
570 extern const char *githstrerror(int herror);
571 #endif
572
573 #ifdef NO_MEMMEM
574 #define memmem gitmemmem
575 void *gitmemmem(const void *haystack, size_t haystacklen,
576 const void *needle, size_t needlelen);
577 #endif
578
579 #ifdef NO_GETPAGESIZE
580 #define getpagesize() sysconf(_SC_PAGESIZE)
581 #endif
582
583 #ifdef FREAD_READS_DIRECTORIES
584 #ifdef fopen
585 #undef fopen
586 #endif
587 #define fopen(a,b) git_fopen(a,b)
588 extern FILE *git_fopen(const char*, const char*);
589 #endif
590
591 #ifdef SNPRINTF_RETURNS_BOGUS
592 #ifdef snprintf
593 #undef snprintf
594 #endif
595 #define snprintf git_snprintf
596 extern int git_snprintf(char *str, size_t maxsize,
597 const char *format, ...);
598 #ifdef vsnprintf
599 #undef vsnprintf
600 #endif
601 #define vsnprintf git_vsnprintf
602 extern int git_vsnprintf(char *str, size_t maxsize,
603 const char *format, va_list ap);
604 #endif
605
606 #ifdef __GLIBC_PREREQ
607 #if __GLIBC_PREREQ(2, 1)
608 #define HAVE_STRCHRNUL
609 #define HAVE_MEMPCPY
610 #endif
611 #endif
612
613 #ifndef HAVE_STRCHRNUL
614 #define strchrnul gitstrchrnul
615 static inline char *gitstrchrnul(const char *s, int c)
616 {
617 while (*s && *s != c)
618 s++;
619 return (char *)s;
620 }
621 #endif
622
623 #ifndef HAVE_MEMPCPY
624 #define mempcpy gitmempcpy
625 static inline void *gitmempcpy(void *dest, const void *src, size_t n)
626 {
627 return (char *)memcpy(dest, src, n) + n;
628 }
629 #endif
630
631 #ifdef NO_INET_PTON
632 int inet_pton(int af, const char *src, void *dst);
633 #endif
634
635 #ifdef NO_INET_NTOP
636 const char *inet_ntop(int af, const void *src, char *dst, size_t size);
637 #endif
638
639 #ifdef NO_PTHREADS
640 #define atexit git_atexit
641 extern int git_atexit(void (*handler)(void));
642 #endif
643
644 extern void release_pack_memory(size_t);
645
646 typedef void (*try_to_free_t)(size_t);
647 extern try_to_free_t set_try_to_free_routine(try_to_free_t);
648
649 #ifdef HAVE_ALLOCA_H
650 # include <alloca.h>
651 # define xalloca(size) (alloca(size))
652 # define xalloca_free(p) do {} while (0)
653 #else
654 # define xalloca(size) (xmalloc(size))
655 # define xalloca_free(p) (free(p))
656 #endif
657 extern char *xstrdup(const char *str);
658 extern void *xmalloc(size_t size);
659 extern void *xmallocz(size_t size);
660 extern void *xmallocz_gently(size_t size);
661 extern void *xmemdupz(const void *data, size_t len);
662 extern char *xstrndup(const char *str, size_t len);
663 extern void *xrealloc(void *ptr, size_t size);
664 extern void *xcalloc(size_t nmemb, size_t size);
665 extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
666 extern ssize_t xread(int fd, void *buf, size_t len);
667 extern ssize_t xwrite(int fd, const void *buf, size_t len);
668 extern ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
669 extern int xdup(int fd);
670 extern FILE *xfdopen(int fd, const char *mode);
671 extern int xmkstemp(char *template);
672 extern int xmkstemp_mode(char *template, int mode);
673 extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
674 extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
675 extern char *xgetcwd(void);
676
677 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x)))
678
679 static inline char *xstrdup_or_null(const char *str)
680 {
681 return str ? xstrdup(str) : NULL;
682 }
683
684 static inline size_t xsize_t(off_t len)
685 {
686 if (len > (size_t) len)
687 die("Cannot handle files this big");
688 return (size_t)len;
689 }
690
691 /* in ctype.c, for kwset users */
692 extern const unsigned char tolower_trans_tbl[256];
693
694 /* Sane ctype - no locale, and works with signed chars */
695 #undef isascii
696 #undef isspace
697 #undef isdigit
698 #undef isalpha
699 #undef isalnum
700 #undef isprint
701 #undef islower
702 #undef isupper
703 #undef tolower
704 #undef toupper
705 #undef iscntrl
706 #undef ispunct
707 #undef isxdigit
708
709 extern const unsigned char sane_ctype[256];
710 #define GIT_SPACE 0x01
711 #define GIT_DIGIT 0x02
712 #define GIT_ALPHA 0x04
713 #define GIT_GLOB_SPECIAL 0x08
714 #define GIT_REGEX_SPECIAL 0x10
715 #define GIT_PATHSPEC_MAGIC 0x20
716 #define GIT_CNTRL 0x40
717 #define GIT_PUNCT 0x80
718 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
719 #define isascii(x) (((x) & ~0x7f) == 0)
720 #define isspace(x) sane_istest(x,GIT_SPACE)
721 #define isdigit(x) sane_istest(x,GIT_DIGIT)
722 #define isalpha(x) sane_istest(x,GIT_ALPHA)
723 #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
724 #define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
725 #define islower(x) sane_iscase(x, 1)
726 #define isupper(x) sane_iscase(x, 0)
727 #define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
728 #define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
729 #define iscntrl(x) (sane_istest(x,GIT_CNTRL))
730 #define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \
731 GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC)
732 #define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
733 #define tolower(x) sane_case((unsigned char)(x), 0x20)
734 #define toupper(x) sane_case((unsigned char)(x), 0)
735 #define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
736
737 static inline int sane_case(int x, int high)
738 {
739 if (sane_istest(x, GIT_ALPHA))
740 x = (x & ~0x20) | high;
741 return x;
742 }
743
744 static inline int sane_iscase(int x, int is_lower)
745 {
746 if (!sane_istest(x, GIT_ALPHA))
747 return 0;
748
749 if (is_lower)
750 return (x & 0x20) != 0;
751 else
752 return (x & 0x20) == 0;
753 }
754
755 static inline int strtoul_ui(char const *s, int base, unsigned int *result)
756 {
757 unsigned long ul;
758 char *p;
759
760 errno = 0;
761 ul = strtoul(s, &p, base);
762 if (errno || *p || p == s || (unsigned int) ul != ul)
763 return -1;
764 *result = ul;
765 return 0;
766 }
767
768 static inline int strtol_i(char const *s, int base, int *result)
769 {
770 long ul;
771 char *p;
772
773 errno = 0;
774 ul = strtol(s, &p, base);
775 if (errno || *p || p == s || (int) ul != ul)
776 return -1;
777 *result = ul;
778 return 0;
779 }
780
781 #ifdef INTERNAL_QSORT
782 void git_qsort(void *base, size_t nmemb, size_t size,
783 int(*compar)(const void *, const void *));
784 #define qsort git_qsort
785 #endif
786
787 #ifndef DIR_HAS_BSD_GROUP_SEMANTICS
788 # define FORCE_DIR_SET_GID S_ISGID
789 #else
790 # define FORCE_DIR_SET_GID 0
791 #endif
792
793 #ifdef NO_NSEC
794 #undef USE_NSEC
795 #define ST_CTIME_NSEC(st) 0
796 #define ST_MTIME_NSEC(st) 0
797 #else
798 #ifdef USE_ST_TIMESPEC
799 #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))
800 #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))
801 #else
802 #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))
803 #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))
804 #endif
805 #endif
806
807 #ifdef UNRELIABLE_FSTAT
808 #define fstat_is_reliable() 0
809 #else
810 #define fstat_is_reliable() 1
811 #endif
812
813 #ifndef va_copy
814 /*
815 * Since an obvious implementation of va_list would be to make it a
816 * pointer into the stack frame, a simple assignment will work on
817 * many systems. But let's try to be more portable.
818 */
819 #ifdef __va_copy
820 #define va_copy(dst, src) __va_copy(dst, src)
821 #else
822 #define va_copy(dst, src) ((dst) = (src))
823 #endif
824 #endif
825
826 #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__C99_MACRO_WITH_VA_ARGS)
827 #define HAVE_VARIADIC_MACROS 1
828 #endif
829
830 /*
831 * Preserves errno, prints a message, but gives no warning for ENOENT.
832 * Returns 0 on success, which includes trying to unlink an object that does
833 * not exist.
834 */
835 int unlink_or_warn(const char *path);
836 /*
837 * Tries to unlink file. Returns 0 if unlink succeeded
838 * or the file already didn't exist. Returns -1 and
839 * appends a message to err suitable for
840 * 'error("%s", err->buf)' on error.
841 */
842 int unlink_or_msg(const char *file, struct strbuf *err);
843 /*
844 * Preserves errno, prints a message, but gives no warning for ENOENT.
845 * Returns 0 on success, which includes trying to remove a directory that does
846 * not exist.
847 */
848 int rmdir_or_warn(const char *path);
849 /*
850 * Calls the correct function out of {unlink,rmdir}_or_warn based on
851 * the supplied file mode.
852 */
853 int remove_or_warn(unsigned int mode, const char *path);
854
855 /*
856 * Call access(2), but warn for any error except "missing file"
857 * (ENOENT or ENOTDIR).
858 */
859 #define ACCESS_EACCES_OK (1U << 0)
860 int access_or_warn(const char *path, int mode, unsigned flag);
861 int access_or_die(const char *path, int mode, unsigned flag);
862
863 /* Warn on an inaccessible file that ought to be accessible */
864 void warn_on_inaccessible(const char *path);
865
866 /* Get the passwd entry for the UID of the current process. */
867 struct passwd *xgetpwuid_self(void);
868
869 #ifdef GMTIME_UNRELIABLE_ERRORS
870 struct tm *git_gmtime(const time_t *);
871 struct tm *git_gmtime_r(const time_t *, struct tm *);
872 #define gmtime git_gmtime
873 #define gmtime_r git_gmtime_r
874 #endif
875
876 #if !defined(USE_PARENS_AROUND_GETTEXT_N) && defined(__GNUC__)
877 #define USE_PARENS_AROUND_GETTEXT_N 1
878 #endif
879
880 #endif