PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/git-compat-util.h

https://gitlab.com/nmusco/git
C Header | 726 lines | 547 code | 104 blank | 75 comment | 51 complexity | 46ed7dd8b25acad2ec068c5f65c085cc MD5 | raw file
  1. #ifndef GIT_COMPAT_UTIL_H
  2. #define GIT_COMPAT_UTIL_H
  3. #define _FILE_OFFSET_BITS 64
  4. #ifndef FLEX_ARRAY
  5. /*
  6. * See if our compiler is known to support flexible array members.
  7. */
  8. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && (!defined(__SUNPRO_C) || (__SUNPRO_C > 0x580))
  9. # define FLEX_ARRAY /* empty */
  10. #elif defined(__GNUC__)
  11. # if (__GNUC__ >= 3)
  12. # define FLEX_ARRAY /* empty */
  13. # else
  14. # define FLEX_ARRAY 0 /* older GNU extension */
  15. # endif
  16. #endif
  17. /*
  18. * Otherwise, default to safer but a bit wasteful traditional style
  19. */
  20. #ifndef FLEX_ARRAY
  21. # define FLEX_ARRAY 1
  22. #endif
  23. #endif
  24. #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
  25. #define bitsizeof(x) (CHAR_BIT * sizeof(x))
  26. #define maximum_signed_value_of_type(a) \
  27. (INTMAX_MAX >> (bitsizeof(intmax_t) - bitsizeof(a)))
  28. #define maximum_unsigned_value_of_type(a) \
  29. (UINTMAX_MAX >> (bitsizeof(uintmax_t) - bitsizeof(a)))
  30. /*
  31. * Signed integer overflow is undefined in C, so here's a helper macro
  32. * to detect if the sum of two integers will overflow.
  33. *
  34. * Requires: a >= 0, typeof(a) equals typeof(b)
  35. */
  36. #define signed_add_overflows(a, b) \
  37. ((b) > maximum_signed_value_of_type(a) - (a))
  38. #define unsigned_add_overflows(a, b) \
  39. ((b) > maximum_unsigned_value_of_type(a) - (a))
  40. #ifdef __GNUC__
  41. #define TYPEOF(x) (__typeof__(x))
  42. #else
  43. #define TYPEOF(x)
  44. #endif
  45. #define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (bitsizeof(x) - (bits))))
  46. #define HAS_MULTI_BITS(i) ((i) & ((i) - 1)) /* checks if an integer has more than 1 bit set */
  47. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  48. /* Approximation of the length of the decimal representation of this type. */
  49. #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
  50. #if defined(__sun__)
  51. /*
  52. * On Solaris, when _XOPEN_EXTENDED is set, its header file
  53. * forces the programs to be XPG4v2, defeating any _XOPEN_SOURCE
  54. * setting to say we are XPG5 or XPG6. Also on Solaris,
  55. * XPG6 programs must be compiled with a c99 compiler, while
  56. * non XPG6 programs must be compiled with a pre-c99 compiler.
  57. */
  58. # if __STDC_VERSION__ - 0 >= 199901L
  59. # define _XOPEN_SOURCE 600
  60. # else
  61. # define _XOPEN_SOURCE 500
  62. # endif
  63. #elif !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && \
  64. !defined(_M_UNIX) && !defined(__sgi) && !defined(__DragonFly__) && \
  65. !defined(__TANDEM) && !defined(__QNX__) && !defined(__MirBSD__)
  66. #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
  67. #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
  68. #endif
  69. #define _ALL_SOURCE 1
  70. #define _GNU_SOURCE 1
  71. #define _BSD_SOURCE 1
  72. #define _NETBSD_SOURCE 1
  73. #define _SGI_SOURCE 1
  74. #if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */
  75. # if defined (_MSC_VER) && !defined(_WIN32_WINNT)
  76. # define _WIN32_WINNT 0x0502
  77. # endif
  78. #define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */
  79. #include <winsock2.h>
  80. #include <windows.h>
  81. #define GIT_WINDOWS_NATIVE
  82. #endif
  83. #include <unistd.h>
  84. #include <stdio.h>
  85. #include <sys/stat.h>
  86. #include <fcntl.h>
  87. #include <stddef.h>
  88. #include <stdlib.h>
  89. #include <stdarg.h>
  90. #include <string.h>
  91. #ifdef HAVE_STRINGS_H
  92. #include <strings.h> /* for strcasecmp() */
  93. #endif
  94. #include <errno.h>
  95. #include <limits.h>
  96. #ifdef NEEDS_SYS_PARAM_H
  97. #include <sys/param.h>
  98. #endif
  99. #include <sys/types.h>
  100. #include <dirent.h>
  101. #include <sys/time.h>
  102. #include <time.h>
  103. #include <signal.h>
  104. #include <assert.h>
  105. #include <regex.h>
  106. #include <utime.h>
  107. #include <syslog.h>
  108. #ifndef NO_SYS_POLL_H
  109. #include <sys/poll.h>
  110. #else
  111. #include <poll.h>
  112. #endif
  113. #if defined(__MINGW32__)
  114. /* pull in Windows compatibility stuff */
  115. #include "compat/mingw.h"
  116. #elif defined(_MSC_VER)
  117. #include "compat/msvc.h"
  118. #else
  119. #include <sys/wait.h>
  120. #include <sys/resource.h>
  121. #include <sys/socket.h>
  122. #include <sys/ioctl.h>
  123. #include <termios.h>
  124. #ifndef NO_SYS_SELECT_H
  125. #include <sys/select.h>
  126. #endif
  127. #include <netinet/in.h>
  128. #include <netinet/tcp.h>
  129. #include <arpa/inet.h>
  130. #include <netdb.h>
  131. #include <pwd.h>
  132. #include <sys/un.h>
  133. #ifndef NO_INTTYPES_H
  134. #include <inttypes.h>
  135. #else
  136. #include <stdint.h>
  137. #endif
  138. #ifdef NO_INTPTR_T
  139. /*
  140. * On I16LP32, ILP32 and LP64 "long" is the save bet, however
  141. * on LLP86, IL33LLP64 and P64 it needs to be "long long",
  142. * while on IP16 and IP16L32 it is "int" (resp. "short")
  143. * Size needs to match (or exceed) 'sizeof(void *)'.
  144. * We can't take "long long" here as not everybody has it.
  145. */
  146. typedef long intptr_t;
  147. typedef unsigned long uintptr_t;
  148. #endif
  149. #if defined(__CYGWIN__)
  150. #undef _XOPEN_SOURCE
  151. #include <grp.h>
  152. #define _XOPEN_SOURCE 600
  153. #else
  154. #undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */
  155. #include <grp.h>
  156. #define _ALL_SOURCE 1
  157. #endif
  158. #endif
  159. /* used on Mac OS X */
  160. #ifdef PRECOMPOSE_UNICODE
  161. #include "compat/precompose_utf8.h"
  162. #else
  163. #define precompose_str(in,i_nfd2nfc)
  164. #define precompose_argv(c,v)
  165. #define probe_utf8_pathname_composition(a,b)
  166. #endif
  167. #ifdef MKDIR_WO_TRAILING_SLASH
  168. #define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))
  169. extern int compat_mkdir_wo_trailing_slash(const char*, mode_t);
  170. #endif
  171. #ifdef NO_STRUCT_ITIMERVAL
  172. struct itimerval {
  173. struct timeval it_interval;
  174. struct timeval it_value;
  175. }
  176. #endif
  177. #ifdef NO_SETITIMER
  178. #define setitimer(which,value,ovalue)
  179. #endif
  180. #ifndef NO_LIBGEN_H
  181. #include <libgen.h>
  182. #else
  183. #define basename gitbasename
  184. extern char *gitbasename(char *);
  185. #endif
  186. #ifndef NO_ICONV
  187. #include <iconv.h>
  188. #endif
  189. #ifndef NO_OPENSSL
  190. #include <openssl/ssl.h>
  191. #include <openssl/err.h>
  192. #endif
  193. /* On most systems <netdb.h> would have given us this, but
  194. * not on some systems (e.g. z/OS).
  195. */
  196. #ifndef NI_MAXHOST
  197. #define NI_MAXHOST 1025
  198. #endif
  199. #ifndef NI_MAXSERV
  200. #define NI_MAXSERV 32
  201. #endif
  202. /* On most systems <limits.h> would have given us this, but
  203. * not on some systems (e.g. GNU/Hurd).
  204. */
  205. #ifndef PATH_MAX
  206. #define PATH_MAX 4096
  207. #endif
  208. #ifndef PRIuMAX
  209. #define PRIuMAX "llu"
  210. #endif
  211. #ifndef PRIu32
  212. #define PRIu32 "u"
  213. #endif
  214. #ifndef PRIx32
  215. #define PRIx32 "x"
  216. #endif
  217. #ifndef PRIo32
  218. #define PRIo32 "o"
  219. #endif
  220. #ifndef PATH_SEP
  221. #define PATH_SEP ':'
  222. #endif
  223. #ifdef HAVE_PATHS_H
  224. #include <paths.h>
  225. #endif
  226. #ifndef _PATH_DEFPATH
  227. #define _PATH_DEFPATH "/usr/local/bin:/usr/bin:/bin"
  228. #endif
  229. #ifndef STRIP_EXTENSION
  230. #define STRIP_EXTENSION ""
  231. #endif
  232. #ifndef has_dos_drive_prefix
  233. #define has_dos_drive_prefix(path) 0
  234. #endif
  235. #ifndef is_dir_sep
  236. #define is_dir_sep(c) ((c) == '/')
  237. #endif
  238. #ifndef find_last_dir_sep
  239. #define find_last_dir_sep(path) strrchr(path, '/')
  240. #endif
  241. #if defined(__HP_cc) && (__HP_cc >= 61000)
  242. #define NORETURN __attribute__((noreturn))
  243. #define NORETURN_PTR
  244. #elif defined(__GNUC__) && !defined(NO_NORETURN)
  245. #define NORETURN __attribute__((__noreturn__))
  246. #define NORETURN_PTR __attribute__((__noreturn__))
  247. #elif defined(_MSC_VER)
  248. #define NORETURN __declspec(noreturn)
  249. #define NORETURN_PTR
  250. #else
  251. #define NORETURN
  252. #define NORETURN_PTR
  253. #ifndef __attribute__
  254. #define __attribute__(x)
  255. #endif
  256. #endif
  257. /* The sentinel attribute is valid from gcc version 4.0 */
  258. #if defined(__GNUC__) && (__GNUC__ >= 4)
  259. #define LAST_ARG_MUST_BE_NULL __attribute__((sentinel))
  260. #else
  261. #define LAST_ARG_MUST_BE_NULL
  262. #endif
  263. #include "compat/bswap.h"
  264. #include "wildmatch.h"
  265. /* General helper functions */
  266. extern void vreportf(const char *prefix, const char *err, va_list params);
  267. extern void vwritef(int fd, const char *prefix, const char *err, va_list params);
  268. extern NORETURN void usage(const char *err);
  269. extern NORETURN void usagef(const char *err, ...) __attribute__((format (printf, 1, 2)));
  270. extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
  271. extern NORETURN void die_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
  272. extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
  273. extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
  274. #ifndef NO_OPENSSL
  275. #ifdef APPLE_COMMON_CRYPTO
  276. #include "compat/apple-common-crypto.h"
  277. #else
  278. #include <openssl/evp.h>
  279. #include <openssl/hmac.h>
  280. #endif /* APPLE_COMMON_CRYPTO */
  281. #include <openssl/x509v3.h>
  282. #endif /* NO_OPENSSL */
  283. /*
  284. * Let callers be aware of the constant return value; this can help
  285. * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
  286. * because some compilers may not support variadic macros. Since we're only
  287. * trying to help gcc, anyway, it's OK; other compilers will fall back to
  288. * using the function as usual.
  289. */
  290. #if defined(__GNUC__) && ! defined(__clang__)
  291. #define error(...) (error(__VA_ARGS__), -1)
  292. #endif
  293. extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
  294. extern void set_error_routine(void (*routine)(const char *err, va_list params));
  295. extern void set_die_is_recursing_routine(int (*routine)(void));
  296. extern int starts_with(const char *str, const char *prefix);
  297. extern int ends_with(const char *str, const char *suffix);
  298. static inline const char *skip_prefix(const char *str, const char *prefix)
  299. {
  300. do {
  301. if (!*prefix)
  302. return str;
  303. } while (*str++ == *prefix++);
  304. return NULL;
  305. }
  306. #if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
  307. #ifndef PROT_READ
  308. #define PROT_READ 1
  309. #define PROT_WRITE 2
  310. #define MAP_PRIVATE 1
  311. #endif
  312. #define mmap git_mmap
  313. #define munmap git_munmap
  314. extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
  315. extern int git_munmap(void *start, size_t length);
  316. #else /* NO_MMAP || USE_WIN32_MMAP */
  317. #include <sys/mman.h>
  318. #endif /* NO_MMAP || USE_WIN32_MMAP */
  319. #ifdef NO_MMAP
  320. /* This value must be multiple of (pagesize * 2) */
  321. #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
  322. #else /* NO_MMAP */
  323. /* This value must be multiple of (pagesize * 2) */
  324. #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
  325. (sizeof(void*) >= 8 \
  326. ? 1 * 1024 * 1024 * 1024 \
  327. : 32 * 1024 * 1024)
  328. #endif /* NO_MMAP */
  329. #ifndef MAP_FAILED
  330. #define MAP_FAILED ((void *)-1)
  331. #endif
  332. #ifdef NO_ST_BLOCKS_IN_STRUCT_STAT
  333. #define on_disk_bytes(st) ((st).st_size)
  334. #else
  335. #define on_disk_bytes(st) ((st).st_blocks * 512)
  336. #endif
  337. #define DEFAULT_PACKED_GIT_LIMIT \
  338. ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
  339. #ifdef NO_PREAD
  340. #define pread git_pread
  341. extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
  342. #endif
  343. /*
  344. * Forward decl that will remind us if its twin in cache.h changes.
  345. * This function is used in compat/pread.c. But we can't include
  346. * cache.h there.
  347. */
  348. extern ssize_t read_in_full(int fd, void *buf, size_t count);
  349. #ifdef NO_SETENV
  350. #define setenv gitsetenv
  351. extern int gitsetenv(const char *, const char *, int);
  352. #endif
  353. #ifdef NO_MKDTEMP
  354. #define mkdtemp gitmkdtemp
  355. extern char *gitmkdtemp(char *);
  356. #endif
  357. #ifdef NO_MKSTEMPS
  358. #define mkstemps gitmkstemps
  359. extern int gitmkstemps(char *, int);
  360. #endif
  361. #ifdef NO_UNSETENV
  362. #define unsetenv gitunsetenv
  363. extern void gitunsetenv(const char *);
  364. #endif
  365. #ifdef NO_STRCASESTR
  366. #define strcasestr gitstrcasestr
  367. extern char *gitstrcasestr(const char *haystack, const char *needle);
  368. #endif
  369. #ifdef NO_STRLCPY
  370. #define strlcpy gitstrlcpy
  371. extern size_t gitstrlcpy(char *, const char *, size_t);
  372. #endif
  373. #ifdef NO_STRTOUMAX
  374. #define strtoumax gitstrtoumax
  375. extern uintmax_t gitstrtoumax(const char *, char **, int);
  376. #define strtoimax gitstrtoimax
  377. extern intmax_t gitstrtoimax(const char *, char **, int);
  378. #endif
  379. #ifdef NO_HSTRERROR
  380. #define hstrerror githstrerror
  381. extern const char *githstrerror(int herror);
  382. #endif
  383. #ifdef NO_MEMMEM
  384. #define memmem gitmemmem
  385. void *gitmemmem(const void *haystack, size_t haystacklen,
  386. const void *needle, size_t needlelen);
  387. #endif
  388. #ifdef NO_GETPAGESIZE
  389. #define getpagesize() sysconf(_SC_PAGESIZE)
  390. #endif
  391. #ifdef FREAD_READS_DIRECTORIES
  392. #ifdef fopen
  393. #undef fopen
  394. #endif
  395. #define fopen(a,b) git_fopen(a,b)
  396. extern FILE *git_fopen(const char*, const char*);
  397. #endif
  398. #ifdef SNPRINTF_RETURNS_BOGUS
  399. #ifdef snprintf
  400. #undef snprintf
  401. #endif
  402. #define snprintf git_snprintf
  403. extern int git_snprintf(char *str, size_t maxsize,
  404. const char *format, ...);
  405. #ifdef vsnprintf
  406. #undef vsnprintf
  407. #endif
  408. #define vsnprintf git_vsnprintf
  409. extern int git_vsnprintf(char *str, size_t maxsize,
  410. const char *format, va_list ap);
  411. #endif
  412. #ifdef __GLIBC_PREREQ
  413. #if __GLIBC_PREREQ(2, 1)
  414. #define HAVE_STRCHRNUL
  415. #define HAVE_MEMPCPY
  416. #endif
  417. #endif
  418. #ifndef HAVE_STRCHRNUL
  419. #define strchrnul gitstrchrnul
  420. static inline char *gitstrchrnul(const char *s, int c)
  421. {
  422. while (*s && *s != c)
  423. s++;
  424. return (char *)s;
  425. }
  426. #endif
  427. #ifndef HAVE_MEMPCPY
  428. #define mempcpy gitmempcpy
  429. static inline void *gitmempcpy(void *dest, const void *src, size_t n)
  430. {
  431. return (char *)memcpy(dest, src, n) + n;
  432. }
  433. #endif
  434. #ifdef NO_INET_PTON
  435. int inet_pton(int af, const char *src, void *dst);
  436. #endif
  437. #ifdef NO_INET_NTOP
  438. const char *inet_ntop(int af, const void *src, char *dst, size_t size);
  439. #endif
  440. extern void release_pack_memory(size_t);
  441. typedef void (*try_to_free_t)(size_t);
  442. extern try_to_free_t set_try_to_free_routine(try_to_free_t);
  443. extern char *xstrdup(const char *str);
  444. extern void *xmalloc(size_t size);
  445. extern void *xmallocz(size_t size);
  446. extern void *xmemdupz(const void *data, size_t len);
  447. extern char *xstrndup(const char *str, size_t len);
  448. extern void *xrealloc(void *ptr, size_t size);
  449. extern void *xcalloc(size_t nmemb, size_t size);
  450. extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
  451. extern ssize_t xread(int fd, void *buf, size_t len);
  452. extern ssize_t xwrite(int fd, const void *buf, size_t len);
  453. extern int xdup(int fd);
  454. extern FILE *xfdopen(int fd, const char *mode);
  455. extern int xmkstemp(char *template);
  456. extern int xmkstemp_mode(char *template, int mode);
  457. extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
  458. extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
  459. static inline size_t xsize_t(off_t len)
  460. {
  461. if (len > (size_t) len)
  462. die("Cannot handle files this big");
  463. return (size_t)len;
  464. }
  465. static inline int has_extension(const char *filename, const char *ext)
  466. {
  467. size_t len = strlen(filename);
  468. size_t extlen = strlen(ext);
  469. return len > extlen && !memcmp(filename + len - extlen, ext, extlen);
  470. }
  471. /* in ctype.c, for kwset users */
  472. extern const char tolower_trans_tbl[256];
  473. /* Sane ctype - no locale, and works with signed chars */
  474. #undef isascii
  475. #undef isspace
  476. #undef isdigit
  477. #undef isalpha
  478. #undef isalnum
  479. #undef isprint
  480. #undef islower
  481. #undef isupper
  482. #undef tolower
  483. #undef toupper
  484. #undef iscntrl
  485. #undef ispunct
  486. #undef isxdigit
  487. extern const unsigned char sane_ctype[256];
  488. #define GIT_SPACE 0x01
  489. #define GIT_DIGIT 0x02
  490. #define GIT_ALPHA 0x04
  491. #define GIT_GLOB_SPECIAL 0x08
  492. #define GIT_REGEX_SPECIAL 0x10
  493. #define GIT_PATHSPEC_MAGIC 0x20
  494. #define GIT_CNTRL 0x40
  495. #define GIT_PUNCT 0x80
  496. #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
  497. #define isascii(x) (((x) & ~0x7f) == 0)
  498. #define isspace(x) sane_istest(x,GIT_SPACE)
  499. #define isdigit(x) sane_istest(x,GIT_DIGIT)
  500. #define isalpha(x) sane_istest(x,GIT_ALPHA)
  501. #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
  502. #define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
  503. #define islower(x) sane_iscase(x, 1)
  504. #define isupper(x) sane_iscase(x, 0)
  505. #define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
  506. #define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
  507. #define iscntrl(x) (sane_istest(x,GIT_CNTRL))
  508. #define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \
  509. GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC)
  510. #define isxdigit(x) (hexval_table[x] != -1)
  511. #define tolower(x) sane_case((unsigned char)(x), 0x20)
  512. #define toupper(x) sane_case((unsigned char)(x), 0)
  513. #define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
  514. static inline int sane_case(int x, int high)
  515. {
  516. if (sane_istest(x, GIT_ALPHA))
  517. x = (x & ~0x20) | high;
  518. return x;
  519. }
  520. static inline int sane_iscase(int x, int is_lower)
  521. {
  522. if (!sane_istest(x, GIT_ALPHA))
  523. return 0;
  524. if (is_lower)
  525. return (x & 0x20) != 0;
  526. else
  527. return (x & 0x20) == 0;
  528. }
  529. static inline int strtoul_ui(char const *s, int base, unsigned int *result)
  530. {
  531. unsigned long ul;
  532. char *p;
  533. errno = 0;
  534. ul = strtoul(s, &p, base);
  535. if (errno || *p || p == s || (unsigned int) ul != ul)
  536. return -1;
  537. *result = ul;
  538. return 0;
  539. }
  540. static inline int strtol_i(char const *s, int base, int *result)
  541. {
  542. long ul;
  543. char *p;
  544. errno = 0;
  545. ul = strtol(s, &p, base);
  546. if (errno || *p || p == s || (int) ul != ul)
  547. return -1;
  548. *result = ul;
  549. return 0;
  550. }
  551. #ifdef INTERNAL_QSORT
  552. void git_qsort(void *base, size_t nmemb, size_t size,
  553. int(*compar)(const void *, const void *));
  554. #define qsort git_qsort
  555. #endif
  556. #ifndef DIR_HAS_BSD_GROUP_SEMANTICS
  557. # define FORCE_DIR_SET_GID S_ISGID
  558. #else
  559. # define FORCE_DIR_SET_GID 0
  560. #endif
  561. #ifdef NO_NSEC
  562. #undef USE_NSEC
  563. #define ST_CTIME_NSEC(st) 0
  564. #define ST_MTIME_NSEC(st) 0
  565. #else
  566. #ifdef USE_ST_TIMESPEC
  567. #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))
  568. #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))
  569. #else
  570. #define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))
  571. #define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))
  572. #endif
  573. #endif
  574. #ifdef UNRELIABLE_FSTAT
  575. #define fstat_is_reliable() 0
  576. #else
  577. #define fstat_is_reliable() 1
  578. #endif
  579. #ifndef va_copy
  580. /*
  581. * Since an obvious implementation of va_list would be to make it a
  582. * pointer into the stack frame, a simple assignment will work on
  583. * many systems. But let's try to be more portable.
  584. */
  585. #ifdef __va_copy
  586. #define va_copy(dst, src) __va_copy(dst, src)
  587. #else
  588. #define va_copy(dst, src) ((dst) = (src))
  589. #endif
  590. #endif
  591. /*
  592. * Preserves errno, prints a message, but gives no warning for ENOENT.
  593. * Always returns the return value of unlink(2).
  594. */
  595. int unlink_or_warn(const char *path);
  596. /*
  597. * Likewise for rmdir(2).
  598. */
  599. int rmdir_or_warn(const char *path);
  600. /*
  601. * Calls the correct function out of {unlink,rmdir}_or_warn based on
  602. * the supplied file mode.
  603. */
  604. int remove_or_warn(unsigned int mode, const char *path);
  605. /*
  606. * Call access(2), but warn for any error except "missing file"
  607. * (ENOENT or ENOTDIR).
  608. */
  609. #define ACCESS_EACCES_OK (1U << 0)
  610. int access_or_warn(const char *path, int mode, unsigned flag);
  611. int access_or_die(const char *path, int mode, unsigned flag);
  612. /* Warn on an inaccessible file that ought to be accessible */
  613. void warn_on_inaccessible(const char *path);
  614. /* Get the passwd entry for the UID of the current process. */
  615. struct passwd *xgetpwuid_self(void);
  616. #ifdef GMTIME_UNRELIABLE_ERRORS
  617. struct tm *git_gmtime(const time_t *);
  618. struct tm *git_gmtime_r(const time_t *, struct tm *);
  619. #define gmtime git_gmtime
  620. #define gmtime_r git_gmtime_r
  621. #endif
  622. #endif