start_command: do not clobber cmd->env on Windows code path
[git/git.git] / compat / mingw.h
CommitLineData
f4626df5
JS
1#include <winsock2.h>
2
3/*
4 * things that are not available in header files
5 */
6
7typedef int pid_t;
8#define hstrerror strerror
9
10#define S_IFLNK 0120000 /* Symbolic link */
11#define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
12#define S_ISSOCK(x) 0
13#define S_IRGRP 0
14#define S_IWGRP 0
15#define S_IXGRP 0
16#define S_ISGID 0
17#define S_IROTH 0
18#define S_IXOTH 0
19
303e7c48
JS
20#define WIFEXITED(x) 1
21#define WIFSIGNALED(x) 0
f4626df5 22#define WEXITSTATUS(x) ((x) & 0xff)
303e7c48 23#define WTERMSIG(x) SIGTERM
f4626df5 24
d2825065
JS
25#define SIGHUP 1
26#define SIGQUIT 3
27#define SIGKILL 9
28#define SIGPIPE 13
29#define SIGALRM 14
30#define SIGCHLD 17
f4626df5
JS
31
32#define F_GETFD 1
33#define F_SETFD 2
34#define FD_CLOEXEC 0x1
35
36struct passwd {
37 char *pw_name;
38 char *pw_gecos;
39 char *pw_dir;
40};
41
0dbbbc1e
JS
42extern char *getpass(const char *prompt);
43
f4626df5
JS
44struct pollfd {
45 int fd; /* file descriptor */
46 short events; /* requested events */
47 short revents; /* returned events */
48};
49#define POLLIN 1
50#define POLLHUP 2
51
52typedef void (__cdecl *sig_handler_t)(int);
53struct sigaction {
54 sig_handler_t sa_handler;
55 unsigned sa_flags;
56};
57#define sigemptyset(x) (void)0
58#define SA_RESTART 0
59
60struct itimerval {
61 struct timeval it_value, it_interval;
62};
63#define ITIMER_REAL 0
64
f4626df5
JS
65/*
66 * trivial stubs
67 */
68
69static inline int readlink(const char *path, char *buf, size_t bufsiz)
70{ errno = ENOSYS; return -1; }
71static inline int symlink(const char *oldpath, const char *newpath)
72{ errno = ENOSYS; return -1; }
f4626df5
JS
73static inline int fchmod(int fildes, mode_t mode)
74{ errno = ENOSYS; return -1; }
75static inline int fork(void)
76{ errno = ENOSYS; return -1; }
77static inline unsigned int alarm(unsigned int seconds)
78{ return 0; }
79static inline int fsync(int fd)
80{ return 0; }
81static inline int getppid(void)
82{ return 1; }
83static inline void sync(void)
84{}
85static inline int getuid()
86{ return 1; }
87static inline struct passwd *getpwnam(const char *name)
88{ return NULL; }
89static inline int fcntl(int fd, int cmd, long arg)
90{
91 if (cmd == F_GETFD || cmd == F_SETFD)
92 return 0;
93 errno = EINVAL;
94 return -1;
95}
47e3de0e
JS
96/* bash cannot reliably detect negative return codes as failure */
97#define exit(code) exit((code) & 0xff)
f4626df5
JS
98
99/*
100 * simple adaptors
101 */
102
103static inline int mingw_mkdir(const char *path, int mode)
104{
105 return mkdir(path);
106}
107#define mkdir mingw_mkdir
108
132a6e90
JS
109static inline int mingw_unlink(const char *pathname)
110{
111 /* read-only files cannot be removed */
112 chmod(pathname, 0666);
113 return unlink(pathname);
114}
115#define unlink mingw_unlink
116
27e3219f 117static inline int waitpid(pid_t pid, int *status, unsigned options)
f4626df5
JS
118{
119 if (options == 0)
120 return _cwait(status, pid, 0);
121 errno = EINVAL;
122 return -1;
123}
124
f4626df5
JS
125/*
126 * implementations of missing functions
127 */
128
897bb8cb 129int pipe(int filedes[2]);
f4626df5
JS
130unsigned int sleep (unsigned int seconds);
131int mkstemp(char *template);
132int gettimeofday(struct timeval *tv, void *tz);
133int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
134struct tm *gmtime_r(const time_t *timep, struct tm *result);
135struct tm *localtime_r(const time_t *timep, struct tm *result);
136int getpagesize(void); /* defined in MinGW's libgcc.a */
137struct passwd *getpwuid(int uid);
138int setitimer(int type, struct itimerval *in, struct itimerval *out);
139int sigaction(int sig, struct sigaction *in, struct sigaction *out);
7be401e0 140int link(const char *oldpath, const char *newpath);
80ba074f 141
25fe217b
JS
142/*
143 * replacements of existing functions
144 */
145
3e4a1ba0
JS
146int mingw_open (const char *filename, int oflags, ...);
147#define open mingw_open
148
25fe217b
JS
149char *mingw_getcwd(char *pointer, int len);
150#define getcwd mingw_getcwd
151
6fd6aec4
JS
152char *mingw_getenv(const char *name);
153#define getenv mingw_getenv
154
746fb857
JS
155struct hostent *mingw_gethostbyname(const char *host);
156#define gethostbyname mingw_gethostbyname
157
158int mingw_socket(int domain, int type, int protocol);
159#define socket mingw_socket
160
161int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
162#define connect mingw_connect
163
ea9e98c3
JS
164int mingw_rename(const char*, const char*);
165#define rename mingw_rename
166
b130a72b
JL
167#ifdef USE_WIN32_MMAP
168int mingw_getpagesize(void);
169#define getpagesize mingw_getpagesize
170#endif
171
5411bdc4
MSO
172/* Use mingw_lstat() instead of lstat()/stat() and
173 * mingw_fstat() instead of fstat() on Windows.
174 */
1d4e4cd4
JS
175#define off_t off64_t
176#define stat _stati64
177#define lseek _lseeki64
180964f0
JS
178int mingw_lstat(const char *file_name, struct stat *buf);
179int mingw_fstat(int fd, struct stat *buf);
5411bdc4
MSO
180#define fstat mingw_fstat
181#define lstat mingw_lstat
d5e31235 182#define _stati64(x,y) mingw_lstat(x,y)
5411bdc4 183
7c0ffa1c
JS
184int mingw_utime(const char *file_name, const struct utimbuf *times);
185#define utime mingw_utime
186
7e5d7768 187pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env);
f1a4dfb8
JS
188void mingw_execvp(const char *cmd, char *const *argv);
189#define execvp mingw_execvp
190
cd800eec
SP
191static inline unsigned int git_ntohl(unsigned int x)
192{ return (unsigned int)ntohl(x); }
193#define ntohl git_ntohl
194
6072fc31
JS
195sig_handler_t mingw_signal(int sig, sig_handler_t handler);
196#define signal mingw_signal
197
c09df8a7
PH
198/*
199 * ANSI emulation wrappers
200 */
201
202int winansi_fputs(const char *str, FILE *stream);
203int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
204int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
205#define fputs winansi_fputs
206#define printf(...) winansi_printf(__VA_ARGS__)
207#define fprintf(...) winansi_fprintf(__VA_ARGS__)
208
80ba074f
JS
209/*
210 * git specific compatibility
211 */
212
25fe217b
JS
213#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
214#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
80ba074f 215#define PATH_SEP ';'
82f8d969 216#define PRIuMAX "I64u"
ba26f296 217
4804aabc
SP
218void mingw_open_html(const char *path);
219#define open_html mingw_open_html
220
ba26f296
JS
221/*
222 * helpers
223 */
224
2affea41 225char **make_augmented_environ(const char *const *vars);
ba26f296 226void free_environ(char **env);
35eeef47
JS
227
228/*
229 * A replacement of main() that ensures that argv[0] has a path
230 */
231
22537765
SP
232#define main(c,v) dummy_decl_mingw_main(); \
233static int mingw_main(); \
234int main(int argc, const char **argv) \
35eeef47 235{ \
35eeef47
JS
236 argv[0] = xstrdup(_pgmptr); \
237 return mingw_main(argc, argv); \
238} \
239static int mingw_main(c,v)
e16c60d9
MSO
240
241#ifndef NO_MINGW_REPLACE_READDIR
242/*
243 * A replacement of readdir, to ensure that it reads the file type at
244 * the same time. This avoid extra unneeded lstats in git on MinGW
245 */
246#undef DT_UNKNOWN
247#undef DT_DIR
248#undef DT_REG
249#undef DT_LNK
250#define DT_UNKNOWN 0
251#define DT_DIR 1
252#define DT_REG 2
253#define DT_LNK 3
254
255struct mingw_dirent
256{
257 long d_ino; /* Always zero. */
258 union {
259 unsigned short d_reclen; /* Always zero. */
260 unsigned char d_type; /* Reimplementation adds this */
261 };
262 unsigned short d_namlen; /* Length of name in d_name. */
263 char d_name[FILENAME_MAX]; /* File name. */
264};
265#define dirent mingw_dirent
266#define readdir(x) mingw_readdir(x)
267struct dirent *mingw_readdir(DIR *dir);
268#endif // !NO_MINGW_REPLACE_READDIR