Git fork
1#ifndef COMPAT_MINGW_POSIX_H
2#define COMPAT_MINGW_POSIX_H
3
4#ifdef __MINGW64_VERSION_MAJOR
5#include <stdint.h>
6#include <wchar.h>
7typedef _sigset_t sigset_t;
8#endif
9#include <winsock2.h>
10#include <ws2tcpip.h>
11
12/* MinGW-w64 reports to have flockfile, but it does not actually have it. */
13#ifdef __MINGW64_VERSION_MAJOR
14#undef _POSIX_THREAD_SAFE_FUNCTIONS
15#endif
16
17/*
18 * things that are not available in header files
19 */
20
21typedef int uid_t;
22typedef int socklen_t;
23#ifndef __MINGW64_VERSION_MAJOR
24typedef int pid_t;
25#define hstrerror strerror
26#endif
27
28#define S_IFLNK 0120000 /* Symbolic link */
29#define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
30#define S_ISSOCK(x) 0
31
32#ifndef S_IRWXG
33#define S_IRGRP 0
34#define S_IWGRP 0
35#define S_IXGRP 0
36#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
37#endif
38#ifndef S_IRWXO
39#define S_IROTH 0
40#define S_IWOTH 0
41#define S_IXOTH 0
42#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
43#endif
44
45#define S_ISUID 0004000
46#define S_ISGID 0002000
47#define S_ISVTX 0001000
48
49#define WIFEXITED(x) 1
50#define WIFSIGNALED(x) 0
51#define WEXITSTATUS(x) ((x) & 0xff)
52#define WTERMSIG(x) SIGTERM
53
54#ifndef EWOULDBLOCK
55#define EWOULDBLOCK EAGAIN
56#endif
57#ifndef ELOOP
58#define ELOOP EMLINK
59#endif
60#define SHUT_WR SD_SEND
61
62#define SIGHUP 1
63#define SIGQUIT 3
64#define SIGKILL 9
65#define SIGPIPE 13
66#define SIGALRM 14
67#define SIGCHLD 17
68
69#define F_GETFD 1
70#define F_SETFD 2
71#define FD_CLOEXEC 0x1
72
73#if !defined O_CLOEXEC && defined O_NOINHERIT
74#define O_CLOEXEC O_NOINHERIT
75#endif
76
77#ifndef EAFNOSUPPORT
78#define EAFNOSUPPORT WSAEAFNOSUPPORT
79#endif
80#ifndef ECONNABORTED
81#define ECONNABORTED WSAECONNABORTED
82#endif
83#ifndef ENOTSOCK
84#define ENOTSOCK WSAENOTSOCK
85#endif
86
87struct passwd {
88 char *pw_name;
89 char *pw_gecos;
90 char *pw_dir;
91};
92
93typedef void (__cdecl *sig_handler_t)(int);
94struct sigaction {
95 sig_handler_t sa_handler;
96 unsigned sa_flags;
97};
98#define SA_RESTART 0
99#define SA_NOCLDSTOP 1
100
101struct itimerval {
102 struct timeval it_value, it_interval;
103};
104#define ITIMER_REAL 0
105
106struct utsname {
107 char sysname[16];
108 char nodename[1];
109 char release[16];
110 char version[16];
111 char machine[1];
112};
113
114/*
115 * sanitize preprocessor namespace polluted by Windows headers defining
116 * macros which collide with git local versions
117 */
118#undef HELP_COMMAND /* from winuser.h */
119
120/*
121 * trivial stubs
122 */
123
124static inline int readlink(const char *path UNUSED, char *buf UNUSED, size_t bufsiz UNUSED)
125{ errno = ENOSYS; return -1; }
126static inline int symlink(const char *oldpath UNUSED, const char *newpath UNUSED)
127{ errno = ENOSYS; return -1; }
128static inline int fchmod(int fildes UNUSED, mode_t mode UNUSED)
129{ errno = ENOSYS; return -1; }
130#ifndef __MINGW64_VERSION_MAJOR
131static inline pid_t fork(void)
132{ errno = ENOSYS; return -1; }
133#endif
134static inline unsigned int alarm(unsigned int seconds UNUSED)
135{ return 0; }
136static inline int fsync(int fd)
137{ return _commit(fd); }
138static inline void sync(void)
139{}
140static inline uid_t getuid(void)
141{ return 1; }
142static inline struct passwd *getpwnam(const char *name UNUSED)
143{ return NULL; }
144static inline int fcntl(int fd UNUSED, int cmd, ...)
145{
146 if (cmd == F_GETFD || cmd == F_SETFD)
147 return 0;
148 errno = EINVAL;
149 return -1;
150}
151
152#define sigemptyset(x) (void)0
153static inline int sigaddset(sigset_t *set UNUSED, int signum UNUSED)
154{ return 0; }
155#define SIG_BLOCK 0
156#define SIG_UNBLOCK 0
157static inline int sigprocmask(int how UNUSED, const sigset_t *set UNUSED, sigset_t *oldset UNUSED)
158{ return 0; }
159static inline pid_t getppid(void)
160{ return 1; }
161static inline pid_t getpgid(pid_t pid)
162{ return pid == 0 ? getpid() : pid; }
163static inline pid_t tcgetpgrp(int fd UNUSED)
164{ return getpid(); }
165
166/*
167 * simple adaptors
168 */
169
170int mingw_mkdir(const char *path, int mode);
171#define mkdir mingw_mkdir
172
173#define WNOHANG 1
174pid_t waitpid(pid_t pid, int *status, int options);
175
176#define kill mingw_kill
177int mingw_kill(pid_t pid, int sig);
178
179#define locate_in_PATH mingw_locate_in_PATH
180char *mingw_locate_in_PATH(const char *cmd);
181
182/*
183 * implementations of missing functions
184 */
185
186int pipe(int filedes[2]);
187unsigned int sleep (unsigned int seconds);
188int mkstemp(char *template);
189int gettimeofday(struct timeval *tv, void *tz);
190#ifndef __MINGW64_VERSION_MAJOR
191struct tm *gmtime_r(const time_t *timep, struct tm *result);
192struct tm *localtime_r(const time_t *timep, struct tm *result);
193#endif
194int getpagesize(void); /* defined in MinGW's libgcc.a */
195struct passwd *getpwuid(uid_t uid);
196int setitimer(int type, struct itimerval *in, struct itimerval *out);
197int sigaction(int sig, struct sigaction *in, struct sigaction *out);
198int link(const char *oldpath, const char *newpath);
199int uname(struct utsname *buf);
200
201/*
202 * replacements of existing functions
203 */
204
205int mingw_unlink(const char *pathname, int handle_in_use_error);
206#ifdef MINGW_DONT_HANDLE_IN_USE_ERROR
207# define unlink(path) mingw_unlink(path, 0)
208#else
209# define unlink(path) mingw_unlink(path, 1)
210#endif
211
212int mingw_rmdir(const char *path);
213#define rmdir mingw_rmdir
214
215int mingw_open (const char *filename, int oflags, ...);
216#define open mingw_open
217#undef OPEN_RETURNS_EINTR
218
219int mingw_fgetc(FILE *stream);
220#define fgetc mingw_fgetc
221
222FILE *mingw_fopen (const char *filename, const char *otype);
223#define fopen mingw_fopen
224
225FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);
226#define freopen mingw_freopen
227
228int mingw_fflush(FILE *stream);
229#define fflush mingw_fflush
230
231ssize_t mingw_write(int fd, const void *buf, size_t len);
232#define write mingw_write
233
234int mingw_access(const char *filename, int mode);
235#undef access
236#define access mingw_access
237
238int mingw_chdir(const char *dirname);
239#define chdir mingw_chdir
240
241int mingw_chmod(const char *filename, int mode);
242#define chmod mingw_chmod
243
244char *mingw_mktemp(char *template);
245#define mktemp mingw_mktemp
246
247char *mingw_getcwd(char *pointer, int len);
248#define getcwd mingw_getcwd
249
250#ifdef NO_UNSETENV
251#error "NO_UNSETENV is incompatible with the Windows-specific startup code!"
252#endif
253
254/*
255 * We bind *env() routines (even the mingw_ ones) to private mingw_ versions.
256 * These talk to the CRT using UNICODE/wchar_t, but maintain the original
257 * narrow-char API.
258 *
259 * Note that the MSCRT maintains both ANSI (getenv()) and UNICODE (_wgetenv())
260 * routines and stores both versions of each environment variable in parallel
261 * (and secretly updates both when you set one or the other), but it uses CP_ACP
262 * to do the conversion rather than CP_UTF8.
263 *
264 * Since everything in the git code base is UTF8, we define the mingw_ routines
265 * to access the CRT using the UNICODE routines and manually convert them to
266 * UTF8. This also avoids round-trip problems.
267 *
268 * This also helps with our linkage, since "_wenviron" is publicly exported
269 * from the CRT. But to access "_environ" we would have to statically link
270 * to the CRT (/MT).
271 *
272 * We require NO_SETENV (and let gitsetenv() call our mingw_putenv).
273 */
274#define getenv mingw_getenv
275#define putenv mingw_putenv
276#define unsetenv mingw_putenv
277char *mingw_getenv(const char *name);
278int mingw_putenv(const char *name);
279
280int mingw_gethostname(char *host, int namelen);
281#define gethostname mingw_gethostname
282
283struct hostent *mingw_gethostbyname(const char *host);
284#define gethostbyname mingw_gethostbyname
285
286int mingw_getaddrinfo(const char *node, const char *service,
287 const struct addrinfo *hints, struct addrinfo **res);
288#define getaddrinfo mingw_getaddrinfo
289
290int mingw_socket(int domain, int type, int protocol);
291#define socket mingw_socket
292
293int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
294#define connect mingw_connect
295
296int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz);
297#define bind mingw_bind
298
299int mingw_setsockopt(int sockfd, int lvl, int optname, void *optval, int optlen);
300#define setsockopt mingw_setsockopt
301
302int mingw_shutdown(int sockfd, int how);
303#define shutdown mingw_shutdown
304
305int mingw_listen(int sockfd, int backlog);
306#define listen mingw_listen
307
308int mingw_accept(int sockfd, struct sockaddr *sa, socklen_t *sz);
309#define accept mingw_accept
310
311int mingw_rename(const char*, const char*);
312#define rename mingw_rename
313
314#if defined(USE_WIN32_MMAP) || defined(_MSC_VER)
315int mingw_getpagesize(void);
316#define getpagesize mingw_getpagesize
317#endif
318
319int win32_fsync_no_flush(int fd);
320#define fsync_no_flush win32_fsync_no_flush
321
322#define FSYNC_COMPONENTS_PLATFORM_DEFAULT (FSYNC_COMPONENTS_DEFAULT | FSYNC_COMPONENT_LOOSE_OBJECT)
323#define FSYNC_METHOD_DEFAULT (FSYNC_METHOD_BATCH)
324
325struct rlimit {
326 unsigned int rlim_cur;
327};
328#define RLIMIT_NOFILE 0
329
330static inline int getrlimit(int resource, struct rlimit *rlp)
331{
332 if (resource != RLIMIT_NOFILE) {
333 errno = EINVAL;
334 return -1;
335 }
336
337 rlp->rlim_cur = 2048;
338 return 0;
339}
340
341/*
342 * Use mingw specific stat()/lstat()/fstat() implementations on Windows,
343 * including our own struct stat with 64 bit st_size and nanosecond-precision
344 * file times.
345 */
346#ifndef __MINGW64_VERSION_MAJOR
347#define off_t off64_t
348#define lseek _lseeki64
349#ifndef _MSC_VER
350struct timespec {
351 time_t tv_sec;
352 long tv_nsec;
353};
354#endif
355#endif
356
357struct mingw_stat {
358 _dev_t st_dev;
359 _ino_t st_ino;
360 _mode_t st_mode;
361 short st_nlink;
362 short st_uid;
363 short st_gid;
364 _dev_t st_rdev;
365 off64_t st_size;
366 struct timespec st_atim;
367 struct timespec st_mtim;
368 struct timespec st_ctim;
369};
370
371#define st_atime st_atim.tv_sec
372#define st_mtime st_mtim.tv_sec
373#define st_ctime st_ctim.tv_sec
374
375#ifdef stat
376#undef stat
377#endif
378#define stat mingw_stat
379int mingw_lstat(const char *file_name, struct stat *buf);
380int mingw_stat(const char *file_name, struct stat *buf);
381int mingw_fstat(int fd, struct stat *buf);
382#ifdef fstat
383#undef fstat
384#endif
385#define fstat mingw_fstat
386#ifdef lstat
387#undef lstat
388#endif
389#define lstat mingw_lstat
390
391
392int mingw_utime(const char *file_name, const struct utimbuf *times);
393#define utime mingw_utime
394size_t mingw_strftime(char *s, size_t max,
395 const char *format, const struct tm *tm);
396#define strftime mingw_strftime
397
398pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
399 const char *dir,
400 int fhin, int fhout, int fherr);
401int mingw_execvp(const char *cmd, char *const *argv);
402#define execvp mingw_execvp
403int mingw_execv(const char *cmd, char *const *argv);
404#define execv mingw_execv
405
406static inline unsigned int git_ntohl(unsigned int x)
407{ return (unsigned int)ntohl(x); }
408#define ntohl git_ntohl
409
410sig_handler_t mingw_signal(int sig, sig_handler_t handler);
411#define signal mingw_signal
412
413int mingw_raise(int sig);
414#define raise mingw_raise
415
416/*
417 * ANSI emulation wrappers
418 */
419
420int winansi_isatty(int fd);
421#define isatty winansi_isatty
422
423int winansi_dup2(int oldfd, int newfd);
424#define dup2 winansi_dup2
425
426void winansi_init(void);
427HANDLE winansi_get_osfhandle(int fd);
428
429#if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
430#define PRIuMAX "I64u"
431#define PRId64 "I64d"
432#else
433#include <inttypes.h>
434#endif
435
436#endif /* COMPAT_MINGW_POSIX_H */