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