69b1dde3ca07bdb695eb5c87f124ec3b9a34b714
   1#include <winsock2.h>
   2
   3/*
   4 * things that are not available in header files
   5 */
   6
   7typedef int pid_t;
   8#define hstrerror strerror
   9
  10#define S_IFLNK    0120000 /* Symbolic link */
  11#define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
  12#define S_ISSOCK(x) 0
  13#define S_IRGRP 0
  14#define S_IWGRP 0
  15#define S_IXGRP 0
  16#define S_ISGID 0
  17#define S_IROTH 0
  18#define S_IXOTH 0
  19
  20#define WIFEXITED(x) ((unsigned)(x) < 259)      /* STILL_ACTIVE */
  21#define WEXITSTATUS(x) ((x) & 0xff)
  22#define WIFSIGNALED(x) ((unsigned)(x) > 259)
  23
  24#define SIGKILL 0
  25#define SIGCHLD 0
  26#define SIGPIPE 0
  27#define SIGHUP 0
  28#define SIGQUIT 0
  29#define SIGALRM 100
  30
  31#define F_GETFD 1
  32#define F_SETFD 2
  33#define FD_CLOEXEC 0x1
  34
  35struct passwd {
  36        char *pw_name;
  37        char *pw_gecos;
  38        char *pw_dir;
  39};
  40
  41struct pollfd {
  42        int fd;           /* file descriptor */
  43        short events;     /* requested events */
  44        short revents;    /* returned events */
  45};
  46#define POLLIN 1
  47#define POLLHUP 2
  48
  49typedef void (__cdecl *sig_handler_t)(int);
  50struct sigaction {
  51        sig_handler_t sa_handler;
  52        unsigned sa_flags;
  53};
  54#define sigemptyset(x) (void)0
  55#define SA_RESTART 0
  56
  57struct itimerval {
  58        struct timeval it_value, it_interval;
  59};
  60#define ITIMER_REAL 0
  61
  62#define st_blocks st_size/512   /* will be cleaned up later */
  63
  64/*
  65 * trivial stubs
  66 */
  67
  68static inline int readlink(const char *path, char *buf, size_t bufsiz)
  69{ errno = ENOSYS; return -1; }
  70static inline int symlink(const char *oldpath, const char *newpath)
  71{ errno = ENOSYS; return -1; }
  72static inline int link(const char *oldpath, const char *newpath)
  73{ errno = ENOSYS; return -1; }
  74static inline int fchmod(int fildes, mode_t mode)
  75{ errno = ENOSYS; return -1; }
  76static inline int fork(void)
  77{ errno = ENOSYS; return -1; }
  78static inline unsigned int alarm(unsigned int seconds)
  79{ return 0; }
  80static inline int fsync(int fd)
  81{ return 0; }
  82static inline int getppid(void)
  83{ return 1; }
  84static inline void sync(void)
  85{}
  86static inline int getuid()
  87{ return 1; }
  88static inline struct passwd *getpwnam(const char *name)
  89{ return NULL; }
  90static inline int fcntl(int fd, int cmd, long arg)
  91{
  92        if (cmd == F_GETFD || cmd == F_SETFD)
  93                return 0;
  94        errno = EINVAL;
  95        return -1;
  96}
  97
  98/*
  99 * simple adaptors
 100 */
 101
 102static inline int mingw_mkdir(const char *path, int mode)
 103{
 104        return mkdir(path);
 105}
 106#define mkdir mingw_mkdir
 107
 108static inline int mingw_unlink(const char *pathname)
 109{
 110        /* read-only files cannot be removed */
 111        chmod(pathname, 0666);
 112        return unlink(pathname);
 113}
 114#define unlink mingw_unlink
 115
 116static inline int waitpid(pid_t pid, unsigned *status, unsigned options)
 117{
 118        if (options == 0)
 119                return _cwait(status, pid, 0);
 120        errno = EINVAL;
 121        return -1;
 122}
 123
 124/*
 125 * implementations of missing functions
 126 */
 127
 128int pipe(int filedes[2]);
 129unsigned int sleep (unsigned int seconds);
 130int mkstemp(char *template);
 131int gettimeofday(struct timeval *tv, void *tz);
 132int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
 133struct tm *gmtime_r(const time_t *timep, struct tm *result);
 134struct tm *localtime_r(const time_t *timep, struct tm *result);
 135int getpagesize(void);  /* defined in MinGW's libgcc.a */
 136struct passwd *getpwuid(int uid);
 137int setitimer(int type, struct itimerval *in, struct itimerval *out);
 138int sigaction(int sig, struct sigaction *in, struct sigaction *out);
 139
 140/*
 141 * replacements of existing functions
 142 */
 143
 144int mingw_open (const char *filename, int oflags, ...);
 145#define open mingw_open
 146
 147char *mingw_getcwd(char *pointer, int len);
 148#define getcwd mingw_getcwd
 149
 150struct hostent *mingw_gethostbyname(const char *host);
 151#define gethostbyname mingw_gethostbyname
 152
 153int mingw_socket(int domain, int type, int protocol);
 154#define socket mingw_socket
 155
 156int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
 157#define connect mingw_connect
 158
 159int mingw_rename(const char*, const char*);
 160#define rename mingw_rename
 161
 162/* Use mingw_lstat() instead of lstat()/stat() and
 163 * mingw_fstat() instead of fstat() on Windows.
 164 */
 165int mingw_lstat(const char *file_name, struct stat *buf);
 166int mingw_fstat(int fd, struct stat *buf);
 167#define fstat mingw_fstat
 168#define lstat mingw_lstat
 169#define stat(x,y) mingw_lstat(x,y)
 170
 171pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env);
 172void mingw_execvp(const char *cmd, char *const *argv);
 173#define execvp mingw_execvp
 174
 175sig_handler_t mingw_signal(int sig, sig_handler_t handler);
 176#define signal mingw_signal
 177
 178/*
 179 * git specific compatibility
 180 */
 181
 182#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
 183#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
 184#define PATH_SEP ';'
 185#define PRIuMAX "I64u"
 186
 187/*
 188 * helpers
 189 */
 190
 191char **copy_environ(void);
 192void free_environ(char **env);
 193char **env_setenv(char **env, const char *name);