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