finder: don't do own item-id calcluation
[git-cheetah/kirill.git] / common / git-compat-util.h
blob3d37ac281e73cf0033230e0145c0b0f85cfce6fb
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 <unistd.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <stdarg.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <time.h>
40 #include <signal.h>
41 #include <sys/stat.h>
42 #include <sys/time.h>
43 #ifdef _WIN32
44 #include <io.h>
45 #include <sys/utime.h>
46 #include <process.h>
47 #endif /* _WIN32 */
49 #if defined(_MSC_VER)
50 /* MSVC defines mkdir here, not in io.h */
51 #include <direct.h>
52 #endif
54 #ifndef PATH_MAX
56 * with MSVC, if we define _POSIX_ we loose half of useful functions
57 * (e.g. chmod, open, close), otherwise we don't have PATH_MAX
58 * because of #ifdef in <io.h>.
59 * So, when build under MSVC, don't define _POSIX_
61 #define PATH_MAX 512
62 #endif
64 #ifdef _WIN32
65 #ifdef _SSIZE_T_DEFINED
66 #define _SSIZE_T_
67 #endif
69 /* from mingw/include/sys/types.h */
70 #ifndef _SSIZE_T_
71 #define _SSIZE_T_
72 typedef long _ssize_t;
74 #ifndef _NO_OLDNAMES
75 typedef _ssize_t ssize_t;
76 #endif
77 #endif /* Not _SSIZE_T_ */
78 #ifndef _MODE_T_
79 #define _MODE_T_
80 typedef unsigned short _mode_t;
82 #ifndef _NO_OLDNAMES
83 typedef _mode_t mode_t;
84 #endif
85 #endif /* Not _MODE_T_ */
88 /* from mingw/include/io.h */
89 /* Some defines for _access nAccessMode (MS doesn't define them, but
90 * it doesn't seem to hurt to add them). */
91 #define F_OK 0 /* Check for file existence */
92 /* Well maybe it does hurt. On newer versions of MSVCRT, an access mode
93 of 1 causes invalid parameter error. */
94 #define X_OK 1 /* MS access() doesn't check for execute permission. */
95 #define W_OK 2 /* Check for write permission */
96 #define R_OK 4 /* Check for read permission */
97 #endif /* _WIN32 */
99 /* from mingw/include/string.h */
100 #define strcasecmp(__sz1, __sz2) (_stricmp((__sz1), (__sz2)))
102 /* from mingw/include/stdint.h */
103 typedef unsigned uint32_t;
105 #ifdef __GNUC__
106 #define NORETURN __attribute__((__noreturn__))
107 #else
109 #if defined(_MSC_VER)
110 #define NORETURN __declspec(noreturn)
111 #else
112 #define NORETURN
113 #endif /* _MSC_VER */
115 #ifndef __attribute__
116 #define __attribute__(x)
117 #endif
118 #define __CRT_INLINE
119 #endif
121 extern int error(const char * format, ...);
122 extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
124 #ifndef HAVE_STRCHRNUL
125 #define strchrnul gitstrchrnul
126 static inline char *gitstrchrnul(const char *s, int c)
128 while (*s && *s != c)
129 s++;
130 return (char *)s;
132 #endif
134 extern time_t tm_to_time_t(const struct tm *tm);
136 extern void release_pack_memory(size_t, int);
138 extern char *xstrdup(const char *str);
139 extern void *xmalloc(size_t size);
140 extern void *xrealloc(void *ptr, size_t size);
141 extern void *xcalloc(size_t nmemb, size_t size);
142 extern ssize_t xread(int fd, void *buf, size_t len);
144 #ifdef NO_STRLCPY
145 #define strlcpy gitstrlcpy
146 extern size_t gitstrlcpy(char *, const char *, size_t);
147 #endif
149 #ifdef _WIN32
150 #define snprintf _snprintf
151 #endif
153 #ifdef NO_MMAP
155 #ifndef PROT_READ
156 #define PROT_READ 1
157 #define PROT_WRITE 2
158 #define MAP_PRIVATE 1
159 #define MAP_FAILED ((void*)-1)
160 #endif
162 #define mmap git_mmap
163 #define munmap git_munmap
164 extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
165 extern int git_munmap(void *start, size_t length);
167 /* This value must be multiple of (pagesize * 2) */
168 #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
170 #else /* NO_MMAP */
172 #include <sys/mman.h>
174 /* This value must be multiple of (pagesize * 2) */
175 #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
176 (sizeof(void*) >= 8 \
177 ? 1 * 1024 * 1024 * 1024 \
178 : 32 * 1024 * 1024)
180 #endif /* NO_MMAP */
182 #ifdef NO_PREAD
183 #define pread git_pread
184 extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
185 #endif
187 * Forward decl that will remind us if its twin in cache.h changes.
188 * This function is used in compat/pread.c. But we can't include
189 * cache.h there.
191 extern ssize_t read_in_full(int fd, void *buf, size_t count);
193 #ifdef _WIN32
194 #include "../compat/mingw.h"
195 #endif
197 /* some defines not available on osx */
198 #ifndef MAX_PATH
199 #define MAX_PATH PATH_MAX
200 #endif
202 #ifndef BOOL
203 #define BOOL int
204 #endif
206 #ifndef TRUE
207 #define TRUE 1
208 #endif
210 #ifndef FALSE
211 #define FALSE 0
212 #endif
214 #ifndef UINT
215 #define UINT unsigned int
216 #endif
218 #ifdef _WIN32
219 #define PATH_SEPERATOR '\\'
220 #endif
222 /* default PATH_SEPERATOR is / */
223 #ifndef PATH_SEPERATOR
224 #define PATH_SEPERATOR '/'
225 #endif
227 #endif