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)
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
{
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
{
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 */
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
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
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
{
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) */
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))
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
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
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
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