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