mingw: implement getppid
[git/kusma.git] / compat / mingw.h
blobd79943099ea2303b9cfaf22ae5a806d1d9874055
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 #define hstrerror strerror
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
21 #define WIFEXITED(x) 1
22 #define WIFSIGNALED(x) 0
23 #define WEXITSTATUS(x) ((x) & 0xff)
24 #define WTERMSIG(x) SIGTERM
26 #define SIGHUP 1
27 #define SIGQUIT 3
28 #define SIGKILL 9
29 #define SIGPIPE 13
30 #define SIGALRM 14
31 #define SIGCHLD 17
33 #define F_GETFD 1
34 #define F_SETFD 2
35 #define FD_CLOEXEC 0x1
37 struct passwd {
38 char *pw_name;
39 char *pw_gecos;
40 char *pw_dir;
43 extern char *getpass(const char *prompt);
45 #ifndef POLLIN
46 struct pollfd {
47 int fd; /* file descriptor */
48 short events; /* requested events */
49 short revents; /* returned events */
51 #define POLLIN 1
52 #define POLLHUP 2
53 #endif
55 typedef void (__cdecl *sig_handler_t)(int);
56 struct sigaction {
57 sig_handler_t sa_handler;
58 unsigned sa_flags;
60 #define sigemptyset(x) (void)0
61 #define SA_RESTART 0
63 struct itimerval {
64 struct timeval it_value, it_interval;
66 #define ITIMER_REAL 0
69 * trivial stubs
72 static inline int readlink(const char *path, char *buf, size_t bufsiz)
73 { errno = ENOSYS; return -1; }
74 static inline int symlink(const char *oldpath, const char *newpath)
75 { errno = ENOSYS; return -1; }
76 static inline int fchmod(int fildes, mode_t mode)
77 { errno = ENOSYS; return -1; }
78 static inline int fork(void)
79 { errno = ENOSYS; return -1; }
80 static inline unsigned int alarm(unsigned int seconds)
81 { return 0; }
82 static inline int fsync(int fd)
83 { return _commit(fd); }
84 pid_t getppid(void);
85 static inline void sync(void)
87 static inline int getuid()
88 { return 1; }
89 static inline struct passwd *getpwnam(const char *name)
90 { return NULL; }
91 static inline int fcntl(int fd, int cmd, ...)
93 if (cmd == F_GETFD || cmd == F_SETFD)
94 return 0;
95 errno = EINVAL;
96 return -1;
98 /* bash cannot reliably detect negative return codes as failure */
99 #define exit(code) exit((code) & 0xff)
102 * simple adaptors
105 static inline int mingw_mkdir(const char *path, int mode)
107 return mkdir(path);
109 #define mkdir mingw_mkdir
111 static inline int mingw_unlink(const char *pathname)
113 /* read-only files cannot be removed */
114 chmod(pathname, 0666);
115 return unlink(pathname);
117 #define unlink mingw_unlink
119 static inline int waitpid(pid_t pid, int *status, unsigned options)
121 if (options == 0)
122 return _cwait(status, pid, 0);
123 errno = EINVAL;
124 return -1;
127 #ifndef NO_OPENSSL
128 #include <openssl/ssl.h>
129 static inline int mingw_SSL_set_fd(SSL *ssl, int fd)
131 return SSL_set_fd(ssl, _get_osfhandle(fd));
133 #define SSL_set_fd mingw_SSL_set_fd
135 static inline int mingw_SSL_set_rfd(SSL *ssl, int fd)
137 return SSL_set_rfd(ssl, _get_osfhandle(fd));
139 #define SSL_set_rfd mingw_SSL_set_rfd
141 static inline int mingw_SSL_set_wfd(SSL *ssl, int fd)
143 return SSL_set_wfd(ssl, _get_osfhandle(fd));
145 #define SSL_set_wfd mingw_SSL_set_wfd
146 #endif
149 * implementations of missing functions
152 int pipe(int filedes[2]);
153 unsigned int sleep (unsigned int seconds);
154 int mkstemp(char *template);
155 int gettimeofday(struct timeval *tv, void *tz);
156 int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
157 struct tm *gmtime_r(const time_t *timep, struct tm *result);
158 struct tm *localtime_r(const time_t *timep, struct tm *result);
159 int getpagesize(void); /* defined in MinGW's libgcc.a */
160 struct passwd *getpwuid(int uid);
161 int setitimer(int type, struct itimerval *in, struct itimerval *out);
162 int sigaction(int sig, struct sigaction *in, struct sigaction *out);
163 int link(const char *oldpath, const char *newpath);
166 * replacements of existing functions
169 int mingw_open (const char *filename, int oflags, ...);
170 #define open mingw_open
172 ssize_t mingw_write(int fd, const void *buf, size_t count);
173 #define write mingw_write
175 FILE *mingw_fopen (const char *filename, const char *otype);
176 #define fopen mingw_fopen
178 FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);
179 #define freopen mingw_freopen
181 char *mingw_getcwd(char *pointer, int len);
182 #define getcwd mingw_getcwd
184 char *mingw_getenv(const char *name);
185 #define getenv mingw_getenv
187 struct hostent *mingw_gethostbyname(const char *host);
188 #define gethostbyname mingw_gethostbyname
190 void mingw_freeaddrinfo(struct addrinfo *res);
191 #define freeaddrinfo mingw_freeaddrinfo
193 int mingw_getaddrinfo(const char *node, const char *service,
194 const struct addrinfo *hints, struct addrinfo **res);
195 #define getaddrinfo mingw_getaddrinfo
197 int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen,
198 char *host, DWORD hostlen, char *serv, DWORD servlen,
199 int flags);
200 #define getnameinfo mingw_getnameinfo
202 int mingw_socket(int domain, int type, int protocol);
203 #define socket mingw_socket
205 int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
206 #define connect mingw_connect
208 int mingw_rename(const char*, const char*);
209 #define rename mingw_rename
211 #if defined(USE_WIN32_MMAP) || defined(_MSC_VER)
212 int mingw_getpagesize(void);
213 #define getpagesize mingw_getpagesize
214 #endif
216 /* Use mingw_lstat() instead of lstat()/stat() and
217 * mingw_fstat() instead of fstat() on Windows.
219 #define off_t off64_t
220 #define lseek _lseeki64
221 #ifndef ALREADY_DECLARED_STAT_FUNCS
222 #define stat _stati64
223 int mingw_lstat(const char *file_name, struct stat *buf);
224 int mingw_fstat(int fd, struct stat *buf);
225 #define fstat mingw_fstat
226 #define lstat mingw_lstat
227 #define _stati64(x,y) mingw_lstat(x,y)
228 #endif
230 int mingw_utime(const char *file_name, const struct utimbuf *times);
231 #define utime mingw_utime
233 pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
234 const char *dir,
235 int fhin, int fhout, int fherr);
236 void mingw_execvp(const char *cmd, char *const *argv);
237 #define execvp mingw_execvp
239 static inline unsigned int git_ntohl(unsigned int x)
240 { return (unsigned int)ntohl(x); }
241 #define ntohl git_ntohl
243 sig_handler_t mingw_signal(int sig, sig_handler_t handler);
244 #define signal mingw_signal
247 * ANSI emulation wrappers
250 int winansi_fputs(const char *str, FILE *stream);
251 int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
252 int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
253 #define fputs winansi_fputs
254 #define printf(...) winansi_printf(__VA_ARGS__)
255 #define fprintf(...) winansi_fprintf(__VA_ARGS__)
258 * git specific compatibility
261 #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
262 #define is_dir_sep(c) ((c) == '/' || (c) == '\\')
263 #define PATH_SEP ';'
264 #define PRIuMAX "I64u"
266 void mingw_open_html(const char *path);
267 #define open_html mingw_open_html
270 * helpers
273 char **make_augmented_environ(const char *const *vars);
274 void free_environ(char **env);
277 * A replacement of main() that ensures that argv[0] has a path
278 * and that default fmode and std(in|out|err) are in binary mode
281 #define main(c,v) dummy_decl_mingw_main(); \
282 static int mingw_main(); \
283 int main(int argc, const char **argv) \
285 _fmode = _O_BINARY; \
286 _setmode(_fileno(stdin), _O_BINARY); \
287 _setmode(_fileno(stdout), _O_BINARY); \
288 _setmode(_fileno(stderr), _O_BINARY); \
289 argv[0] = xstrdup(_pgmptr); \
290 return mingw_main(argc, argv); \
292 static int mingw_main(c,v)
294 #ifndef NO_MINGW_REPLACE_READDIR
296 * A replacement of readdir, to ensure that it reads the file type at
297 * the same time. This avoid extra unneeded lstats in git on MinGW
299 #undef DT_UNKNOWN
300 #undef DT_DIR
301 #undef DT_REG
302 #undef DT_LNK
303 #define DT_UNKNOWN 0
304 #define DT_DIR 1
305 #define DT_REG 2
306 #define DT_LNK 3
308 struct mingw_dirent
310 long d_ino; /* Always zero. */
311 union {
312 unsigned short d_reclen; /* Always zero. */
313 unsigned char d_type; /* Reimplementation adds this */
315 unsigned short d_namlen; /* Length of name in d_name. */
316 char d_name[FILENAME_MAX]; /* File name. */
318 #define dirent mingw_dirent
319 #define readdir(x) mingw_readdir(x)
320 struct dirent *mingw_readdir(DIR *dir);
321 #endif // !NO_MINGW_REPLACE_READDIR
324 * Used by Pthread API implementation for Windows
326 extern int err_win_to_posix(DWORD winerr);