]> git.ipfire.org Git - thirdparty/git.git/blame - compat/mingw.h
Merge branch 'jn/update-contrib-example-merge'
[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;
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
303e7c48
JS
21#define WIFEXITED(x) 1
22#define WIFSIGNALED(x) 0
f4626df5 23#define WEXITSTATUS(x) ((x) & 0xff)
303e7c48 24#define WTERMSIG(x) SIGTERM
f4626df5 25
d2825065
JS
26#define SIGHUP 1
27#define SIGQUIT 3
28#define SIGKILL 9
29#define SIGPIPE 13
30#define SIGALRM 14
31#define SIGCHLD 17
f4626df5
JS
32
33#define F_GETFD 1
34#define F_SETFD 2
35#define FD_CLOEXEC 0x1
36
37struct passwd {
38 char *pw_name;
39 char *pw_gecos;
40 char *pw_dir;
41};
42
0dbbbc1e
JS
43extern char *getpass(const char *prompt);
44
435bdf8c 45#ifndef POLLIN
f4626df5
JS
46struct 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
435bdf8c 53#endif
f4626df5
JS
54
55typedef void (__cdecl *sig_handler_t)(int);
56struct sigaction {
57 sig_handler_t sa_handler;
58 unsigned sa_flags;
59};
60#define sigemptyset(x) (void)0
61#define SA_RESTART 0
62
63struct itimerval {
64 struct timeval it_value, it_interval;
65};
66#define ITIMER_REAL 0
67
f4626df5
JS
68/*
69 * trivial stubs
70 */
71
72static inline int readlink(const char *path, char *buf, size_t bufsiz)
73{ errno = ENOSYS; return -1; }
74static inline int symlink(const char *oldpath, const char *newpath)
75{ errno = ENOSYS; return -1; }
f4626df5
JS
76static inline int fchmod(int fildes, mode_t mode)
77{ errno = ENOSYS; return -1; }
78static inline int fork(void)
79{ errno = ENOSYS; return -1; }
80static inline unsigned int alarm(unsigned int seconds)
81{ return 0; }
82static inline int fsync(int fd)
75f6929a 83{ return _commit(fd); }
f4626df5
JS
84static inline int getppid(void)
85{ return 1; }
86static inline void sync(void)
87{}
88static inline int getuid()
89{ return 1; }
90static inline struct passwd *getpwnam(const char *name)
91{ return NULL; }
5f8763a8 92static inline int fcntl(int fd, int cmd, ...)
f4626df5
JS
93{
94 if (cmd == F_GETFD || cmd == F_SETFD)
95 return 0;
96 errno = EINVAL;
97 return -1;
98}
47e3de0e
JS
99/* bash cannot reliably detect negative return codes as failure */
100#define exit(code) exit((code) & 0xff)
f4626df5
JS
101
102/*
103 * simple adaptors
104 */
105
106static inline int mingw_mkdir(const char *path, int mode)
107{
108 return mkdir(path);
109}
110#define mkdir mingw_mkdir
111
132a6e90
JS
112static 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
27e3219f 120static inline int waitpid(pid_t pid, int *status, unsigned options)
f4626df5
JS
121{
122 if (options == 0)
123 return _cwait(status, pid, 0);
124 errno = EINVAL;
125 return -1;
126}
127
514213bf
EFL
128#ifndef NO_OPENSSL
129#include <openssl/ssl.h>
130static 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
136static 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
142static 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
f4626df5
JS
149/*
150 * implementations of missing functions
151 */
152
897bb8cb 153int pipe(int filedes[2]);
f4626df5
JS
154unsigned int sleep (unsigned int seconds);
155int mkstemp(char *template);
156int gettimeofday(struct timeval *tv, void *tz);
157int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
158struct tm *gmtime_r(const time_t *timep, struct tm *result);
159struct tm *localtime_r(const time_t *timep, struct tm *result);
160int getpagesize(void); /* defined in MinGW's libgcc.a */
161struct passwd *getpwuid(int uid);
162int setitimer(int type, struct itimerval *in, struct itimerval *out);
163int sigaction(int sig, struct sigaction *in, struct sigaction *out);
7be401e0 164int link(const char *oldpath, const char *newpath);
80ba074f 165
25fe217b
JS
166/*
167 * replacements of existing functions
168 */
169
3e4a1ba0
JS
170int mingw_open (const char *filename, int oflags, ...);
171#define open mingw_open
172
c8b29645
RS
173ssize_t mingw_write(int fd, const void *buf, size_t count);
174#define write mingw_write
175
3fdcdbdf
JS
176FILE *mingw_fopen (const char *filename, const char *otype);
177#define fopen mingw_fopen
178
179FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);
180#define freopen mingw_freopen
181
25fe217b
JS
182char *mingw_getcwd(char *pointer, int len);
183#define getcwd mingw_getcwd
184
6fd6aec4
JS
185char *mingw_getenv(const char *name);
186#define getenv mingw_getenv
187
746fb857
JS
188struct hostent *mingw_gethostbyname(const char *host);
189#define gethostbyname mingw_gethostbyname
190
fe3b2b7b
MS
191void mingw_freeaddrinfo(struct addrinfo *res);
192#define freeaddrinfo mingw_freeaddrinfo
193
194int mingw_getaddrinfo(const char *node, const char *service,
195 const struct addrinfo *hints, struct addrinfo **res);
196#define getaddrinfo mingw_getaddrinfo
197
198int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen,
199 char *host, DWORD hostlen, char *serv, DWORD servlen,
200 int flags);
201#define getnameinfo mingw_getnameinfo
202
746fb857
JS
203int mingw_socket(int domain, int type, int protocol);
204#define socket mingw_socket
205
206int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
207#define connect mingw_connect
208
ea9e98c3
JS
209int mingw_rename(const char*, const char*);
210#define rename mingw_rename
211
b1b95204 212#if defined(USE_WIN32_MMAP) || defined(_MSC_VER)
b130a72b
JL
213int mingw_getpagesize(void);
214#define getpagesize mingw_getpagesize
215#endif
216
5411bdc4
MSO
217/* Use mingw_lstat() instead of lstat()/stat() and
218 * mingw_fstat() instead of fstat() on Windows.
219 */
1d4e4cd4 220#define off_t off64_t
1d4e4cd4 221#define lseek _lseeki64
b6f714f8
RJ
222#ifndef ALREADY_DECLARED_STAT_FUNCS
223#define stat _stati64
180964f0
JS
224int mingw_lstat(const char *file_name, struct stat *buf);
225int mingw_fstat(int fd, struct stat *buf);
5411bdc4
MSO
226#define fstat mingw_fstat
227#define lstat mingw_lstat
d5e31235 228#define _stati64(x,y) mingw_lstat(x,y)
b6f714f8 229#endif
5411bdc4 230
7c0ffa1c
JS
231int mingw_utime(const char *file_name, const struct utimbuf *times);
232#define utime mingw_utime
233
75301f90 234pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
f9a2743c 235 const char *dir,
75301f90 236 int fhin, int fhout, int fherr);
f1a4dfb8
JS
237void mingw_execvp(const char *cmd, char *const *argv);
238#define execvp mingw_execvp
239
cd800eec
SP
240static inline unsigned int git_ntohl(unsigned int x)
241{ return (unsigned int)ntohl(x); }
242#define ntohl git_ntohl
243
6072fc31
JS
244sig_handler_t mingw_signal(int sig, sig_handler_t handler);
245#define signal mingw_signal
246
c09df8a7
PH
247/*
248 * ANSI emulation wrappers
249 */
250
251int winansi_fputs(const char *str, FILE *stream);
252int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
253int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
254#define fputs winansi_fputs
255#define printf(...) winansi_printf(__VA_ARGS__)
256#define fprintf(...) winansi_fprintf(__VA_ARGS__)
257
80ba074f
JS
258/*
259 * git specific compatibility
260 */
261
25fe217b
JS
262#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
263#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
80ba074f 264#define PATH_SEP ';'
82f8d969 265#define PRIuMAX "I64u"
ba26f296 266
4804aabc
SP
267void mingw_open_html(const char *path);
268#define open_html mingw_open_html
269
ba26f296
JS
270/*
271 * helpers
272 */
273
2affea41 274char **make_augmented_environ(const char *const *vars);
ba26f296 275void free_environ(char **env);
35eeef47
JS
276
277/*
278 * A replacement of main() that ensures that argv[0] has a path
a6ca8c62 279 * and that default fmode and std(in|out|err) are in binary mode
35eeef47
JS
280 */
281
22537765
SP
282#define main(c,v) dummy_decl_mingw_main(); \
283static int mingw_main(); \
284int main(int argc, const char **argv) \
35eeef47 285{ \
a6ca8c62
MSO
286 _fmode = _O_BINARY; \
287 _setmode(_fileno(stdin), _O_BINARY); \
288 _setmode(_fileno(stdout), _O_BINARY); \
289 _setmode(_fileno(stderr), _O_BINARY); \
35eeef47
JS
290 argv[0] = xstrdup(_pgmptr); \
291 return mingw_main(argc, argv); \
292} \
293static int mingw_main(c,v)
e16c60d9
MSO
294
295#ifndef NO_MINGW_REPLACE_READDIR
296/*
297 * A replacement of readdir, to ensure that it reads the file type at
298 * the same time. This avoid extra unneeded lstats in git on MinGW
299 */
300#undef DT_UNKNOWN
301#undef DT_DIR
302#undef DT_REG
303#undef DT_LNK
304#define DT_UNKNOWN 0
305#define DT_DIR 1
306#define DT_REG 2
307#define DT_LNK 3
308
309struct mingw_dirent
310{
311 long d_ino; /* Always zero. */
312 union {
313 unsigned short d_reclen; /* Always zero. */
314 unsigned char d_type; /* Reimplementation adds this */
315 };
316 unsigned short d_namlen; /* Length of name in d_name. */
317 char d_name[FILENAME_MAX]; /* File name. */
318};
319#define dirent mingw_dirent
320#define readdir(x) mingw_readdir(x)
321struct dirent *mingw_readdir(DIR *dir);
322#endif // !NO_MINGW_REPLACE_READDIR
44626dc7
AH
323
324/*
325 * Used by Pthread API implementation for Windows
326 */
327extern int err_win_to_posix(DWORD winerr);