compat / mingw.hon commit MinGW: truncate exit()'s argument to lowest 8 bits (47e3de0)
   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 SIGHUP 1
  25#define SIGQUIT 3
  26#define SIGKILL 9
  27#define SIGPIPE 13
  28#define SIGALRM 14
  29#define SIGCHLD 17
  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
  41extern char *getpass(const char *prompt);
  42
  43struct pollfd {
  44        int fd;           /* file descriptor */
  45        short events;     /* requested events */
  46        short revents;    /* returned events */
  47};
  48#define POLLIN 1
  49#define POLLHUP 2
  50
  51typedef void (__cdecl *sig_handler_t)(int);
  52struct sigaction {
  53        sig_handler_t sa_handler;
  54        unsigned sa_flags;
  55};
  56#define sigemptyset(x) (void)0
  57#define SA_RESTART 0
  58
  59struct itimerval {
  60        struct timeval it_value, it_interval;
  61};
  62#define ITIMER_REAL 0
  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 fchmod(int fildes, mode_t mode)
  73{ errno = ENOSYS; return -1; }
  74static inline int fork(void)
  75{ errno = ENOSYS; return -1; }
  76static inline unsigned int alarm(unsigned int seconds)
  77{ return 0; }
  78static inline int fsync(int fd)
  79{ return 0; }
  80static inline int getppid(void)
  81{ return 1; }
  82static inline void sync(void)
  83{}
  84static inline int getuid()
  85{ return 1; }
  86static inline struct passwd *getpwnam(const char *name)
  87{ return NULL; }
  88static inline int fcntl(int fd, int cmd, long arg)
  89{
  90        if (cmd == F_GETFD || cmd == F_SETFD)
  91                return 0;
  92        errno = EINVAL;
  93        return -1;
  94}
  95/* bash cannot reliably detect negative return codes as failure */
  96#define exit(code) exit((code) & 0xff)
  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, int *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);
 139int link(const char *oldpath, const char *newpath);
 140
 141/*
 142 * replacements of existing functions
 143 */
 144
 145int mingw_open (const char *filename, int oflags, ...);
 146#define open mingw_open
 147
 148char *mingw_getcwd(char *pointer, int len);
 149#define getcwd mingw_getcwd
 150
 151char *mingw_getenv(const char *name);
 152#define getenv mingw_getenv
 153
 154struct hostent *mingw_gethostbyname(const char *host);
 155#define gethostbyname mingw_gethostbyname
 156
 157int mingw_socket(int domain, int type, int protocol);
 158#define socket mingw_socket
 159
 160int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
 161#define connect mingw_connect
 162
 163int mingw_rename(const char*, const char*);
 164#define rename mingw_rename
 165
 166#ifdef USE_WIN32_MMAP
 167int mingw_getpagesize(void);
 168#define getpagesize mingw_getpagesize
 169#endif
 170
 171/* Use mingw_lstat() instead of lstat()/stat() and
 172 * mingw_fstat() instead of fstat() on Windows.
 173 */
 174#define off_t off64_t
 175#define stat _stati64
 176#define lseek _lseeki64
 177int mingw_lstat(const char *file_name, struct stat *buf);
 178int mingw_fstat(int fd, struct stat *buf);
 179#define fstat mingw_fstat
 180#define lstat mingw_lstat
 181#define _stati64(x,y) mingw_lstat(x,y)
 182
 183int mingw_utime(const char *file_name, const struct utimbuf *times);
 184#define utime mingw_utime
 185
 186pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env);
 187void mingw_execvp(const char *cmd, char *const *argv);
 188#define execvp mingw_execvp
 189
 190static inline unsigned int git_ntohl(unsigned int x)
 191{ return (unsigned int)ntohl(x); }
 192#define ntohl git_ntohl
 193
 194sig_handler_t mingw_signal(int sig, sig_handler_t handler);
 195#define signal mingw_signal
 196
 197/*
 198 * ANSI emulation wrappers
 199 */
 200
 201int winansi_fputs(const char *str, FILE *stream);
 202int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
 203int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
 204#define fputs winansi_fputs
 205#define printf(...) winansi_printf(__VA_ARGS__)
 206#define fprintf(...) winansi_fprintf(__VA_ARGS__)
 207
 208/*
 209 * git specific compatibility
 210 */
 211
 212#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
 213#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
 214#define PATH_SEP ';'
 215#define PRIuMAX "I64u"
 216
 217void mingw_open_html(const char *path);
 218#define open_html mingw_open_html
 219
 220/*
 221 * helpers
 222 */
 223
 224char **copy_environ(void);
 225void free_environ(char **env);
 226char **env_setenv(char **env, const char *name);
 227
 228/*
 229 * A replacement of main() that ensures that argv[0] has a path
 230 */
 231
 232#define main(c,v) dummy_decl_mingw_main(); \
 233static int mingw_main(); \
 234int main(int argc, const char **argv) \
 235{ \
 236        argv[0] = xstrdup(_pgmptr); \
 237        return mingw_main(argc, argv); \
 238} \
 239static int mingw_main(c,v)
 240
 241#ifndef NO_MINGW_REPLACE_READDIR
 242/*
 243 * A replacement of readdir, to ensure that it reads the file type at
 244 * the same time. This avoid extra unneeded lstats in git on MinGW
 245 */
 246#undef DT_UNKNOWN
 247#undef DT_DIR
 248#undef DT_REG
 249#undef DT_LNK
 250#define DT_UNKNOWN      0
 251#define DT_DIR          1
 252#define DT_REG          2
 253#define DT_LNK          3
 254
 255struct mingw_dirent
 256{
 257        long            d_ino;                  /* Always zero. */
 258        union {
 259                unsigned short  d_reclen;       /* Always zero. */
 260                unsigned char   d_type;         /* Reimplementation adds this */
 261        };
 262        unsigned short  d_namlen;               /* Length of name in d_name. */
 263        char            d_name[FILENAME_MAX];   /* File name. */
 264};
 265#define dirent mingw_dirent
 266#define readdir(x) mingw_readdir(x)
 267struct dirent *mingw_readdir(DIR *dir);
 268#endif // !NO_MINGW_REPLACE_READDIR