git-compat-util.h: split out POSIX-emulating bits

The "git-compat-util.h" header is a treasure trove of various bits and pieces used throughout the project. It basically mixes two different things into one: - Providing a POSIX-like interface even on platforms that aren't POSIX-compliant. - Providing low-level functionality that is specific to Git. This intermixing is a bit of a problem for the reftable library as we don't want to recreate the POSIX-like interface there. But neither do we want to pull in the Git-specific functionality, as it is otherwise quite easy to start depending on the Git codebase again. Split out a new header "compat/posix.h" that only contains the bits and pieces relevant for the emulation of POSIX, which we will start using in the next commit. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Feb 18, 2025 at 10:20 UTC 75a044f748f2c37a2a08854acd49ed1fbdb86bb1
2 files changed +543 -529
compat/posix.h new
+541
@@ -0,0 +1,541 @@
1 +#ifndef COMPAT_POSIX_H
2 +#define COMPAT_POSIX_H
3 +
4 +#define _FILE_OFFSET_BITS 64
5 +
6 +/*
7 + * Derived from Linux "Features Test Macro" header
8 + * Convenience macros to test the versions of gcc (or
9 + * a compatible compiler).
10 + * Use them like this:
11 + * #if GIT_GNUC_PREREQ (2,8)
12 + * ... code requiring gcc 2.8 or later ...
13 + * #endif
14 + *
15 + * This macro of course is not part of POSIX, but we need it for the UNUSED
16 + * macro which is used by some of our POSIX compatibility wrappers.
17 +*/
18 +#if defined(__GNUC__) && defined(__GNUC_MINOR__)
19 +# define GIT_GNUC_PREREQ(maj, min) \
20 + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
21 +#else
22 + #define GIT_GNUC_PREREQ(maj, min) 0
23 +#endif
24 +
25 +/*
26 + * UNUSED marks a function parameter that is always unused. It also
27 + * can be used to annotate a function, a variable, or a type that is
28 + * always unused.
29 + *
30 + * A callback interface may dictate that a function accepts a
31 + * parameter at that position, but the implementation of the function
32 + * may not need to use the parameter. In such a case, mark the parameter
33 + * with UNUSED.
34 + *
35 + * When a parameter may be used or unused, depending on conditional
36 + * compilation, consider using MAYBE_UNUSED instead.
37 + */
38 +#if GIT_GNUC_PREREQ(4, 5)
39 +#define UNUSED __attribute__((unused)) \
40 + __attribute__((deprecated ("parameter declared as UNUSED")))
41 +#elif defined(__GNUC__)
42 +#define UNUSED __attribute__((unused)) \
43 + __attribute__((deprecated))
44 +#else
45 +#define UNUSED
46 +#endif
47 +
48 +#ifdef __MINGW64__
49 +#define _POSIX_C_SOURCE 1
50 +#elif 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 + !defined(__CYGWIN__)
67 +#define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
68 +#define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
69 +#endif
70 +#define _ALL_SOURCE 1
71 +#define _GNU_SOURCE 1
72 +#define _BSD_SOURCE 1
73 +#define _DEFAULT_SOURCE 1
74 +#define _NETBSD_SOURCE 1
75 +#define _SGI_SOURCE 1
76 +
77 +#if defined(WIN32) && !defined(__CYGWIN__) /* Both MinGW and MSVC */
78 +# if !defined(_WIN32_WINNT)
79 +# define _WIN32_WINNT 0x0600
80 +# endif
81 +#define WIN32_LEAN_AND_MEAN /* stops windows.h including winsock.h */
82 +#include <winsock2.h>
83 +#ifndef NO_UNIX_SOCKETS
84 +#include <afunix.h>
85 +#endif
86 +#include <windows.h>
87 +#define GIT_WINDOWS_NATIVE
88 +#endif
89 +
90 +#include <unistd.h>
91 +#include <stdio.h>
92 +#include <sys/stat.h>
93 +#include <fcntl.h>
94 +#include <stddef.h>
95 +#include <stdlib.h>
96 +#include <stdarg.h>
97 +#include <stdbool.h>
98 +#include <string.h>
99 +#ifdef HAVE_STRINGS_H
100 +#include <strings.h> /* for strcasecmp() */
101 +#endif
102 +#include <errno.h>
103 +#include <limits.h>
104 +#include <locale.h>
105 +#ifdef NEEDS_SYS_PARAM_H
106 +#include <sys/param.h>
107 +#endif
108 +#include <sys/types.h>
109 +#include <dirent.h>
110 +#include <sys/time.h>
111 +#include <time.h>
112 +#include <signal.h>
113 +#include <assert.h>
114 +#include <regex.h>
115 +#include <utime.h>
116 +#include <syslog.h>
117 +#if !defined(NO_POLL_H)
118 +#include <poll.h>
119 +#elif !defined(NO_SYS_POLL_H)
120 +#include <sys/poll.h>
121 +#else
122 +/* Pull the compat stuff */
123 +#include <poll.h>
124 +#endif
125 +#ifdef HAVE_BSD_SYSCTL
126 +#include <sys/sysctl.h>
127 +#endif
128 +
129 +#if defined(__MINGW32__)
130 +#include "mingw-posix.h"
131 +#elif defined(_MSC_VER)
132 +#include "msvc-posix.h"
133 +#else
134 +#include <sys/utsname.h>
135 +#include <sys/wait.h>
136 +#include <sys/resource.h>
137 +#include <sys/socket.h>
138 +#include <sys/ioctl.h>
139 +#include <sys/statvfs.h>
140 +#include <termios.h>
141 +#ifndef NO_SYS_SELECT_H
142 +#include <sys/select.h>
143 +#endif
144 +#include <netinet/in.h>
145 +#include <netinet/tcp.h>
146 +#include <arpa/inet.h>
147 +#include <netdb.h>
148 +#include <pwd.h>
149 +#include <sys/un.h>
150 +#ifndef NO_INTTYPES_H
151 +#include <inttypes.h>
152 +#else
153 +#include <stdint.h>
154 +#endif
155 +#ifdef HAVE_ARC4RANDOM_LIBBSD
156 +#include <bsd/stdlib.h>
157 +#endif
158 +#ifdef HAVE_GETRANDOM
159 +#include <sys/random.h>
160 +#endif
161 +#ifdef NO_INTPTR_T
162 +/*
163 + * On I16LP32, ILP32 and LP64 "long" is the safe bet, however
164 + * on LLP86, IL33LLP64 and P64 it needs to be "long long",
165 + * while on IP16 and IP16L32 it is "int" (resp. "short")
166 + * Size needs to match (or exceed) 'sizeof(void *)'.
167 + * We can't take "long long" here as not everybody has it.
168 + */
169 +typedef long intptr_t;
170 +typedef unsigned long uintptr_t;
171 +#endif
172 +#undef _ALL_SOURCE /* AIX 5.3L defines a struct list with _ALL_SOURCE. */
173 +#include <grp.h>
174 +#define _ALL_SOURCE 1
175 +#endif
176 +
177 +#ifdef MKDIR_WO_TRAILING_SLASH
178 +#define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))
179 +int compat_mkdir_wo_trailing_slash(const char*, mode_t);
180 +#endif
181 +
182 +#ifdef time
183 +#undef time
184 +#endif
185 +static inline time_t git_time(time_t *tloc)
186 +{
187 + struct timeval tv;
188 +
189 + /*
190 + * Avoid time(NULL), which can disagree with gettimeofday(2)
191 + * and filesystem timestamps.
192 + */
193 + gettimeofday(&tv, NULL);
194 +
195 + if (tloc)
196 + *tloc = tv.tv_sec;
197 + return tv.tv_sec;
198 +}
199 +#define time git_time
200 +
201 +#ifdef NO_STRUCT_ITIMERVAL
202 +struct itimerval {
203 + struct timeval it_interval;
204 + struct timeval it_value;
205 +};
206 +#endif
207 +
208 +#ifdef NO_SETITIMER
209 +static inline int git_setitimer(int which UNUSED,
210 + const struct itimerval *value UNUSED,
211 + struct itimerval *newvalue UNUSED) {
212 + return 0; /* pretend success */
213 +}
214 +#undef setitimer
215 +#define setitimer(which,value,ovalue) git_setitimer(which,value,ovalue)
216 +#endif
217 +
218 +#ifndef NO_LIBGEN_H
219 +#include <libgen.h>
220 +#else
221 +#define basename gitbasename
222 +char *gitbasename(char *);
223 +#define dirname gitdirname
224 +char *gitdirname(char *);
225 +#endif
226 +
227 +#ifndef NO_ICONV
228 +#include <iconv.h>
229 +#endif
230 +
231 +/* On most systems <netdb.h> would have given us this, but
232 + * not on some systems (e.g. z/OS).
233 + */
234 +#ifndef NI_MAXHOST
235 +#define NI_MAXHOST 1025
236 +#endif
237 +
238 +#ifndef NI_MAXSERV
239 +#define NI_MAXSERV 32
240 +#endif
241 +
242 +/* On most systems <limits.h> would have given us this, but
243 + * not on some systems (e.g. GNU/Hurd).
244 + */
245 +#ifndef PATH_MAX
246 +#define PATH_MAX 4096
247 +#endif
248 +
249 +#ifndef NAME_MAX
250 +#define NAME_MAX 255
251 +#endif
252 +
253 +typedef uintmax_t timestamp_t;
254 +#define PRItime PRIuMAX
255 +#define parse_timestamp strtoumax
256 +#define TIME_MAX UINTMAX_MAX
257 +#define TIME_MIN 0
258 +
259 +int lstat_cache_aware_rmdir(const char *path);
260 +#if !defined(__MINGW32__) && !defined(_MSC_VER)
261 +#define rmdir lstat_cache_aware_rmdir
262 +#endif
263 +
264 +#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
265 +
266 +#ifndef PROT_READ
267 +#define PROT_READ 1
268 +#define PROT_WRITE 2
269 +#define MAP_PRIVATE 1
270 +#endif
271 +
272 +#define mmap git_mmap
273 +#define munmap git_munmap
274 +void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
275 +int git_munmap(void *start, size_t length);
276 +
277 +#else /* NO_MMAP || USE_WIN32_MMAP */
278 +
279 +#include <sys/mman.h>
280 +
281 +#endif /* NO_MMAP || USE_WIN32_MMAP */
282 +
283 +#ifndef MAP_FAILED
284 +#define MAP_FAILED ((void *)-1)
285 +#endif
286 +
287 +#ifdef NEEDS_MODE_TRANSLATION
288 +#undef S_IFMT
289 +#undef S_IFREG
290 +#undef S_IFDIR
291 +#undef S_IFLNK
292 +#undef S_IFBLK
293 +#undef S_IFCHR
294 +#undef S_IFIFO
295 +#undef S_IFSOCK
296 +#define S_IFMT 0170000
297 +#define S_IFREG 0100000
298 +#define S_IFDIR 0040000
299 +#define S_IFLNK 0120000
300 +#define S_IFBLK 0060000
301 +#define S_IFCHR 0020000
302 +#define S_IFIFO 0010000
303 +#define S_IFSOCK 0140000
304 +#ifdef stat
305 +#undef stat
306 +#endif
307 +#define stat(path, buf) git_stat(path, buf)
308 +int git_stat(const char *, struct stat *);
309 +#ifdef fstat
310 +#undef fstat
311 +#endif
312 +#define fstat(fd, buf) git_fstat(fd, buf)
313 +int git_fstat(int, struct stat *);
314 +#ifdef lstat
315 +#undef lstat
316 +#endif
317 +#define lstat(path, buf) git_lstat(path, buf)
318 +int git_lstat(const char *, struct stat *);
319 +#endif
320 +
321 +#ifdef NO_PREAD
322 +#define pread git_pread
323 +ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
324 +#endif
325 +
326 +#ifdef NO_SETENV
327 +#define setenv gitsetenv
328 +int gitsetenv(const char *, const char *, int);
329 +#endif
330 +
331 +#ifdef NO_MKDTEMP
332 +#define mkdtemp gitmkdtemp
333 +char *gitmkdtemp(char *);
334 +#endif
335 +
336 +#ifdef NO_UNSETENV
337 +#define unsetenv gitunsetenv
338 +int gitunsetenv(const char *);
339 +#endif
340 +
341 +#ifdef NO_STRCASESTR
342 +#define strcasestr gitstrcasestr
343 +char *gitstrcasestr(const char *haystack, const char *needle);
344 +#endif
345 +
346 +#ifdef NO_STRLCPY
347 +#define strlcpy gitstrlcpy
348 +size_t gitstrlcpy(char *, const char *, size_t);
349 +#endif
350 +
351 +#ifdef NO_STRTOUMAX
352 +#define strtoumax gitstrtoumax
353 +uintmax_t gitstrtoumax(const char *, char **, int);
354 +#define strtoimax gitstrtoimax
355 +intmax_t gitstrtoimax(const char *, char **, int);
356 +#endif
357 +
358 +#ifdef NO_HSTRERROR
359 +#define hstrerror githstrerror
360 +const char *githstrerror(int herror);
361 +#endif
362 +
363 +#ifdef NO_MEMMEM
364 +#define memmem gitmemmem
365 +void *gitmemmem(const void *haystack, size_t haystacklen,
366 + const void *needle, size_t needlelen);
367 +#endif
368 +
369 +#ifdef OVERRIDE_STRDUP
370 +#ifdef strdup
371 +#undef strdup
372 +#endif
373 +#define strdup gitstrdup
374 +char *gitstrdup(const char *s);
375 +#endif
376 +
377 +#ifdef NO_GETPAGESIZE
378 +#define getpagesize() sysconf(_SC_PAGESIZE)
379 +#endif
380 +
381 +#ifndef O_CLOEXEC
382 +#define O_CLOEXEC 0
383 +#endif
384 +
385 +#ifdef FREAD_READS_DIRECTORIES
386 +# if !defined(SUPPRESS_FOPEN_REDEFINITION)
387 +# ifdef fopen
388 +# undef fopen
389 +# endif
390 +# define fopen(a,b) git_fopen(a,b)
391 +# endif
392 +FILE *git_fopen(const char*, const char*);
393 +#endif
394 +
395 +#ifdef SNPRINTF_RETURNS_BOGUS
396 +#ifdef snprintf
397 +#undef snprintf
398 +#endif
399 +#define snprintf git_snprintf
400 +int git_snprintf(char *str, size_t maxsize,
401 + const char *format, ...);
402 +#ifdef vsnprintf
403 +#undef vsnprintf
404 +#endif
405 +#define vsnprintf git_vsnprintf
406 +int git_vsnprintf(char *str, size_t maxsize,
407 + const char *format, va_list ap);
408 +#endif
409 +
410 +#ifdef OPEN_RETURNS_EINTR
411 +#undef open
412 +#define open git_open_with_retry
413 +int git_open_with_retry(const char *path, int flag, ...);
414 +#endif
415 +
416 +#ifdef __GLIBC_PREREQ
417 +#if __GLIBC_PREREQ(2, 1)
418 +#define HAVE_STRCHRNUL
419 +#endif
420 +#endif
421 +
422 +#ifndef HAVE_STRCHRNUL
423 +#define strchrnul gitstrchrnul
424 +static inline char *gitstrchrnul(const char *s, int c)
425 +{
426 + while (*s && *s != c)
427 + s++;
428 + return (char *)s;
429 +}
430 +#endif
431 +
432 +#ifdef NO_INET_PTON
433 +int inet_pton(int af, const char *src, void *dst);
434 +#endif
435 +
436 +#ifdef NO_INET_NTOP
437 +const char *inet_ntop(int af, const void *src, char *dst, size_t size);
438 +#endif
439 +
440 +#ifdef NO_PTHREADS
441 +#define atexit git_atexit
442 +int git_atexit(void (*handler)(void));
443 +#endif
444 +
445 +#ifndef HOST_NAME_MAX
446 +#define HOST_NAME_MAX 256
447 +#endif
448 +
449 +#include "../sane-ctype.h"
450 +
451 +void git_stable_qsort(void *base, size_t nmemb, size_t size,
452 + int(*compar)(const void *, const void *));
453 +#ifdef INTERNAL_QSORT
454 +#define qsort git_stable_qsort
455 +#endif
456 +
457 +#define QSORT(base, n, compar) sane_qsort((base), (n), sizeof(*(base)), compar)
458 +static inline void sane_qsort(void *base, size_t nmemb, size_t size,
459 + int(*compar)(const void *, const void *))
460 +{
461 + if (nmemb > 1)
462 + qsort(base, nmemb, size, compar);
463 +}
464 +
465 +#define STABLE_QSORT(base, n, compar) \
466 + git_stable_qsort((base), (n), sizeof(*(base)), compar)
467 +
468 +#ifndef HAVE_ISO_QSORT_S
469 +int git_qsort_s(void *base, size_t nmemb, size_t size,
470 + int (*compar)(const void *, const void *, void *), void *ctx);
471 +#define qsort_s git_qsort_s
472 +#endif
473 +
474 +#define QSORT_S(base, n, compar, ctx) do { \
475 + if (qsort_s((base), (n), sizeof(*(base)), compar, ctx)) \
476 + BUG("qsort_s() failed"); \
477 +} while (0)
478 +
479 +#ifdef NO_NSEC
480 +#undef USE_NSEC
481 +#define ST_CTIME_NSEC(st) 0
482 +#define ST_MTIME_NSEC(st) 0
483 +#else
484 +#ifdef USE_ST_TIMESPEC
485 +#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctimespec.tv_nsec))
486 +#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtimespec.tv_nsec))
487 +#else
488 +#define ST_CTIME_NSEC(st) ((unsigned int)((st).st_ctim.tv_nsec))
489 +#define ST_MTIME_NSEC(st) ((unsigned int)((st).st_mtim.tv_nsec))
490 +#endif
491 +#endif
492 +
493 +#ifndef va_copy
494 +/*
495 + * Since an obvious implementation of va_list would be to make it a
496 + * pointer into the stack frame, a simple assignment will work on
497 + * many systems. But let's try to be more portable.
498 + */
499 +#ifdef __va_copy
500 +#define va_copy(dst, src) __va_copy(dst, src)
501 +#else
502 +#define va_copy(dst, src) ((dst) = (src))
503 +#endif
504 +#endif
505 +
506 +#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
507 +static inline void git_flockfile(FILE *fh UNUSED)
508 +{
509 + ; /* nothing */
510 +}
511 +static inline void git_funlockfile(FILE *fh UNUSED)
512 +{
513 + ; /* nothing */
514 +}
515 +#undef flockfile
516 +#undef funlockfile
517 +#undef getc_unlocked
518 +#define flockfile(fh) git_flockfile(fh)
519 +#define funlockfile(fh) git_funlockfile(fh)
520 +#define getc_unlocked(fh) getc(fh)
521 +#endif
522 +
523 +#ifdef FILENO_IS_A_MACRO
524 +int git_fileno(FILE *stream);
525 +# ifndef COMPAT_CODE_FILENO
526 +# undef fileno
527 +# define fileno(p) git_fileno(p)
528 +# endif
529 +#endif
530 +
531 +#ifdef NEED_ACCESS_ROOT_HANDLER
532 +int git_access(const char *path, int mode);
533 +# ifndef COMPAT_CODE_ACCESS
534 +# ifdef access
535 +# undef access
536 +# endif
537 +# define access(path, mode) git_access(path, mode)
538 +# endif
539 +#endif
540 +
541 +#endif /* COMPAT_POSIX_H */
git-compat-util.h
+2 -529
@@ -23,26 +23,9 @@
23 #include <crtdbg.h>
24 #endif
25
26 -struct strbuf;
27 -
28 -
29 -#define _FILE_OFFSET_BITS 64
26 +#include "compat/posix.h"
27
31 -
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
39 -*/
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
28 +struct strbuf;
29
30 #if defined(__GNUC__) || defined(__clang__)
31 # define PRAGMA(pragma) _Pragma(#pragma)
@@ -176,71 +159,6 @@ DISABLE_WARNING(-Wsign-compare)
159 /* Approximation of the length of the decimal representation of this type. */
160 #define decimal_length(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
161
179 -#ifdef __MINGW64__
180 -#define _POSIX_C_SOURCE 1
181 -#elif defined(__sun__)
182 - /*
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.
188 - */
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
207 -
208 -/*
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.
212 - *
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.
217 - *
218 - * When a parameter may be used or unused, depending on conditional
219 - * compilation, consider using MAYBE_UNUSED instead.
220 - */
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
230 -
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
243 -
162 #if defined(NO_UNIX_SOCKETS) || !defined(GIT_WINDOWS_NATIVE)
163 static inline int _have_unix_sockets(void)
164 {
@@ -253,45 +171,6 @@ static inline int _have_unix_sockets(void)
171 #define have_unix_sockets _have_unix_sockets
172 #endif
173
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
294 -
174 /* Used by compat/win32/path-utils.h, and more */
175 static inline int is_xplatform_dir_sep(int c)
176 {
@@ -308,48 +187,6 @@ static inline int is_xplatform_dir_sep(int c)
187 #elif defined(_MSC_VER)
188 #include "compat/win32/path-utils.h"
189 #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
340 -/*
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.
346 - */
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
190 #endif
191
192 /* used on Mac OS X */
@@ -370,60 +207,6 @@ static inline const char *precompose_string_if_needed(const char *in)
207 #define probe_utf8_pathname_composition()
208 #endif
209
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
377 -
378 -#ifdef time
379 -#undef time
380 -#endif
381 -static inline time_t git_time(time_t *tloc)
382 -{
383 - struct timeval tv;
384 -
385 - /*
386 - * Avoid time(NULL), which can disagree with gettimeofday(2)
387 - * and filesystem timestamps.
388 - */
389 - gettimeofday(&tv, NULL);
390 -
391 - if (tloc)
392 - *tloc = tv.tv_sec;
393 - return tv.tv_sec;
394 -}
395 -#define time git_time
396 -
397 -#ifdef NO_STRUCT_ITIMERVAL
398 -struct itimerval {
399 - struct timeval it_interval;
400 - struct timeval it_value;
401 -};
402 -#endif
403 -
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 */
409 -}
410 -#undef setitimer
411 -#define setitimer(which,value,ovalue) git_setitimer(which,value,ovalue)
412 -#endif
413 -
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
422 -
423 -#ifndef NO_ICONV
424 -#include <iconv.h>
425 -#endif
426 -
210 #ifndef NO_OPENSSL
211 #ifdef __APPLE__
212 #undef __AVAILABILITY_MACROS_USES_AVAILABILITY
@@ -441,34 +224,6 @@ char *gitdirname(char *);
224 # include <sys/sysinfo.h>
225 #endif
226
444 -/* On most systems <netdb.h> would have given us this, but
445 - * not on some systems (e.g. z/OS).
446 - */
447 -#ifndef NI_MAXHOST
448 -#define NI_MAXHOST 1025
449 -#endif
450 -
451 -#ifndef NI_MAXSERV
452 -#define NI_MAXSERV 32
453 -#endif
454 -
455 -/* On most systems <limits.h> would have given us this, but
456 - * not on some systems (e.g. GNU/Hurd).
457 - */
458 -#ifndef PATH_MAX
459 -#define PATH_MAX 4096
460 -#endif
461 -
462 -#ifndef NAME_MAX
463 -#define NAME_MAX 255
464 -#endif
465 -
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
471 -
227 #ifndef PATH_SEP
228 #define PATH_SEP ':'
229 #endif
@@ -492,11 +247,6 @@ static inline int noop_core_config(const char *var UNUSED,
247 #define platform_core_config noop_core_config
248 #endif
249
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
499 -
250 #ifndef has_dos_drive_prefix
251 static inline int git_has_dos_drive_prefix(const char *path UNUSED)
252 {
@@ -824,25 +574,6 @@ static inline bool strip_suffix(const char *str, const char *suffix,
574 memcpy(_swap_b_ptr, _swap_buffer, sizeof(a)); \
575 } while (0)
576
827 -#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
828 -
829 -#ifndef PROT_READ
830 -#define PROT_READ 1
831 -#define PROT_WRITE 2
832 -#define MAP_PRIVATE 1
833 -#endif
834 -
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);
839 -
840 -#else /* NO_MMAP || USE_WIN32_MMAP */
841 -
842 -#include <sys/mman.h>
843 -
844 -#endif /* NO_MMAP || USE_WIN32_MMAP */
845 -
577 #ifdef NO_MMAP
578
579 /* This value must be multiple of (pagesize * 2) */
@@ -858,177 +589,15 @@ int git_munmap(void *start, size_t length);
589
590 #endif /* NO_MMAP */
591
861 -#ifndef MAP_FAILED
862 -#define MAP_FAILED ((void *)-1)
863 -#endif
864 -
592 #ifdef NO_ST_BLOCKS_IN_STRUCT_STAT
593 #define on_disk_bytes(st) ((st).st_size)
594 #else
595 #define on_disk_bytes(st) ((st).st_blocks * 512)
596 #endif
597
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
904 -
598 #define DEFAULT_PACKED_GIT_LIMIT \
599 ((1024L * 1024L) * (size_t)(sizeof(void*) >= 8 ? (32 * 1024L * 1024L) : 256))
600
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
912 -
913 -#ifdef NO_SETENV
914 -#define setenv gitsetenv
915 -int gitsetenv(const char *, const char *, int);
916 -#endif
917 -
918 -#ifdef NO_MKDTEMP
919 -#define mkdtemp gitmkdtemp
920 -char *gitmkdtemp(char *);
921 -#endif
922 -
923 -#ifdef NO_UNSETENV
924 -#define unsetenv gitunsetenv
925 -int gitunsetenv(const char *);
926 -#endif
927 -
928 -#ifdef NO_STRCASESTR
929 -#define strcasestr gitstrcasestr
930 -char *gitstrcasestr(const char *haystack, const char *needle);
931 -#endif
932 -
933 -#ifdef NO_STRLCPY
934 -#define strlcpy gitstrlcpy
935 -size_t gitstrlcpy(char *, const char *, size_t);
936 -#endif
937 -
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
944 -
945 -#ifdef NO_HSTRERROR
946 -#define hstrerror githstrerror
947 -const char *githstrerror(int herror);
948 -#endif
949 -
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
955 -
956 -#ifdef OVERRIDE_STRDUP
957 -#ifdef strdup
958 -#undef strdup
959 -#endif
960 -#define strdup gitstrdup
961 -char *gitstrdup(const char *s);
962 -#endif
963 -
964 -#ifdef NO_GETPAGESIZE
965 -#define getpagesize() sysconf(_SC_PAGESIZE)
966 -#endif
967 -
968 -#ifndef O_CLOEXEC
969 -#define O_CLOEXEC 0
970 -#endif
971 -
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
981 -
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
996 -
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
1002 -
1003 -#ifdef __GLIBC_PREREQ
1004 -#if __GLIBC_PREREQ(2, 1)
1005 -#define HAVE_STRCHRNUL
1006 -#endif
1007 -#endif
1008 -
1009 -#ifndef HAVE_STRCHRNUL
1010 -#define strchrnul gitstrchrnul
1011 -static inline char *gitstrchrnul(const char *s, int c)
1012 -{
1013 - while (*s && *s != c)
1014 - s++;
1015 - return (char *)s;
1016 -}
1017 -#endif
1018 -
1019 -#ifdef NO_INET_PTON
1020 -int inet_pton(int af, const char *src, void *dst);
1021 -#endif
1022 -
1023 -#ifdef NO_INET_NTOP
1024 -const char *inet_ntop(int af, const void *src, char *dst, size_t size);
1025 -#endif
1026 -
1027 -#ifdef NO_PTHREADS
1028 -#define atexit git_atexit
1029 -int git_atexit(void (*handler)(void));
1030 -#endif
1031 -
601 static inline size_t st_add(size_t a, size_t b)
602 {
603 if (unsigned_add_overflows(a, b))
@@ -1295,12 +864,6 @@ static inline size_t xsize_t(off_t len)
864 return (size_t) len;
865 }
866
1298 -#ifndef HOST_NAME_MAX
1299 -#define HOST_NAME_MAX 256
1300 -#endif
1301 -
1302 -#include "sane-ctype.h"
1303 -
867 /*
868 * Like skip_prefix, but compare case-insensitively. Note that the comparison
869 * is done via tolower(), so it is strictly ASCII (no multi-byte characters or
@@ -1366,34 +929,6 @@ static inline int strtol_i(char const *s, int base, int *result)
929 return 0;
930 }
931
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
1374 -
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 *))
1378 -{
1379 - if (nmemb > 1)
1380 - qsort(base, nmemb, size, compar);
1381 -}
1382 -
1383 -#define STABLE_QSORT(base, n, compar) \
1384 - git_stable_qsort((base), (n), sizeof(*(base)), compar)
1385 -
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
1391 -
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)
1396 -
932 #ifndef REG_STARTEND
933 #error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
934 #endif
@@ -1418,39 +953,12 @@ int git_regcomp(regex_t *preg, const char *pattern, int cflags);
953 # define FORCE_DIR_SET_GID 0
954 #endif
955
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
1434 -
956 #ifdef UNRELIABLE_FSTAT
957 #define fstat_is_reliable() 0
958 #else
959 #define fstat_is_reliable() 1
960 #endif
961
1441 -#ifndef va_copy
1442 -/*
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.
1446 - */
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
1453 -
962 /* usage.c: only to be used for testing BUG() implementation (see test-tool) */
963 extern int BUG_exit_code;
964
@@ -1480,41 +988,6 @@ void bug_fl(const char *file, int line, const char *fmt, ...);
988 # define SHELL_PATH "/bin/sh"
989 #endif
990
1483 -#ifndef _POSIX_THREAD_SAFE_FUNCTIONS
1484 -static inline void git_flockfile(FILE *fh UNUSED)
1485 -{
1486 - ; /* nothing */
1487 -}
1488 -static inline void git_funlockfile(FILE *fh UNUSED)
1489 -{
1490 - ; /* nothing */
1491 -}
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
1499 -
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
1507 -
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
1517 -
991 /*
992 * Our code often opens a path to an optional file, to work on its
993 * contents when we can successfully open it. We can ignore a failure