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