10e2e4cacb75c34bfdc78bede36132c5498af811
   1#ifdef __MINGW64_VERSION_MAJOR
   2#include <stdint.h>
   3#include <wchar.h>
   4typedef _sigset_t sigset_t;
   5#endif
   6#include <winsock2.h>
   7#include <ws2tcpip.h>
   8
   9/* MinGW-w64 reports to have flockfile, but it does not actually have it. */
  10#ifdef __MINGW64_VERSION_MAJOR
  11#undef _POSIX_THREAD_SAFE_FUNCTIONS
  12#endif
  13
  14/*
  15 * things that are not available in header files
  16 */
  17
  18typedef int uid_t;
  19typedef int socklen_t;
  20#ifndef __MINGW64_VERSION_MAJOR
  21typedef int pid_t;
  22#define hstrerror strerror
  23#endif
  24
  25#define S_IFLNK    0120000 /* Symbolic link */
  26#define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
  27#define S_ISSOCK(x) 0
  28
  29#define S_IRGRP 0
  30#define S_IWGRP 0
  31#define S_IXGRP 0
  32#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
  33#define S_IROTH 0
  34#define S_IWOTH 0
  35#define S_IXOTH 0
  36#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
  37
  38#define S_ISUID 0004000
  39#define S_ISGID 0002000
  40#define S_ISVTX 0001000
  41
  42#define WIFEXITED(x) 1
  43#define WIFSIGNALED(x) 0
  44#define WEXITSTATUS(x) ((x) & 0xff)
  45#define WTERMSIG(x) SIGTERM
  46
  47#ifndef EWOULDBLOCK
  48#define EWOULDBLOCK EAGAIN
  49#endif
  50#ifndef ELOOP
  51#define ELOOP EMLINK
  52#endif
  53#define SHUT_WR SD_SEND
  54
  55#define SIGHUP 1
  56#define SIGQUIT 3
  57#define SIGKILL 9
  58#define SIGPIPE 13
  59#define SIGALRM 14
  60#define SIGCHLD 17
  61
  62#define F_GETFD 1
  63#define F_SETFD 2
  64#define FD_CLOEXEC 0x1
  65
  66#ifndef EAFNOSUPPORT
  67#define EAFNOSUPPORT WSAEAFNOSUPPORT
  68#endif
  69#ifndef ECONNABORTED
  70#define ECONNABORTED WSAECONNABORTED
  71#endif
  72
  73struct passwd {
  74        char *pw_name;
  75        char *pw_gecos;
  76        char *pw_dir;
  77};
  78
  79typedef void (__cdecl *sig_handler_t)(int);
  80struct sigaction {
  81        sig_handler_t sa_handler;
  82        unsigned sa_flags;
  83};
  84#define SA_RESTART 0
  85
  86struct itimerval {
  87        struct timeval it_value, it_interval;
  88};
  89#define ITIMER_REAL 0
  90
  91struct utsname {
  92        char sysname[16];
  93        char nodename[1];
  94        char release[16];
  95        char version[16];
  96        char machine[1];
  97};
  98
  99/*
 100 * sanitize preprocessor namespace polluted by Windows headers defining
 101 * macros which collide with git local versions
 102 */
 103#undef HELP_COMMAND /* from winuser.h */
 104
 105/*
 106 * trivial stubs
 107 */
 108
 109static inline int readlink(const char *path, char *buf, size_t bufsiz)
 110{ errno = ENOSYS; return -1; }
 111static inline int symlink(const char *oldpath, const char *newpath)
 112{ errno = ENOSYS; return -1; }
 113static inline int fchmod(int fildes, mode_t mode)
 114{ errno = ENOSYS; return -1; }
 115#ifndef __MINGW64_VERSION_MAJOR
 116static inline pid_t fork(void)
 117{ errno = ENOSYS; return -1; }
 118#endif
 119static inline unsigned int alarm(unsigned int seconds)
 120{ return 0; }
 121static inline int fsync(int fd)
 122{ return _commit(fd); }
 123static inline void sync(void)
 124{}
 125static inline uid_t getuid(void)
 126{ return 1; }
 127static inline struct passwd *getpwnam(const char *name)
 128{ return NULL; }
 129static inline int fcntl(int fd, int cmd, ...)
 130{
 131        if (cmd == F_GETFD || cmd == F_SETFD)
 132                return 0;
 133        errno = EINVAL;
 134        return -1;
 135}
 136/* bash cannot reliably detect negative return codes as failure */
 137#define exit(code) exit((code) & 0xff)
 138#define sigemptyset(x) (void)0
 139static inline int sigaddset(sigset_t *set, int signum)
 140{ return 0; }
 141#define SIG_UNBLOCK 0
 142static inline int sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
 143{ return 0; }
 144static inline pid_t getppid(void)
 145{ return 1; }
 146static inline pid_t getpgid(pid_t pid)
 147{ return pid == 0 ? getpid() : pid; }
 148static inline pid_t tcgetpgrp(int fd)
 149{ return getpid(); }
 150
 151/*
 152 * simple adaptors
 153 */
 154
 155int mingw_mkdir(const char *path, int mode);
 156#define mkdir mingw_mkdir
 157
 158#define WNOHANG 1
 159pid_t waitpid(pid_t pid, int *status, int options);
 160
 161#define kill mingw_kill
 162int mingw_kill(pid_t pid, int sig);
 163
 164#ifndef NO_OPENSSL
 165#include <openssl/ssl.h>
 166static inline int mingw_SSL_set_fd(SSL *ssl, int fd)
 167{
 168        return SSL_set_fd(ssl, _get_osfhandle(fd));
 169}
 170#define SSL_set_fd mingw_SSL_set_fd
 171
 172static inline int mingw_SSL_set_rfd(SSL *ssl, int fd)
 173{
 174        return SSL_set_rfd(ssl, _get_osfhandle(fd));
 175}
 176#define SSL_set_rfd mingw_SSL_set_rfd
 177
 178static inline int mingw_SSL_set_wfd(SSL *ssl, int fd)
 179{
 180        return SSL_set_wfd(ssl, _get_osfhandle(fd));
 181}
 182#define SSL_set_wfd mingw_SSL_set_wfd
 183#endif
 184
 185/*
 186 * implementations of missing functions
 187 */
 188
 189int pipe(int filedes[2]);
 190unsigned int sleep (unsigned int seconds);
 191int mkstemp(char *template);
 192int gettimeofday(struct timeval *tv, void *tz);
 193#ifndef __MINGW64_VERSION_MAJOR
 194struct tm *gmtime_r(const time_t *timep, struct tm *result);
 195struct tm *localtime_r(const time_t *timep, struct tm *result);
 196#endif
 197int getpagesize(void);  /* defined in MinGW's libgcc.a */
 198struct passwd *getpwuid(uid_t uid);
 199int setitimer(int type, struct itimerval *in, struct itimerval *out);
 200int sigaction(int sig, struct sigaction *in, struct sigaction *out);
 201int link(const char *oldpath, const char *newpath);
 202int uname(struct utsname *buf);
 203
 204/*
 205 * replacements of existing functions
 206 */
 207
 208int mingw_unlink(const char *pathname);
 209#define unlink mingw_unlink
 210
 211int mingw_rmdir(const char *path);
 212#define rmdir mingw_rmdir
 213
 214int mingw_open (const char *filename, int oflags, ...);
 215#define open mingw_open
 216
 217int mingw_fgetc(FILE *stream);
 218#define fgetc mingw_fgetc
 219
 220FILE *mingw_fopen (const char *filename, const char *otype);
 221#define fopen mingw_fopen
 222
 223FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);
 224#define freopen mingw_freopen
 225
 226int mingw_fflush(FILE *stream);
 227#define fflush mingw_fflush
 228
 229ssize_t mingw_write(int fd, const void *buf, size_t len);
 230#define write mingw_write
 231
 232int mingw_access(const char *filename, int mode);
 233#undef access
 234#define access mingw_access
 235
 236int mingw_chdir(const char *dirname);
 237#define chdir mingw_chdir
 238
 239int mingw_chmod(const char *filename, int mode);
 240#define chmod mingw_chmod
 241
 242char *mingw_mktemp(char *template);
 243#define mktemp mingw_mktemp
 244
 245char *mingw_getcwd(char *pointer, int len);
 246#define getcwd mingw_getcwd
 247
 248char *mingw_getenv(const char *name);
 249#define getenv mingw_getenv
 250int mingw_putenv(const char *namevalue);
 251#define putenv mingw_putenv
 252#define unsetenv mingw_putenv
 253
 254int mingw_gethostname(char *host, int namelen);
 255#define gethostname mingw_gethostname
 256
 257struct hostent *mingw_gethostbyname(const char *host);
 258#define gethostbyname mingw_gethostbyname
 259
 260void mingw_freeaddrinfo(struct addrinfo *res);
 261#define freeaddrinfo mingw_freeaddrinfo
 262
 263int mingw_getaddrinfo(const char *node, const char *service,
 264                      const struct addrinfo *hints, struct addrinfo **res);
 265#define getaddrinfo mingw_getaddrinfo
 266
 267int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen,
 268                      char *host, DWORD hostlen, char *serv, DWORD servlen,
 269                      int flags);
 270#define getnameinfo mingw_getnameinfo
 271
 272int mingw_socket(int domain, int type, int protocol);
 273#define socket mingw_socket
 274
 275int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
 276#define connect mingw_connect
 277
 278int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz);
 279#define bind mingw_bind
 280
 281int mingw_setsockopt(int sockfd, int lvl, int optname, void *optval, int optlen);
 282#define setsockopt mingw_setsockopt
 283
 284int mingw_shutdown(int sockfd, int how);
 285#define shutdown mingw_shutdown
 286
 287int mingw_listen(int sockfd, int backlog);
 288#define listen mingw_listen
 289
 290int mingw_accept(int sockfd, struct sockaddr *sa, socklen_t *sz);
 291#define accept mingw_accept
 292
 293int mingw_rename(const char*, const char*);
 294#define rename mingw_rename
 295
 296#if defined(USE_WIN32_MMAP) || defined(_MSC_VER)
 297int mingw_getpagesize(void);
 298#define getpagesize mingw_getpagesize
 299#endif
 300
 301struct rlimit {
 302        unsigned int rlim_cur;
 303};
 304#define RLIMIT_NOFILE 0
 305
 306static inline int getrlimit(int resource, struct rlimit *rlp)
 307{
 308        if (resource != RLIMIT_NOFILE) {
 309                errno = EINVAL;
 310                return -1;
 311        }
 312
 313        rlp->rlim_cur = 2048;
 314        return 0;
 315}
 316
 317/*
 318 * Use mingw specific stat()/lstat()/fstat() implementations on Windows.
 319 */
 320#ifndef __MINGW64_VERSION_MAJOR
 321#define off_t off64_t
 322#define lseek _lseeki64
 323#endif
 324
 325/* use struct stat with 64 bit st_size */
 326#ifdef stat
 327#undef stat
 328#endif
 329#define stat _stati64
 330int mingw_lstat(const char *file_name, struct stat *buf);
 331int mingw_stat(const char *file_name, struct stat *buf);
 332int mingw_fstat(int fd, struct stat *buf);
 333#ifdef fstat
 334#undef fstat
 335#endif
 336#define fstat mingw_fstat
 337#ifdef lstat
 338#undef lstat
 339#endif
 340#define lstat mingw_lstat
 341
 342#ifndef _stati64
 343# define _stati64(x,y) mingw_stat(x,y)
 344#elif defined (_USE_32BIT_TIME_T)
 345# define _stat32i64(x,y) mingw_stat(x,y)
 346#else
 347# define _stat64(x,y) mingw_stat(x,y)
 348#endif
 349
 350int mingw_utime(const char *file_name, const struct utimbuf *times);
 351#define utime mingw_utime
 352
 353pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
 354                     const char *dir,
 355                     int fhin, int fhout, int fherr);
 356int mingw_execvp(const char *cmd, char *const *argv);
 357#define execvp mingw_execvp
 358int mingw_execv(const char *cmd, char *const *argv);
 359#define execv mingw_execv
 360
 361static inline unsigned int git_ntohl(unsigned int x)
 362{ return (unsigned int)ntohl(x); }
 363#define ntohl git_ntohl
 364
 365sig_handler_t mingw_signal(int sig, sig_handler_t handler);
 366#define signal mingw_signal
 367
 368int mingw_raise(int sig);
 369#define raise mingw_raise
 370
 371/*
 372 * ANSI emulation wrappers
 373 */
 374
 375void winansi_init(void);
 376HANDLE winansi_get_osfhandle(int fd);
 377
 378/*
 379 * git specific compatibility
 380 */
 381
 382#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
 383#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
 384static inline char *mingw_find_last_dir_sep(const char *path)
 385{
 386        char *ret = NULL;
 387        for (; *path; ++path)
 388                if (is_dir_sep(*path))
 389                        ret = (char *)path;
 390        return ret;
 391}
 392#define find_last_dir_sep mingw_find_last_dir_sep
 393int mingw_offset_1st_component(const char *path);
 394#define offset_1st_component mingw_offset_1st_component
 395#define PATH_SEP ';'
 396#ifndef __MINGW64_VERSION_MAJOR
 397#define PRIuMAX "I64u"
 398#define PRId64 "I64d"
 399#else
 400#include <inttypes.h>
 401#endif
 402
 403void mingw_open_html(const char *path);
 404#define open_html mingw_open_html
 405
 406void mingw_mark_as_git_dir(const char *dir);
 407#define mark_as_git_dir mingw_mark_as_git_dir
 408
 409/**
 410 * Converts UTF-8 encoded string to UTF-16LE.
 411 *
 412 * To support repositories with legacy-encoded file names, invalid UTF-8 bytes
 413 * 0xa0 - 0xff are converted to corresponding printable Unicode chars \u00a0 -
 414 * \u00ff, and invalid UTF-8 bytes 0x80 - 0x9f (which would make non-printable
 415 * Unicode) are converted to hex-code.
 416 *
 417 * Lead-bytes not followed by an appropriate number of trail-bytes, over-long
 418 * encodings and 4-byte encodings > \u10ffff are detected as invalid UTF-8.
 419 *
 420 * Maximum space requirement for the target buffer is two wide chars per UTF-8
 421 * char (((strlen(utf) * 2) + 1) [* sizeof(wchar_t)]).
 422 *
 423 * The maximum space is needed only if the entire input string consists of
 424 * invalid UTF-8 bytes in range 0x80-0x9f, as per the following table:
 425 *
 426 *               |                   | UTF-8 | UTF-16 |
 427 *   Code point  |  UTF-8 sequence   | bytes | words  | ratio
 428 * --------------+-------------------+-------+--------+-------
 429 * 000000-00007f | 0-7f              |   1   |   1    |  1
 430 * 000080-0007ff | c2-df + 80-bf     |   2   |   1    |  0.5
 431 * 000800-00ffff | e0-ef + 2 * 80-bf |   3   |   1    |  0.33
 432 * 010000-10ffff | f0-f4 + 3 * 80-bf |   4   |  2 (a) |  0.5
 433 * invalid       | 80-9f             |   1   |  2 (b) |  2
 434 * invalid       | a0-ff             |   1   |   1    |  1
 435 *
 436 * (a) encoded as UTF-16 surrogate pair
 437 * (b) encoded as two hex digits
 438 *
 439 * Note that, while the UTF-8 encoding scheme can be extended to 5-byte, 6-byte
 440 * or even indefinite-byte sequences, the largest valid code point \u10ffff
 441 * encodes as only 4 UTF-8 bytes.
 442 *
 443 * Parameters:
 444 * wcs: wide char target buffer
 445 * utf: string to convert
 446 * wcslen: size of target buffer (in wchar_t's)
 447 * utflen: size of string to convert, or -1 if 0-terminated
 448 *
 449 * Returns:
 450 * length of converted string (_wcslen(wcs)), or -1 on failure
 451 *
 452 * Errors:
 453 * EINVAL: one of the input parameters is invalid (e.g. NULL)
 454 * ERANGE: the output buffer is too small
 455 */
 456int xutftowcsn(wchar_t *wcs, const char *utf, size_t wcslen, int utflen);
 457
 458/**
 459 * Simplified variant of xutftowcsn, assumes input string is \0-terminated.
 460 */
 461static inline int xutftowcs(wchar_t *wcs, const char *utf, size_t wcslen)
 462{
 463        return xutftowcsn(wcs, utf, wcslen, -1);
 464}
 465
 466/**
 467 * Simplified file system specific variant of xutftowcsn, assumes output
 468 * buffer size is MAX_PATH wide chars and input string is \0-terminated,
 469 * fails with ENAMETOOLONG if input string is too long.
 470 */
 471static inline int xutftowcs_path(wchar_t *wcs, const char *utf)
 472{
 473        int result = xutftowcsn(wcs, utf, MAX_PATH, -1);
 474        if (result < 0 && errno == ERANGE)
 475                errno = ENAMETOOLONG;
 476        return result;
 477}
 478
 479/**
 480 * Converts UTF-16LE encoded string to UTF-8.
 481 *
 482 * Maximum space requirement for the target buffer is three UTF-8 chars per
 483 * wide char ((_wcslen(wcs) * 3) + 1).
 484 *
 485 * The maximum space is needed only if the entire input string consists of
 486 * UTF-16 words in range 0x0800-0xd7ff or 0xe000-0xffff (i.e. \u0800-\uffff
 487 * modulo surrogate pairs), as per the following table:
 488 *
 489 *               |                       | UTF-16 | UTF-8 |
 490 *   Code point  |  UTF-16 sequence      | words  | bytes | ratio
 491 * --------------+-----------------------+--------+-------+-------
 492 * 000000-00007f | 0000-007f             |   1    |   1   |  1
 493 * 000080-0007ff | 0080-07ff             |   1    |   2   |  2
 494 * 000800-00ffff | 0800-d7ff / e000-ffff |   1    |   3   |  3
 495 * 010000-10ffff | d800-dbff + dc00-dfff |   2    |   4   |  2
 496 *
 497 * Note that invalid code points > 10ffff cannot be represented in UTF-16.
 498 *
 499 * Parameters:
 500 * utf: target buffer
 501 * wcs: wide string to convert
 502 * utflen: size of target buffer
 503 *
 504 * Returns:
 505 * length of converted string, or -1 on failure
 506 *
 507 * Errors:
 508 * EINVAL: one of the input parameters is invalid (e.g. NULL)
 509 * ERANGE: the output buffer is too small
 510 */
 511int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen);
 512
 513/*
 514 * A critical section used in the implementation of the spawn
 515 * functions (mingw_spawnv[p]e()) and waitpid(). Intialised in
 516 * the replacement main() macro below.
 517 */
 518extern CRITICAL_SECTION pinfo_cs;
 519
 520/*
 521 * A replacement of main() that adds win32 specific initialization.
 522 */
 523
 524void mingw_startup();
 525#define main(c,v) dummy_decl_mingw_main(); \
 526static int mingw_main(c,v); \
 527int main(int argc, char **argv) \
 528{ \
 529        mingw_startup(); \
 530        return mingw_main(__argc, (void *)__argv); \
 531} \
 532static int mingw_main(c,v)
 533
 534/*
 535 * Used by Pthread API implementation for Windows
 536 */
 537extern int err_win_to_posix(DWORD winerr);