Add ANSI control code emulation for the Windows console
[git/git.git] / compat / mingw.h
CommitLineData
f4626df5
JS
1#include <winsock2.h>
2
3/*
4 * things that are not available in header files
5 */
6
7typedef 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
35struct passwd {
36 char *pw_name;
37 char *pw_gecos;
38 char *pw_dir;
39};
40
41struct 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
49typedef void (__cdecl *sig_handler_t)(int);
50struct sigaction {
51 sig_handler_t sa_handler;
52 unsigned sa_flags;
53};
54#define sigemptyset(x) (void)0
55#define SA_RESTART 0
56
57struct itimerval {
58 struct timeval it_value, it_interval;
59};
60#define ITIMER_REAL 0
61
f4626df5
JS
62/*
63 * trivial stubs
64 */
65
66static inline int readlink(const char *path, char *buf, size_t bufsiz)
67{ errno = ENOSYS; return -1; }
68static inline int symlink(const char *oldpath, const char *newpath)
69{ errno = ENOSYS; return -1; }
70static inline int link(const char *oldpath, const char *newpath)
71{ errno = ENOSYS; return -1; }
72static inline int fchmod(int fildes, mode_t mode)
73{ errno = ENOSYS; return -1; }
74static inline int fork(void)
75{ errno = ENOSYS; return -1; }
76static inline unsigned int alarm(unsigned int seconds)
77{ return 0; }
78static inline int fsync(int fd)
79{ return 0; }
80static inline int getppid(void)
81{ return 1; }
82static inline void sync(void)
83{}
84static inline int getuid()
85{ return 1; }
86static inline struct passwd *getpwnam(const char *name)
87{ return NULL; }
88static 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
100static inline int mingw_mkdir(const char *path, int mode)
101{
102 return mkdir(path);
103}
104#define mkdir mingw_mkdir
105
132a6e90
JS
106static 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
f4626df5
JS
114static 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
f4626df5
JS
122/*
123 * implementations of missing functions
124 */
125
897bb8cb 126int pipe(int filedes[2]);
f4626df5
JS
127unsigned int sleep (unsigned int seconds);
128int mkstemp(char *template);
129int gettimeofday(struct timeval *tv, void *tz);
130int poll(struct pollfd *ufds, unsigned int nfds, int timeout);
131struct tm *gmtime_r(const time_t *timep, struct tm *result);
132struct tm *localtime_r(const time_t *timep, struct tm *result);
133int getpagesize(void); /* defined in MinGW's libgcc.a */
134struct passwd *getpwuid(int uid);
135int setitimer(int type, struct itimerval *in, struct itimerval *out);
136int sigaction(int sig, struct sigaction *in, struct sigaction *out);
80ba074f 137
25fe217b
JS
138/*
139 * replacements of existing functions
140 */
141
3e4a1ba0
JS
142int mingw_open (const char *filename, int oflags, ...);
143#define open mingw_open
144
25fe217b
JS
145char *mingw_getcwd(char *pointer, int len);
146#define getcwd mingw_getcwd
147
6fd6aec4
JS
148char *mingw_getenv(const char *name);
149#define getenv mingw_getenv
150
746fb857
JS
151struct hostent *mingw_gethostbyname(const char *host);
152#define gethostbyname mingw_gethostbyname
153
154int mingw_socket(int domain, int type, int protocol);
155#define socket mingw_socket
156
157int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz);
158#define connect mingw_connect
159
ea9e98c3
JS
160int mingw_rename(const char*, const char*);
161#define rename mingw_rename
162
5411bdc4
MSO
163/* Use mingw_lstat() instead of lstat()/stat() and
164 * mingw_fstat() instead of fstat() on Windows.
fc2ded5b 165 * struct stat is redefined because it lacks the st_blocks member.
5411bdc4 166 */
fc2ded5b
JS
167struct mingw_stat {
168 unsigned st_mode;
169 time_t st_mtime, st_atime, st_ctime;
170 unsigned st_dev, st_ino, st_uid, st_gid;
171 size_t st_size;
172 size_t st_blocks;
173};
174int mingw_lstat(const char *file_name, struct mingw_stat *buf);
175int mingw_fstat(int fd, struct mingw_stat *buf);
5411bdc4
MSO
176#define fstat mingw_fstat
177#define lstat mingw_lstat
fc2ded5b
JS
178#define stat mingw_stat
179static inline int mingw_stat(const char *file_name, struct mingw_stat *buf)
180{ return mingw_lstat(file_name, buf); }
5411bdc4 181
7c0ffa1c
JS
182int mingw_utime(const char *file_name, const struct utimbuf *times);
183#define utime mingw_utime
184
7e5d7768 185pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **env);
f1a4dfb8
JS
186void mingw_execvp(const char *cmd, char *const *argv);
187#define execvp mingw_execvp
188
cd800eec
SP
189static inline unsigned int git_ntohl(unsigned int x)
190{ return (unsigned int)ntohl(x); }
191#define ntohl git_ntohl
192
6072fc31
JS
193sig_handler_t mingw_signal(int sig, sig_handler_t handler);
194#define signal mingw_signal
195
c09df8a7
PH
196/*
197 * ANSI emulation wrappers
198 */
199
200int winansi_fputs(const char *str, FILE *stream);
201int winansi_printf(const char *format, ...) __attribute__((format (printf, 1, 2)));
202int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format (printf, 2, 3)));
203#define fputs winansi_fputs
204#define printf(...) winansi_printf(__VA_ARGS__)
205#define fprintf(...) winansi_fprintf(__VA_ARGS__)
206
80ba074f
JS
207/*
208 * git specific compatibility
209 */
210
25fe217b
JS
211#define has_dos_drive_prefix(path) (isalpha(*(path)) && (path)[1] == ':')
212#define is_dir_sep(c) ((c) == '/' || (c) == '\\')
80ba074f 213#define PATH_SEP ';'
82f8d969 214#define PRIuMAX "I64u"
ba26f296 215
4804aabc
SP
216void mingw_open_html(const char *path);
217#define open_html mingw_open_html
218
ba26f296
JS
219/*
220 * helpers
221 */
222
223char **copy_environ(void);
224void free_environ(char **env);
225char **env_setenv(char **env, const char *name);