1 #ifdef __MINGW64_VERSION_MAJOR
4 typedef _sigset_t sigset_t;
9 #ifdef __MINGW64_VERSION_MAJOR
11 * In Git for Windows, we cannot rely on `uname -m` to report the correct
12 * architecture: /usr/bin/uname.exe will report the architecture with which the
13 * current MSYS2 runtime was built, not the architecture for which we are
14 * currently compiling (both 32-bit and 64-bit `git.exe` is built in the 64-bit
15 * Git for Windows SDK).
18 /* This was figured out by looking at `cpp -dM </dev/null`'s output */
19 #if defined(__x86_64__)
20 #define GIT_HOST_CPU "x86_64"
21 #elif defined(__i686__)
22 #define GIT_HOST_CPU "i686"
24 #error "Unknown architecture"
28 /* MinGW-w64 reports to have flockfile, but it does not actually have it. */
29 #ifdef __MINGW64_VERSION_MAJOR
30 #undef _POSIX_THREAD_SAFE_FUNCTIONS
33 extern int mingw_core_config(const char *var, const char *value, void *cb);
34 #define platform_core_config mingw_core_config
37 * things that are not available in header files
41 typedef int socklen_t;
42 #ifndef __MINGW64_VERSION_MAJOR
44 #define hstrerror strerror
47 #define S_IFLNK 0120000 /* Symbolic link */
48 #define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
55 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
61 #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
64 #define S_ISUID 0004000
65 #define S_ISGID 0002000
66 #define S_ISVTX 0001000
68 #define WIFEXITED(x) 1
69 #define WIFSIGNALED(x) 0
70 #define WEXITSTATUS(x) ((x) & 0xff)
71 #define WTERMSIG(x) SIGTERM
74 #define EWOULDBLOCK EAGAIN
79 #define SHUT_WR SD_SEND
90 #define FD_CLOEXEC 0x1
92 #if !defined O_CLOEXEC && defined O_NOINHERIT
93 #define O_CLOEXEC O_NOINHERIT
97 #define EAFNOSUPPORT WSAEAFNOSUPPORT
100 #define ECONNABORTED WSAECONNABORTED
103 #define ENOTSOCK WSAENOTSOCK
112 typedef void (__cdecl *sig_handler_t)(int);
114 sig_handler_t sa_handler;
120 struct timeval it_value, it_interval;
122 #define ITIMER_REAL 0
133 * sanitize preprocessor namespace polluted by Windows headers defining
134 * macros which collide with git local versions
136 #undef HELP_COMMAND /* from winuser.h */
142 static inline int readlink(const char *path, char *buf, size_t bufsiz)
143 { errno = ENOSYS; return -1; }
144 static inline int symlink(const char *oldpath, const char *newpath)
145 { errno = ENOSYS; return -1; }
146 static inline int fchmod(int fildes, mode_t mode)
147 { errno = ENOSYS; return -1; }
148 #ifndef __MINGW64_VERSION_MAJOR
149 static inline pid_t fork(void)
150 { errno = ENOSYS; return -1; }
152 static inline unsigned int alarm(unsigned int seconds)
154 static inline int fsync(int fd)
155 { return _commit(fd); }
156 static inline void sync(void)
158 static inline uid_t getuid(void)
160 static inline struct passwd *getpwnam(const char *name)
162 static inline int fcntl(int fd, int cmd, ...)
164 if (cmd == F_GETFD || cmd == F_SETFD)
169 /* bash cannot reliably detect negative return codes as failure */
170 #define exit(code) exit((code) & 0xff)
171 #define sigemptyset(x) (void)0
172 static inline int sigaddset(sigset_t *set, int signum)
175 #define SIG_UNBLOCK 0
176 static inline int sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
178 static inline pid_t getppid(void)
180 static inline pid_t getpgid(pid_t pid)
181 { return pid == 0 ? getpid() : pid; }
182 static inline pid_t tcgetpgrp(int fd)
189 int mingw_mkdir(const char *path, int mode);
190 #define mkdir mingw_mkdir
193 pid_t waitpid(pid_t pid, int *status, int options);
195 #define kill mingw_kill
196 int mingw_kill(pid_t pid, int sig);
199 #include <openssl/ssl.h>
200 static inline int mingw_SSL_set_fd(SSL *ssl, int fd)
202 return SSL_set_fd(ssl, _get_osfhandle(fd));
204 #define SSL_set_fd mingw_SSL_set_fd
206 static inline int mingw_SSL_set_rfd(SSL *ssl, int fd)
208 return SSL_set_rfd(ssl, _get_osfhandle(fd));
210 #define SSL_set_rfd mingw_SSL_set_rfd
212 static inline int mingw_SSL_set_wfd(SSL *ssl, int fd)
214 return SSL_set_wfd(ssl, _get_osfhandle(fd));
216 #define SSL_set_wfd mingw_SSL_set_wfd
220 * implementations of missing functions
223 int pipe(int filedes[2]);
224 unsigned int sleep (unsigned int seconds);
225 int mkstemp(char *template);
226 int gettimeofday(struct timeval *tv, void *tz);
227 #ifndef __MINGW64_VERSION_MAJOR
228 struct tm *gmtime_r(const time_t *timep, struct tm *result);
229 struct tm *localtime_r(const time_t *timep, struct tm *result);
231 int getpagesize(void); /* defined in MinGW's libgcc.a */
232 struct passwd *getpwuid(uid_t uid);
233 int setitimer(int type, struct itimerval *in, struct itimerval *out);
234 int sigaction(int sig, struct sigaction *in, struct sigaction *out);
235 int link(const char *oldpath, const char *newpath);
236 int uname(struct utsname *buf);
239 * replacements of existing functions
242 int mingw_unlink(const char *pathname);
243 #define unlink mingw_unlink
245 int mingw_rmdir(const char *path);
246 #define rmdir mingw_rmdir
248 int mingw_open (const char *filename, int oflags, ...);
249 #define open mingw_open
251 int mingw_fgetc(FILE *stream);
252 #define fgetc mingw_fgetc
254 FILE *mingw_fopen (const char *filename, const char *otype);
255 #define fopen mingw_fopen
257 FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);
258 #define freopen mingw_freopen
260 int mingw_fflush(FILE *stream);
261 #define fflush mingw_fflush
263 ssize_t mingw_write(int fd, const void *buf, size_t len);
264 #define write mingw_write
266 int mingw_access(const char *filename, int mode);
268 #define access mingw_access
270 int mingw_chdir(const char *dirname);
271 #define chdir mingw_chdir
273 int mingw_chmod(const char *filename, int mode);
274 #define chmod mingw_chmod
276 char *mingw_mktemp(char *template);
277 #define mktemp mingw_mktemp
279 char *mingw_getcwd(char *pointer, int len);
280 #define getcwd mingw_getcwd
283 #error "NO_UNSETENV is incompatible with the Windows-specific startup code!"
287 * We bind *env() routines (even the mingw_ ones) to private mingw_ versions.
288 * These talk to the CRT using UNICODE/wchar_t, but maintain the original
291 * Note that the MSCRT maintains both ANSI (getenv()) and UNICODE (_wgetenv())
292 * routines and stores both versions of each environment variable in parallel
293 * (and secretly updates both when you set one or the other), but it uses CP_ACP
294 * to do the conversion rather than CP_UTF8.
296 * Since everything in the git code base is UTF8, we define the mingw_ routines
297 * to access the CRT using the UNICODE routines and manually convert them to
298 * UTF8. This also avoids round-trip problems.
300 * This also helps with our linkage, since "_wenviron" is publicly exported
301 * from the CRT. But to access "_environ" we would have to statically link
304 * We require NO_SETENV (and let gitsetenv() call our mingw_putenv).
306 #define getenv mingw_getenv
307 #define putenv mingw_putenv
308 #define unsetenv mingw_putenv
309 char *mingw_getenv(const char *name);
310 int mingw_putenv(const char *name);
312 int mingw_gethostname(char *host, int namelen);
313 #define gethostname mingw_gethostname
315 struct hostent *mingw_gethostbyname(const char *host);
316 #define gethostbyname mingw_gethostbyname
318 void mingw_freeaddrinfo(struct addrinfo *res);
319 #define freeaddrinfo mingw_freeaddrinfo
321 int mingw_getaddrinfo(const char *node, const char *service,
322 const struct addrinfo *hints, struct addrinfo **res);
323 #define getaddrinfo mingw_getaddrinfo
325 int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen,
326 char *host, DWORD hostlen, char *serv, DWORD servlen,
328 #define getnameinfo mingw_getnameinfo
330 int mingw_socket(int domain, int type, int protocol);
331 #define socket mingw_socket
333 int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
334 #define connect mingw_connect
336 int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz);
337 #define bind mingw_bind
339 int mingw_setsockopt(int sockfd, int lvl, int optname, void *optval, int optlen);
340 #define setsockopt mingw_setsockopt
342 int mingw_shutdown(int sockfd, int how);
343 #define shutdown mingw_shutdown
345 int mingw_listen(int sockfd, int backlog);
346 #define listen mingw_listen
348 int mingw_accept(int sockfd, struct sockaddr *sa, socklen_t *sz);
349 #define accept mingw_accept
351 int mingw_rename(const char*, const char*);
352 #define rename mingw_rename
354 #if defined(USE_WIN32_MMAP) || defined(_MSC_VER)
355 int mingw_getpagesize(void);
356 #define getpagesize mingw_getpagesize
360 unsigned int rlim_cur;
362 #define RLIMIT_NOFILE 0
364 static inline int getrlimit(int resource, struct rlimit *rlp)
366 if (resource != RLIMIT_NOFILE) {
371 rlp->rlim_cur = 2048;
376 * Use mingw specific stat()/lstat()/fstat() implementations on Windows,
377 * including our own struct stat with 64 bit st_size and nanosecond-precision
380 #ifndef __MINGW64_VERSION_MAJOR
381 #define off_t off64_t
382 #define lseek _lseeki64
398 struct timespec st_atim;
399 struct timespec st_mtim;
400 struct timespec st_ctim;
403 #define st_atime st_atim.tv_sec
404 #define st_mtime st_mtim.tv_sec
405 #define st_ctime st_ctim.tv_sec
410 #define stat mingw_stat
411 int mingw_lstat(const char *file_name, struct stat *buf);
412 int mingw_stat(const char *file_name, struct stat *buf);
413 int mingw_fstat(int fd, struct stat *buf);
417 #define fstat mingw_fstat
421 #define lstat mingw_lstat
424 int mingw_utime(const char *file_name, const struct utimbuf *times);
425 #define utime mingw_utime
426 size_t mingw_strftime(char *s, size_t max,
427 const char *format, const struct tm *tm);
428 #define strftime mingw_strftime
430 pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
432 int fhin, int fhout, int fherr);
433 int mingw_execvp(const char *cmd, char *const *argv);
434 #define execvp mingw_execvp
435 int mingw_execv(const char *cmd, char *const *argv);
436 #define execv mingw_execv
438 static inline unsigned int git_ntohl(unsigned int x)
439 { return (unsigned int)ntohl(x); }
440 #define ntohl git_ntohl
442 sig_handler_t mingw_signal(int sig, sig_handler_t handler);
443 #define signal mingw_signal
445 int mingw_raise(int sig);
446 #define raise mingw_raise
449 * ANSI emulation wrappers
452 int winansi_isatty(int fd);
453 #define isatty winansi_isatty
455 int winansi_dup2(int oldfd, int newfd);
456 #define dup2 winansi_dup2
458 void winansi_init(void);
459 HANDLE winansi_get_osfhandle(int fd);
462 * git specific compatibility
465 #define has_dos_drive_prefix(path) \
466 (isalpha(*(path)) && (path)[1] == ':' ? 2 : 0)
467 int mingw_skip_dos_drive_prefix(char **path);
468 #define skip_dos_drive_prefix mingw_skip_dos_drive_prefix
469 static inline int mingw_is_dir_sep(int c)
471 return c == '/' || c == '\\';
473 #define is_dir_sep mingw_is_dir_sep
474 static inline char *mingw_find_last_dir_sep(const char *path)
477 for (; *path; ++path)
478 if (is_dir_sep(*path))
482 static inline void convert_slashes(char *path)
484 for (; *path; path++)
488 #define find_last_dir_sep mingw_find_last_dir_sep
489 int mingw_offset_1st_component(const char *path);
490 #define offset_1st_component mingw_offset_1st_component
492 extern char *mingw_query_user_email(void);
493 #define query_user_email mingw_query_user_email
494 #if !defined(__MINGW64_VERSION_MAJOR) && (!defined(_MSC_VER) || _MSC_VER < 1800)
495 #define PRIuMAX "I64u"
496 #define PRId64 "I64d"
498 #include <inttypes.h>
502 * Converts UTF-8 encoded string to UTF-16LE.
504 * To support repositories with legacy-encoded file names, invalid UTF-8 bytes
505 * 0xa0 - 0xff are converted to corresponding printable Unicode chars \u00a0 -
506 * \u00ff, and invalid UTF-8 bytes 0x80 - 0x9f (which would make non-printable
507 * Unicode) are converted to hex-code.
509 * Lead-bytes not followed by an appropriate number of trail-bytes, over-long
510 * encodings and 4-byte encodings > \u10ffff are detected as invalid UTF-8.
512 * Maximum space requirement for the target buffer is two wide chars per UTF-8
513 * char (((strlen(utf) * 2) + 1) [* sizeof(wchar_t)]).
515 * The maximum space is needed only if the entire input string consists of
516 * invalid UTF-8 bytes in range 0x80-0x9f, as per the following table:
518 * | | UTF-8 | UTF-16 |
519 * Code point | UTF-8 sequence | bytes | words | ratio
520 * --------------+-------------------+-------+--------+-------
521 * 000000-00007f | 0-7f | 1 | 1 | 1
522 * 000080-0007ff | c2-df + 80-bf | 2 | 1 | 0.5
523 * 000800-00ffff | e0-ef + 2 * 80-bf | 3 | 1 | 0.33
524 * 010000-10ffff | f0-f4 + 3 * 80-bf | 4 | 2 (a) | 0.5
525 * invalid | 80-9f | 1 | 2 (b) | 2
526 * invalid | a0-ff | 1 | 1 | 1
528 * (a) encoded as UTF-16 surrogate pair
529 * (b) encoded as two hex digits
531 * Note that, while the UTF-8 encoding scheme can be extended to 5-byte, 6-byte
532 * or even indefinite-byte sequences, the largest valid code point \u10ffff
533 * encodes as only 4 UTF-8 bytes.
536 * wcs: wide char target buffer
537 * utf: string to convert
538 * wcslen: size of target buffer (in wchar_t's)
539 * utflen: size of string to convert, or -1 if 0-terminated
542 * length of converted string (_wcslen(wcs)), or -1 on failure
545 * EINVAL: one of the input parameters is invalid (e.g. NULL)
546 * ERANGE: the output buffer is too small
548 int xutftowcsn(wchar_t *wcs, const char *utf, size_t wcslen, int utflen);
551 * Simplified variant of xutftowcsn, assumes input string is \0-terminated.
553 static inline int xutftowcs(wchar_t *wcs, const char *utf, size_t wcslen)
555 return xutftowcsn(wcs, utf, wcslen, -1);
559 * Simplified file system specific variant of xutftowcsn, assumes output
560 * buffer size is MAX_PATH wide chars and input string is \0-terminated,
561 * fails with ENAMETOOLONG if input string is too long.
563 static inline int xutftowcs_path(wchar_t *wcs, const char *utf)
565 int result = xutftowcsn(wcs, utf, MAX_PATH, -1);
566 if (result < 0 && errno == ERANGE)
567 errno = ENAMETOOLONG;
572 * Converts UTF-16LE encoded string to UTF-8.
574 * Maximum space requirement for the target buffer is three UTF-8 chars per
575 * wide char ((_wcslen(wcs) * 3) + 1).
577 * The maximum space is needed only if the entire input string consists of
578 * UTF-16 words in range 0x0800-0xd7ff or 0xe000-0xffff (i.e. \u0800-\uffff
579 * modulo surrogate pairs), as per the following table:
581 * | | UTF-16 | UTF-8 |
582 * Code point | UTF-16 sequence | words | bytes | ratio
583 * --------------+-----------------------+--------+-------+-------
584 * 000000-00007f | 0000-007f | 1 | 1 | 1
585 * 000080-0007ff | 0080-07ff | 1 | 2 | 2
586 * 000800-00ffff | 0800-d7ff / e000-ffff | 1 | 3 | 3
587 * 010000-10ffff | d800-dbff + dc00-dfff | 2 | 4 | 2
589 * Note that invalid code points > 10ffff cannot be represented in UTF-16.
593 * wcs: wide string to convert
594 * utflen: size of target buffer
597 * length of converted string, or -1 on failure
600 * EINVAL: one of the input parameters is invalid (e.g. NULL)
601 * ERANGE: the output buffer is too small
603 int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen);
606 * A critical section used in the implementation of the spawn
607 * functions (mingw_spawnv[p]e()) and waitpid(). Intialised in
608 * the replacement main() macro below.
610 extern CRITICAL_SECTION pinfo_cs;
613 * A replacement of main() that adds win32 specific initialization.
616 void mingw_startup(void);
617 #define main(c,v) dummy_decl_mingw_main(void); \
618 static int mingw_main(c,v); \
619 int main(int argc, const char **argv) \
622 return mingw_main(__argc, (void *)__argv); \
624 static int mingw_main(c,v)
627 * Used by Pthread API implementation for Windows
629 extern int err_win_to_posix(DWORD winerr);