make wd_from_path platform independent
[git-cheetah/kirill.git] / compat / mingw.h
blob6448e26cb69f715275c4986e83fd838fed41907a
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 SIGKILL 0
25 #define SIGCHLD 0
26 #define SIGPIPE 0
27 #define SIGHUP 0
28 #define SIGQUIT 0
29 #define SIGALRM 100
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 #ifdef __GNUC__
42 /* in MSVC, it's defined in winsock2.h */
43 struct pollfd {
44 int fd; /* file descriptor */
45 short events; /* requested events */
46 short revents; /* returned events */
48 #define POLLIN 1
49 #define POLLHUP 2
50 #endif /* __GNUC__ */
52 typedef void (__cdecl *sig_handler_t)(int);
53 struct sigaction {
54 sig_handler_t sa_handler;
55 unsigned sa_flags;
57 #define sigemptyset(x) (void)0
58 #define SA_RESTART 0
60 struct itimerval {
61 struct timeval it_value, it_interval;
63 #define ITIMER_REAL 0
66 * trivial stubs
69 static inline int readlink(const char *path, char *buf, size_t bufsiz)
70 { errno = ENOSYS; return -1; }
71 static inline int symlink(const char *oldpath, const char *newpath)
72 { errno = ENOSYS; return -1; }
73 static inline int link(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)
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)
93 if (cmd == F_GETFD || cmd == F_SETFD)
94 return 0;
95 errno = EINVAL;
96 return -1;
100 * simple adaptors
103 static inline int mingw_mkdir(const char *path, int mode)
105 return mkdir(path);
107 #define mkdir mingw_mkdir
109 static inline int mingw_unlink(const char *pathname)
111 /* read-only files cannot be removed */
112 chmod(pathname, 0666);
113 return unlink(pathname);
115 #define unlink mingw_unlink
117 static inline int waitpid(pid_t pid, unsigned *status, unsigned options)
119 if (options == 0)
120 return _cwait(status, pid, 0);
121 errno = EINVAL;
122 return -1;
126 * implementations of missing functions
129 int pipe(int filedes[2]);
130 unsigned int sleep (unsigned int seconds);
131 int mkstemp(char *template);
132 int gettimeofday(struct timeval *tv, void *tz);
133 int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
134 struct tm *gmtime_r(const time_t *timep, struct tm *result);
135 struct tm *localtime_r(const time_t *timep, struct tm *result);
136 int getpagesize(void); /* defined in MinGW's libgcc.a */
137 struct passwd *getpwuid(int uid);
138 int setitimer(int type, struct itimerval *in, struct itimerval *out);
139 int sigaction(int sig, struct sigaction *in, struct sigaction *out);
142 * replacements of existing functions
145 int mingw_open (const char *filename, int oflags, ...);
146 #define open mingw_open
148 char *mingw_getcwd(char *pointer, int len);
149 #define getcwd mingw_getcwd
151 char *mingw_getenv(const char *name);
152 #define getenv mingw_getenv
154 struct hostent *mingw_gethostbyname(const char *host);
155 #define gethostbyname mingw_gethostbyname
157 int mingw_socket(int domain, int type, int protocol);
158 #define socket mingw_socket
160 int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
161 #define connect mingw_connect
163 int mingw_rename(const char*, const char*);
164 #define rename mingw_rename
166 /* Use mingw_lstat() instead of lstat()/stat() and
167 * mingw_fstat() instead of fstat() on Windows.
168 * struct stat is redefined because it lacks the st_blocks member.
170 struct mingw_stat {
171 unsigned st_mode;
172 time_t st_mtime, st_atime, st_ctime;
173 unsigned st_dev, st_ino, st_uid, st_gid;
174 size_t st_size;
175 size_t st_blocks;
177 int mingw_lstat(const char *file_name, struct mingw_stat *buf);
178 int mingw_fstat(int fd, struct mingw_stat *buf);
179 #define fstat mingw_fstat
180 #define lstat mingw_lstat
181 #define stat mingw_stat
182 static inline int mingw_stat(const char *file_name, struct mingw_stat *buf)
183 { return mingw_lstat(file_name, buf); }
185 int mingw_utime(const char *file_name, const struct utimbuf *times);
186 #define utime mingw_utime
188 pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env);
189 pid_t mingw_spawnvpe_cwd(const char *cmd, const char **argv, char **env,
190 const char *working_directory);
191 void mingw_execvp(const char *cmd, char *const *argv);
192 #define execvp mingw_execvp
194 static inline unsigned int git_ntohl(unsigned int x)
195 { return (unsigned int)ntohl(x); }
196 #define ntohl git_ntohl
198 sig_handler_t mingw_signal(int sig, sig_handler_t handler);
199 #define signal mingw_signal
202 * ANSI emulation wrappers
204 #ifndef _MSC_VER
205 /* Until va_copy available is MSVC */
206 int winansi_fputs(const char *str, FILE *stream);
207 int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
208 int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
209 #define fputs winansi_fputs
210 #define printf(...) winansi_printf(__VA_ARGS__)
211 #define fprintf(...) winansi_fprintf(__VA_ARGS__)
212 #endif
215 * git specific compatibility
218 #define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
219 #define is_dir_sep(c) ((c) == '/' || (c) == '\\')
220 #define PATH_SEP ';'
221 #define PRIuMAX "I64u"
223 void mingw_open_html(const char *path);
224 #define open_html mingw_open_html
227 * helpers
230 char **copy_environ(void);
231 void free_environ(char **env);
232 char **env_setenv(char **env, const char *name);
235 * A replacement of main() that ensures that argv[0] has a path
238 #define main(c,v) dummy_decl_mingw_main(); \
239 static int mingw_main(); \
240 int main(int argc, const char **argv) \
242 argv[0] = xstrdup(_pgmptr); \
243 return mingw_main(argc, argv); \
245 static int mingw_main(c,v)