Git fork
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(__NetBSD__) && \
64 !defined(__OpenBSD__) && !defined(__DragonFly__) && !defined(__MirBSD__) && \
65 !defined(__USLC__) && !defined(_M_UNIX) && !defined(__sgi) && \
66 !defined(__TANDEM) && !defined(__QNX__) && !defined(__CYGWIN__)
67#define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500 */
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 */
169typedef long intptr_t;
170typedef 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))
179int compat_mkdir_wo_trailing_slash(const char*, mode_t);
180#endif
181
182#ifdef time
183#undef time
184#endif
185static 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
202struct itimerval {
203 struct timeval it_interval;
204 struct timeval it_value;
205};
206#endif
207
208#ifdef NO_SETITIMER
209static 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
222char *gitbasename(char *);
223#define dirname gitdirname
224char *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
253typedef 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
259int 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
274void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
275int 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)
308int git_stat(const char *, struct stat *);
309#ifdef fstat
310#undef fstat
311#endif
312#define fstat(fd, buf) git_fstat(fd, buf)
313int git_fstat(int, struct stat *);
314#ifdef lstat
315#undef lstat
316#endif
317#define lstat(path, buf) git_lstat(path, buf)
318int git_lstat(const char *, struct stat *);
319#endif
320
321#ifdef NO_PREAD
322#define pread git_pread
323ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
324#endif
325
326#ifdef NO_SETENV
327#define setenv gitsetenv
328int gitsetenv(const char *, const char *, int);
329#endif
330
331#ifdef NO_MKDTEMP
332#define mkdtemp gitmkdtemp
333char *gitmkdtemp(char *);
334#endif
335
336#ifdef NO_UNSETENV
337#define unsetenv gitunsetenv
338int gitunsetenv(const char *);
339#endif
340
341#ifdef NO_STRCASESTR
342#define strcasestr gitstrcasestr
343char *gitstrcasestr(const char *haystack, const char *needle);
344#endif
345
346#ifdef NO_STRLCPY
347#define strlcpy gitstrlcpy
348size_t gitstrlcpy(char *, const char *, size_t);
349#endif
350
351#ifdef NO_STRTOUMAX
352#define strtoumax gitstrtoumax
353uintmax_t gitstrtoumax(const char *, char **, int);
354#define strtoimax gitstrtoimax
355intmax_t gitstrtoimax(const char *, char **, int);
356#endif
357
358#ifdef NO_HSTRERROR
359#define hstrerror githstrerror
360const char *githstrerror(int herror);
361#endif
362
363#ifdef NO_MEMMEM
364#define memmem gitmemmem
365void *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
374char *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
392FILE *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
400int git_snprintf(char *str, size_t maxsize,
401 const char *format, ...);
402#ifdef vsnprintf
403#undef vsnprintf
404#endif
405#define vsnprintf git_vsnprintf
406int 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
413int 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
424static 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
433int inet_pton(int af, const char *src, void *dst);
434#endif
435
436#ifdef NO_INET_NTOP
437const 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
442int 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
451void 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)
458static 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
469int 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
507static inline void git_flockfile(FILE *fh UNUSED)
508{
509 ; /* nothing */
510}
511static 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
524int 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
532int 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 */