The seventh batch
[alt-git.git] / git-compat-util.h
blobd43dd248c4b8cba4921d69dce683f97784b108fd
1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
4 #if __STDC_VERSION__ - 0 < 199901L
5 /*
6 * Git is in a testing period for mandatory C99 support in the compiler. If
7 * your compiler is reasonably recent, you can try to enable C99 support (or,
8 * for MSVC, C11 support). If you encounter a problem and can't enable C99
9 * support with your compiler (such as with "-std=gnu99") and don't have access
10 * to one with this support, such as GCC or Clang, you can remove this #if
11 * directive, but please report the details of your system to
12 * git@vger.kernel.org.
14 #error "Required C99 support is in a test phase. Please see git-compat-util.h for more details."
15 #endif
17 #ifdef USE_MSVC_CRTDBG
19 * For these to work they must appear very early in each
20 * file -- before most of the standard header files.
22 #include <stdlib.h>
23 #include <crtdbg.h>
24 #endif
26 struct strbuf;
29 #define _FILE_OFFSET_BITS 64
32 /* Derived from Linux "Features Test Macro" header
33 * Convenience macros to test the versions of gcc (or
34 * a compatible compiler).
35 * Use them like this:
36 * #if GIT_GNUC_PREREQ (2,8)
37 * ... code requiring gcc 2.8 or later ...
38 * #endif
40 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
41 # define GIT_GNUC_PREREQ(maj, min) \
42 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
43 #else
44 #define GIT_GNUC_PREREQ(maj, min) 0
45 #endif
47 #if defined(__GNUC__) || defined(__clang__)
48 # define PRAGMA(pragma) _Pragma(#pragma)
49 # define DISABLE_WARNING(warning) PRAGMA(GCC diagnostic ignored #warning)
50 #else
51 # define DISABLE_WARNING(warning)
52 #endif
54 #ifdef DISABLE_SIGN_COMPARE_WARNINGS
55 DISABLE_WARNING(-Wsign-compare)
56 #endif
58 #ifndef FLEX_ARRAY
60 * See if our compiler is known to support flexible array members.
64 * Check vendor specific quirks first, before checking the
65 * __STDC_VERSION__, as vendor compilers can lie and we need to be
66 * able to work them around. Note that by not defining FLEX_ARRAY
67 * here, we can fall back to use the "safer but a bit wasteful" one
68 * later.
70 #if defined(__SUNPRO_C) && (__SUNPRO_C <= 0x580)
71 #elif defined(__GNUC__)
72 # if (__GNUC__ >= 3)
73 # define FLEX_ARRAY /* empty */
74 # else
75 # define FLEX_ARRAY 0 /* older GNU extension */
76 # endif
77 #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
78 # define FLEX_ARRAY /* empty */
79 #endif
82 * Otherwise, default to safer but a bit wasteful traditional style
84 #ifndef FLEX_ARRAY
85 # define FLEX_ARRAY 1
86 #endif
87 #endif
91 * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.
92 * @cond: the compile-time condition which must be true.
94 * Your compile will fail if the condition isn't true, or can't be evaluated
95 * by the compiler. This can be used in an expression: its value is "0".
97 * Example:
98 * #define foo_to_char(foo) \
99 * ((char *)(foo) \
100 * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0))
102 #define BUILD_ASSERT_OR_ZERO(cond) \
103 (sizeof(char [1 - 2*!(cond)]) - 1)
105 #if GIT_GNUC_PREREQ(3, 1)
106 /* &arr[0] degrades to a pointer: a different type from an array */
107 # define BARF_UNLESS_AN_ARRAY(arr) \
108 BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(__typeof__(arr), \
109 __typeof__(&(arr)[0])))
110 # define BARF_UNLESS_COPYABLE(dst, src) \
111 BUILD_ASSERT_OR_ZERO(__builtin_types_compatible_p(__typeof__(*(dst)), \
112 __typeof__(*(src))))
113 #else
114 # define BARF_UNLESS_AN_ARRAY(arr) 0
115 # define BARF_UNLESS_COPYABLE(dst, src) \
116 BUILD_ASSERT_OR_ZERO(0 ? ((*(dst) = *(src)), 0) : \
117 sizeof(*(dst)) == sizeof(*(src)))
118 #endif
120 * ARRAY_SIZE - get the number of elements in a visible array
121 * @x: the array whose size you want.
123 * This does not work on pointers, or arrays declared as [], or
124 * function parameters. With correct compiler support, such usage
125 * will cause a build error (see the build_assert_or_zero macro).
127 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + BARF_UNLESS_AN_ARRAY(x))
129 #define bitsizeof(x) (CHAR_BIT * sizeof(x))
131 #define maximum_signed_value_of_type(a) \
132 (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a)))
134 #define maximum_unsigned_value_of_type(a) \
135 (UINTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a)))
138 * Signed integer overflow is undefined in C, so here's a helper macro
139 * to detect if the sum of two integers will overflow.
141 * Requires: a >= 0, typeof(a) equals typeof(b)
143 #define signed_add_overflows(a, b) \
144 ((b) > maximum_signed_value_of_type(a) - (a))
146 #define unsigned_add_overflows(a, b) \
147 ((b) > maximum_unsigned_value_of_type(a) - (a))
150 * Returns true if the multiplication of "a" and "b" will
151 * overflow. The types of "a" and "b" must match and must be unsigned.
152 * Note that this macro evaluates "a" twice!
154 #define unsigned_mult_overflows(a, b) \
155 ((a) && (b) > maximum_unsigned_value_of_type(a) / (a))
158 * Returns true if the left shift of "a" by "shift" bits will
159 * overflow. The type of "a" must be unsigned.
161 #define unsigned_left_shift_overflows(a, shift) \
162 ((shift) < bitsizeof(a) && \
163 (a) > maximum_unsigned_value_of_type(a) >> (shift))
165 #ifdef __GNUC__
166 #define TYPEOF(x) (__typeof__(x))
167 #else
168 #define TYPEOF(x)
169 #endif
171 #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (bitsizeof(x) - (bits))))
172 #define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
174 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
176 /* Approximation of the length of the decimal representation of this type. */
177 #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
179 #ifdef __MINGW64__
180 #define _POSIX_C_SOURCE 1
181 #elif defined(__sun__)
183 * On Solaris, when _XOPEN_EXTENDED is set, its header file
184 * forces the programs to be XPG4v2, defeating any _XOPEN_SOURCE
185 * setting to say we are XPG5 or XPG6. Also on Solaris,
186 * XPG6 programs must be compiled with a c99 compiler, while
187 * non XPG6 programs must be compiled with a pre-c99 compiler.
189 # if __STDC_VERSION__ - 0 >= 199901L
190 # define _XOPEN_SOURCE 600
191 # else
192 # define _XOPEN_SOURCE 500
193 # endif
194 #elif !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && \
195 !defined(_M_UNIX) && !defined(__sgi) && !defined(__DragonFly__) && \
196 !defined(__TANDEM) && !defined(__QNX__) && !defined(__MirBSD__) && \
197 !defined(__CYGWIN__)
198 #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
199 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
200 #endif
201 #define _ALL_SOURCE 1
202 #define _GNU_SOURCE 1
203 #define _BSD_SOURCE 1
204 #define _DEFAULT_SOURCE 1
205 #define _NETBSD_SOURCE 1
206 #define _SGI_SOURCE 1
209 * UNUSED marks a function parameter that is always unused. It also
210 * can be used to annotate a function, a variable, or a type that is
211 * always unused.
213 * A callback interface may dictate that a function accepts a
214 * parameter at that position, but the implementation of the function
215 * may not need to use the parameter. In such a case, mark the parameter
216 * with UNUSED.
218 * When a parameter may be used or unused, depending on conditional
219 * compilation, consider using MAYBE_UNUSED instead.
221 #if GIT_GNUC_PREREQ(4, 5)
222 #define UNUSED __attribute__((unused)) \
223 __attribute__((deprecated ("parameter declared as UNUSED")))
224 #elif defined(__GNUC__)
225 #define UNUSED __attribute__((unused)) \
226 __attribute__((deprecated))
227 #else
228 #define UNUSED
229 #endif
231 #if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */
232 # if !defined(_WIN32_WINNT)
233 # define _WIN32_WINNT 0x0600
234 # endif
235 #define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */
236 #include <winsock2.h>
237 #ifndef NO_UNIX_SOCKETS
238 #include <afunix.h>
239 #endif
240 #include <windows.h>
241 #define GIT_WINDOWS_NATIVE
242 #endif
244 #if defined(NO_UNIX_SOCKETS) || !defined(GIT_WINDOWS_NATIVE)
245 static inline int _have_unix_sockets(void)
247 #if defined(NO_UNIX_SOCKETS)
248 return 0;
249 #else
250 return 1;
251 #endif
253 #define have_unix_sockets _have_unix_sockets
254 #endif
256 #include <unistd.h>
257 #include <stdio.h>
258 #include <sys/stat.h>
259 #include <fcntl.h>
260 #include <stddef.h>
261 #include <stdlib.h>
262 #include <stdarg.h>
263 #include <stdbool.h>
264 #include <string.h>
265 #ifdef HAVE_STRINGS_H
266 #include <strings.h> /* for strcasecmp() */
267 #endif
268 #include <errno.h>
269 #include <limits.h>
270 #include <locale.h>
271 #ifdef NEEDS_SYS_PARAM_H
272 #include <sys/param.h>
273 #endif
274 #include <sys/types.h>
275 #include <dirent.h>
276 #include <sys/time.h>
277 #include <time.h>
278 #include <signal.h>
279 #include <assert.h>
280 #include <regex.h>
281 #include <utime.h>
282 #include <syslog.h>
283 #if !defined(NO_POLL_H)
284 #include <poll.h>
285 #elif !defined(NO_SYS_POLL_H)
286 #include <sys/poll.h>
287 #else
288 /* Pull the compat stuff */
289 #include <poll.h>
290 #endif
291 #ifdef HAVE_BSD_SYSCTL
292 #include <sys/sysctl.h>
293 #endif
295 /* Used by compat/win32/path-utils.h, and more */
296 static inline int is_xplatform_dir_sep(int c)
298 return c == '/' || c == '\\';
301 #if defined(__CYGWIN__)
302 #include "compat/win32/path-utils.h"
303 #endif
304 #if defined(__MINGW32__)
305 /* pull in Windows compatibility stuff */
306 #include "compat/win32/path-utils.h"
307 #include "compat/mingw.h"
308 #elif defined(_MSC_VER)
309 #include "compat/win32/path-utils.h"
310 #include "compat/msvc.h"
311 #else
312 #include <sys/utsname.h>
313 #include <sys/wait.h>
314 #include <sys/resource.h>
315 #include <sys/socket.h>
316 #include <sys/ioctl.h>
317 #include <sys/statvfs.h>
318 #include <termios.h>
319 #ifndef NO_SYS_SELECT_H
320 #include <sys/select.h>
321 #endif
322 #include <netinet/in.h>
323 #include <netinet/tcp.h>
324 #include <arpa/inet.h>
325 #include <netdb.h>
326 #include <pwd.h>
327 #include <sys/un.h>
328 #ifndef NO_INTTYPES_H
329 #include <inttypes.h>
330 #else
331 #include <stdint.h>
332 #endif
333 #ifdef HAVE_ARC4RANDOM_LIBBSD
334 #include <bsd/stdlib.h>
335 #endif
336 #ifdef HAVE_GETRANDOM
337 #include <sys/random.h>
338 #endif
339 #ifdef NO_INTPTR_T
341 * On I16LP32, ILP32 and LP64 "long" is the safe bet, however
342 * on LLP86, IL33LLP64 and P64 it needs to be "long long",
343 * while on IP16 and IP16L32 it is "int" (resp. "short")
344 * Size needs to match (or exceed) 'sizeof(void *)'.
345 * We can't take "long long" here as not everybody has it.
347 typedef long intptr_t;
348 typedef unsigned long uintptr_t;
349 #endif
350 #undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */
351 #include <grp.h>
352 #define _ALL_SOURCE 1
353 #endif
355 /* used on Mac OS X */
356 #ifdef PRECOMPOSE_UNICODE
357 #include "compat/precompose_utf8.h"
358 #else
359 static inline const char *precompose_argv_prefix(int argc UNUSED,
360 const char **argv UNUSED,
361 const char *prefix)
363 return prefix;
365 static inline const char *precompose_string_if_needed(const char *in)
367 return in;
370 #define probe_utf8_pathname_composition()
371 #endif
373 #ifdef MKDIR_WO_TRAILING_SLASH
374 #define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))
375 int compat_mkdir_wo_trailing_slash(const char*, mode_t);
376 #endif
378 #ifdef time
379 #undef time
380 #endif
381 static inline time_t git_time(time_t *tloc)
383 struct timeval tv;
386 * Avoid time(NULL), which can disagree with gettimeofday(2)
387 * and filesystem timestamps.
389 gettimeofday(&tv, NULL);
391 if (tloc)
392 *tloc = tv.tv_sec;
393 return tv.tv_sec;
395 #define time git_time
397 #ifdef NO_STRUCT_ITIMERVAL
398 struct itimerval {
399 struct timeval it_interval;
400 struct timeval it_value;
402 #endif
404 #ifdef NO_SETITIMER
405 static inline int git_setitimer(int which UNUSED,
406 const struct itimerval *value UNUSED,
407 struct itimerval *newvalue UNUSED) {
408 return 0; /* pretend success */
410 #undef setitimer
411 #define setitimer(which,value,ovalue) git_setitimer(which,value,ovalue)
412 #endif
414 #ifndef NO_LIBGEN_H
415 #include <libgen.h>
416 #else
417 #define basename gitbasename
418 char *gitbasename(char *);
419 #define dirname gitdirname
420 char *gitdirname(char *);
421 #endif
423 #ifndef NO_ICONV
424 #include <iconv.h>
425 #endif
427 #ifndef NO_OPENSSL
428 #ifdef __APPLE__
429 #undef __AVAILABILITY_MACROS_USES_AVAILABILITY
430 #define __AVAILABILITY_MACROS_USES_AVAILABILITY 0
431 #include <AvailabilityMacros.h>
432 #undef DEPRECATED_ATTRIBUTE
433 #define DEPRECATED_ATTRIBUTE
434 #undef __AVAILABILITY_MACROS_USES_AVAILABILITY
435 #endif
436 #include <openssl/ssl.h>
437 #include <openssl/err.h>
438 #endif
440 #ifdef HAVE_SYSINFO
441 # include <sys/sysinfo.h>
442 #endif
444 /* On most systems <netdb.h> would have given us this, but
445 * not on some systems (e.g. z/OS).
447 #ifndef NI_MAXHOST
448 #define NI_MAXHOST 1025
449 #endif
451 #ifndef NI_MAXSERV
452 #define NI_MAXSERV 32
453 #endif
455 /* On most systems <limits.h> would have given us this, but
456 * not on some systems (e.g. GNU/Hurd).
458 #ifndef PATH_MAX
459 #define PATH_MAX 4096
460 #endif
462 #ifndef NAME_MAX
463 #define NAME_MAX 255
464 #endif
466 typedef uintmax_t timestamp_t;
467 #define PRItime PRIuMAX
468 #define parse_timestamp strtoumax
469 #define TIME_MAX UINTMAX_MAX
470 #define TIME_MIN 0
472 #ifndef PATH_SEP
473 #define PATH_SEP ':'
474 #endif
476 #ifdef HAVE_PATHS_H
477 #include <paths.h>
478 #endif
479 #ifndef _PATH_DEFPATH
480 #define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin"
481 #endif
483 #ifndef platform_core_config
484 struct config_context;
485 static inline int noop_core_config(const char *var UNUSED,
486 const char *value UNUSED,
487 const struct config_context *ctx UNUSED,
488 void *cb UNUSED)
490 return 0;
492 #define platform_core_config noop_core_config
493 #endif
495 int lstat_cache_aware_rmdir(const char *path);
496 #if !defined(__MINGW32__) && !defined(_MSC_VER)
497 #define rmdir lstat_cache_aware_rmdir
498 #endif
500 #ifndef has_dos_drive_prefix
501 static inline int git_has_dos_drive_prefix(const char *path UNUSED)
503 return 0;
505 #define has_dos_drive_prefix git_has_dos_drive_prefix
506 #endif
508 #ifndef skip_dos_drive_prefix
509 static inline int git_skip_dos_drive_prefix(char **path UNUSED)
511 return 0;
513 #define skip_dos_drive_prefix git_skip_dos_drive_prefix
514 #endif
516 static inline int git_is_dir_sep(int c)
518 return c == '/';
520 #ifndef is_dir_sep
521 #define is_dir_sep git_is_dir_sep
522 #endif
524 #ifndef offset_1st_component
525 static inline int git_offset_1st_component(const char *path)
527 return is_dir_sep(path[0]);
529 #define offset_1st_component git_offset_1st_component
530 #endif
532 #ifndef fspathcmp
533 #define fspathcmp git_fspathcmp
534 #endif
536 #ifndef fspathncmp
537 #define fspathncmp git_fspathncmp
538 #endif
540 #ifndef is_valid_path
541 #define is_valid_path(path) 1
542 #endif
544 #ifndef is_path_owned_by_current_user
546 #ifdef __TANDEM
547 #define ROOT_UID 65535
548 #else
549 #define ROOT_UID 0
550 #endif
553 * Do not use this function when
554 * (1) geteuid() did not say we are running as 'root', or
555 * (2) using this function will compromise the system.
557 * PORTABILITY WARNING:
558 * This code assumes uid_t is unsigned because that is what sudo does.
559 * If your uid_t type is signed and all your ids are positive then it
560 * should all work fine.
561 * If your version of sudo uses negative values for uid_t or it is
562 * buggy and return an overflowed value in SUDO_UID, then git might
563 * fail to grant access to your repository properly or even mistakenly
564 * grant access to someone else.
565 * In the unlikely scenario this happened to you, and that is how you
566 * got to this message, we would like to know about it; so sent us an
567 * email to git@vger.kernel.org indicating which platform you are
568 * using and which version of sudo, so we can improve this logic and
569 * maybe provide you with a patch that would prevent this issue again
570 * in the future.
572 static inline void extract_id_from_env(const char *env, uid_t *id)
574 const char *real_uid = getenv(env);
576 /* discard anything empty to avoid a more complex check below */
577 if (real_uid && *real_uid) {
578 char *endptr = NULL;
579 unsigned long env_id;
581 errno = 0;
582 /* silent overflow errors could trigger a bug here */
583 env_id = strtoul(real_uid, &endptr, 10);
584 if (!*endptr && !errno)
585 *id = env_id;
589 static inline int is_path_owned_by_current_uid(const char *path,
590 struct strbuf *report UNUSED)
592 struct stat st;
593 uid_t euid;
595 if (lstat(path, &st))
596 return 0;
598 euid = geteuid();
599 if (euid == ROOT_UID)
601 if (st.st_uid == ROOT_UID)
602 return 1;
603 else
604 extract_id_from_env("SUDO_UID", &euid);
607 return st.st_uid == euid;
610 #define is_path_owned_by_current_user is_path_owned_by_current_uid
611 #endif
613 #ifndef find_last_dir_sep
614 static inline char *git_find_last_dir_sep(const char *path)
616 return strrchr(path, '/');
618 #define find_last_dir_sep git_find_last_dir_sep
619 #endif
621 #ifndef has_dir_sep
622 static inline int git_has_dir_sep(const char *path)
624 return !!strchr(path, '/');
626 #define has_dir_sep(path) git_has_dir_sep(path)
627 #endif
629 #ifndef query_user_email
630 #define query_user_email() NULL
631 #endif
633 #ifdef __TANDEM
634 #include <floss.h(floss_execl,floss_execlp,floss_execv,floss_execvp)>
635 #include <floss.h(floss_getpwuid)>
636 #ifndef NSIG
638 * NonStop NSE and NSX do not provide NSIG. SIGGUARDIAN(99) is the highest
639 * known, by detective work using kill -l as a list is all signals
640 * instead of signal.h where it should be.
642 # define NSIG 100
643 #endif
644 #endif
646 #if defined(__HP_cc) && (__HP_cc >= 61000)
647 #define NORETURN __attribute__((noreturn))
648 #define NORETURN_PTR
649 #elif defined(__GNUC__) && !defined(NO_NORETURN)
650 #define NORETURN __attribute__((__noreturn__))
651 #define NORETURN_PTR __attribute__((__noreturn__))
652 #elif defined(_MSC_VER)
653 #define NORETURN __declspec(noreturn)
654 #define NORETURN_PTR
655 #else
656 #define NORETURN
657 #define NORETURN_PTR
658 #ifndef __GNUC__
659 #ifndef __attribute__
660 #define __attribute__(x)
661 #endif
662 #endif
663 #endif
665 /* The sentinel attribute is valid from gcc version 4.0 */
666 #if defined(__GNUC__) && (__GNUC__ >= 4)
667 #define LAST_ARG_MUST_BE_NULL __attribute__((sentinel))
668 /* warn_unused_result exists as of gcc 3.4.0, but be lazy and check 4.0 */
669 #define RESULT_MUST_BE_USED __attribute__ ((warn_unused_result))
670 #else
671 #define LAST_ARG_MUST_BE_NULL
672 #define RESULT_MUST_BE_USED
673 #endif
676 * MAYBE_UNUSED marks a function parameter that may be unused, but
677 * whose use is not an error. It also can be used to annotate a
678 * function, a variable, or a type that may be unused.
680 * Depending on a configuration, all uses of such a thing may become
681 * #ifdef'ed away. Marking it with UNUSED would give a warning in a
682 * compilation where it is indeed used, and not marking it at all
683 * would give a warning in a compilation where it is unused. In such
684 * a case, MAYBE_UNUSED is the appropriate annotation to use.
686 #define MAYBE_UNUSED __attribute__((__unused__))
688 #include "compat/bswap.h"
690 #include "wrapper.h"
692 /* General helper functions */
693 NORETURN void usage(const char *err);
694 NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
695 NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
696 NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
697 int die_message(const char *err, ...) __attribute__((format (printf, 1, 2)));
698 int die_message_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
699 int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
700 int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
701 void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
702 void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
704 void show_usage_if_asked(int ac, const char **av, const char *err);
706 #ifndef NO_OPENSSL
707 #ifdef APPLE_COMMON_CRYPTO
708 #include "compat/apple-common-crypto.h"
709 #else
710 #include <openssl/evp.h>
711 #include <openssl/hmac.h>
712 #endif /* APPLE_COMMON_CRYPTO */
713 #include <openssl/x509v3.h>
714 #endif /* NO_OPENSSL */
716 #ifdef HAVE_OPENSSL_CSPRNG
717 #include <openssl/rand.h>
718 #endif
721 * Let callers be aware of the constant return value; this can help
722 * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
723 * because other compilers may be confused by this.
725 #if defined(__GNUC__)
726 static inline int const_error(void)
728 return -1;
730 #define error(...) (error(__VA_ARGS__), const_error())
731 #define error_errno(...) (error_errno(__VA_ARGS__), const_error())
732 #endif
734 typedef void (*report_fn)(const char *, va_list params);
736 void set_die_routine(NORETURN_PTR report_fn routine);
737 report_fn get_die_message_routine(void);
738 void set_error_routine(report_fn routine);
739 report_fn get_error_routine(void);
740 void set_warn_routine(report_fn routine);
741 report_fn get_warn_routine(void);
742 void set_die_is_recursing_routine(int (*routine)(void));
745 * If the string "str" begins with the string found in "prefix", return true.
746 * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
747 * the string right after the prefix).
749 * Otherwise, return false and leave "out" untouched.
751 * Examples:
753 * [extract branch name, fail if not a branch]
754 * if (!skip_prefix(ref, "refs/heads/", &branch)
755 * return -1;
757 * [skip prefix if present, otherwise use whole string]
758 * skip_prefix(name, "refs/heads/", &name);
760 static inline bool skip_prefix(const char *str, const char *prefix,
761 const char **out)
763 do {
764 if (!*prefix) {
765 *out = str;
766 return true;
768 } while (*str++ == *prefix++);
769 return false;
773 * Like skip_prefix, but promises never to read past "len" bytes of the input
774 * buffer, and returns the remaining number of bytes in "out" via "outlen".
776 static inline bool skip_prefix_mem(const char *buf, size_t len,
777 const char *prefix,
778 const char **out, size_t *outlen)
780 size_t prefix_len = strlen(prefix);
781 if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) {
782 *out = buf + prefix_len;
783 *outlen = len - prefix_len;
784 return true;
786 return false;
790 * If buf ends with suffix, return true and subtract the length of the suffix
791 * from *len. Otherwise, return false and leave *len untouched.
793 static inline bool strip_suffix_mem(const char *buf, size_t *len,
794 const char *suffix)
796 size_t suflen = strlen(suffix);
797 if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
798 return false;
799 *len -= suflen;
800 return true;
804 * If str ends with suffix, return true and set *len to the size of the string
805 * without the suffix. Otherwise, return false and set *len to the size of the
806 * string.
808 * Note that we do _not_ NUL-terminate str to the new length.
810 static inline bool strip_suffix(const char *str, const char *suffix,
811 size_t *len)
813 *len = strlen(str);
814 return strip_suffix_mem(str, len, suffix);
817 #define SWAP(a, b) do { \
818 void *_swap_a_ptr = &(a); \
819 void *_swap_b_ptr = &(b); \
820 unsigned char _swap_buffer[sizeof(a)]; \
821 memcpy(_swap_buffer, _swap_a_ptr, sizeof(a)); \
822 memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) + \
823 BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \
824 memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \
825 } while (0)
827 #if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
829 #ifndef PROT_READ
830 #define PROT_READ 1
831 #define PROT_WRITE 2
832 #define MAP_PRIVATE 1
833 #endif
835 #define mmap git_mmap
836 #define munmap git_munmap
837 void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
838 int git_munmap(void *start, size_t length);
840 #else /* NO_MMAP || USE_WIN32_MMAP */
842 #include <sys/mman.h>
844 #endif /* NO_MMAP || USE_WIN32_MMAP */
846 #ifdef NO_MMAP
848 /* This value must be multiple of (pagesize * 2) */
849 #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
851 #else /* NO_MMAP */
853 /* This value must be multiple of (pagesize * 2) */
854 #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
855 (sizeof(void*) >= 8 \
856 ? 1 * 1024 * 1024 * 1024 \
857 : 32 * 1024 * 1024)
859 #endif /* NO_MMAP */
861 #ifndef MAP_FAILED
862 #define MAP_FAILED ((void *)-1)
863 #endif
865 #ifdef NO_ST_BLOCKS_IN_STRUCT_STAT
866 #define on_disk_bytes(st) ((st).st_size)
867 #else
868 #define on_disk_bytes(st) ((st).st_blocks * 512)
869 #endif
871 #ifdef NEEDS_MODE_TRANSLATION
872 #undef S_IFMT
873 #undef S_IFREG
874 #undef S_IFDIR
875 #undef S_IFLNK
876 #undef S_IFBLK
877 #undef S_IFCHR
878 #undef S_IFIFO
879 #undef S_IFSOCK
880 #define S_IFMT 0170000
881 #define S_IFREG 0100000
882 #define S_IFDIR 0040000
883 #define S_IFLNK 0120000
884 #define S_IFBLK 0060000
885 #define S_IFCHR 0020000
886 #define S_IFIFO 0010000
887 #define S_IFSOCK 0140000
888 #ifdef stat
889 #undef stat
890 #endif
891 #define stat(path, buf) git_stat(path, buf)
892 int git_stat(const char *, struct stat *);
893 #ifdef fstat
894 #undef fstat
895 #endif
896 #define fstat(fd, buf) git_fstat(fd, buf)
897 int git_fstat(int, struct stat *);
898 #ifdef lstat
899 #undef lstat
900 #endif
901 #define lstat(path, buf) git_lstat(path, buf)
902 int git_lstat(const char *, struct stat *);
903 #endif
905 #define DEFAULT_PACKED_GIT_LIMIT \
906 ((1024L * 1024L) * (size_t)(sizeof(void*) >= 8 ? (32 * 1024L * 1024L) : 256))
908 #ifdef NO_PREAD
909 #define pread git_pread
910 ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
911 #endif
913 #ifdef NO_SETENV
914 #define setenv gitsetenv
915 int gitsetenv(const char *, const char *, int);
916 #endif
918 #ifdef NO_MKDTEMP
919 #define mkdtemp gitmkdtemp
920 char *gitmkdtemp(char *);
921 #endif
923 #ifdef NO_UNSETENV
924 #define unsetenv gitunsetenv
925 int gitunsetenv(const char *);
926 #endif
928 #ifdef NO_STRCASESTR
929 #define strcasestr gitstrcasestr
930 char *gitstrcasestr(const char *haystack, const char *needle);
931 #endif
933 #ifdef NO_STRLCPY
934 #define strlcpy gitstrlcpy
935 size_t gitstrlcpy(char *, const char *, size_t);
936 #endif
938 #ifdef NO_STRTOUMAX
939 #define strtoumax gitstrtoumax
940 uintmax_t gitstrtoumax(const char *, char **, int);
941 #define strtoimax gitstrtoimax
942 intmax_t gitstrtoimax(const char *, char **, int);
943 #endif
945 #ifdef NO_HSTRERROR
946 #define hstrerror githstrerror
947 const char *githstrerror(int herror);
948 #endif
950 #ifdef NO_MEMMEM
951 #define memmem gitmemmem
952 void *gitmemmem(const void *haystack, size_t haystacklen,
953 const void *needle, size_t needlelen);
954 #endif
956 #ifdef OVERRIDE_STRDUP
957 #ifdef strdup
958 #undef strdup
959 #endif
960 #define strdup gitstrdup
961 char *gitstrdup(const char *s);
962 #endif
964 #ifdef NO_GETPAGESIZE
965 #define getpagesize() sysconf(_SC_PAGESIZE)
966 #endif
968 #ifndef O_CLOEXEC
969 #define O_CLOEXEC 0
970 #endif
972 #ifdef FREAD_READS_DIRECTORIES
973 # if !defined(SUPPRESS_FOPEN_REDEFINITION)
974 # ifdef fopen
975 # undef fopen
976 # endif
977 # define fopen(a,b) git_fopen(a,b)
978 # endif
979 FILE *git_fopen(const char*, const char*);
980 #endif
982 #ifdef SNPRINTF_RETURNS_BOGUS
983 #ifdef snprintf
984 #undef snprintf
985 #endif
986 #define snprintf git_snprintf
987 int git_snprintf(char *str, size_t maxsize,
988 const char *format, ...);
989 #ifdef vsnprintf
990 #undef vsnprintf
991 #endif
992 #define vsnprintf git_vsnprintf
993 int git_vsnprintf(char *str, size_t maxsize,
994 const char *format, va_list ap);
995 #endif
997 #ifdef OPEN_RETURNS_EINTR
998 #undef open
999 #define open git_open_with_retry
1000 int git_open_with_retry(const char *path, int flag, ...);
1001 #endif
1003 #ifdef __GLIBC_PREREQ
1004 #if __GLIBC_PREREQ(2, 1)
1005 #define HAVE_STRCHRNUL
1006 #endif
1007 #endif
1009 #ifndef HAVE_STRCHRNUL
1010 #define strchrnul gitstrchrnul
1011 static inline char *gitstrchrnul(const char *s, int c)
1013 while (*s && *s != c)
1014 s++;
1015 return (char *)s;
1017 #endif
1019 #ifdef NO_INET_PTON
1020 int inet_pton(int af, const char *src, void *dst);
1021 #endif
1023 #ifdef NO_INET_NTOP
1024 const char *inet_ntop(int af, const void *src, char *dst, size_t size);
1025 #endif
1027 #ifdef NO_PTHREADS
1028 #define atexit git_atexit
1029 int git_atexit(void (*handler)(void));
1030 #endif
1032 static inline size_t st_add(size_t a, size_t b)
1034 if (unsigned_add_overflows(a, b))
1035 die("size_t overflow: %"PRIuMAX" + %"PRIuMAX,
1036 (uintmax_t)a, (uintmax_t)b);
1037 return a + b;
1039 #define st_add3(a,b,c) st_add(st_add((a),(b)),(c))
1040 #define st_add4(a,b,c,d) st_add(st_add3((a),(b),(c)),(d))
1042 static inline size_t st_mult(size_t a, size_t b)
1044 if (unsigned_mult_overflows(a, b))
1045 die("size_t overflow: %"PRIuMAX" * %"PRIuMAX,
1046 (uintmax_t)a, (uintmax_t)b);
1047 return a * b;
1050 static inline size_t st_sub(size_t a, size_t b)
1052 if (a < b)
1053 die("size_t underflow: %"PRIuMAX" - %"PRIuMAX,
1054 (uintmax_t)a, (uintmax_t)b);
1055 return a - b;
1058 static inline size_t st_left_shift(size_t a, unsigned shift)
1060 if (unsigned_left_shift_overflows(a, shift))
1061 die("size_t overflow: %"PRIuMAX" << %u",
1062 (uintmax_t)a, shift);
1063 return a << shift;
1066 static inline unsigned long cast_size_t_to_ulong(size_t a)
1068 if (a != (unsigned long)a)
1069 die("object too large to read on this platform: %"
1070 PRIuMAX" is cut off to %lu",
1071 (uintmax_t)a, (unsigned long)a);
1072 return (unsigned long)a;
1075 static inline uint32_t cast_size_t_to_uint32_t(size_t a)
1077 if (a != (uint32_t)a)
1078 die("object too large to read on this platform: %"
1079 PRIuMAX" is cut off to %u",
1080 (uintmax_t)a, (uint32_t)a);
1081 return (uint32_t)a;
1084 static inline int cast_size_t_to_int(size_t a)
1086 if (a > INT_MAX)
1087 die("number too large to represent as int on this platform: %"PRIuMAX,
1088 (uintmax_t)a);
1089 return (int)a;
1093 * Limit size of IO chunks, because huge chunks only cause pain. OS X
1094 * 64-bit is buggy, returning EINVAL if len >= INT_MAX; and even in
1095 * the absence of bugs, large chunks can result in bad latencies when
1096 * you decide to kill the process.
1098 * We pick 8 MiB as our default, but if the platform defines SSIZE_MAX
1099 * that is smaller than that, clip it to SSIZE_MAX, as a call to
1100 * read(2) or write(2) larger than that is allowed to fail. As the last
1101 * resort, we allow a port to pass via CFLAGS e.g. "-DMAX_IO_SIZE=value"
1102 * to override this, if the definition of SSIZE_MAX given by the platform
1103 * is broken.
1105 #ifndef MAX_IO_SIZE
1106 # define MAX_IO_SIZE_DEFAULT (8*1024*1024)
1107 # if defined(SSIZE_MAX) && (SSIZE_MAX < MAX_IO_SIZE_DEFAULT)
1108 # define MAX_IO_SIZE SSIZE_MAX
1109 # else
1110 # define MAX_IO_SIZE MAX_IO_SIZE_DEFAULT
1111 # endif
1112 #endif
1114 #ifdef HAVE_ALLOCA_H
1115 # include <alloca.h>
1116 # define xalloca(size) (alloca(size))
1117 # define xalloca_free(p) do {} while (0)
1118 #else
1119 # define xalloca(size) (xmalloc(size))
1120 # define xalloca_free(p) (free(p))
1121 #endif
1124 * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
1125 * that ptr is used twice, so don't pass e.g. ptr++.
1127 #define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0)
1129 #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
1130 #define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
1131 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
1133 #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
1134 BARF_UNLESS_COPYABLE((dst), (src)))
1135 static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
1137 if (n)
1138 memcpy(dst, src, st_mult(size, n));
1141 #define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
1142 BARF_UNLESS_COPYABLE((dst), (src)))
1143 static inline void move_array(void *dst, const void *src, size_t n, size_t size)
1145 if (n)
1146 memmove(dst, src, st_mult(size, n));
1149 #define DUP_ARRAY(dst, src, n) do { \
1150 size_t dup_array_n_ = (n); \
1151 COPY_ARRAY(ALLOC_ARRAY((dst), dup_array_n_), (src), dup_array_n_); \
1152 } while (0)
1155 * These functions help you allocate structs with flex arrays, and copy
1156 * the data directly into the array. For example, if you had:
1158 * struct foo {
1159 * int bar;
1160 * char name[FLEX_ARRAY];
1161 * };
1163 * you can do:
1165 * struct foo *f;
1166 * FLEX_ALLOC_MEM(f, name, src, len);
1168 * to allocate a "foo" with the contents of "src" in the "name" field.
1169 * The resulting struct is automatically zero'd, and the flex-array field
1170 * is NUL-terminated (whether the incoming src buffer was or not).
1172 * The FLEXPTR_* variants operate on structs that don't use flex-arrays,
1173 * but do want to store a pointer to some extra data in the same allocated
1174 * block. For example, if you have:
1176 * struct foo {
1177 * char *name;
1178 * int bar;
1179 * };
1181 * you can do:
1183 * struct foo *f;
1184 * FLEXPTR_ALLOC_STR(f, name, src);
1186 * and "name" will point to a block of memory after the struct, which will be
1187 * freed along with the struct (but the pointer can be repointed anywhere).
1189 * The *_STR variants accept a string parameter rather than a ptr/len
1190 * combination.
1192 * Note that these macros will evaluate the first parameter multiple
1193 * times, and it must be assignable as an lvalue.
1195 #define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
1196 size_t flex_array_len_ = (len); \
1197 (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
1198 memcpy((void *)(x)->flexname, (buf), flex_array_len_); \
1199 } while (0)
1200 #define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
1201 size_t flex_array_len_ = (len); \
1202 (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
1203 memcpy((x) + 1, (buf), flex_array_len_); \
1204 (x)->ptrname = (void *)((x)+1); \
1205 } while(0)
1206 #define FLEX_ALLOC_STR(x, flexname, str) \
1207 FLEX_ALLOC_MEM((x), flexname, (str), strlen(str))
1208 #define FLEXPTR_ALLOC_STR(x, ptrname, str) \
1209 FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
1211 #define alloc_nr(x) (((x)+16)*3/2)
1214 * Dynamically growing an array using realloc() is error prone and boring.
1216 * Define your array with:
1218 * - a pointer (`item`) that points at the array, initialized to `NULL`
1219 * (although please name the variable based on its contents, not on its
1220 * type);
1222 * - an integer variable (`alloc`) that keeps track of how big the current
1223 * allocation is, initialized to `0`;
1225 * - another integer variable (`nr`) to keep track of how many elements the
1226 * array currently has, initialized to `0`.
1228 * Then before adding `n`th element to the item, call `ALLOC_GROW(item, n,
1229 * alloc)`. This ensures that the array can hold at least `n` elements by
1230 * calling `realloc(3)` and adjusting `alloc` variable.
1232 * ------------
1233 * sometype *item;
1234 * size_t nr;
1235 * size_t alloc
1237 * for (i = 0; i < nr; i++)
1238 * if (we like item[i] already)
1239 * return;
1241 * // we did not like any existing one, so add one
1242 * ALLOC_GROW(item, nr + 1, alloc);
1243 * item[nr++] = value you like;
1244 * ------------
1246 * You are responsible for updating the `nr` variable.
1248 * If you need to specify the number of elements to allocate explicitly
1249 * then use the macro `REALLOC_ARRAY(item, alloc)` instead of `ALLOC_GROW`.
1251 * Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
1252 * added niceties.
1254 * DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
1256 #define ALLOC_GROW(x, nr, alloc) \
1257 do { \
1258 if ((nr) > alloc) { \
1259 if (alloc_nr(alloc) < (nr)) \
1260 alloc = (nr); \
1261 else \
1262 alloc = alloc_nr(alloc); \
1263 REALLOC_ARRAY(x, alloc); \
1265 } while (0)
1268 * Similar to ALLOC_GROW but handles updating of the nr value and
1269 * zeroing the bytes of the newly-grown array elements.
1271 * DO NOT USE any expression with side-effect for any of the
1272 * arguments.
1274 #define ALLOC_GROW_BY(x, nr, increase, alloc) \
1275 do { \
1276 if (increase) { \
1277 size_t new_nr = nr + (increase); \
1278 if (new_nr < nr) \
1279 BUG("negative growth in ALLOC_GROW_BY"); \
1280 ALLOC_GROW(x, new_nr, alloc); \
1281 memset((x) + nr, 0, sizeof(*(x)) * (increase)); \
1282 nr = new_nr; \
1284 } while (0)
1286 static inline char *xstrdup_or_null(const char *str)
1288 return str ? xstrdup(str) : NULL;
1291 static inline size_t xsize_t(off_t len)
1293 if (len < 0 || (uintmax_t) len > SIZE_MAX)
1294 die("Cannot handle files this big");
1295 return (size_t) len;
1298 #ifndef HOST_NAME_MAX
1299 #define HOST_NAME_MAX 256
1300 #endif
1302 #include "sane-ctype.h"
1305 * Like skip_prefix, but compare case-insensitively. Note that the comparison
1306 * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
1307 * locale-specific conversions).
1309 static inline int skip_iprefix(const char *str, const char *prefix,
1310 const char **out)
1312 do {
1313 if (!*prefix) {
1314 *out = str;
1315 return 1;
1317 } while (tolower(*str++) == tolower(*prefix++));
1318 return 0;
1322 * Like skip_prefix_mem, but compare case-insensitively. Note that the
1323 * comparison is done via tolower(), so it is strictly ASCII (no multi-byte
1324 * characters or locale-specific conversions).
1326 static inline int skip_iprefix_mem(const char *buf, size_t len,
1327 const char *prefix,
1328 const char **out, size_t *outlen)
1330 do {
1331 if (!*prefix) {
1332 *out = buf;
1333 *outlen = len;
1334 return 1;
1336 } while (len-- > 0 && tolower(*buf++) == tolower(*prefix++));
1337 return 0;
1340 static inline int strtoul_ui(char const *s, int base, unsigned int *result)
1342 unsigned long ul;
1343 char *p;
1345 errno = 0;
1346 /* negative values would be accepted by strtoul */
1347 if (strchr(s, '-'))
1348 return -1;
1349 ul = strtoul(s, &p, base);
1350 if (errno || *p || p == s || (unsigned int) ul != ul)
1351 return -1;
1352 *result = ul;
1353 return 0;
1356 static inline int strtol_i(char const *s, int base, int *result)
1358 long ul;
1359 char *p;
1361 errno = 0;
1362 ul = strtol(s, &p, base);
1363 if (errno || *p || p == s || (int) ul != ul)
1364 return -1;
1365 *result = ul;
1366 return 0;
1369 void git_stable_qsort(void *base, size_t nmemb, size_t size,
1370 int(*compar)(const void *, const void *));
1371 #ifdef INTERNAL_QSORT
1372 #define qsort git_stable_qsort
1373 #endif
1375 #define QSORT(base, n, compar) sane_qsort((base), (n), sizeof(*(base)), compar)
1376 static inline void sane_qsort(void *base, size_t nmemb, size_t size,
1377 int(*compar)(const void *, const void *))
1379 if (nmemb > 1)
1380 qsort(base, nmemb, size, compar);
1383 #define STABLE_QSORT(base, n, compar) \
1384 git_stable_qsort((base), (n), sizeof(*(base)), compar)
1386 #ifndef HAVE_ISO_QSORT_S
1387 int git_qsort_s(void *base, size_t nmemb, size_t size,
1388 int (*compar)(const void *, const void *, void *), void *ctx);
1389 #define qsort_s git_qsort_s
1390 #endif
1392 #define QSORT_S(base, n, compar, ctx) do { \
1393 if (qsort_s((base), (n), sizeof(*(base)), compar, ctx)) \
1394 BUG("qsort_s() failed"); \
1395 } while (0)
1397 #ifndef REG_STARTEND
1398 #error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
1399 #endif
1401 static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
1402 size_t nmatch, regmatch_t pmatch[], int eflags)
1404 assert(nmatch > 0 && pmatch);
1405 pmatch[0].rm_so = 0;
1406 pmatch[0].rm_eo = size;
1407 return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND);
1410 #ifdef USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
1411 int git_regcomp(regex_t *preg, const char *pattern, int cflags);
1412 #define regcomp git_regcomp
1413 #endif
1415 #ifndef DIR_HAS_BSD_GROUP_SEMANTICS
1416 # define FORCE_DIR_SET_GID S_ISGID
1417 #else
1418 # define FORCE_DIR_SET_GID 0
1419 #endif
1421 #ifdef NO_NSEC
1422 #undef USE_NSEC
1423 #define ST_CTIME_NSEC(st) 0
1424 #define ST_MTIME_NSEC(st) 0
1425 #else
1426 #ifdef USE_ST_TIMESPEC
1427 #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))
1428 #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))
1429 #else
1430 #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))
1431 #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))
1432 #endif
1433 #endif
1435 #ifdef UNRELIABLE_FSTAT
1436 #define fstat_is_reliable() 0
1437 #else
1438 #define fstat_is_reliable() 1
1439 #endif
1441 #ifndef va_copy
1443 * Since an obvious implementation of va_list would be to make it a
1444 * pointer into the stack frame, a simple assignment will work on
1445 * many systems. But let's try to be more portable.
1447 #ifdef __va_copy
1448 #define va_copy(dst, src) __va_copy(dst, src)
1449 #else
1450 #define va_copy(dst, src) ((dst) = (src))
1451 #endif
1452 #endif
1454 /* usage.c: only to be used for testing BUG() implementation (see test-tool) */
1455 extern int BUG_exit_code;
1457 /* usage.c: if bug() is called we should have a BUG_if_bug() afterwards */
1458 extern int bug_called_must_BUG;
1460 __attribute__((format (printf, 3, 4))) NORETURN
1461 void BUG_fl(const char *file, int line, const char *fmt, ...);
1462 #define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__)
1463 __attribute__((format (printf, 3, 4)))
1464 void bug_fl(const char *file, int line, const char *fmt, ...);
1465 #define bug(...) bug_fl(__FILE__, __LINE__, __VA_ARGS__)
1466 #define BUG_if_bug(...) do { \
1467 if (bug_called_must_BUG) \
1468 BUG_fl(__FILE__, __LINE__, __VA_ARGS__); \
1469 } while (0)
1471 #ifndef FSYNC_METHOD_DEFAULT
1472 #ifdef __APPLE__
1473 #define FSYNC_METHOD_DEFAULT FSYNC_METHOD_WRITEOUT_ONLY
1474 #else
1475 #define FSYNC_METHOD_DEFAULT FSYNC_METHOD_FSYNC
1476 #endif
1477 #endif
1479 #ifndef SHELL_PATH
1480 # define SHELL_PATH "/bin/sh"
1481 #endif
1483 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
1484 static inline void git_flockfile(FILE *fh UNUSED)
1486 ; /* nothing */
1488 static inline void git_funlockfile(FILE *fh UNUSED)
1490 ; /* nothing */
1492 #undef flockfile
1493 #undef funlockfile
1494 #undef getc_unlocked
1495 #define flockfile(fh) git_flockfile(fh)
1496 #define funlockfile(fh) git_funlockfile(fh)
1497 #define getc_unlocked(fh) getc(fh)
1498 #endif
1500 #ifdef FILENO_IS_A_MACRO
1501 int git_fileno(FILE *stream);
1502 # ifndef COMPAT_CODE_FILENO
1503 # undef fileno
1504 # define fileno(p) git_fileno(p)
1505 # endif
1506 #endif
1508 #ifdef NEED_ACCESS_ROOT_HANDLER
1509 int git_access(const char *path, int mode);
1510 # ifndef COMPAT_CODE_ACCESS
1511 # ifdef access
1512 # undef access
1513 # endif
1514 # define access(path, mode) git_access(path, mode)
1515 # endif
1516 #endif
1519 * Our code often opens a path to an optional file, to work on its
1520 * contents when we can successfully open it. We can ignore a failure
1521 * to open if such an optional file does not exist, but we do want to
1522 * report a failure in opening for other reasons (e.g. we got an I/O
1523 * error, or the file is there, but we lack the permission to open).
1525 * Call this function after seeing an error from open() or fopen() to
1526 * see if the errno indicates a missing file that we can safely ignore.
1528 static inline int is_missing_file_error(int errno_)
1530 return (errno_ == ENOENT || errno_ == ENOTDIR);
1533 int cmd_main(int, const char **);
1536 * Intercept all calls to exit() and route them to trace2 to
1537 * optionally emit a message before calling the real exit().
1539 int common_exit(const char *file, int line, int code);
1540 #define exit(code) exit(common_exit(__FILE__, __LINE__, (code)))
1542 #define z_const
1543 #include <zlib.h>
1545 #if ZLIB_VERNUM < 0x1290
1547 * This is uncompress2, which is only available in zlib >= 1.2.9
1548 * (released as of early 2017). See compat/zlib-uncompress2.c.
1550 int uncompress2(Bytef *dest, uLongf *destLen, const Bytef *source,
1551 uLong *sourceLen);
1552 #endif
1555 * This include must come after system headers, since it introduces macros that
1556 * replace system names.
1558 #include "banned.h"
1561 * container_of - Get the address of an object containing a field.
1563 * @ptr: pointer to the field.
1564 * @type: type of the object.
1565 * @member: name of the field within the object.
1567 #define container_of(ptr, type, member) \
1568 ((type *) ((char *)(ptr) - offsetof(type, member)))
1571 * helper function for `container_of_or_null' to avoid multiple
1572 * evaluation of @ptr
1574 static inline void *container_of_or_null_offset(void *ptr, size_t offset)
1576 return ptr ? (char *)ptr - offset : NULL;
1580 * like `container_of', but allows returned value to be NULL
1582 #define container_of_or_null(ptr, type, member) \
1583 (type *)container_of_or_null_offset(ptr, offsetof(type, member))
1586 * like offsetof(), but takes a pointer to a variable of type which
1587 * contains @member, instead of a specified type.
1588 * @ptr is subject to multiple evaluation since we can't rely on __typeof__
1589 * everywhere.
1591 #if defined(__GNUC__) /* clang sets this, too */
1592 #define OFFSETOF_VAR(ptr, member) offsetof(__typeof__(*ptr), member)
1593 #else /* !__GNUC__ */
1594 #define OFFSETOF_VAR(ptr, member) \
1595 ((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
1596 #endif /* !__GNUC__ */
1598 #endif