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 | |
| 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 | |
| 35 | struct passwd { |
| 36 | char *pw_name; |
| 37 | char *pw_gecos; |
| 38 | char *pw_dir; |
| 39 | }; |
| 40 | |
| 41 | struct 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 | |
| 49 | typedef void (__cdecl *sig_handler_t)(int); |
| 50 | struct sigaction { |
| 51 | sig_handler_t sa_handler; |
| 52 | unsigned sa_flags; |
| 53 | }; |
| 54 | #define sigemptyset(x) (void)0 |
| 55 | #define SA_RESTART 0 |
| 56 | |
| 57 | struct itimerval { |
| 58 | struct timeval it_value, it_interval; |
| 59 | }; |
| 60 | #define ITIMER_REAL 0 |
| 61 | |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 62 | /* |
| 63 | * trivial stubs |
| 64 | */ |
| 65 | |
| 66 | static inline int readlink(const char *path, char *buf, size_t bufsiz) |
| 67 | { errno = ENOSYS; return -1; } |
| 68 | static inline int symlink(const char *oldpath, const char *newpath) |
| 69 | { errno = ENOSYS; return -1; } |
| 70 | static inline int link(const char *oldpath, const char *newpath) |
| 71 | { errno = ENOSYS; return -1; } |
| 72 | static inline int fchmod(int fildes, mode_t mode) |
| 73 | { errno = ENOSYS; return -1; } |
| 74 | static inline int fork(void) |
| 75 | { errno = ENOSYS; return -1; } |
| 76 | static inline unsigned int alarm(unsigned int seconds) |
| 77 | { return 0; } |
| 78 | static inline int fsync(int fd) |
| 79 | { return 0; } |
| 80 | static inline int getppid(void) |
| 81 | { return 1; } |
| 82 | static inline void sync(void) |
| 83 | {} |
| 84 | static inline int getuid() |
| 85 | { return 1; } |
| 86 | static inline struct passwd *getpwnam(const char *name) |
| 87 | { return NULL; } |
| 88 | static 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 | |
| 96 | /* |
| 97 | * simple adaptors |
| 98 | */ |
| 99 | |
| 100 | static inline int mingw_mkdir(const char *path, int mode) |
| 101 | { |
| 102 | return mkdir(path); |
| 103 | } |
| 104 | #define mkdir mingw_mkdir |
| 105 | |
Johannes Schindelin | 132a6e9 | 2007-01-23 13:39:09 +0100 | [diff] [blame] | 106 | static inline int mingw_unlink(const char *pathname) |
| 107 | { |
| 108 | /* read-only files cannot be removed */ |
| 109 | chmod(pathname, 0666); |
| 110 | return unlink(pathname); |
| 111 | } |
| 112 | #define unlink mingw_unlink |
| 113 | |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 114 | static inline int waitpid(pid_t pid, unsigned *status, unsigned options) |
| 115 | { |
| 116 | if (options == 0) |
| 117 | return _cwait(status, pid, 0); |
| 118 | errno = EINVAL; |
| 119 | return -1; |
| 120 | } |
| 121 | |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 122 | /* |
| 123 | * implementations of missing functions |
| 124 | */ |
| 125 | |
Johannes Sixt | 897bb8c | 2007-12-07 22:05:36 +0100 | [diff] [blame] | 126 | int pipe(int filedes[2]); |
Johannes Sixt | f4626df | 2007-12-01 21:24:59 +0100 | [diff] [blame] | 127 | unsigned int sleep (unsigned int seconds); |
| 128 | int mkstemp(char *template); |
| 129 | int gettimeofday(struct timeval *tv, void *tz); |
| 130 | int poll(struct pollfd *ufds, unsigned int nfds, int timeout); |
| 131 | struct tm *gmtime_r(const time_t *timep, struct tm *result); |
| 132 | struct tm *localtime_r(const time_t *timep, struct tm *result); |
| 133 | int getpagesize(void); /* defined in MinGW's libgcc.a */ |
| 134 | struct passwd *getpwuid(int uid); |
| 135 | int setitimer(int type, struct itimerval *in, struct itimerval *out); |
| 136 | int sigaction(int sig, struct sigaction *in, struct sigaction *out); |
Johannes Sixt | 80ba074 | 2007-12-03 21:55:57 +0100 | [diff] [blame] | 137 | |
| 138 | /* |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 139 | * replacements of existing functions |
| 140 | */ |
| 141 | |
Johannes Sixt | 3e4a1ba | 2007-11-15 22:22:47 +0100 | [diff] [blame] | 142 | int mingw_open (const char *filename, int oflags, ...); |
| 143 | #define open mingw_open |
| 144 | |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 145 | char *mingw_getcwd(char *pointer, int len); |
| 146 | #define getcwd mingw_getcwd |
| 147 | |
Johannes Sixt | 6fd6aec | 2008-06-22 11:35:21 +0200 | [diff] [blame] | 148 | char *mingw_getenv(const char *name); |
| 149 | #define getenv mingw_getenv |
| 150 | |
Johannes Sixt | 746fb85 | 2007-12-26 13:51:18 +0100 | [diff] [blame] | 151 | struct hostent *mingw_gethostbyname(const char *host); |
| 152 | #define gethostbyname mingw_gethostbyname |
| 153 | |
| 154 | int mingw_socket(int domain, int type, int protocol); |
| 155 | #define socket mingw_socket |
| 156 | |
| 157 | int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz); |
| 158 | #define connect mingw_connect |
| 159 | |
Johannes Sixt | ea9e98c | 2007-12-07 22:19:40 +0100 | [diff] [blame] | 160 | int mingw_rename(const char*, const char*); |
| 161 | #define rename mingw_rename |
| 162 | |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 163 | /* Use mingw_lstat() instead of lstat()/stat() and |
| 164 | * mingw_fstat() instead of fstat() on Windows. |
| 165 | */ |
Johannes Sixt | 180964f | 2008-08-18 22:01:06 +0200 | [diff] [blame] | 166 | int mingw_lstat(const char *file_name, struct stat *buf); |
| 167 | int mingw_fstat(int fd, struct stat *buf); |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 168 | #define fstat mingw_fstat |
| 169 | #define lstat mingw_lstat |
Johannes Sixt | 180964f | 2008-08-18 22:01:06 +0200 | [diff] [blame] | 170 | #define stat(x,y) mingw_lstat(x,y) |
Marius Storm-Olsen | 5411bdc | 2007-09-03 20:40:26 +0200 | [diff] [blame] | 171 | |
Johannes Sixt | 7c0ffa1 | 2007-09-07 13:05:00 +0200 | [diff] [blame] | 172 | int mingw_utime(const char *file_name, const struct utimbuf *times); |
| 173 | #define utime mingw_utime |
| 174 | |
Johannes Sixt | 7e5d776 | 2007-11-24 22:49:16 +0100 | [diff] [blame] | 175 | pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env); |
Johannes Sixt | f1a4dfb | 2007-12-04 12:38:32 +0100 | [diff] [blame] | 176 | void mingw_execvp(const char *cmd, char *const *argv); |
| 177 | #define execvp mingw_execvp |
| 178 | |
Steffen Prohaska | cd800ee | 2007-11-17 19:16:53 +0100 | [diff] [blame] | 179 | static inline unsigned int git_ntohl(unsigned int x) |
| 180 | { return (unsigned int)ntohl(x); } |
| 181 | #define ntohl git_ntohl |
| 182 | |
Johannes Sixt | 6072fc3 | 2007-11-13 10:14:45 +0100 | [diff] [blame] | 183 | sig_handler_t mingw_signal(int sig, sig_handler_t handler); |
| 184 | #define signal mingw_signal |
| 185 | |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 186 | /* |
Peter Harris | c09df8a | 2008-07-18 09:34:44 +0200 | [diff] [blame] | 187 | * ANSI emulation wrappers |
| 188 | */ |
| 189 | |
| 190 | int winansi_fputs(const char *str, FILE *stream); |
| 191 | int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2))); |
| 192 | int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3))); |
| 193 | #define fputs winansi_fputs |
| 194 | #define printf(...) winansi_printf(__VA_ARGS__) |
| 195 | #define fprintf(...) winansi_fprintf(__VA_ARGS__) |
| 196 | |
| 197 | /* |
Johannes Sixt | 80ba074 | 2007-12-03 21:55:57 +0100 | [diff] [blame] | 198 | * git specific compatibility |
| 199 | */ |
| 200 | |
Johannes Sixt | 25fe217 | 2008-03-05 21:51:27 +0100 | [diff] [blame] | 201 | #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':') |
| 202 | #define is_dir_sep(c) ((c) == '/' || (c) == '\\') |
Johannes Sixt | 80ba074 | 2007-12-03 21:55:57 +0100 | [diff] [blame] | 203 | #define PATH_SEP ';' |
Johannes Sixt | 82f8d96 | 2007-03-23 10:57:05 +0100 | [diff] [blame] | 204 | #define PRIuMAX "I64u" |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 205 | |
Steffen Prohaska | 4804aab | 2008-07-13 22:31:20 +0200 | [diff] [blame] | 206 | void mingw_open_html(const char *path); |
| 207 | #define open_html mingw_open_html |
| 208 | |
Johannes Sixt | ba26f29 | 2007-12-07 22:08:59 +0100 | [diff] [blame] | 209 | /* |
| 210 | * helpers |
| 211 | */ |
| 212 | |
| 213 | char **copy_environ(void); |
| 214 | void free_environ(char **env); |
| 215 | char **env_setenv(char **env, const char *name); |
Johannes Sixt | 35eeef4 | 2008-07-21 21:19:57 +0200 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * A replacement of main() that ensures that argv[0] has a path |
| 219 | */ |
| 220 | |
Steffen Prohaska | 2253776 | 2008-07-26 11:41:44 +0200 | [diff] [blame] | 221 | #define main(c,v) dummy_decl_mingw_main(); \ |
| 222 | static int mingw_main(); \ |
| 223 | int main(int argc, const char **argv) \ |
Johannes Sixt | 35eeef4 | 2008-07-21 21:19:57 +0200 | [diff] [blame] | 224 | { \ |
Johannes Sixt | 35eeef4 | 2008-07-21 21:19:57 +0200 | [diff] [blame] | 225 | argv[0] = xstrdup(_pgmptr); \ |
| 226 | return mingw_main(argc, argv); \ |
| 227 | } \ |
| 228 | static int mingw_main(c,v) |