Use strict C-syntax to make it buildable with VS
[git-cheetah/kirill.git] / common / git-compat-util.h
blob61de7782a124cff7b3efd9a4fd2b80db004d2ca3
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 /* from mingw/include/sys/types.h */
66 #ifndef _SSIZE_T_
67 #define _SSIZE_T_
68 typedef long _ssize_t;
70 #ifndef _NO_OLDNAMES
71 typedef _ssize_t ssize_t;
72 #endif
73 #endif /* Not _SSIZE_T_ */
75 #ifndef _MODE_T_
76 #define _MODE_T_
77 typedef unsigned short _mode_t;
79 #ifndef _NO_OLDNAMES
80 typedef _mode_t mode_t;
81 #endif
82 #endif /* Not _MODE_T_ */
85 /* from mingw/include/io.h */
86 /* Some defines for _access nAccessMode (MS doesn't define them, but
87 * it doesn't seem to hurt to add them). */
88 #define F_OK 0 /* Check for file existence */
89 /* Well maybe it does hurt. On newer versions of MSVCRT, an access mode
90 of 1 causes invalid parameter error. */
91 #define X_OK 1 /* MS access() doesn't check for execute permission. */
92 #define W_OK 2 /* Check for write permission */
93 #define R_OK 4 /* Check for read permission */
94 #endif /* _WIN32 */
96 /* from mingw/include/string.h */
97 #define strcasecmp(__sz1, __sz2) (_stricmp((__sz1), (__sz2)))
99 /* from mingw/include/stdint.h */
100 typedef unsigned uint32_t;
102 #ifdef __GNUC__
103 #define NORETURN __attribute__((__noreturn__))
104 #else
106 #if defined(_MSC_VER)
107 #define NORETURN __declspec(noreturn)
108 #else
109 #define NORETURN
110 #endif /* _MSC_VER */
112 #ifndef __attribute__
113 #define __attribute__(x)
114 #endif
115 #define __CRT_INLINE
116 #endif
118 extern int error(const char * format, ...);
119 extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
121 #ifndef HAVE_STRCHRNUL
122 #define strchrnul gitstrchrnul
123 static inline char *gitstrchrnul(const char *s, int c)
125 while (*s && *s != c)
126 s++;
127 return (char *)s;
129 #endif
131 extern time_t tm_to_time_t(const struct tm *tm);
133 extern void release_pack_memory(size_t, int);
135 extern char *xstrdup(const char *str);
136 extern void *xmalloc(size_t size);
137 extern void *xrealloc(void *ptr, size_t size);
138 extern void *xcalloc(size_t nmemb, size_t size);
139 extern ssize_t xread(int fd, void *buf, size_t len);
141 #ifdef NO_STRLCPY
142 #define strlcpy gitstrlcpy
143 extern size_t gitstrlcpy(char *, const char *, size_t);
144 #endif
146 #ifdef _WIN32
147 #define snprintf _snprintf
148 #endif
150 #ifdef NO_MMAP
152 #ifndef PROT_READ
153 #define PROT_READ 1
154 #define PROT_WRITE 2
155 #define MAP_PRIVATE 1
156 #define MAP_FAILED ((void*)-1)
157 #endif
159 #define mmap git_mmap
160 #define munmap git_munmap
161 extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
162 extern int git_munmap(void *start, size_t length);
164 /* This value must be multiple of (pagesize * 2) */
165 #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
167 #else /* NO_MMAP */
169 #include <sys/mman.h>
171 /* This value must be multiple of (pagesize * 2) */
172 #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
173 (sizeof(void*) >= 8 \
174 ? 1 * 1024 * 1024 * 1024 \
175 : 32 * 1024 * 1024)
177 #endif /* NO_MMAP */
179 #ifdef NO_PREAD
180 #define pread git_pread
181 extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
182 #endif
184 * Forward decl that will remind us if its twin in cache.h changes.
185 * This function is used in compat/pread.c. But we can't include
186 * cache.h there.
188 extern ssize_t read_in_full(int fd, void *buf, size_t count);
190 #ifdef _WIN32
191 #include "../compat/mingw.h"
192 #endif
194 /* some defines not available on osx */
195 #ifndef MAX_PATH
196 #define MAX_PATH PATH_MAX
197 #endif
199 #ifndef BOOL
200 #define BOOL int
201 #endif
203 #ifndef TRUE
204 #define TRUE 1
205 #endif
207 #ifndef FALSE
208 #define FALSE 0
209 #endif
211 #ifndef UINT
212 #define UINT unsigned int
213 #endif
215 #ifdef _WIN32
216 #define PATH_SEPERATOR '\\'
217 #endif
219 /* default PATH_SEPERATOR is / */
220 #ifndef PATH_SEPERATOR
221 #define PATH_SEPERATOR '/'
222 #endif
224 #endif