Code

Merge branch 'jl/submodule-add-noname'
[git.git] / compat / mingw.h
1 #include <winsock2.h>
3 /*
4  * things that are not available in header files
5  */
7 typedef int pid_t;
8 #define hstrerror strerror
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
20 #define WIFEXITED(x) 1
21 #define WIFSIGNALED(x) 0
22 #define WEXITSTATUS(x) ((x) & 0xff)
23 #define WTERMSIG(x) SIGTERM
25 #define SIGHUP 1
26 #define SIGQUIT 3
27 #define SIGKILL 9
28 #define SIGPIPE 13
29 #define SIGALRM 14
30 #define SIGCHLD 17
32 #define F_GETFD 1
33 #define F_SETFD 2
34 #define FD_CLOEXEC 0x1
36 struct passwd {
37         char *pw_name;
38         char *pw_gecos;
39         char *pw_dir;
40 };
42 extern char *getpass(const char *prompt);
44 #ifndef POLLIN
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
52 #endif
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
62 struct itimerval {
63         struct timeval it_value, it_interval;
64 };
65 #define ITIMER_REAL 0
67 /*
68  * trivial stubs
69  */
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; }
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 }
98 /* bash cannot reliably detect negative return codes as failure */
99 #define exit(code) exit((code) & 0xff)
101 /*
102  * simple adaptors
103  */
105 static inline int mingw_mkdir(const char *path, int mode)
107         return mkdir(path);
109 #define mkdir mingw_mkdir
111 static inline int mingw_unlink(const char *pathname)
113         /* read-only files cannot be removed */
114         chmod(pathname, 0666);
115         return unlink(pathname);
117 #define unlink mingw_unlink
119 static inline int waitpid(pid_t pid, int *status, unsigned options)
121         if (options == 0)
122                 return _cwait(status, pid, 0);
123         errno = EINVAL;
124         return -1;
127 #ifndef NO_OPENSSL
128 #include <openssl/ssl.h>
129 static inline int mingw_SSL_set_fd(SSL *ssl, int fd)
131         return SSL_set_fd(ssl, _get_osfhandle(fd));
133 #define SSL_set_fd mingw_SSL_set_fd
135 static inline int mingw_SSL_set_rfd(SSL *ssl, int fd)
137         return SSL_set_rfd(ssl, _get_osfhandle(fd));
139 #define SSL_set_rfd mingw_SSL_set_rfd
141 static inline int mingw_SSL_set_wfd(SSL *ssl, int fd)
143         return SSL_set_wfd(ssl, _get_osfhandle(fd));
145 #define SSL_set_wfd mingw_SSL_set_wfd
146 #endif
148 /*
149  * implementations of missing functions
150  */
152 int pipe(int filedes[2]);
153 unsigned int sleep (unsigned int seconds);
154 int mkstemp(char *template);
155 int gettimeofday(struct timeval *tv, void *tz);
156 int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
157 struct tm *gmtime_r(const time_t *timep, struct tm *result);
158 struct tm *localtime_r(const time_t *timep, struct tm *result);
159 int getpagesize(void);  /* defined in MinGW's libgcc.a */
160 struct passwd *getpwuid(int uid);
161 int setitimer(int type, struct itimerval *in, struct itimerval *out);
162 int sigaction(int sig, struct sigaction *in, struct sigaction *out);
163 int link(const char *oldpath, const char *newpath);
165 /*
166  * replacements of existing functions
167  */
169 int mingw_open (const char *filename, int oflags, ...);
170 #define open mingw_open
172 char *mingw_getcwd(char *pointer, int len);
173 #define getcwd mingw_getcwd
175 char *mingw_getenv(const char *name);
176 #define getenv mingw_getenv
178 struct hostent *mingw_gethostbyname(const char *host);
179 #define gethostbyname mingw_gethostbyname
181 int mingw_socket(int domain, int type, int protocol);
182 #define socket mingw_socket
184 int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
185 #define connect mingw_connect
187 int mingw_rename(const char*, const char*);
188 #define rename mingw_rename
190 #ifdef USE_WIN32_MMAP
191 int mingw_getpagesize(void);
192 #define getpagesize mingw_getpagesize
193 #endif
195 /* Use mingw_lstat() instead of lstat()/stat() and
196  * mingw_fstat() instead of fstat() on Windows.
197  */
198 #define off_t off64_t
199 #define stat _stati64
200 #define lseek _lseeki64
201 int mingw_lstat(const char *file_name, struct stat *buf);
202 int mingw_fstat(int fd, struct stat *buf);
203 #define fstat mingw_fstat
204 #define lstat mingw_lstat
205 #define _stati64(x,y) mingw_lstat(x,y)
207 int mingw_utime(const char *file_name, const struct utimbuf *times);
208 #define utime mingw_utime
210 pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env);
211 void mingw_execvp(const char *cmd, char *const *argv);
212 #define execvp mingw_execvp
214 static inline unsigned int git_ntohl(unsigned int x)
215 { return (unsigned int)ntohl(x); }
216 #define ntohl git_ntohl
218 sig_handler_t mingw_signal(int sig, sig_handler_t handler);
219 #define signal mingw_signal
221 /*
222  * ANSI emulation wrappers
223  */
225 int winansi_fputs(const char *str, FILE *stream);
226 int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
227 int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
228 #define fputs winansi_fputs
229 #define printf(...) winansi_printf(__VA_ARGS__)
230 #define fprintf(...) winansi_fprintf(__VA_ARGS__)
232 /*
233  * git specific compatibility
234  */
236 #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
237 #define is_dir_sep(c) ((c) == '/' || (c) == '\\')
238 #define PATH_SEP ';'
239 #define PRIuMAX "I64u"
241 void mingw_open_html(const char *path);
242 #define open_html mingw_open_html
244 /*
245  * helpers
246  */
248 char **make_augmented_environ(const char *const *vars);
249 void free_environ(char **env);
251 /*
252  * A replacement of main() that ensures that argv[0] has a path
253  * and that default fmode and std(in|out|err) are in binary mode
254  */
256 #define main(c,v) dummy_decl_mingw_main(); \
257 static int mingw_main(); \
258 int main(int argc, const char **argv) \
259 { \
260         _fmode = _O_BINARY; \
261         _setmode(_fileno(stdin), _O_BINARY); \
262         _setmode(_fileno(stdout), _O_BINARY); \
263         _setmode(_fileno(stderr), _O_BINARY); \
264         argv[0] = xstrdup(_pgmptr); \
265         return mingw_main(argc, argv); \
266 } \
267 static int mingw_main(c,v)
269 #ifndef NO_MINGW_REPLACE_READDIR
270 /*
271  * A replacement of readdir, to ensure that it reads the file type at
272  * the same time. This avoid extra unneeded lstats in git on MinGW
273  */
274 #undef DT_UNKNOWN
275 #undef DT_DIR
276 #undef DT_REG
277 #undef DT_LNK
278 #define DT_UNKNOWN      0
279 #define DT_DIR          1
280 #define DT_REG          2
281 #define DT_LNK          3
283 struct mingw_dirent
285         long            d_ino;                  /* Always zero. */
286         union {
287                 unsigned short  d_reclen;       /* Always zero. */
288                 unsigned char   d_type;         /* Reimplementation adds this */
289         };
290         unsigned short  d_namlen;               /* Length of name in d_name. */
291         char            d_name[FILENAME_MAX];   /* File name. */
292 };
293 #define dirent mingw_dirent
294 #define readdir(x) mingw_readdir(x)
295 struct dirent *mingw_readdir(DIR *dir);
296 #endif // !NO_MINGW_REPLACE_READDIR