THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1316 lines
35 KiB

  1. #ifndef GIT_COMPAT_UTIL_H
  2. #define GIT_COMPAT_UTIL_H
  3. #ifdef USE_MSVC_CRTDBG
  4. /*
  5. * For these to work they must appear very early in each
  6. * file -- before most of the standard header files.
  7. */
  8. #include <stdlib.h>
  9. #include <crtdbg.h>
  10. #endif
  11. #define _FILE_OFFSET_BITS 64
  12. /* Derived from Linux "Features Test Macro" header
  13. * Convenience macros to test the versions of gcc (or
  14. * a compatible compiler).
  15. * Use them like this:
  16. * #if GIT_GNUC_PREREQ (2,8)
  17. * ... code requiring gcc 2.8 or later ...
  18. * #endif
  19. */
  20. #if defined(__GNUC__) && defined(__GNUC_MINOR__)
  21. # define GIT_GNUC_PREREQ(maj, min) \
  22. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  23. #else
  24. #define GIT_GNUC_PREREQ(maj, min) 0
  25. #endif
  26. #ifndef FLEX_ARRAY
  27. /*
  28. * See if our compiler is known to support flexible array members.
  29. */
  30. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && (!defined(__SUNPRO_C) || (__SUNPRO_C > 0x580))
  31. # define FLEX_ARRAY /* empty */
  32. #elif defined(__GNUC__)
  33. # if (__GNUC__ >= 3)
  34. # define FLEX_ARRAY /* empty */
  35. # else
  36. # define FLEX_ARRAY 0 /* older GNU extension */
  37. # endif
  38. #endif
  39. /*
  40. * Otherwise, default to safer but a bit wasteful traditional style
  41. */
  42. #ifndef FLEX_ARRAY
  43. # define FLEX_ARRAY 1
  44. #endif
  45. #endif
  46. /*
  47. * BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.
  48. * @cond: the compile-time condition which must be true.
  49. *
  50. * Your compile will fail if the condition isn't true, or can't be evaluated
  51. * by the compiler. This can be used in an expression: its value is "0".
  52. *
  53. * Example:
  54. * #define foo_to_char(foo) \
  55. * ((char *)(foo) \
  56. * + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0))
  57. */
  58. #define BUILD_ASSERT_OR_ZERO(cond) \
  59. (sizeof(char [1 - 2*!(cond)]) - 1)
  60. #if GIT_GNUC_PREREQ(3, 1)
  61. /* &arr[0] degrades to a pointer: a different type from an array */
  62. # define BARF_UNLESS_AN_ARRAY(arr) \
  63. BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(__typeof__(arr), \
  64. __typeof__(&(arr)[0])))
  65. #else
  66. # define BARF_UNLESS_AN_ARRAY(arr) 0
  67. #endif
  68. /*
  69. * ARRAY_SIZE - get the number of elements in a visible array
  70. * <at> x: the array whose size you want.
  71. *
  72. * This does not work on pointers, or arrays declared as [], or
  73. * function parameters. With correct compiler support, such usage
  74. * will cause a build error (see the build_assert_or_zero macro).
  75. */
  76. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + BARF_UNLESS_AN_ARRAY(x))
  77. #define bitsizeof(x) (CHAR_BIT * sizeof(x))
  78. #define maximum_signed_value_of_type(a) \
  79. (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a)))
  80. #define maximum_unsigned_value_of_type(a) \
  81. (UINTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a)))
  82. /*
  83. * Signed integer overflow is undefined in C, so here's a helper macro
  84. * to detect if the sum of two integers will overflow.
  85. *
  86. * Requires: a >= 0, typeof(a) equals typeof(b)
  87. */
  88. #define signed_add_overflows(a, b) \
  89. ((b) > maximum_signed_value_of_type(a) - (a))
  90. #define unsigned_add_overflows(a, b) \
  91. ((b) > maximum_unsigned_value_of_type(a) - (a))
  92. /*
  93. * Returns true if the multiplication of "a" and "b" will
  94. * overflow. The types of "a" and "b" must match and must be unsigned.
  95. * Note that this macro evaluates "a" twice!
  96. */
  97. #define unsigned_mult_overflows(a, b) \
  98. ((a) && (b) > maximum_unsigned_value_of_type(a) / (a))
  99. #ifdef __GNUC__
  100. #define TYPEOF(x) (__typeof__(x))
  101. #else
  102. #define TYPEOF(x)
  103. #endif
  104. #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (bitsizeof(x) - (bits))))
  105. #define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
  106. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  107. /* Approximation of the length of the decimal representation of this type. */
  108. #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
  109. #if defined(__sun__)
  110. /*
  111. * On Solaris, when _XOPEN_EXTENDED is set, its header file
  112. * forces the programs to be XPG4v2, defeating any _XOPEN_SOURCE
  113. * setting to say we are XPG5 or XPG6. Also on Solaris,
  114. * XPG6 programs must be compiled with a c99 compiler, while
  115. * non XPG6 programs must be compiled with a pre-c99 compiler.
  116. */
  117. # if __STDC_VERSION__ - 0 >= 199901L
  118. # define _XOPEN_SOURCE 600
  119. # else
  120. # define _XOPEN_SOURCE 500
  121. # endif
  122. #elif !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && \
  123. !defined(_M_UNIX) && !defined(__sgi) && !defined(__DragonFly__) && \
  124. !defined(__TANDEM) && !defined(__QNX__) && !defined(__MirBSD__) && \
  125. !defined(__CYGWIN__)
  126. #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
  127. #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
  128. #endif
  129. #define _ALL_SOURCE 1
  130. #define _GNU_SOURCE 1
  131. #define _BSD_SOURCE 1
  132. #define _DEFAULT_SOURCE 1
  133. #define _NETBSD_SOURCE 1
  134. #define _SGI_SOURCE 1
  135. #if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */
  136. # if !defined(_WIN32_WINNT)
  137. # define _WIN32_WINNT 0x0600
  138. # endif
  139. #define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */
  140. #include <winsock2.h>
  141. #include <windows.h>
  142. #define GIT_WINDOWS_NATIVE
  143. #endif
  144. #include <unistd.h>
  145. #include <stdio.h>
  146. #include <sys/stat.h>
  147. #include <fcntl.h>
  148. #include <stddef.h>
  149. #include <stdlib.h>
  150. #include <stdarg.h>
  151. #include <string.h>
  152. #ifdef HAVE_STRINGS_H
  153. #include <strings.h> /* for strcasecmp() */
  154. #endif
  155. #include <errno.h>
  156. #include <limits.h>
  157. #ifdef NEEDS_SYS_PARAM_H
  158. #include <sys/param.h>
  159. #endif
  160. #include <sys/types.h>
  161. #include <dirent.h>
  162. #include <sys/time.h>
  163. #include <time.h>
  164. #include <signal.h>
  165. #include <assert.h>
  166. #include <regex.h>
  167. #include <utime.h>
  168. #include <syslog.h>
  169. #if !defined(NO_POLL_H)
  170. #include <poll.h>
  171. #elif !defined(NO_SYS_POLL_H)
  172. #include <sys/poll.h>
  173. #else
  174. /* Pull the compat stuff */
  175. #include <poll.h>
  176. #endif
  177. #ifdef HAVE_BSD_SYSCTL
  178. #include <sys/sysctl.h>
  179. #endif
  180. #if defined(__CYGWIN__)
  181. #include "compat/win32/path-utils.h"
  182. #endif
  183. #if defined(__MINGW32__)
  184. /* pull in Windows compatibility stuff */
  185. #include "compat/win32/path-utils.h"
  186. #include "compat/mingw.h"
  187. #elif defined(_MSC_VER)
  188. #include "compat/win32/path-utils.h"
  189. #include "compat/msvc.h"
  190. #else
  191. #include <sys/utsname.h>
  192. #include <sys/wait.h>
  193. #include <sys/resource.h>
  194. #include <sys/socket.h>
  195. #include <sys/ioctl.h>
  196. #include <termios.h>
  197. #ifndef NO_SYS_SELECT_H
  198. #include <sys/select.h>
  199. #endif
  200. #include <netinet/in.h>
  201. #include <netinet/tcp.h>
  202. #include <arpa/inet.h>
  203. #include <netdb.h>
  204. #include <pwd.h>
  205. #include <sys/un.h>
  206. #ifndef NO_INTTYPES_H
  207. #include <inttypes.h>
  208. #else
  209. #include <stdint.h>
  210. #endif
  211. #ifdef NO_INTPTR_T
  212. /*
  213. * On I16LP32, ILP32 and LP64 "long" is the safe bet, however
  214. * on LLP86, IL33LLP64 and P64 it needs to be "long long",
  215. * while on IP16 and IP16L32 it is "int" (resp. "short")
  216. * Size needs to match (or exceed) 'sizeof(void *)'.
  217. * We can't take "long long" here as not everybody has it.
  218. */
  219. typedef long intptr_t;
  220. typedef unsigned long uintptr_t;
  221. #endif
  222. #undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */
  223. #include <grp.h>
  224. #define _ALL_SOURCE 1
  225. #endif
  226. /* used on Mac OS X */
  227. #ifdef PRECOMPOSE_UNICODE
  228. #include "compat/precompose_utf8.h"
  229. #else
  230. #define precompose_str(in,i_nfd2nfc)
  231. #define precompose_argv(c,v)
  232. #define probe_utf8_pathname_composition()
  233. #endif
  234. #ifdef MKDIR_WO_TRAILING_SLASH
  235. #define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))
  236. int compat_mkdir_wo_trailing_slash(const char*, mode_t);
  237. #endif
  238. #ifdef NO_STRUCT_ITIMERVAL
  239. struct itimerval {
  240. struct timeval it_interval;
  241. struct timeval it_value;
  242. };
  243. #endif
  244. #ifdef NO_SETITIMER
  245. #define setitimer(which,value,ovalue)
  246. #endif
  247. #ifndef NO_LIBGEN_H
  248. #include <libgen.h>
  249. #else
  250. #define basename gitbasename
  251. char *gitbasename(char *);
  252. #define dirname gitdirname
  253. char *gitdirname(char *);
  254. #endif
  255. #ifndef NO_ICONV
  256. #include <iconv.h>
  257. #endif
  258. #ifndef NO_OPENSSL
  259. #ifdef __APPLE__
  260. #define __AVAILABILITY_MACROS_USES_AVAILABILITY 0
  261. #include <AvailabilityMacros.h>
  262. #undef DEPRECATED_ATTRIBUTE
  263. #define DEPRECATED_ATTRIBUTE
  264. #undef __AVAILABILITY_MACROS_USES_AVAILABILITY
  265. #endif
  266. #include <openssl/ssl.h>
  267. #include <openssl/err.h>
  268. #endif
  269. #ifdef HAVE_SYSINFO
  270. # include <sys/sysinfo.h>
  271. #endif
  272. /* On most systems <netdb.h> would have given us this, but
  273. * not on some systems (e.g. z/OS).
  274. */
  275. #ifndef NI_MAXHOST
  276. #define NI_MAXHOST 1025
  277. #endif
  278. #ifndef NI_MAXSERV
  279. #define NI_MAXSERV 32
  280. #endif
  281. /* On most systems <limits.h> would have given us this, but
  282. * not on some systems (e.g. GNU/Hurd).
  283. */
  284. #ifndef PATH_MAX
  285. #define PATH_MAX 4096
  286. #endif
  287. #ifndef PRIuMAX
  288. #define PRIuMAX "llu"
  289. #endif
  290. #ifndef SCNuMAX
  291. #define SCNuMAX PRIuMAX
  292. #endif
  293. #ifndef PRIu32
  294. #define PRIu32 "u"
  295. #endif
  296. #ifndef PRIx32
  297. #define PRIx32 "x"
  298. #endif
  299. #ifndef PRIo32
  300. #define PRIo32 "o"
  301. #endif
  302. typedef uintmax_t timestamp_t;
  303. #define PRItime PRIuMAX
  304. #define parse_timestamp strtoumax
  305. #define TIME_MAX UINTMAX_MAX
  306. #ifndef PATH_SEP
  307. #define PATH_SEP ':'
  308. #endif
  309. #ifdef HAVE_PATHS_H
  310. #include <paths.h>
  311. #endif
  312. #ifndef _PATH_DEFPATH
  313. #define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin"
  314. #endif
  315. #ifndef platform_core_config
  316. static inline int noop_core_config(const char *var, const char *value, void *cb)
  317. {
  318. return 0;
  319. }
  320. #define platform_core_config noop_core_config
  321. #endif
  322. #ifndef has_dos_drive_prefix
  323. static inline int git_has_dos_drive_prefix(const char *path)
  324. {
  325. return 0;
  326. }
  327. #define has_dos_drive_prefix git_has_dos_drive_prefix
  328. #endif
  329. #ifndef skip_dos_drive_prefix
  330. static inline int git_skip_dos_drive_prefix(char **path)
  331. {
  332. return 0;
  333. }
  334. #define skip_dos_drive_prefix git_skip_dos_drive_prefix
  335. #endif
  336. #ifndef is_dir_sep
  337. static inline int git_is_dir_sep(int c)
  338. {
  339. return c == '/';
  340. }
  341. #define is_dir_sep git_is_dir_sep
  342. #endif
  343. #ifndef offset_1st_component
  344. static inline int git_offset_1st_component(const char *path)
  345. {
  346. return is_dir_sep(path[0]);
  347. }
  348. #define offset_1st_component git_offset_1st_component
  349. #endif
  350. #ifndef find_last_dir_sep
  351. static inline char *git_find_last_dir_sep(const char *path)
  352. {
  353. return strrchr(path, '/');
  354. }
  355. #define find_last_dir_sep git_find_last_dir_sep
  356. #endif
  357. #ifndef query_user_email
  358. #define query_user_email() NULL
  359. #endif
  360. #ifdef __TANDEM
  361. #include <floss.h(floss_execl,floss_execlp,floss_execv,floss_execvp)>
  362. #include <floss.h(floss_getpwuid)>
  363. #ifndef NSIG
  364. /*
  365. * NonStop NSE and NSX do not provide NSIG. SIGGUARDIAN(99) is the highest
  366. * known, by detective work using kill -l as a list is all signals
  367. * instead of signal.h where it should be.
  368. */
  369. # define NSIG 100
  370. #endif
  371. #endif
  372. #if defined(__HP_cc) && (__HP_cc >= 61000)
  373. #define NORETURN __attribute__((noreturn))
  374. #define NORETURN_PTR
  375. #elif defined(__GNUC__) && !defined(NO_NORETURN)
  376. #define NORETURN __attribute__((__noreturn__))
  377. #define NORETURN_PTR __attribute__((__noreturn__))
  378. #elif defined(_MSC_VER)
  379. #define NORETURN __declspec(noreturn)
  380. #define NORETURN_PTR
  381. #else
  382. #define NORETURN
  383. #define NORETURN_PTR
  384. #ifndef __GNUC__
  385. #ifndef __attribute__
  386. #define __attribute__(x)
  387. #endif
  388. #endif
  389. #endif
  390. /* The sentinel attribute is valid from gcc version 4.0 */
  391. #if defined(__GNUC__) && (__GNUC__ >= 4)
  392. #define LAST_ARG_MUST_BE_NULL __attribute__((sentinel))
  393. #else
  394. #define LAST_ARG_MUST_BE_NULL
  395. #endif
  396. #define MAYBE_UNUSED __attribute__((__unused__))
  397. #include "compat/bswap.h"
  398. #include "wildmatch.h"
  399. struct strbuf;
  400. /* General helper functions */
  401. void vreportf(const char *prefix, const char *err, va_list params);
  402. NORETURN void usage(const char *err);
  403. NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
  404. NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
  405. NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
  406. int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
  407. int error_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
  408. void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
  409. void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
  410. #ifndef NO_OPENSSL
  411. #ifdef APPLE_COMMON_CRYPTO
  412. #include "compat/apple-common-crypto.h"
  413. #else
  414. #include <openssl/evp.h>
  415. #include <openssl/hmac.h>
  416. #endif /* APPLE_COMMON_CRYPTO */
  417. #include <openssl/x509v3.h>
  418. #endif /* NO_OPENSSL */
  419. /*
  420. * Let callers be aware of the constant return value; this can help
  421. * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
  422. * because some compilers may not support variadic macros. Since we're only
  423. * trying to help gcc, anyway, it's OK; other compilers will fall back to
  424. * using the function as usual.
  425. */
  426. #if defined(__GNUC__)
  427. static inline int const_error(void)
  428. {
  429. return -1;
  430. }
  431. #define error(...) (error(__VA_ARGS__), const_error())
  432. #define error_errno(...) (error_errno(__VA_ARGS__), const_error())
  433. #endif
  434. void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
  435. void set_error_routine(void (*routine)(const char *err, va_list params));
  436. extern void (*get_error_routine(void))(const char *err, va_list params);
  437. void set_warn_routine(void (*routine)(const char *warn, va_list params));
  438. extern void (*get_warn_routine(void))(const char *warn, va_list params);
  439. void set_die_is_recursing_routine(int (*routine)(void));
  440. int starts_with(const char *str, const char *prefix);
  441. int istarts_with(const char *str, const char *prefix);
  442. /*
  443. * If the string "str" begins with the string found in "prefix", return 1.
  444. * The "out" parameter is set to "str + strlen(prefix)" (i.e., to the point in
  445. * the string right after the prefix).
  446. *
  447. * Otherwise, return 0 and leave "out" untouched.
  448. *
  449. * Examples:
  450. *
  451. * [extract branch name, fail if not a branch]
  452. * if (!skip_prefix(ref, "refs/heads/", &branch)
  453. * return -1;
  454. *
  455. * [skip prefix if present, otherwise use whole string]
  456. * skip_prefix(name, "refs/heads/", &name);
  457. */
  458. static inline int skip_prefix(const char *str, const char *prefix,
  459. const char **out)
  460. {
  461. do {
  462. if (!*prefix) {
  463. *out = str;
  464. return 1;
  465. }
  466. } while (*str++ == *prefix++);
  467. return 0;
  468. }
  469. /*
  470. * If the string "str" is the same as the string in "prefix", then the "arg"
  471. * parameter is set to the "def" parameter and 1 is returned.
  472. * If the string "str" begins with the string found in "prefix" and then a
  473. * "=" sign, then the "arg" parameter is set to "str + strlen(prefix) + 1"
  474. * (i.e., to the point in the string right after the prefix and the "=" sign),
  475. * and 1 is returned.
  476. *
  477. * Otherwise, return 0 and leave "arg" untouched.
  478. *
  479. * When we accept both a "--key" and a "--key=<val>" option, this function
  480. * can be used instead of !strcmp(arg, "--key") and then
  481. * skip_prefix(arg, "--key=", &arg) to parse such an option.
  482. */
  483. int skip_to_optional_arg_default(const char *str, const char *prefix,
  484. const char **arg, const char *def);
  485. static inline int skip_to_optional_arg(const char *str, const char *prefix,
  486. const char **arg)
  487. {
  488. return skip_to_optional_arg_default(str, prefix, arg, "");
  489. }
  490. /*
  491. * Like skip_prefix, but promises never to read past "len" bytes of the input
  492. * buffer, and returns the remaining number of bytes in "out" via "outlen".
  493. */
  494. static inline int skip_prefix_mem(const char *buf, size_t len,
  495. const char *prefix,
  496. const char **out, size_t *outlen)
  497. {
  498. size_t prefix_len = strlen(prefix);
  499. if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) {
  500. *out = buf + prefix_len;
  501. *outlen = len - prefix_len;
  502. return 1;
  503. }
  504. return 0;
  505. }
  506. /*
  507. * If buf ends with suffix, return 1 and subtract the length of the suffix
  508. * from *len. Otherwise, return 0 and leave *len untouched.
  509. */
  510. static inline int strip_suffix_mem(const char *buf, size_t *len,
  511. const char *suffix)
  512. {
  513. size_t suflen = strlen(suffix);
  514. if (*len < suflen || memcmp(buf + (*len - suflen), suffix, suflen))
  515. return 0;
  516. *len -= suflen;
  517. return 1;
  518. }
  519. /*
  520. * If str ends with suffix, return 1 and set *len to the size of the string
  521. * without the suffix. Otherwise, return 0 and set *len to the size of the
  522. * string.
  523. *
  524. * Note that we do _not_ NUL-terminate str to the new length.
  525. */
  526. static inline int strip_suffix(const char *str, const char *suffix, size_t *len)
  527. {
  528. *len = strlen(str);
  529. return strip_suffix_mem(str, len, suffix);
  530. }
  531. static inline int ends_with(const char *str, const char *suffix)
  532. {
  533. size_t len;
  534. return strip_suffix(str, suffix, &len);
  535. }
  536. #define SWAP(a, b) do { \
  537. void *_swap_a_ptr = &(a); \
  538. void *_swap_b_ptr = &(b); \
  539. unsigned char _swap_buffer[sizeof(a)]; \
  540. memcpy(_swap_buffer, _swap_a_ptr, sizeof(a)); \
  541. memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) + \
  542. BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \
  543. memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \
  544. } while (0)
  545. #if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
  546. #ifndef PROT_READ
  547. #define PROT_READ 1
  548. #define PROT_WRITE 2
  549. #define MAP_PRIVATE 1
  550. #endif
  551. #define mmap git_mmap
  552. #define munmap git_munmap
  553. void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
  554. int git_munmap(void *start, size_t length);
  555. #else /* NO_MMAP || USE_WIN32_MMAP */
  556. #include <sys/mman.h>
  557. #endif /* NO_MMAP || USE_WIN32_MMAP */
  558. #ifdef NO_MMAP
  559. /* This value must be multiple of (pagesize * 2) */
  560. #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
  561. #else /* NO_MMAP */
  562. /* This value must be multiple of (pagesize * 2) */
  563. #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
  564. (sizeof(void*) >= 8 \
  565. ? 1 * 1024 * 1024 * 1024 \
  566. : 32 * 1024 * 1024)
  567. #endif /* NO_MMAP */
  568. #ifndef MAP_FAILED
  569. #define MAP_FAILED ((void *)-1)
  570. #endif
  571. #ifdef NO_ST_BLOCKS_IN_STRUCT_STAT
  572. #define on_disk_bytes(st) ((st).st_size)
  573. #else
  574. #define on_disk_bytes(st) ((st).st_blocks * 512)
  575. #endif
  576. #ifdef NEEDS_MODE_TRANSLATION
  577. #undef S_IFMT
  578. #undef S_IFREG
  579. #undef S_IFDIR
  580. #undef S_IFLNK
  581. #undef S_IFBLK
  582. #undef S_IFCHR
  583. #undef S_IFIFO
  584. #undef S_IFSOCK
  585. #define S_IFMT 0170000
  586. #define S_IFREG 0100000
  587. #define S_IFDIR 0040000
  588. #define S_IFLNK 0120000
  589. #define S_IFBLK 0060000
  590. #define S_IFCHR 0020000
  591. #define S_IFIFO 0010000
  592. #define S_IFSOCK 0140000
  593. #ifdef stat
  594. #undef stat
  595. #endif
  596. #define stat(path, buf) git_stat(path, buf)
  597. int git_stat(const char *, struct stat *);
  598. #ifdef fstat
  599. #undef fstat
  600. #endif
  601. #define fstat(fd, buf) git_fstat(fd, buf)
  602. int git_fstat(int, struct stat *);
  603. #ifdef lstat
  604. #undef lstat
  605. #endif
  606. #define lstat(path, buf) git_lstat(path, buf)
  607. int git_lstat(const char *, struct stat *);
  608. #endif
  609. #define DEFAULT_PACKED_GIT_LIMIT \
  610. ((1024L * 1024L) * (size_t)(sizeof(void*) >= 8 ? (32 * 1024L * 1024L) : 256))
  611. #ifdef NO_PREAD
  612. #define pread git_pread
  613. ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
  614. #endif
  615. /*
  616. * Forward decl that will remind us if its twin in cache.h changes.
  617. * This function is used in compat/pread.c. But we can't include
  618. * cache.h there.
  619. */
  620. ssize_t read_in_full(int fd, void *buf, size_t count);
  621. #ifdef NO_SETENV
  622. #define setenv gitsetenv
  623. int gitsetenv(const char *, const char *, int);
  624. #endif
  625. #ifdef NO_MKDTEMP
  626. #define mkdtemp gitmkdtemp
  627. char *gitmkdtemp(char *);
  628. #endif
  629. #ifdef NO_UNSETENV
  630. #define unsetenv gitunsetenv
  631. void gitunsetenv(const char *);
  632. #endif
  633. #ifdef NO_STRCASESTR
  634. #define strcasestr gitstrcasestr
  635. char *gitstrcasestr(const char *haystack, const char *needle);
  636. #endif
  637. #ifdef NO_STRLCPY
  638. #define strlcpy gitstrlcpy
  639. size_t gitstrlcpy(char *, const char *, size_t);
  640. #endif
  641. #ifdef NO_STRTOUMAX
  642. #define strtoumax gitstrtoumax
  643. uintmax_t gitstrtoumax(const char *, char **, int);
  644. #define strtoimax gitstrtoimax
  645. intmax_t gitstrtoimax(const char *, char **, int);
  646. #endif
  647. #ifdef NO_HSTRERROR
  648. #define hstrerror githstrerror
  649. const char *githstrerror(int herror);
  650. #endif
  651. #ifdef NO_MEMMEM
  652. #define memmem gitmemmem
  653. void *gitmemmem(const void *haystack, size_t haystacklen,
  654. const void *needle, size_t needlelen);
  655. #endif
  656. #ifdef OVERRIDE_STRDUP
  657. #ifdef strdup
  658. #undef strdup
  659. #endif
  660. #define strdup gitstrdup
  661. char *gitstrdup(const char *s);
  662. #endif
  663. #ifdef NO_GETPAGESIZE
  664. #define getpagesize() sysconf(_SC_PAGESIZE)
  665. #endif
  666. #ifndef O_CLOEXEC
  667. #define O_CLOEXEC 0
  668. #endif
  669. #ifdef FREAD_READS_DIRECTORIES
  670. # if !defined(SUPPRESS_FOPEN_REDEFINITION)
  671. # ifdef fopen
  672. # undef fopen
  673. # endif
  674. # define fopen(a,b) git_fopen(a,b)
  675. # endif
  676. FILE *git_fopen(const char*, const char*);
  677. #endif
  678. #ifdef SNPRINTF_RETURNS_BOGUS
  679. #ifdef snprintf
  680. #undef snprintf
  681. #endif
  682. #define snprintf git_snprintf
  683. int git_snprintf(char *str, size_t maxsize,
  684. const char *format, ...);
  685. #ifdef vsnprintf
  686. #undef vsnprintf
  687. #endif
  688. #define vsnprintf git_vsnprintf
  689. int git_vsnprintf(char *str, size_t maxsize,
  690. const char *format, va_list ap);
  691. #endif
  692. #ifdef __GLIBC_PREREQ
  693. #if __GLIBC_PREREQ(2, 1)
  694. #define HAVE_STRCHRNUL
  695. #endif
  696. #endif
  697. #ifndef HAVE_STRCHRNUL
  698. #define strchrnul gitstrchrnul
  699. static inline char *gitstrchrnul(const char *s, int c)
  700. {
  701. while (*s && *s != c)
  702. s++;
  703. return (char *)s;
  704. }
  705. #endif
  706. #ifdef NO_INET_PTON
  707. int inet_pton(int af, const char *src, void *dst);
  708. #endif
  709. #ifdef NO_INET_NTOP
  710. const char *inet_ntop(int af, const void *src, char *dst, size_t size);
  711. #endif
  712. #ifdef NO_PTHREADS
  713. #define atexit git_atexit
  714. int git_atexit(void (*handler)(void));
  715. #endif
  716. typedef void (*try_to_free_t)(size_t);
  717. try_to_free_t set_try_to_free_routine(try_to_free_t);
  718. static inline size_t st_add(size_t a, size_t b)
  719. {
  720. if (unsigned_add_overflows(a, b))
  721. die("size_t overflow: %"PRIuMAX" + %"PRIuMAX,
  722. (uintmax_t)a, (uintmax_t)b);
  723. return a + b;
  724. }
  725. #define st_add3(a,b,c) st_add(st_add((a),(b)),(c))
  726. #define st_add4(a,b,c,d) st_add(st_add3((a),(b),(c)),(d))
  727. static inline size_t st_mult(size_t a, size_t b)
  728. {
  729. if (unsigned_mult_overflows(a, b))
  730. die("size_t overflow: %"PRIuMAX" * %"PRIuMAX,
  731. (uintmax_t)a, (uintmax_t)b);
  732. return a * b;
  733. }
  734. static inline size_t st_sub(size_t a, size_t b)
  735. {
  736. if (a < b)
  737. die("size_t underflow: %"PRIuMAX" - %"PRIuMAX,
  738. (uintmax_t)a, (uintmax_t)b);
  739. return a - b;
  740. }
  741. #ifdef HAVE_ALLOCA_H
  742. # include <alloca.h>
  743. # define xalloca(size) (alloca(size))
  744. # define xalloca_free(p) do {} while (0)
  745. #else
  746. # define xalloca(size) (xmalloc(size))
  747. # define xalloca_free(p) (free(p))
  748. #endif
  749. char *xstrdup(const char *str);
  750. void *xmalloc(size_t size);
  751. void *xmallocz(size_t size);
  752. void *xmallocz_gently(size_t size);
  753. void *xmemdupz(const void *data, size_t len);
  754. char *xstrndup(const char *str, size_t len);
  755. void *xrealloc(void *ptr, size_t size);
  756. void *xcalloc(size_t nmemb, size_t size);
  757. void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
  758. void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
  759. int xopen(const char *path, int flags, ...);
  760. ssize_t xread(int fd, void *buf, size_t len);
  761. ssize_t xwrite(int fd, const void *buf, size_t len);
  762. ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
  763. int xdup(int fd);
  764. FILE *xfopen(const char *path, const char *mode);
  765. FILE *xfdopen(int fd, const char *mode);
  766. int xmkstemp(char *temp_filename);
  767. int xmkstemp_mode(char *temp_filename, int mode);
  768. char *xgetcwd(void);
  769. FILE *fopen_for_writing(const char *path);
  770. FILE *fopen_or_warn(const char *path, const char *mode);
  771. /*
  772. * FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
  773. * that ptr is used twice, so don't pass e.g. ptr++.
  774. */
  775. #define FREE_AND_NULL(p) do { free(p); (p) = NULL; } while (0)
  776. #define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
  777. #define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)));
  778. #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
  779. #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
  780. BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
  781. static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
  782. {
  783. if (n)
  784. memcpy(dst, src, st_mult(size, n));
  785. }
  786. #define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
  787. BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
  788. static inline void move_array(void *dst, const void *src, size_t n, size_t size)
  789. {
  790. if (n)
  791. memmove(dst, src, st_mult(size, n));
  792. }
  793. /*
  794. * These functions help you allocate structs with flex arrays, and copy
  795. * the data directly into the array. For example, if you had:
  796. *
  797. * struct foo {
  798. * int bar;
  799. * char name[FLEX_ARRAY];
  800. * };
  801. *
  802. * you can do:
  803. *
  804. * struct foo *f;
  805. * FLEX_ALLOC_MEM(f, name, src, len);
  806. *
  807. * to allocate a "foo" with the contents of "src" in the "name" field.
  808. * The resulting struct is automatically zero'd, and the flex-array field
  809. * is NUL-terminated (whether the incoming src buffer was or not).
  810. *
  811. * The FLEXPTR_* variants operate on structs that don't use flex-arrays,
  812. * but do want to store a pointer to some extra data in the same allocated
  813. * block. For example, if you have:
  814. *
  815. * struct foo {
  816. * char *name;
  817. * int bar;
  818. * };
  819. *
  820. * you can do:
  821. *
  822. * struct foo *f;
  823. * FLEXPTR_ALLOC_STR(f, name, src);
  824. *
  825. * and "name" will point to a block of memory after the struct, which will be
  826. * freed along with the struct (but the pointer can be repointed anywhere).
  827. *
  828. * The *_STR variants accept a string parameter rather than a ptr/len
  829. * combination.
  830. *
  831. * Note that these macros will evaluate the first parameter multiple
  832. * times, and it must be assignable as an lvalue.
  833. */
  834. #define FLEX_ALLOC_MEM(x, flexname, buf, len) do { \
  835. size_t flex_array_len_ = (len); \
  836. (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
  837. memcpy((void *)(x)->flexname, (buf), flex_array_len_); \
  838. } while (0)
  839. #define FLEXPTR_ALLOC_MEM(x, ptrname, buf, len) do { \
  840. size_t flex_array_len_ = (len); \
  841. (x) = xcalloc(1, st_add3(sizeof(*(x)), flex_array_len_, 1)); \
  842. memcpy((x) + 1, (buf), flex_array_len_); \
  843. (x)->ptrname = (void *)((x)+1); \
  844. } while(0)
  845. #define FLEX_ALLOC_STR(x, flexname, str) \
  846. FLEX_ALLOC_MEM((x), flexname, (str), strlen(str))
  847. #define FLEXPTR_ALLOC_STR(x, ptrname, str) \
  848. FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str))
  849. static inline char *xstrdup_or_null(const char *str)
  850. {
  851. return str ? xstrdup(str) : NULL;
  852. }
  853. static inline size_t xsize_t(off_t len)
  854. {
  855. size_t size = (size_t) len;
  856. if (len != (off_t) size)
  857. die("Cannot handle files this big");
  858. return size;
  859. }
  860. __attribute__((format (printf, 3, 4)))
  861. int xsnprintf(char *dst, size_t max, const char *fmt, ...);
  862. #ifndef HOST_NAME_MAX
  863. #define HOST_NAME_MAX 256
  864. #endif
  865. int xgethostname(char *buf, size_t len);
  866. /* in ctype.c, for kwset users */
  867. extern const unsigned char tolower_trans_tbl[256];
  868. /* Sane ctype - no locale, and works with signed chars */
  869. #undef isascii
  870. #undef isspace
  871. #undef isdigit
  872. #undef isalpha
  873. #undef isalnum
  874. #undef isprint
  875. #undef islower
  876. #undef isupper
  877. #undef tolower
  878. #undef toupper
  879. #undef iscntrl
  880. #undef ispunct
  881. #undef isxdigit
  882. extern const unsigned char sane_ctype[256];
  883. #define GIT_SPACE 0x01
  884. #define GIT_DIGIT 0x02
  885. #define GIT_ALPHA 0x04
  886. #define GIT_GLOB_SPECIAL 0x08
  887. #define GIT_REGEX_SPECIAL 0x10
  888. #define GIT_PATHSPEC_MAGIC 0x20
  889. #define GIT_CNTRL 0x40
  890. #define GIT_PUNCT 0x80
  891. #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
  892. #define isascii(x) (((x) & ~0x7f) == 0)
  893. #define isspace(x) sane_istest(x,GIT_SPACE)
  894. #define isdigit(x) sane_istest(x,GIT_DIGIT)
  895. #define isalpha(x) sane_istest(x,GIT_ALPHA)
  896. #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
  897. #define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
  898. #define islower(x) sane_iscase(x, 1)
  899. #define isupper(x) sane_iscase(x, 0)
  900. #define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
  901. #define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
  902. #define iscntrl(x) (sane_istest(x,GIT_CNTRL))
  903. #define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \
  904. GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC)
  905. #define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1)
  906. #define tolower(x) sane_case((unsigned char)(x), 0x20)
  907. #define toupper(x) sane_case((unsigned char)(x), 0)
  908. #define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
  909. static inline int sane_case(int x, int high)
  910. {
  911. if (sane_istest(x, GIT_ALPHA))
  912. x = (x & ~0x20) | high;
  913. return x;
  914. }
  915. static inline int sane_iscase(int x, int is_lower)
  916. {
  917. if (!sane_istest(x, GIT_ALPHA))
  918. return 0;
  919. if (is_lower)
  920. return (x & 0x20) != 0;
  921. else
  922. return (x & 0x20) == 0;
  923. }
  924. /*
  925. * Like skip_prefix, but compare case-insensitively. Note that the comparison
  926. * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
  927. * locale-specific conversions).
  928. */
  929. static inline int skip_iprefix(const char *str, const char *prefix,
  930. const char **out)
  931. {
  932. do {
  933. if (!*prefix) {
  934. *out = str;
  935. return 1;
  936. }
  937. } while (tolower(*str++) == tolower(*prefix++));
  938. return 0;
  939. }
  940. static inline int strtoul_ui(char const *s, int base, unsigned int *result)
  941. {
  942. unsigned long ul;
  943. char *p;
  944. errno = 0;
  945. /* negative values would be accepted by strtoul */
  946. if (strchr(s, '-'))
  947. return -1;
  948. ul = strtoul(s, &p, base);
  949. if (errno || *p || p == s || (unsigned int) ul != ul)
  950. return -1;
  951. *result = ul;
  952. return 0;
  953. }
  954. static inline int strtol_i(char const *s, int base, int *result)
  955. {
  956. long ul;
  957. char *p;
  958. errno = 0;
  959. ul = strtol(s, &p, base);
  960. if (errno || *p || p == s || (int) ul != ul)
  961. return -1;
  962. *result = ul;
  963. return 0;
  964. }
  965. #ifdef INTERNAL_QSORT
  966. void git_qsort(void *base, size_t nmemb, size_t size,
  967. int(*compar)(const void *, const void *));
  968. #define qsort git_qsort
  969. #endif
  970. #define QSORT(base, n, compar) sane_qsort((base), (n), sizeof(*(base)), compar)
  971. static inline void sane_qsort(void *base, size_t nmemb, size_t size,
  972. int(*compar)(const void *, const void *))
  973. {
  974. if (nmemb > 1)
  975. qsort(base, nmemb, size, compar);
  976. }
  977. #ifndef HAVE_ISO_QSORT_S
  978. int git_qsort_s(void *base, size_t nmemb, size_t size,
  979. int (*compar)(const void *, const void *, void *), void *ctx);
  980. #define qsort_s git_qsort_s
  981. #endif
  982. #define QSORT_S(base, n, compar, ctx) do { \
  983. if (qsort_s((base), (n), sizeof(*(base)), compar, ctx)) \
  984. BUG("qsort_s() failed"); \
  985. } while (0)
  986. #ifndef REG_STARTEND
  987. #error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
  988. #endif
  989. static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
  990. size_t nmatch, regmatch_t pmatch[], int eflags)
  991. {
  992. assert(nmatch > 0 && pmatch);
  993. pmatch[0].rm_so = 0;
  994. pmatch[0].rm_eo = size;
  995. return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND);
  996. }
  997. #ifndef DIR_HAS_BSD_GROUP_SEMANTICS
  998. # define FORCE_DIR_SET_GID S_ISGID
  999. #else
  1000. # define FORCE_DIR_SET_GID 0
  1001. #endif
  1002. #ifdef NO_NSEC
  1003. #undef USE_NSEC
  1004. #define ST_CTIME_NSEC(st) 0
  1005. #define ST_MTIME_NSEC(st) 0
  1006. #else
  1007. #ifdef USE_ST_TIMESPEC
  1008. #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))
  1009. #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))
  1010. #else
  1011. #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))
  1012. #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))
  1013. #endif
  1014. #endif
  1015. #ifdef UNRELIABLE_FSTAT
  1016. #define fstat_is_reliable() 0
  1017. #else
  1018. #define fstat_is_reliable() 1
  1019. #endif
  1020. #ifndef va_copy
  1021. /*
  1022. * Since an obvious implementation of va_list would be to make it a
  1023. * pointer into the stack frame, a simple assignment will work on
  1024. * many systems. But let's try to be more portable.
  1025. */
  1026. #ifdef __va_copy
  1027. #define va_copy(dst, src) __va_copy(dst, src)
  1028. #else
  1029. #define va_copy(dst, src) ((dst) = (src))
  1030. #endif
  1031. #endif
  1032. #if defined(__GNUC__) || (_MSC_VER >= 1400) || defined(__C99_MACRO_WITH_VA_ARGS)
  1033. #define HAVE_VARIADIC_MACROS 1
  1034. #endif
  1035. /* usage.c: only to be used for testing BUG() implementation (see test-tool) */
  1036. extern int BUG_exit_code;
  1037. #ifdef HAVE_VARIADIC_MACROS
  1038. __attribute__((format (printf, 3, 4))) NORETURN
  1039. void BUG_fl(const char *file, int line, const char *fmt, ...);
  1040. #define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__)
  1041. #else
  1042. __attribute__((format (printf, 1, 2))) NORETURN
  1043. void BUG(const char *fmt, ...);
  1044. #endif
  1045. /*
  1046. * Preserves errno, prints a message, but gives no warning for ENOENT.
  1047. * Returns 0 on success, which includes trying to unlink an object that does
  1048. * not exist.
  1049. */
  1050. int unlink_or_warn(const char *path);
  1051. /*
  1052. * Tries to unlink file. Returns 0 if unlink succeeded
  1053. * or the file already didn't exist. Returns -1 and
  1054. * appends a message to err suitable for
  1055. * 'error("%s", err->buf)' on error.
  1056. */
  1057. int unlink_or_msg(const char *file, struct strbuf *err);
  1058. /*
  1059. * Preserves errno, prints a message, but gives no warning for ENOENT.
  1060. * Returns 0 on success, which includes trying to remove a directory that does
  1061. * not exist.
  1062. */
  1063. int rmdir_or_warn(const char *path);
  1064. /*
  1065. * Calls the correct function out of {unlink,rmdir}_or_warn based on
  1066. * the supplied file mode.
  1067. */
  1068. int remove_or_warn(unsigned int mode, const char *path);
  1069. /*
  1070. * Call access(2), but warn for any error except "missing file"
  1071. * (ENOENT or ENOTDIR).
  1072. */
  1073. #define ACCESS_EACCES_OK (1U << 0)
  1074. int access_or_warn(const char *path, int mode, unsigned flag);
  1075. int access_or_die(const char *path, int mode, unsigned flag);
  1076. /* Warn on an inaccessible file if errno indicates this is an error */
  1077. int warn_on_fopen_errors(const char *path);
  1078. #ifdef GMTIME_UNRELIABLE_ERRORS
  1079. struct tm *git_gmtime(const time_t *);
  1080. struct tm *git_gmtime_r(const time_t *, struct tm *);
  1081. #define gmtime git_gmtime
  1082. #define gmtime_r git_gmtime_r
  1083. #endif
  1084. #if !defined(USE_PARENS_AROUND_GETTEXT_N) && defined(__GNUC__)
  1085. #define USE_PARENS_AROUND_GETTEXT_N 1
  1086. #endif
  1087. #ifndef SHELL_PATH
  1088. # define SHELL_PATH "/bin/sh"
  1089. #endif
  1090. #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
  1091. #define flockfile(fh)
  1092. #define funlockfile(fh)
  1093. #define getc_unlocked(fh) getc(fh)
  1094. #endif
  1095. #ifdef FILENO_IS_A_MACRO
  1096. int git_fileno(FILE *stream);
  1097. # ifndef COMPAT_CODE_FILENO
  1098. # undef fileno
  1099. # define fileno(p) git_fileno(p)
  1100. # endif
  1101. #endif
  1102. #ifdef NEED_ACCESS_ROOT_HANDLER
  1103. int git_access(const char *path, int mode);
  1104. # ifndef COMPAT_CODE_ACCESS
  1105. # ifdef access
  1106. # undef access
  1107. # endif
  1108. # define access(path, mode) git_access(path, mode)
  1109. # endif
  1110. #endif
  1111. /*
  1112. * Our code often opens a path to an optional file, to work on its
  1113. * contents when we can successfully open it. We can ignore a failure
  1114. * to open if such an optional file does not exist, but we do want to
  1115. * report a failure in opening for other reasons (e.g. we got an I/O
  1116. * error, or the file is there, but we lack the permission to open).
  1117. *
  1118. * Call this function after seeing an error from open() or fopen() to
  1119. * see if the errno indicates a missing file that we can safely ignore.
  1120. */
  1121. static inline int is_missing_file_error(int errno_)
  1122. {
  1123. return (errno_ == ENOENT || errno_ == ENOTDIR);
  1124. }
  1125. int cmd_main(int, const char **);
  1126. /*
  1127. * Intercept all calls to exit() and route them to trace2 to
  1128. * optionally emit a message before calling the real exit().
  1129. */
  1130. int trace2_cmd_exit_fl(const char *file, int line, int code);
  1131. #define exit(code) exit(trace2_cmd_exit_fl(__FILE__, __LINE__, (code)))
  1132. /*
  1133. * You can mark a stack variable with UNLEAK(var) to avoid it being
  1134. * reported as a leak by tools like LSAN or valgrind. The argument
  1135. * should generally be the variable itself (not its address and not what
  1136. * it points to). It's safe to use this on pointers which may already
  1137. * have been freed, or on pointers which may still be in use.
  1138. *
  1139. * Use this _only_ for a variable that leaks by going out of scope at
  1140. * program exit (so only from cmd_* functions or their direct helpers).
  1141. * Normal functions, especially those which may be called multiple
  1142. * times, should actually free their memory. This is only meant as
  1143. * an annotation, and does nothing in non-leak-checking builds.
  1144. */
  1145. #ifdef SUPPRESS_ANNOTATED_LEAKS
  1146. void unleak_memory(const void *ptr, size_t len);
  1147. #define UNLEAK(var) unleak_memory(&(var), sizeof(var))
  1148. #else
  1149. #define UNLEAK(var) do {} while (0)
  1150. #endif
  1151. /*
  1152. * This include must come after system headers, since it introduces macros that
  1153. * replace system names.
  1154. */
  1155. #include "banned.h"
  1156. #endif