Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 1 | #include <winsock2.h> |
| 2 | |
| 3 | /* |
| 4 | * things that are not available in header files |
| 5 | */ |
| 6 | |
| 7 | typedef 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 | |
Johannes Sixt | 303e7c4 | 2009-07-04 21:26:38 +0200 | [diff] [blame] | 20 | #define WIFEXITED(x) 1 |
| 21 | #define WIFSIGNALED(x) 0 |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 22 | #define WEXITSTATUS(x) ((x) & 0xff) |
Johannes Sixt | 303e7c4 | 2009-07-04 21:26:38 +0200 | [diff] [blame] | 23 | #define WTERMSIG(x) SIGTERM |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 24 | |
Johannes Sixt | d282506 | 2009-01-22 00:57:34 -0500 | [diff] [blame] | 25 | #define SIGHUP 1 |
| 26 | #define SIGQUIT 3 |
| 27 | #define SIGKILL 9 |
| 28 | #define SIGPIPE 13 |
| 29 | #define SIGALRM 14 |
| 30 | #define SIGCHLD 17 |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 31 | |
| 32 | #define F_GETFD 1 |
| 33 | #define F_SETFD 2 |
| 34 | #define FD_CLOEXEC 0x1 |
| 35 | |
| 36 | struct passwd { |
| 37 | char *pw_name; |
| 38 | char *pw_gecos; |
| 39 | char *pw_dir; |
| 40 | }; |
| 41 | |
Johannes Schindelin | 0dbbbc1 | 2009-05-23 10:04:50 +0200 | [diff] [blame] | 42 | extern char *getpass(const char *prompt); |
| 43 | |
Marius Storm-Olsen | 435bdf8 | 2009-09-16 10:20:26 +0200 | [diff] [blame] | 44 | #ifndef POLLIN |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 45 | struct pollfd { |
| 46 | int fd; /* file descriptor */ |
| 47 | short events; /* requested events */ |
| 48 | short revents; /* returned events */ |
| 49 | }; |
| 50 | #define POLLIN 1 |
| 51 | #define POLLHUP 2 |
Marius Storm-Olsen | 435bdf8 | 2009-09-16 10:20:26 +0200 | [diff] [blame] | 52 | #endif |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 53 | |
| 54 | typedef void (__cdecl *sig_handler_t)(int); |
| 55 | struct sigaction { |
| 56 | sig_handler_t sa_handler; |
| 57 | unsigned sa_flags; |
| 58 | }; |
| 59 | #define sigemptyset(x) (void)0 |
| 60 | #define SA_RESTART 0 |
| 61 | |
| 62 | struct itimerval { |
| 63 | struct timeval it_value, it_interval; |
| 64 | }; |
| 65 | #define ITIMER_REAL 0 |
| 66 | |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 67 | /* |
| 68 | * trivial stubs |
| 69 | */ |
| 70 | |
| 71 | static inline int readlink(const char *path, char *buf, size_t bufsiz) |
| 72 | { errno = ENOSYS; return -1; } |
| 73 | static inline int symlink(const char *oldpath, const char *newpath) |
| 74 | { errno = ENOSYS; return -1; } |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 75 | static inline int fchmod(int fildes, mode_t mode) |
| 76 | { errno = ENOSYS; return -1; } |
| 77 | static inline int fork(void) |
| 78 | { errno = ENOSYS; return -1; } |
| 79 | static inline unsigned int alarm(unsigned int seconds) |
| 80 | { return 0; } |
| 81 | static inline int fsync(int fd) |
| 82 | { return 0; } |
| 83 | static inline int getppid(void) |
| 84 | { return 1; } |
| 85 | static inline void sync(void) |
| 86 | {} |
| 87 | static inline int getuid() |
| 88 | { return 1; } |
| 89 | static inline struct passwd *getpwnam(const char *name) |
| 90 | { return NULL; } |
| 91 | static inline int fcntl(int fd, int cmd, long arg) |
| 92 | { |
| 93 | if (cmd == F_GETFD || cmd == F_SETFD) |
| 94 | return 0; |
| 95 | errno = EINVAL; |
| 96 | return -1; |
| 97 | } |
Johannes Sixt | 47e3de0 | 2009-07-05 20:57:46 +0200 | [diff] [blame] | 98 | /* bash cannot reliably detect negative return codes as failure */ |
| 99 | #define exit(code) exit((code) & 0xff) |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * simple adaptors |
| 103 | */ |
| 104 | |
| 105 | static inline int mingw_mkdir(const char *path, int mode) |
| 106 | { |
| 107 | return mkdir(path); |
| 108 | } |
| 109 | #define mkdir mingw_mkdir |
| 110 | |
Johannes Schindelin | 132a6e9 | 2007-01-23 13:39:09 +0100 | [diff] [blame] | 111 | static inline int mingw_unlink(const char *pathname) |
| 112 | { |
| 113 | /* read-only files cannot be removed */ |
| 114 | chmod(pathname, 0666); |
| 115 | return unlink(pathname); |
| 116 | } |
| 117 | #define unlink mingw_unlink |
| 118 | |
Johannes Schindelin | 27e3219 | 2009-05-23 10:04:49 +0200 | [diff] [blame] | 119 | static inline int waitpid(pid_t pid, int *status, unsigned options) |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 120 | { |
| 121 | if (options == 0) |
| 122 | return _cwait(status, pid, 0); |
| 123 | errno = EINVAL; |
| 124 | return -1; |
| 125 | } |
| 126 | |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 127 | /* |
| 128 | * implementations of missing functions |
| 129 | */ |
| 130 | |
Johannes Sixt | 897bb8c | 2007-12-07 22:05:36 +0100 | [diff] [blame] | 131 | int pipe(int filedes[2]); |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 132 | unsigned int sleep (unsigned int seconds); |
| 133 | int mkstemp(char *template); |
| 134 | int gettimeofday(struct timeval *tv, void *tz); |
| 135 | int poll(struct pollfd *ufds, unsigned int nfds, int timeout); |
| 136 | struct tm *gmtime_r(const time_t *timep, struct tm *result); |
| 137 | struct tm *localtime_r(const time_t *timep, struct tm *result); |
| 138 | int getpagesize(void); /* defined in MinGW's libgcc.a */ |
| 139 | struct passwd *getpwuid(int uid); |
| 140 | int setitimer(int type, struct itimerval *in, struct itimerval *out); |
| 141 | int sigaction(int sig, struct sigaction *in, struct sigaction *out); |
Petr Kodl | 7be401e | 2009-01-24 15:04:39 +0100 | [diff] [blame] | 142 | int link(const char *oldpath, const char *newpath); |
Johannes Sixt | 80ba074 | 2007-12-03 21:55:57 +0100 | [diff] [blame] | 143 | |
| 144 | /* |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 145 | * replacements of existing functions |
| 146 | */ |
| 147 | |
Johannes Sixt | 3e4a1ba | 2007-11-15 22:22:47 +0100 | [diff] [blame] | 148 | int mingw_open (const char *filename, int oflags, ...); |
| 149 | #define open mingw_open |
| 150 | |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 151 | char *mingw_getcwd(char *pointer, int len); |
| 152 | #define getcwd mingw_getcwd |
| 153 | |
Johannes Sixt | 6fd6aec | 2008-06-22 11:35:21 +0200 | [diff] [blame] | 154 | char *mingw_getenv(const char *name); |
| 155 | #define getenv mingw_getenv |
| 156 | |
Johannes Sixt | 746fb85 | 2007-12-26 13:51:18 +0100 | [diff] [blame] | 157 | struct hostent *mingw_gethostbyname(const char *host); |
| 158 | #define gethostbyname mingw_gethostbyname |
| 159 | |
| 160 | int mingw_socket(int domain, int type, int protocol); |
| 161 | #define socket mingw_socket |
| 162 | |
| 163 | int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz); |
| 164 | #define connect mingw_connect |
| 165 | |
Johannes Sixt | ea9e98c | 2007-12-07 22:19:40 +0100 | [diff] [blame] | 166 | int mingw_rename(const char*, const char*); |
| 167 | #define rename mingw_rename |
| 168 | |
Janos Laube | b130a72 | 2009-03-13 16:50:45 +0100 | [diff] [blame] | 169 | #ifdef USE_WIN32_MMAP |
| 170 | int mingw_getpagesize(void); |
| 171 | #define getpagesize mingw_getpagesize |
| 172 | #endif |
| 173 | |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 174 | /* Use mingw_lstat() instead of lstat()/stat() and |
| 175 | * mingw_fstat() instead of fstat() on Windows. |
| 176 | */ |
Johannes Schindelin | 1d4e4cd | 2009-03-05 17:05:12 +0100 | [diff] [blame] | 177 | #define off_t off64_t |
| 178 | #define stat _stati64 |
| 179 | #define lseek _lseeki64 |
Johannes Sixt | 180964f | 2008-08-18 22:01:06 +0200 | [diff] [blame] | 180 | int mingw_lstat(const char *file_name, struct stat *buf); |
| 181 | int mingw_fstat(int fd, struct stat *buf); |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 182 | #define fstat mingw_fstat |
| 183 | #define lstat mingw_lstat |
Johannes Schindelin | d5e3123 | 2009-03-07 15:37:18 +0100 | [diff] [blame] | 184 | #define _stati64(x,y) mingw_lstat(x,y) |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 185 | |
Johannes Sixt | 7c0ffa1 | 2007-09-07 13:05:00 +0200 | [diff] [blame] | 186 | int mingw_utime(const char *file_name, const struct utimbuf *times); |
| 187 | #define utime mingw_utime |
| 188 | |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 189 | pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 190 | void mingw_execvp(const char *cmd, char *const *argv); |
| 191 | #define execvp mingw_execvp |
| 192 | |
Steffen Prohaska | cd800ee | 2007-11-17 19:16:53 +0100 | [diff] [blame] | 193 | static inline unsigned int git_ntohl(unsigned int x) |
| 194 | { return (unsigned int)ntohl(x); } |
| 195 | #define ntohl git_ntohl |
| 196 | |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 197 | sig_handler_t mingw_signal(int sig, sig_handler_t handler); |
| 198 | #define signal mingw_signal |
| 199 | |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 200 | /* |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 201 | * ANSI emulation wrappers |
| 202 | */ |
| 203 | |
| 204 | int winansi_fputs(const char *str, FILE *stream); |
| 205 | int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2))); |
| 206 | int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3))); |
| 207 | #define fputs winansi_fputs |
| 208 | #define printf(...) winansi_printf(__VA_ARGS__) |
| 209 | #define fprintf(...) winansi_fprintf(__VA_ARGS__) |
| 210 | |
| 211 | /* |
Johannes Sixt | 80ba074 | 2007-12-03 21:55:57 +0100 | [diff] [blame] | 212 | * git specific compatibility |
| 213 | */ |
| 214 | |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 215 | #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':') |
| 216 | #define is_dir_sep(c) ((c) == '/' || (c) == '\\') |
Johannes Sixt | 80ba074 | 2007-12-03 21:55:57 +0100 | [diff] [blame] | 217 | #define PATH_SEP ';' |
Johannes Sixt | 82f8d96 | 2007-03-23 10:57:05 +0100 | [diff] [blame] | 218 | #define PRIuMAX "I64u" |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 219 | |
Steffen Prohaska | 4804aab | 2008-07-13 22:31:20 +0200 | [diff] [blame] | 220 | void mingw_open_html(const char *path); |
| 221 | #define open_html mingw_open_html |
| 222 | |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 223 | /* |
| 224 | * helpers |
| 225 | */ |
| 226 | |
Johannes Sixt | 2affea4 | 2009-09-11 19:40:08 +0200 | [diff] [blame] | 227 | char **make_augmented_environ(const char *const *vars); |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 228 | void free_environ(char **env); |
Johannes Sixt | 35eeef4 | 2008-07-21 21:19:57 +0200 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * A replacement of main() that ensures that argv[0] has a path |
Marius Storm-Olsen | a6ca8c6 | 2009-09-16 10:20:20 +0200 | [diff] [blame] | 232 | * and that default fmode and std(in|out|err) are in binary mode |
Johannes Sixt | 35eeef4 | 2008-07-21 21:19:57 +0200 | [diff] [blame] | 233 | */ |
| 234 | |
Steffen Prohaska | 2253776 | 2008-07-26 11:41:44 +0200 | [diff] [blame] | 235 | #define main(c,v) dummy_decl_mingw_main(); \ |
| 236 | static int mingw_main(); \ |
| 237 | int main(int argc, const char **argv) \ |
Johannes Sixt | 35eeef4 | 2008-07-21 21:19:57 +0200 | [diff] [blame] | 238 | { \ |
Marius Storm-Olsen | a6ca8c6 | 2009-09-16 10:20:20 +0200 | [diff] [blame] | 239 | _fmode = _O_BINARY; \ |
| 240 | _setmode(_fileno(stdin), _O_BINARY); \ |
| 241 | _setmode(_fileno(stdout), _O_BINARY); \ |
| 242 | _setmode(_fileno(stderr), _O_BINARY); \ |
Johannes Sixt | 35eeef4 | 2008-07-21 21:19:57 +0200 | [diff] [blame] | 243 | argv[0] = xstrdup(_pgmptr); \ |
| 244 | return mingw_main(argc, argv); \ |
| 245 | } \ |
| 246 | static int mingw_main(c,v) |
Marius Storm-Olsen | e16c60d | 2009-05-31 18:15:22 +0200 | [diff] [blame] | 247 | |
| 248 | #ifndef NO_MINGW_REPLACE_READDIR |
| 249 | /* |
| 250 | * A replacement of readdir, to ensure that it reads the file type at |
| 251 | * the same time. This avoid extra unneeded lstats in git on MinGW |
| 252 | */ |
| 253 | #undef DT_UNKNOWN |
| 254 | #undef DT_DIR |
| 255 | #undef DT_REG |
| 256 | #undef DT_LNK |
| 257 | #define DT_UNKNOWN 0 |
| 258 | #define DT_DIR 1 |
| 259 | #define DT_REG 2 |
| 260 | #define DT_LNK 3 |
| 261 | |
| 262 | struct mingw_dirent |
| 263 | { |
| 264 | long d_ino; /* Always zero. */ |
| 265 | union { |
| 266 | unsigned short d_reclen; /* Always zero. */ |
| 267 | unsigned char d_type; /* Reimplementation adds this */ |
| 268 | }; |
| 269 | unsigned short d_namlen; /* Length of name in d_name. */ |
| 270 | char d_name[FILENAME_MAX]; /* File name. */ |
| 271 | }; |
| 272 | #define dirent mingw_dirent |
| 273 | #define readdir(x) mingw_readdir(x) |
| 274 | struct dirent *mingw_readdir(DIR *dir); |
| 275 | #endif // !NO_MINGW_REPLACE_READDIR |