]> asedeno.scripts.mit.edu Git - git.git/blob - compat/mingw.h
Windows: start_command: Support non-NULL dir in struct child_process
[git.git] / compat / mingw.h
1 #include <winsock2.h>
2 #include <ws2tcpip.h>
3
4 /*
5  * things that are not available in header files
6  */
7
8 typedef int pid_t;
9 #define hstrerror strerror
10
11 #define S_IFLNK    0120000 /* Symbolic link */
12 #define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
13 #define S_ISSOCK(x) 0
14 #define S_IRGRP 0
15 #define S_IWGRP 0
16 #define S_IXGRP 0
17 #define S_ISGID 0
18 #define S_IROTH 0
19 #define S_IXOTH 0
20
21 #define WIFEXITED(x) 1
22 #define WIFSIGNALED(x) 0
23 #define WEXITSTATUS(x) ((x) & 0xff)
24 #define WTERMSIG(x) SIGTERM
25
26 #define SIGHUP 1
27 #define SIGQUIT 3
28 #define SIGKILL 9
29 #define SIGPIPE 13
30 #define SIGALRM 14
31 #define SIGCHLD 17
32
33 #define F_GETFD 1
34 #define F_SETFD 2
35 #define FD_CLOEXEC 0x1
36
37 struct passwd {
38         char *pw_name;
39         char *pw_gecos;
40         char *pw_dir;
41 };
42
43 extern char *getpass(const char *prompt);
44
45 #ifndef POLLIN
46 struct pollfd {
47         int fd;           /* file descriptor */
48         short events;     /* requested events */
49         short revents;    /* returned events */
50 };
51 #define POLLIN 1
52 #define POLLHUP 2
53 #endif
54
55 typedef void (__cdecl *sig_handler_t)(int);
56 struct sigaction {
57         sig_handler_t sa_handler;
58         unsigned sa_flags;
59 };
60 #define sigemptyset(x) (void)0
61 #define SA_RESTART 0
62
63 struct itimerval {
64         struct timeval it_value, it_interval;
65 };
66 #define ITIMER_REAL 0
67
68 /*
69  * trivial stubs
70  */
71
72 static inline int readlink(const char *path, char *buf, size_t bufsiz)
73 { errno = ENOSYS; return -1; }
74 static inline int symlink(const char *oldpath, const char *newpath)
75 { errno = ENOSYS; return -1; }
76 static inline int fchmod(int fildes, mode_t mode)
77 { errno = ENOSYS; return -1; }
78 static inline int fork(void)
79 { errno = ENOSYS; return -1; }
80 static inline unsigned int alarm(unsigned int seconds)
81 { return 0; }
82 static inline int fsync(int fd)
83 { return 0; }
84 static inline int getppid(void)
85 { return 1; }
86 static inline void sync(void)
87 {}
88 static inline int getuid()
89 { return 1; }
90 static inline struct passwd *getpwnam(const char *name)
91 { return NULL; }
92 static inline int fcntl(int fd, int cmd, long arg)
93 {
94         if (cmd == F_GETFD || cmd == F_SETFD)
95                 return 0;
96         errno = EINVAL;
97         return -1;
98 }
99 /* bash cannot reliably detect negative return codes as failure */
100 #define exit(code) exit((code) & 0xff)
101
102 /*
103  * simple adaptors
104  */
105
106 static inline int mingw_mkdir(const char *path, int mode)
107 {
108         return mkdir(path);
109 }
110 #define mkdir mingw_mkdir
111
112 static inline int mingw_unlink(const char *pathname)
113 {
114         /* read-only files cannot be removed */
115         chmod(pathname, 0666);
116         return unlink(pathname);
117 }
118 #define unlink mingw_unlink
119
120 static inline int waitpid(pid_t pid, int *status, unsigned options)
121 {
122         if (options == 0)
123                 return _cwait(status, pid, 0);
124         errno = EINVAL;
125         return -1;
126 }
127
128 #ifndef NO_OPENSSL
129 #include <openssl/ssl.h>
130 static inline int mingw_SSL_set_fd(SSL *ssl, int fd)
131 {
132         return SSL_set_fd(ssl, _get_osfhandle(fd));
133 }
134 #define SSL_set_fd mingw_SSL_set_fd
135
136 static inline int mingw_SSL_set_rfd(SSL *ssl, int fd)
137 {
138         return SSL_set_rfd(ssl, _get_osfhandle(fd));
139 }
140 #define SSL_set_rfd mingw_SSL_set_rfd
141
142 static inline int mingw_SSL_set_wfd(SSL *ssl, int fd)
143 {
144         return SSL_set_wfd(ssl, _get_osfhandle(fd));
145 }
146 #define SSL_set_wfd mingw_SSL_set_wfd
147 #endif
148
149 /*
150  * implementations of missing functions
151  */
152
153 int pipe(int filedes[2]);
154 unsigned int sleep (unsigned int seconds);
155 int mkstemp(char *template);
156 int gettimeofday(struct timeval *tv, void *tz);
157 int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
158 struct tm *gmtime_r(const time_t *timep, struct tm *result);
159 struct tm *localtime_r(const time_t *timep, struct tm *result);
160 int getpagesize(void);  /* defined in MinGW's libgcc.a */
161 struct passwd *getpwuid(int uid);
162 int setitimer(int type, struct itimerval *in, struct itimerval *out);
163 int sigaction(int sig, struct sigaction *in, struct sigaction *out);
164 int link(const char *oldpath, const char *newpath);
165
166 /*
167  * replacements of existing functions
168  */
169
170 int mingw_open (const char *filename, int oflags, ...);
171 #define open mingw_open
172
173 char *mingw_getcwd(char *pointer, int len);
174 #define getcwd mingw_getcwd
175
176 char *mingw_getenv(const char *name);
177 #define getenv mingw_getenv
178
179 struct hostent *mingw_gethostbyname(const char *host);
180 #define gethostbyname mingw_gethostbyname
181
182 void mingw_freeaddrinfo(struct addrinfo *res);
183 #define freeaddrinfo mingw_freeaddrinfo
184
185 int mingw_getaddrinfo(const char *node, const char *service,
186                       const struct addrinfo *hints, struct addrinfo **res);
187 #define getaddrinfo mingw_getaddrinfo
188
189 int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen,
190                       char *host, DWORD hostlen, char *serv, DWORD servlen,
191                       int flags);
192 #define getnameinfo mingw_getnameinfo
193
194 int mingw_socket(int domain, int type, int protocol);
195 #define socket mingw_socket
196
197 int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
198 #define connect mingw_connect
199
200 int mingw_rename(const char*, const char*);
201 #define rename mingw_rename
202
203 #if defined(USE_WIN32_MMAP) || defined(_MSC_VER)
204 int mingw_getpagesize(void);
205 #define getpagesize mingw_getpagesize
206 #endif
207
208 /* Use mingw_lstat() instead of lstat()/stat() and
209  * mingw_fstat() instead of fstat() on Windows.
210  */
211 #define off_t off64_t
212 #define lseek _lseeki64
213 #ifndef ALREADY_DECLARED_STAT_FUNCS
214 #define stat _stati64
215 int mingw_lstat(const char *file_name, struct stat *buf);
216 int mingw_fstat(int fd, struct stat *buf);
217 #define fstat mingw_fstat
218 #define lstat mingw_lstat
219 #define _stati64(x,y) mingw_lstat(x,y)
220 #endif
221
222 int mingw_utime(const char *file_name, const struct utimbuf *times);
223 #define utime mingw_utime
224
225 pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
226                      const char *dir,
227                      int fhin, int fhout, int fherr);
228 void mingw_execvp(const char *cmd, char *const *argv);
229 #define execvp mingw_execvp
230
231 static inline unsigned int git_ntohl(unsigned int x)
232 { return (unsigned int)ntohl(x); }
233 #define ntohl git_ntohl
234
235 sig_handler_t mingw_signal(int sig, sig_handler_t handler);
236 #define signal mingw_signal
237
238 /*
239  * ANSI emulation wrappers
240  */
241
242 int winansi_fputs(const char *str, FILE *stream);
243 int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
244 int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
245 #define fputs winansi_fputs
246 #define printf(...) winansi_printf(__VA_ARGS__)
247 #define fprintf(...) winansi_fprintf(__VA_ARGS__)
248
249 /*
250  * git specific compatibility
251  */
252
253 #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
254 #define is_dir_sep(c) ((c) == '/' || (c) == '\\')
255 #define PATH_SEP ';'
256 #define PRIuMAX "I64u"
257
258 void mingw_open_html(const char *path);
259 #define open_html mingw_open_html
260
261 /*
262  * helpers
263  */
264
265 char **make_augmented_environ(const char *const *vars);
266 void free_environ(char **env);
267
268 /*
269  * A replacement of main() that ensures that argv[0] has a path
270  * and that default fmode and std(in|out|err) are in binary mode
271  */
272
273 #define main(c,v) dummy_decl_mingw_main(); \
274 static int mingw_main(); \
275 int main(int argc, const char **argv) \
276 { \
277         _fmode = _O_BINARY; \
278         _setmode(_fileno(stdin), _O_BINARY); \
279         _setmode(_fileno(stdout), _O_BINARY); \
280         _setmode(_fileno(stderr), _O_BINARY); \
281         argv[0] = xstrdup(_pgmptr); \
282         return mingw_main(argc, argv); \
283 } \
284 static int mingw_main(c,v)
285
286 #ifndef NO_MINGW_REPLACE_READDIR
287 /*
288  * A replacement of readdir, to ensure that it reads the file type at
289  * the same time. This avoid extra unneeded lstats in git on MinGW
290  */
291 #undef DT_UNKNOWN
292 #undef DT_DIR
293 #undef DT_REG
294 #undef DT_LNK
295 #define DT_UNKNOWN      0
296 #define DT_DIR          1
297 #define DT_REG          2
298 #define DT_LNK          3
299
300 struct mingw_dirent
301 {
302         long            d_ino;                  /* Always zero. */
303         union {
304                 unsigned short  d_reclen;       /* Always zero. */
305                 unsigned char   d_type;         /* Reimplementation adds this */
306         };
307         unsigned short  d_namlen;               /* Length of name in d_name. */
308         char            d_name[FILENAME_MAX];   /* File name. */
309 };
310 #define dirent mingw_dirent
311 #define readdir(x) mingw_readdir(x)
312 struct dirent *mingw_readdir(DIR *dir);
313 #endif // !NO_MINGW_REPLACE_READDIR
314
315 /*
316  * Used by Pthread API implementation for Windows
317  */
318 extern int err_win_to_posix(DWORD winerr);