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