compat / mingw.hon commit mingw: add fallback for rmdir in case directory is in use (4f28810)
   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;
  10typedef int socklen_t;
  11#define hstrerror strerror
  12
  13#define S_IFLNK    0120000 /* Symbolic link */
  14#define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
  15#define S_ISSOCK(x) 0
  16
  17#ifndef _STAT_H_
  18#define S_IRUSR 0
  19#define S_IWUSR 0
  20#define S_IXUSR 0
  21#define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
  22#endif
  23#define S_IRGRP 0
  24#define S_IWGRP 0
  25#define S_IXGRP 0
  26#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
  27#define S_IROTH 0
  28#define S_IWOTH 0
  29#define S_IXOTH 0
  30#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
  31#define S_ISUID 0
  32#define S_ISGID 0
  33#define S_ISVTX 0
  34
  35#define WIFEXITED(x) 1
  36#define WIFSIGNALED(x) 0
  37#define WEXITSTATUS(x) ((x) & 0xff)
  38#define WTERMSIG(x) SIGTERM
  39
  40#define EWOULDBLOCK EAGAIN
  41#define SHUT_WR SD_SEND
  42
  43#define SIGHUP 1
  44#define SIGQUIT 3
  45#define SIGKILL 9
  46#define SIGPIPE 13
  47#define SIGALRM 14
  48#define SIGCHLD 17
  49
  50#define F_GETFD 1
  51#define F_SETFD 2
  52#define FD_CLOEXEC 0x1
  53
  54#define EAFNOSUPPORT WSAEAFNOSUPPORT
  55#define ECONNABORTED WSAECONNABORTED
  56
  57struct passwd {
  58        char *pw_name;
  59        char *pw_gecos;
  60        char *pw_dir;
  61};
  62
  63extern char *getpass(const char *prompt);
  64
  65typedef void (__cdecl *sig_handler_t)(int);
  66struct sigaction {
  67        sig_handler_t sa_handler;
  68        unsigned sa_flags;
  69};
  70#define sigemptyset(x) (void)0
  71#define SA_RESTART 0
  72
  73struct itimerval {
  74        struct timeval it_value, it_interval;
  75};
  76#define ITIMER_REAL 0
  77
  78/*
  79 * sanitize preprocessor namespace polluted by Windows headers defining
  80 * macros which collide with git local versions
  81 */
  82#undef HELP_COMMAND /* from winuser.h */
  83
  84/*
  85 * trivial stubs
  86 */
  87
  88static inline int readlink(const char *path, char *buf, size_t bufsiz)
  89{ errno = ENOSYS; return -1; }
  90static inline int symlink(const char *oldpath, const char *newpath)
  91{ errno = ENOSYS; return -1; }
  92static inline int fchmod(int fildes, mode_t mode)
  93{ errno = ENOSYS; return -1; }
  94static inline pid_t fork(void)
  95{ errno = ENOSYS; return -1; }
  96static inline unsigned int alarm(unsigned int seconds)
  97{ return 0; }
  98static inline int fsync(int fd)
  99{ return _commit(fd); }
 100static inline pid_t getppid(void)
 101{ return 1; }
 102static inline void sync(void)
 103{}
 104static inline uid_t getuid(void)
 105{ return 1; }
 106static inline struct passwd *getpwnam(const char *name)
 107{ return NULL; }
 108static inline int fcntl(int fd, int cmd, ...)
 109{
 110        if (cmd == F_GETFD || cmd == F_SETFD)
 111                return 0;
 112        errno = EINVAL;
 113        return -1;
 114}
 115/* bash cannot reliably detect negative return codes as failure */
 116#define exit(code) exit((code) & 0xff)
 117
 118/*
 119 * simple adaptors
 120 */
 121
 122static inline int mingw_mkdir(const char *path, int mode)
 123{
 124        return mkdir(path);
 125}
 126#define mkdir mingw_mkdir
 127
 128#define WNOHANG 1
 129pid_t waitpid(pid_t pid, int *status, unsigned options);
 130
 131#define kill mingw_kill
 132int mingw_kill(pid_t pid, int sig);
 133
 134#ifndef NO_OPENSSL
 135#include <openssl/ssl.h>
 136static inline int mingw_SSL_set_fd(SSL *ssl, int fd)
 137{
 138        return SSL_set_fd(ssl, _get_osfhandle(fd));
 139}
 140#define SSL_set_fd mingw_SSL_set_fd
 141
 142static inline int mingw_SSL_set_rfd(SSL *ssl, int fd)
 143{
 144        return SSL_set_rfd(ssl, _get_osfhandle(fd));
 145}
 146#define SSL_set_rfd mingw_SSL_set_rfd
 147
 148static inline int mingw_SSL_set_wfd(SSL *ssl, int fd)
 149{
 150        return SSL_set_wfd(ssl, _get_osfhandle(fd));
 151}
 152#define SSL_set_wfd mingw_SSL_set_wfd
 153#endif
 154
 155/*
 156 * implementations of missing functions
 157 */
 158
 159int pipe(int filedes[2]);
 160unsigned int sleep (unsigned int seconds);
 161int mkstemp(char *template);
 162int gettimeofday(struct timeval *tv, void *tz);
 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(uid_t 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_unlink(const char *pathname);
 176#define unlink mingw_unlink
 177
 178int mingw_rmdir(const char *path);
 179#define rmdir mingw_rmdir
 180
 181int mingw_open (const char *filename, int oflags, ...);
 182#define open mingw_open
 183
 184ssize_t mingw_write(int fd, const void *buf, size_t count);
 185#define write mingw_write
 186
 187FILE *mingw_fopen (const char *filename, const char *otype);
 188#define fopen mingw_fopen
 189
 190FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);
 191#define freopen mingw_freopen
 192
 193char *mingw_getcwd(char *pointer, int len);
 194#define getcwd mingw_getcwd
 195
 196char *mingw_getenv(const char *name);
 197#define getenv mingw_getenv
 198
 199struct hostent *mingw_gethostbyname(const char *host);
 200#define gethostbyname mingw_gethostbyname
 201
 202void mingw_freeaddrinfo(struct addrinfo *res);
 203#define freeaddrinfo mingw_freeaddrinfo
 204
 205int mingw_getaddrinfo(const char *node, const char *service,
 206                      const struct addrinfo *hints, struct addrinfo **res);
 207#define getaddrinfo mingw_getaddrinfo
 208
 209int mingw_getnameinfo(const struct sockaddr *sa, socklen_t salen,
 210                      char *host, DWORD hostlen, char *serv, DWORD servlen,
 211                      int flags);
 212#define getnameinfo mingw_getnameinfo
 213
 214int mingw_socket(int domain, int type, int protocol);
 215#define socket mingw_socket
 216
 217int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
 218#define connect mingw_connect
 219
 220int mingw_bind(int sockfd, struct sockaddr *sa, size_t sz);
 221#define bind mingw_bind
 222
 223int mingw_setsockopt(int sockfd, int lvl, int optname, void *optval, int optlen);
 224#define setsockopt mingw_setsockopt
 225
 226int mingw_listen(int sockfd, int backlog);
 227#define listen mingw_listen
 228
 229int mingw_accept(int sockfd, struct sockaddr *sa, socklen_t *sz);
 230#define accept mingw_accept
 231
 232int mingw_rename(const char*, const char*);
 233#define rename mingw_rename
 234
 235#if defined(USE_WIN32_MMAP) || defined(_MSC_VER)
 236int mingw_getpagesize(void);
 237#define getpagesize mingw_getpagesize
 238#endif
 239
 240/* Use mingw_lstat() instead of lstat()/stat() and
 241 * mingw_fstat() instead of fstat() on Windows.
 242 */
 243#define off_t off64_t
 244#define lseek _lseeki64
 245#ifndef ALREADY_DECLARED_STAT_FUNCS
 246#define stat _stati64
 247int mingw_lstat(const char *file_name, struct stat *buf);
 248int mingw_stat(const char *file_name, struct stat *buf);
 249int mingw_fstat(int fd, struct stat *buf);
 250#define fstat mingw_fstat
 251#define lstat mingw_lstat
 252#define _stati64(x,y) mingw_stat(x,y)
 253#endif
 254
 255int mingw_utime(const char *file_name, const struct utimbuf *times);
 256#define utime mingw_utime
 257
 258pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env,
 259                     const char *dir,
 260                     int fhin, int fhout, int fherr);
 261void mingw_execvp(const char *cmd, char *const *argv);
 262#define execvp mingw_execvp
 263void mingw_execv(const char *cmd, char *const *argv);
 264#define execv mingw_execv
 265
 266static inline unsigned int git_ntohl(unsigned int x)
 267{ return (unsigned int)ntohl(x); }
 268#define ntohl git_ntohl
 269
 270sig_handler_t mingw_signal(int sig, sig_handler_t handler);
 271#define signal mingw_signal
 272
 273/*
 274 * ANSI emulation wrappers
 275 */
 276
 277int winansi_fputs(const char *str, FILE *stream);
 278int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
 279int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
 280#define fputs winansi_fputs
 281#define printf(...) winansi_printf(__VA_ARGS__)
 282#define fprintf(...) winansi_fprintf(__VA_ARGS__)
 283
 284/*
 285 * git specific compatibility
 286 */
 287
 288#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
 289#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
 290#define PATH_SEP ';'
 291#define PRIuMAX "I64u"
 292
 293void mingw_open_html(const char *path);
 294#define open_html mingw_open_html
 295
 296/*
 297 * helpers
 298 */
 299
 300char **make_augmented_environ(const char *const *vars);
 301void free_environ(char **env);
 302
 303/*
 304 * A replacement of main() that ensures that argv[0] has a path
 305 * and that default fmode and std(in|out|err) are in binary mode
 306 */
 307
 308#define main(c,v) dummy_decl_mingw_main(); \
 309static int mingw_main(); \
 310int main(int argc, const char **argv) \
 311{ \
 312        extern CRITICAL_SECTION pinfo_cs; \
 313        _fmode = _O_BINARY; \
 314        _setmode(_fileno(stdin), _O_BINARY); \
 315        _setmode(_fileno(stdout), _O_BINARY); \
 316        _setmode(_fileno(stderr), _O_BINARY); \
 317        argv[0] = xstrdup(_pgmptr); \
 318        InitializeCriticalSection(&pinfo_cs); \
 319        return mingw_main(argc, argv); \
 320} \
 321static int mingw_main(c,v)
 322
 323/*
 324 * Used by Pthread API implementation for Windows
 325 */
 326extern int err_win_to_posix(DWORD winerr);