Merge branch 'msys2'
[git-cheetah.git] / common / git-compat-util.h
blob348dfd69cd65a1108eab6e66b7201c521a9c3585
1 #ifndef GIT_COMPAT_UTIL_H
2 #define GIT_COMPAT_UTIL_H
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)
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 #elif defined(_MSC_VER)
17 #define FLEX_ARRAY /* empty */
18 #endif
21 * Otherwise, default to safer but a bit wasteful traditional style
23 #ifndef FLEX_ARRAY
24 # define FLEX_ARRAY 1
25 #endif
26 #endif
28 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <stdarg.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <time.h>
39 #include <signal.h>
40 #include <sys/stat.h>
41 #ifdef _WIN32
42 #include <io.h>
43 #include <sys/utime.h>
44 #include <process.h>
45 #else
46 #include <unistd.h>
47 #include <sys/time.h>
48 #endif /* _WIN32 */
50 #if defined(_MSC_VER)
51 /* MSVC defines mkdir here, not in io.h */
52 #include <direct.h>
53 #endif
55 #ifndef PATH_MAX
57 * with MSVC, if we define _POSIX_ we loose half of useful functions
58 * (e.g. chmod, open, close), otherwise we don't have PATH_MAX
59 * because of #ifdef in <io.h>.
60 * So, when build under MSVC, don't define _POSIX_
62 #define PATH_MAX 512
63 #endif
65 #ifdef _WIN32
66 #ifdef _SSIZE_T_DEFINED
67 #define _SSIZE_T_
68 #endif
70 /* from mingw/include/sys/types.h */
71 #ifndef _SSIZE_T_
72 #define _SSIZE_T_
73 typedef long _ssize_t;
75 #ifndef _NO_OLDNAMES
76 typedef _ssize_t ssize_t;
77 #endif
78 #endif /* Not _SSIZE_T_ */
79 #ifndef _MODE_T_
80 #define _MODE_T_
81 typedef unsigned short _mode_t;
83 #ifndef _NO_OLDNAMES
84 typedef _mode_t mode_t;
85 #endif
86 #endif /* Not _MODE_T_ */
89 /* from mingw/include/io.h */
90 /* Some defines for _access nAccessMode (MS doesn't define them, but
91 * it doesn't seem to hurt to add them). */
92 #define F_OK 0 /* Check for file existence */
93 /* Well maybe it does hurt. On newer versions of MSVCRT, an access mode
94 of 1 causes invalid parameter error. */
95 #define X_OK 1 /* MS access() doesn't check for execute permission. */
96 #define W_OK 2 /* Check for write permission */
97 #define R_OK 4 /* Check for read permission */
98 #endif /* _WIN32 */
100 /* from mingw/include/string.h */
101 #define strcasecmp(__sz1, __sz2) (_stricmp((__sz1), (__sz2)))
103 /* from mingw/include/stdint.h */
104 typedef unsigned uint32_t;
106 #ifdef __GNUC__
107 #define NORETURN __attribute__((__noreturn__))
108 #else
110 #if defined(_MSC_VER)
111 #define NORETURN __declspec(noreturn)
112 #else
113 #define NORETURN
114 #endif /* _MSC_VER */
116 #ifndef __attribute__
117 #define __attribute__(x)
118 #endif
119 #define __CRT_INLINE
120 #endif
122 extern int error(const char * format, ...);
123 extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
125 #ifndef HAVE_STRCHRNUL
126 #define strchrnul gitstrchrnul
127 static inline char *gitstrchrnul(const char *s, int c)
129 while (*s && *s != c)
130 s++;
131 return (char *)s;
133 #endif
135 extern time_t tm_to_time_t(const struct tm *tm);
137 extern void release_pack_memory(size_t, int);
139 extern char *xstrdup(const char *str);
140 extern void *xmalloc(size_t size);
141 extern void *xrealloc(void *ptr, size_t size);
142 extern void *xcalloc(size_t nmemb, size_t size);
143 extern ssize_t xread(int fd, void *buf, size_t len);
145 #ifdef NO_STRLCPY
146 #define strlcpy gitstrlcpy
147 extern size_t gitstrlcpy(char *, const char *, size_t);
148 #endif
150 #ifdef _WIN32
151 #define snprintf _snprintf
152 #endif
154 #ifdef NO_MMAP
156 #ifndef PROT_READ
157 #define PROT_READ 1
158 #define PROT_WRITE 2
159 #define MAP_PRIVATE 1
160 #define MAP_FAILED ((void*)-1)
161 #endif
163 #define mmap git_mmap
164 #define munmap git_munmap
165 extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
166 extern int git_munmap(void *start, size_t length);
168 /* This value must be multiple of (pagesize * 2) */
169 #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
171 #else /* NO_MMAP */
173 #include <sys/mman.h>
175 /* This value must be multiple of (pagesize * 2) */
176 #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
177 (sizeof(void*) >= 8 \
178 ? 1 * 1024 * 1024 * 1024 \
179 : 32 * 1024 * 1024)
181 #endif /* NO_MMAP */
183 #ifdef NO_PREAD
184 #define pread git_pread
185 extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
186 #endif
188 * Forward decl that will remind us if its twin in cache.h changes.
189 * This function is used in compat/pread.c. But we can't include
190 * cache.h there.
192 extern ssize_t read_in_full(int fd, void *buf, size_t count);
194 #ifdef _WIN32
195 #include "../compat/mingw.h"
196 #endif
198 /* some defines not available on osx */
199 #ifndef MAX_PATH
200 #define MAX_PATH PATH_MAX
201 #endif
203 #ifndef BOOL
204 #define BOOL int
205 #endif
207 #ifndef TRUE
208 #define TRUE 1
209 #endif
211 #ifndef FALSE
212 #define FALSE 0
213 #endif
215 #ifndef UINT
216 #define UINT unsigned int
217 #endif
219 #ifdef _WIN32
220 #define PATH_SEPERATOR '\\'
221 #endif
223 /* default PATH_SEPERATOR is / */
224 #ifndef PATH_SEPERATOR
225 #define PATH_SEPERATOR '/'
226 #endif
228 #endif