]> git.ipfire.org Git - thirdparty/git.git/blame - compat/mingw.h
mingw: make failures to unlink or move raise a question
[thirdparty/git.git] / compat / mingw.h
CommitLineData
f4626df5 1#include <winsock2.h>
fe3b2b7b 2#include <ws2tcpip.h>
f4626df5
JS
3
4/*
5 * things that are not available in header files
6 */
7
8typedef int pid_t;
e7cf4e94 9typedef int uid_t;
772991af 10typedef int socklen_t;
f4626df5
JS
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
4091bfc9
SS
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
f4626df5
JS
23#define S_IRGRP 0
24#define S_IWGRP 0
25#define S_IXGRP 0
4091bfc9 26#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
f4626df5 27#define S_IROTH 0
4091bfc9 28#define S_IWOTH 0
f4626df5 29#define S_IXOTH 0
4091bfc9
SS
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
f4626df5 34
303e7c48
JS
35#define WIFEXITED(x) 1
36#define WIFSIGNALED(x) 0
f4626df5 37#define WEXITSTATUS(x) ((x) & 0xff)
303e7c48 38#define WTERMSIG(x) SIGTERM
f4626df5 39
419f37db
IL
40#define EWOULDBLOCK EAGAIN
41#define SHUT_WR SD_SEND
42
d2825065
JS
43#define SIGHUP 1
44#define SIGQUIT 3
45#define SIGKILL 9
46#define SIGPIPE 13
47#define SIGALRM 14
48#define SIGCHLD 17
f4626df5
JS
49
50#define F_GETFD 1
51#define F_SETFD 2
52#define FD_CLOEXEC 0x1
53
772991af
MP
54#define EAFNOSUPPORT WSAEAFNOSUPPORT
55#define ECONNABORTED WSAECONNABORTED
56
f4626df5
JS
57struct passwd {
58 char *pw_name;
59 char *pw_gecos;
60 char *pw_dir;
61};
62
0dbbbc1e
JS
63extern char *getpass(const char *prompt);
64
f4626df5
JS
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
77df1f1e
ES
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
f4626df5
JS
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; }
f4626df5
JS
92static inline int fchmod(int fildes, mode_t mode)
93{ errno = ENOSYS; return -1; }
e7cf4e94 94static inline pid_t fork(void)
f4626df5
JS
95{ errno = ENOSYS; return -1; }
96static inline unsigned int alarm(unsigned int seconds)
97{ return 0; }
98static inline int fsync(int fd)
75f6929a 99{ return _commit(fd); }
e7cf4e94 100static inline pid_t getppid(void)
f4626df5
JS
101{ return 1; }
102static inline void sync(void)
103{}
e7cf4e94 104static inline uid_t getuid(void)
f4626df5
JS
105{ return 1; }
106static inline struct passwd *getpwnam(const char *name)
107{ return NULL; }
5f8763a8 108static inline int fcntl(int fd, int cmd, ...)
f4626df5
JS
109{
110 if (cmd == F_GETFD || cmd == F_SETFD)
111 return 0;
112 errno = EINVAL;
113 return -1;
114}
47e3de0e
JS
115/* bash cannot reliably detect negative return codes as failure */
116#define exit(code) exit((code) & 0xff)
f4626df5
JS
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
ef7108ca 128#define WNOHANG 1
52de4db5 129pid_t waitpid(pid_t pid, int *status, unsigned options);
f4626df5 130
82fc07b7
EFL
131#define kill mingw_kill
132int mingw_kill(pid_t pid, int sig);
133
514213bf
EFL
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
f4626df5
JS
155/*
156 * implementations of missing functions
157 */
158
897bb8cb 159int pipe(int filedes[2]);
f4626df5
JS
160unsigned int sleep (unsigned int seconds);
161int mkstemp(char *template);
162int gettimeofday(struct timeval *tv, void *tz);
f4626df5
JS
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 */
e7cf4e94 166struct passwd *getpwuid(uid_t uid);
f4626df5
JS
167int setitimer(int type, struct itimerval *in, struct itimerval *out);
168int sigaction(int sig, struct sigaction *in, struct sigaction *out);
7be401e0 169int link(const char *oldpath, const char *newpath);
80ba074f 170
25fe217b
JS
171/*
172 * replacements of existing functions
173 */
174
337967fb
HV
175int mingw_unlink(const char *pathname);
176#define unlink mingw_unlink
177
3e4a1ba0
JS
178int mingw_open (const char *filename, int oflags, ...);
179#define open mingw_open
180
c8b29645
RS
181ssize_t mingw_write(int fd, const void *buf, size_t count);
182#define write mingw_write
183
3fdcdbdf
JS
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
25fe217b
JS
190char *mingw_getcwd(char *pointer, int len);
191#define getcwd mingw_getcwd
192
6fd6aec4
JS
193char *mingw_getenv(const char *name);
194#define getenv mingw_getenv
195
746fb857
JS
196struct hostent *mingw_gethostbyname(const char *host);
197#define gethostbyname mingw_gethostbyname
198
fe3b2b7b
MS
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
746fb857
JS
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
772991af
MP
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
ea9e98c3
JS
229int mingw_rename(const char*, const char*);
230#define rename mingw_rename
231
b1b95204 232#if defined(USE_WIN32_MMAP) || defined(_MSC_VER)
b130a72b
JL
233int mingw_getpagesize(void);
234#define getpagesize mingw_getpagesize
235#endif
236
5411bdc4
MSO
237/* Use mingw_lstat() instead of lstat()/stat() and
238 * mingw_fstat() instead of fstat() on Windows.
239 */
1d4e4cd4 240#define off_t off64_t
1d4e4cd4 241#define lseek _lseeki64
b6f714f8
RJ
242#ifndef ALREADY_DECLARED_STAT_FUNCS
243#define stat _stati64
180964f0 244int mingw_lstat(const char *file_name, struct stat *buf);
9b9784ca 245int mingw_stat(const char *file_name, struct stat *buf);
180964f0 246int mingw_fstat(int fd, struct stat *buf);
5411bdc4
MSO
247#define fstat mingw_fstat
248#define lstat mingw_lstat
9b9784ca 249#define _stati64(x,y) mingw_stat(x,y)
b6f714f8 250#endif
5411bdc4 251
7c0ffa1c
JS
252int mingw_utime(const char *file_name, const struct utimbuf *times);
253#define utime mingw_utime
254
75301f90 255pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
f9a2743c 256 const char *dir,
75301f90 257 int fhin, int fhout, int fherr);
f1a4dfb8
JS
258void mingw_execvp(const char *cmd, char *const *argv);
259#define execvp mingw_execvp
5debf9a5
ES
260void mingw_execv(const char *cmd, char *const *argv);
261#define execv mingw_execv
f1a4dfb8 262
cd800eec
SP
263static inline unsigned int git_ntohl(unsigned int x)
264{ return (unsigned int)ntohl(x); }
265#define ntohl git_ntohl
266
6072fc31
JS
267sig_handler_t mingw_signal(int sig, sig_handler_t handler);
268#define signal mingw_signal
269
c09df8a7
PH
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
80ba074f
JS
281/*
282 * git specific compatibility
283 */
284
25fe217b
JS
285#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
286#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
80ba074f 287#define PATH_SEP ';'
82f8d969 288#define PRIuMAX "I64u"
ba26f296 289
4804aabc
SP
290void mingw_open_html(const char *path);
291#define open_html mingw_open_html
292
ba26f296
JS
293/*
294 * helpers
295 */
296
2affea41 297char **make_augmented_environ(const char *const *vars);
ba26f296 298void free_environ(char **env);
35eeef47
JS
299
300/*
301 * A replacement of main() that ensures that argv[0] has a path
a6ca8c62 302 * and that default fmode and std(in|out|err) are in binary mode
35eeef47
JS
303 */
304
22537765
SP
305#define main(c,v) dummy_decl_mingw_main(); \
306static int mingw_main(); \
307int main(int argc, const char **argv) \
35eeef47 308{ \
52de4db5 309 extern CRITICAL_SECTION pinfo_cs; \
a6ca8c62
MSO
310 _fmode = _O_BINARY; \
311 _setmode(_fileno(stdin), _O_BINARY); \
312 _setmode(_fileno(stdout), _O_BINARY); \
313 _setmode(_fileno(stderr), _O_BINARY); \
35eeef47 314 argv[0] = xstrdup(_pgmptr); \
52de4db5 315 InitializeCriticalSection(&pinfo_cs); \
35eeef47
JS
316 return mingw_main(argc, argv); \
317} \
318static int mingw_main(c,v)
e16c60d9 319
44626dc7
AH
320/*
321 * Used by Pthread API implementation for Windows
322 */
323extern int err_win_to_posix(DWORD winerr);