Make mingw_spawn* buildable by VS 2008
[git-cheetah/kirill.git] / git-compat-util.h
blob3a690e1e103a5cff6c94c3a55a205d972bc7fe77
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 <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <stdarg.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <time.h>
38 #include <io.h>
39 #include <sys/stat.h>
40 #include <sys/utime.h>
41 #include <signal.h>
42 #include <process.h>
44 #if defined(_MSC_VER)
45 /* MSVC defines mkdir here, not in io.h */
46 #include <direct.h>
47 #endif
49 #ifndef PATH_MAX
51 * with MSVC, if we define _POSIX_ we loose half of useful functions
52 * (e.g. chmod, open, close), otherwise we don't have PATH_MAX
53 * because of #ifdef in <io.h>.
54 * So, when build under MSVC, don't define _POSIX_
56 #define PATH_MAX 512
57 #endif
59 /* from mingw/include/sys/types.h */
60 #ifndef _SSIZE_T_
61 #define _SSIZE_T_
62 typedef long _ssize_t;
64 #ifndef _NO_OLDNAMES
65 typedef _ssize_t ssize_t;
66 #endif
67 #endif /* Not _SSIZE_T_ */
69 #ifndef _MODE_T_
70 #define _MODE_T_
71 typedef unsigned short _mode_t;
73 #ifndef _NO_OLDNAMES
74 typedef _mode_t mode_t;
75 #endif
76 #endif /* Not _MODE_T_ */
79 /* from mingw/include/io.h */
80 /* Some defines for _access nAccessMode (MS doesn't define them, but
81 * it doesn't seem to hurt to add them). */
82 #define F_OK 0 /* Check for file existence */
83 /* Well maybe it does hurt. On newer versions of MSVCRT, an access mode
84 of 1 causes invalid parameter error. */
85 #define X_OK 1 /* MS access() doesn't check for execute permission. */
86 #define W_OK 2 /* Check for write permission */
87 #define R_OK 4 /* Check for read permission */
89 /* from mingw/include/string.h */
90 #define strcasecmp(__sz1, __sz2) (_stricmp((__sz1), (__sz2)))
92 /* from mingw/include/stdint.h */
93 typedef unsigned uint32_t;
95 #ifdef __GNUC__
96 #define NORETURN __attribute__((__noreturn__))
97 #else
99 #if defined(_MSC_VER)
100 #define NORETURN __declspec(noreturn)
101 #else
102 #define NORETURN
103 #endif /* _MSC_VER */
105 #ifndef __attribute__
106 #define __attribute__(x)
107 #endif
108 #define __CRT_INLINE
109 #endif
111 extern int error(const char * format, ...);
112 extern NORETURN void die(const char *err, ...) __attribute__((format (printf, 1, 2)));
114 #ifndef HAVE_STRCHRNUL
115 #define strchrnul gitstrchrnul
116 static inline char *gitstrchrnul(const char *s, int c)
118 while (*s && *s != c)
119 s++;
120 return (char *)s;
122 #endif
124 extern time_t tm_to_time_t(const struct tm *tm);
126 extern void release_pack_memory(size_t, int);
128 extern char *xstrdup(const char *str);
129 extern void *xmalloc(size_t size);
130 extern void *xrealloc(void *ptr, size_t size);
131 extern void *xcalloc(size_t nmemb, size_t size);
132 extern ssize_t xread(int fd, void *buf, size_t len);
134 #ifdef NO_STRLCPY
135 #define strlcpy gitstrlcpy
136 extern size_t gitstrlcpy(char *, const char *, size_t);
137 #endif
139 #define snprintf _snprintf
141 #ifdef NO_MMAP
143 #ifndef PROT_READ
144 #define PROT_READ 1
145 #define PROT_WRITE 2
146 #define MAP_PRIVATE 1
147 #define MAP_FAILED ((void*)-1)
148 #endif
150 #define mmap git_mmap
151 #define munmap git_munmap
152 extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
153 extern int git_munmap(void *start, size_t length);
155 /* This value must be multiple of (pagesize * 2) */
156 #define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
158 #else /* NO_MMAP */
160 #include <sys/mman.h>
162 /* This value must be multiple of (pagesize * 2) */
163 #define DEFAULT_PACKED_GIT_WINDOW_SIZE \
164 (sizeof(void*) >= 8 \
165 ? 1 * 1024 * 1024 * 1024 \
166 : 32 * 1024 * 1024)
168 #endif /* NO_MMAP */
170 #ifdef NO_PREAD
171 #define pread git_pread
172 extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
173 #endif
175 * Forward decl that will remind us if its twin in cache.h changes.
176 * This function is used in compat/pread.c. But we can't include
177 * cache.h there.
179 extern ssize_t read_in_full(int fd, void *buf, size_t count);
181 #include "compat/mingw.h"
183 #endif