MinGW: use off64_t and lseek64()
[4msysgit-hv.git] / compat / mingw.h
blobbfdfdb4940b375dc096553859591eb37b8c3f480
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) ((unsigned)(x) < 259) /* STILL_ACTIVE */
21 #define WEXITSTATUS(x) ((x) & 0xff)
22 #define WIFSIGNALED(x) ((unsigned)(x) > 259)
24 #define SIGHUP 1
25 #define SIGQUIT 3
26 #define SIGKILL 9
27 #define SIGPIPE 13
28 #define SIGALRM 14
29 #define SIGCHLD 17
31 #define F_GETFD 1
32 #define F_SETFD 2
33 #define FD_CLOEXEC 0x1
35 struct passwd {
36 char *pw_name;
37 char *pw_gecos;
38 char *pw_dir;
41 struct pollfd {
42 int fd; /* file descriptor */
43 short events; /* requested events */
44 short revents; /* returned events */
46 #define POLLIN 1
47 #define POLLHUP 2
49 typedef void (__cdecl *sig_handler_t)(int);
50 struct sigaction {
51 sig_handler_t sa_handler;
52 unsigned sa_flags;
54 #define sigemptyset(x) (void)0
55 #define SA_RESTART 0
57 struct itimerval {
58 struct timeval it_value, it_interval;
60 #define ITIMER_REAL 0
63 * trivial stubs
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 fchmod(int fildes, mode_t mode)
71 { errno = ENOSYS; return -1; }
72 static inline int fork(void)
73 { errno = ENOSYS; return -1; }
74 static inline unsigned int alarm(unsigned int seconds)
75 { return 0; }
76 static inline int fsync(int fd)
77 { return 0; }
78 static inline int getppid(void)
79 { return 1; }
80 static inline void sync(void)
82 static inline int getuid()
83 { return 1; }
84 static inline struct passwd *getpwnam(const char *name)
85 { return NULL; }
86 static inline int fcntl(int fd, int cmd, long arg)
88 if (cmd == F_GETFD || cmd == F_SETFD)
89 return 0;
90 errno = EINVAL;
91 return -1;
95 * simple adaptors
98 static inline int mingw_mkdir(const char *path, int mode)
100 return mkdir(path);
102 #define mkdir mingw_mkdir
104 static inline int mingw_unlink(const char *pathname)
106 /* read-only files cannot be removed */
107 chmod(pathname, 0666);
108 return unlink(pathname);
110 #define unlink mingw_unlink
112 static inline int waitpid(pid_t pid, int *status, unsigned options)
114 if (options == 0)
115 return _cwait(status, pid, 0);
116 errno = EINVAL;
117 return -1;
120 #define off_t off64_t
121 #define lseek lseek64
124 * implementations of missing functions
127 int pipe(int filedes[2]);
128 unsigned int sleep (unsigned int seconds);
129 int mkstemp(char *template);
130 int gettimeofday(struct timeval *tv, void *tz);
131 int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
132 struct tm *gmtime_r(const time_t *timep, struct tm *result);
133 struct tm *localtime_r(const time_t *timep, struct tm *result);
134 int getpagesize(void); /* defined in MinGW's libgcc.a */
135 struct passwd *getpwuid(int uid);
136 int setitimer(int type, struct itimerval *in, struct itimerval *out);
137 int sigaction(int sig, struct sigaction *in, struct sigaction *out);
138 int link(const char *oldpath, const char *newpath);
141 * replacements of existing functions
144 int mingw_open (const char *filename, int oflags, ...);
145 #define open mingw_open
147 char *mingw_getcwd(char *pointer, int len);
148 #define getcwd mingw_getcwd
150 char *mingw_getenv(const char *name);
151 #define getenv mingw_getenv
153 struct hostent *mingw_gethostbyname(const char *host);
154 #define gethostbyname mingw_gethostbyname
156 int mingw_socket(int domain, int type, int protocol);
157 #define socket mingw_socket
159 int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
160 #define connect mingw_connect
162 int mingw_rename(const char*, const char*);
163 #define rename mingw_rename
165 /* Use mingw_lstat() instead of lstat()/stat() and
166 * mingw_fstat() instead of fstat() on Windows.
168 int mingw_lstat(const char *file_name, struct stat *buf);
169 int mingw_fstat(int fd, struct stat *buf);
170 #define fstat mingw_fstat
171 #define lstat mingw_lstat
172 #define stat(x,y) mingw_lstat(x,y)
174 int mingw_utime(const char *file_name, const struct utimbuf *times);
175 #define utime mingw_utime
177 pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env);
178 void mingw_execvp(const char *cmd, char *const *argv);
179 #define execvp mingw_execvp
181 static inline unsigned int git_ntohl(unsigned int x)
182 { return (unsigned int)ntohl(x); }
183 #define ntohl git_ntohl
185 sig_handler_t mingw_signal(int sig, sig_handler_t handler);
186 #define signal mingw_signal
189 * ANSI emulation wrappers
192 int winansi_fputs(const char *str, FILE *stream);
193 int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
194 int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
195 #define fputs winansi_fputs
196 #define printf(...) winansi_printf(__VA_ARGS__)
197 #define fprintf(...) winansi_fprintf(__VA_ARGS__)
200 * git specific compatibility
203 #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
204 #define is_dir_sep(c) ((c) == '/' || (c) == '\\')
205 #define PATH_SEP ';'
206 #if !defined(__GNUC__) || __GNUC__ < 3
207 #define PRIuMAX "I64u"
208 #endif
210 void mingw_open_html(const char *path);
211 #define open_html mingw_open_html
214 * helpers
217 char **copy_environ(void);
218 void free_environ(char **env);
219 char **env_setenv(char **env, const char *name);
222 * A replacement of main() that ensures that argv[0] has a path
225 #define main(c,v) dummy_decl_mingw_main(); \
226 static int mingw_main(); \
227 int main(int argc, const char **argv) \
229 argv[0] = xstrdup(_pgmptr); \
230 return mingw_main(argc, argv); \
232 static int mingw_main(c,v)