MinGW: Use pid_t more consequently, introduce uid_t for greater compatibility
[git/dscho.git] / compat / mingw.h
blob912c4256fd99e95b0b941d89274b63668ceb18b1
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 _commit(fd); }
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 ssize_t mingw_write(int fd, const void *buf, size_t count);
175 #define write mingw_write
177 FILE *mingw_fopen (const char *filename, const char *otype);
178 #define fopen mingw_fopen
180 FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);
181 #define freopen mingw_freopen
183 char *mingw_getcwd(char *pointer, int len);
184 #define getcwd mingw_getcwd
186 char *mingw_getenv(const char *name);
187 #define getenv mingw_getenv
189 struct hostent *mingw_gethostbyname(const char *host);
190 #define gethostbyname mingw_gethostbyname
192 void mingw_freeaddrinfo(struct addrinfo *res);
193 #define freeaddrinfo mingw_freeaddrinfo
195 int mingw_getaddrinfo(const char *node, const char *service,
196 const struct addrinfo *hints, struct addrinfo **res);
197 #define getaddrinfo mingw_getaddrinfo
199 int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen,
200 char *host, DWORD hostlen, char *serv, DWORD servlen,
201 int flags);
202 #define getnameinfo mingw_getnameinfo
204 int mingw_socket(int domain, int type, int protocol);
205 #define socket mingw_socket
207 int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
208 #define connect mingw_connect
210 int mingw_rename(const char*, const char*);
211 #define rename mingw_rename
213 #if defined(USE_WIN32_MMAP) || defined(_MSC_VER)
214 int mingw_getpagesize(void);
215 #define getpagesize mingw_getpagesize
216 #endif
218 /* Use mingw_lstat() instead of lstat()/stat() and
219 * mingw_fstat() instead of fstat() on Windows.
221 #define off_t off64_t
222 #define lseek _lseeki64
223 #ifndef ALREADY_DECLARED_STAT_FUNCS
224 #define stat _stati64
225 int mingw_lstat(const char *file_name, struct stat *buf);
226 int mingw_fstat(int fd, struct stat *buf);
227 #define fstat mingw_fstat
228 #define lstat mingw_lstat
229 #define _stati64(x,y) mingw_lstat(x,y)
230 #endif
232 int mingw_utime(const char *file_name, const struct utimbuf *times);
233 #define utime mingw_utime
235 pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
236 const char *dir,
237 int fhin, int fhout, int fherr);
238 void mingw_execvp(const char *cmd, char *const *argv);
239 #define execvp mingw_execvp
241 static inline unsigned int git_ntohl(unsigned int x)
242 { return (unsigned int)ntohl(x); }
243 #define ntohl git_ntohl
245 sig_handler_t mingw_signal(int sig, sig_handler_t handler);
246 #define signal mingw_signal
249 * ANSI emulation wrappers
252 int winansi_fputs(const char *str, FILE *stream);
253 int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
254 int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
255 #define fputs winansi_fputs
256 #define printf(...) winansi_printf(__VA_ARGS__)
257 #define fprintf(...) winansi_fprintf(__VA_ARGS__)
260 * git specific compatibility
263 #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
264 #define is_dir_sep(c) ((c) == '/' || (c) == '\\')
265 #define PATH_SEP ';'
266 #define PRIuMAX "I64u"
268 void mingw_open_html(const char *path);
269 #define open_html mingw_open_html
272 * helpers
275 char **make_augmented_environ(const char *const *vars);
276 void free_environ(char **env);
279 * A replacement of main() that ensures that argv[0] has a path
280 * and that default fmode and std(in|out|err) are in binary mode
283 #define main(c,v) dummy_decl_mingw_main(); \
284 static int mingw_main(); \
285 int main(int argc, const char **argv) \
287 _fmode = _O_BINARY; \
288 _setmode(_fileno(stdin), _O_BINARY); \
289 _setmode(_fileno(stdout), _O_BINARY); \
290 _setmode(_fileno(stderr), _O_BINARY); \
291 argv[0] = xstrdup(_pgmptr); \
292 return mingw_main(argc, argv); \
294 static int mingw_main(c,v)
296 #ifndef NO_MINGW_REPLACE_READDIR
298 * A replacement of readdir, to ensure that it reads the file type at
299 * the same time. This avoid extra unneeded lstats in git on MinGW
301 #undef DT_UNKNOWN
302 #undef DT_DIR
303 #undef DT_REG
304 #undef DT_LNK
305 #define DT_UNKNOWN 0
306 #define DT_DIR 1
307 #define DT_REG 2
308 #define DT_LNK 3
310 struct mingw_dirent
312 long d_ino; /* Always zero. */
313 union {
314 unsigned short d_reclen; /* Always zero. */
315 unsigned char d_type; /* Reimplementation adds this */
317 unsigned short d_namlen; /* Length of name in d_name. */
318 char d_name[FILENAME_MAX]; /* File name. */
320 #define dirent mingw_dirent
321 #define readdir(x) mingw_readdir(x)
322 struct dirent *mingw_readdir(DIR *dir);
323 #endif // !NO_MINGW_REPLACE_READDIR
326 * Used by Pthread API implementation for Windows
328 extern int err_win_to_posix(DWORD winerr);