]>
Commit | Line | Data |
---|---|---|
f4626df5 JS |
1 | #include "../git-compat-util.h" |
2 | ||
3 | unsigned int _CRT_fmode = _O_BINARY; | |
4 | ||
3e4a1ba0 JS |
5 | #undef open |
6 | int mingw_open (const char *filename, int oflags, ...) | |
7 | { | |
8 | va_list args; | |
9 | unsigned mode; | |
10 | va_start(args, oflags); | |
11 | mode = va_arg(args, int); | |
12 | va_end(args); | |
13 | ||
14 | if (!strcmp(filename, "/dev/null")) | |
15 | filename = "nul"; | |
16 | int fd = open(filename, oflags, mode); | |
17 | if (fd < 0 && (oflags & O_CREAT) && errno == EACCES) { | |
18 | DWORD attrs = GetFileAttributes(filename); | |
19 | if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) | |
20 | errno = EISDIR; | |
21 | } | |
22 | return fd; | |
23 | } | |
24 | ||
f4626df5 JS |
25 | unsigned int sleep (unsigned int seconds) |
26 | { | |
27 | Sleep(seconds*1000); | |
28 | return 0; | |
29 | } | |
30 | ||
31 | int mkstemp(char *template) | |
32 | { | |
33 | char *filename = mktemp(template); | |
34 | if (filename == NULL) | |
35 | return -1; | |
36 | return open(filename, O_RDWR | O_CREAT, 0600); | |
37 | } | |
38 | ||
39 | int gettimeofday(struct timeval *tv, void *tz) | |
40 | { | |
a42a0c2e JS |
41 | SYSTEMTIME st; |
42 | struct tm tm; | |
43 | GetSystemTime(&st); | |
44 | tm.tm_year = st.wYear-1900; | |
45 | tm.tm_mon = st.wMonth-1; | |
46 | tm.tm_mday = st.wDay; | |
47 | tm.tm_hour = st.wHour; | |
48 | tm.tm_min = st.wMinute; | |
49 | tm.tm_sec = st.wSecond; | |
50 | tv->tv_sec = tm_to_time_t(&tm); | |
51 | if (tv->tv_sec < 0) | |
52 | return -1; | |
53 | tv->tv_usec = st.wMilliseconds*1000; | |
54 | return 0; | |
f4626df5 JS |
55 | } |
56 | ||
57 | int poll(struct pollfd *ufds, unsigned int nfds, int timeout) | |
58 | { | |
59 | return -1; | |
60 | } | |
61 | ||
62 | struct tm *gmtime_r(const time_t *timep, struct tm *result) | |
63 | { | |
64 | /* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */ | |
65 | memcpy(result, gmtime(timep), sizeof(struct tm)); | |
66 | return result; | |
67 | } | |
68 | ||
69 | struct tm *localtime_r(const time_t *timep, struct tm *result) | |
70 | { | |
71 | /* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */ | |
72 | memcpy(result, localtime(timep), sizeof(struct tm)); | |
73 | return result; | |
74 | } | |
75 | ||
25fe217b JS |
76 | #undef getcwd |
77 | char *mingw_getcwd(char *pointer, int len) | |
78 | { | |
79 | int i; | |
80 | char *ret = getcwd(pointer, len); | |
81 | if (!ret) | |
82 | return ret; | |
83 | for (i = 0; pointer[i]; i++) | |
84 | if (pointer[i] == '\\') | |
85 | pointer[i] = '/'; | |
86 | return ret; | |
87 | } | |
88 | ||
f1a4dfb8 JS |
89 | static const char *parse_interpreter(const char *cmd) |
90 | { | |
91 | static char buf[100]; | |
92 | char *p, *opt; | |
93 | int n, fd; | |
94 | ||
95 | /* don't even try a .exe */ | |
96 | n = strlen(cmd); | |
97 | if (n >= 4 && !strcasecmp(cmd+n-4, ".exe")) | |
98 | return NULL; | |
99 | ||
100 | fd = open(cmd, O_RDONLY); | |
101 | if (fd < 0) | |
102 | return NULL; | |
103 | n = read(fd, buf, sizeof(buf)-1); | |
104 | close(fd); | |
105 | if (n < 4) /* at least '#!/x' and not error */ | |
106 | return NULL; | |
107 | ||
108 | if (buf[0] != '#' || buf[1] != '!') | |
109 | return NULL; | |
110 | buf[n] = '\0'; | |
111 | p = strchr(buf, '\n'); | |
112 | if (!p) | |
113 | return NULL; | |
114 | ||
115 | *p = '\0'; | |
116 | if (!(p = strrchr(buf+2, '/')) && !(p = strrchr(buf+2, '\\'))) | |
117 | return NULL; | |
118 | /* strip options */ | |
119 | if ((opt = strchr(p+1, ' '))) | |
120 | *opt = '\0'; | |
121 | return p+1; | |
122 | } | |
123 | ||
124 | /* | |
125 | * Splits the PATH into parts. | |
126 | */ | |
127 | static char **get_path_split(void) | |
128 | { | |
129 | char *p, **path, *envpath = getenv("PATH"); | |
130 | int i, n = 0; | |
131 | ||
132 | if (!envpath || !*envpath) | |
133 | return NULL; | |
134 | ||
135 | envpath = xstrdup(envpath); | |
136 | p = envpath; | |
137 | while (p) { | |
138 | char *dir = p; | |
139 | p = strchr(p, ';'); | |
140 | if (p) *p++ = '\0'; | |
141 | if (*dir) { /* not earlier, catches series of ; */ | |
142 | ++n; | |
143 | } | |
144 | } | |
145 | if (!n) | |
146 | return NULL; | |
147 | ||
148 | path = xmalloc((n+1)*sizeof(char*)); | |
149 | p = envpath; | |
150 | i = 0; | |
151 | do { | |
152 | if (*p) | |
153 | path[i++] = xstrdup(p); | |
154 | p = p+strlen(p)+1; | |
155 | } while (i < n); | |
156 | path[i] = NULL; | |
157 | ||
158 | free(envpath); | |
159 | ||
160 | return path; | |
161 | } | |
162 | ||
163 | static void free_path_split(char **path) | |
164 | { | |
165 | if (!path) | |
166 | return; | |
167 | ||
168 | char **p = path; | |
169 | while (*p) | |
170 | free(*p++); | |
171 | free(path); | |
172 | } | |
173 | ||
174 | /* | |
175 | * exe_only means that we only want to detect .exe files, but not scripts | |
176 | * (which do not have an extension) | |
177 | */ | |
178 | static char *lookup_prog(const char *dir, const char *cmd, int isexe, int exe_only) | |
179 | { | |
180 | char path[MAX_PATH]; | |
181 | snprintf(path, sizeof(path), "%s/%s.exe", dir, cmd); | |
182 | ||
183 | if (!isexe && access(path, F_OK) == 0) | |
184 | return xstrdup(path); | |
185 | path[strlen(path)-4] = '\0'; | |
186 | if ((!exe_only || isexe) && access(path, F_OK) == 0) | |
187 | return xstrdup(path); | |
188 | return NULL; | |
189 | } | |
190 | ||
191 | /* | |
192 | * Determines the absolute path of cmd using the the split path in path. | |
193 | * If cmd contains a slash or backslash, no lookup is performed. | |
194 | */ | |
195 | static char *path_lookup(const char *cmd, char **path, int exe_only) | |
196 | { | |
197 | char *prog = NULL; | |
198 | int len = strlen(cmd); | |
199 | int isexe = len >= 4 && !strcasecmp(cmd+len-4, ".exe"); | |
200 | ||
201 | if (strchr(cmd, '/') || strchr(cmd, '\\')) | |
202 | prog = xstrdup(cmd); | |
203 | ||
204 | while (!prog && *path) | |
205 | prog = lookup_prog(*path++, cmd, isexe, exe_only); | |
206 | ||
207 | return prog; | |
208 | } | |
209 | ||
210 | static int try_shell_exec(const char *cmd, char *const *argv, char **env) | |
211 | { | |
212 | const char *interpr = parse_interpreter(cmd); | |
213 | char **path; | |
214 | char *prog; | |
215 | int pid = 0; | |
216 | ||
217 | if (!interpr) | |
218 | return 0; | |
219 | path = get_path_split(); | |
220 | prog = path_lookup(interpr, path, 1); | |
221 | if (prog) { | |
222 | int argc = 0; | |
223 | const char **argv2; | |
224 | while (argv[argc]) argc++; | |
225 | argv2 = xmalloc(sizeof(*argv) * (argc+2)); | |
226 | argv2[0] = (char *)interpr; | |
227 | argv2[1] = (char *)cmd; /* full path to the script file */ | |
228 | memcpy(&argv2[2], &argv[1], sizeof(*argv) * argc); | |
229 | pid = spawnve(_P_NOWAIT, prog, argv2, (const char **)env); | |
230 | if (pid >= 0) { | |
231 | int status; | |
232 | if (waitpid(pid, &status, 0) < 0) | |
233 | status = 255; | |
234 | exit(status); | |
235 | } | |
236 | pid = 1; /* indicate that we tried but failed */ | |
237 | free(prog); | |
238 | free(argv2); | |
239 | } | |
240 | free_path_split(path); | |
241 | return pid; | |
242 | } | |
243 | ||
244 | static void mingw_execve(const char *cmd, char *const *argv, char *const *env) | |
245 | { | |
246 | /* check if git_command is a shell script */ | |
247 | if (!try_shell_exec(cmd, argv, (char **)env)) { | |
248 | int pid, status; | |
249 | ||
250 | pid = spawnve(_P_NOWAIT, cmd, (const char **)argv, (const char **)env); | |
251 | if (pid < 0) | |
252 | return; | |
253 | if (waitpid(pid, &status, 0) < 0) | |
254 | status = 255; | |
255 | exit(status); | |
256 | } | |
257 | } | |
258 | ||
259 | void mingw_execvp(const char *cmd, char *const *argv) | |
260 | { | |
261 | char **path = get_path_split(); | |
262 | char *prog = path_lookup(cmd, path, 0); | |
263 | ||
264 | if (prog) { | |
265 | mingw_execve(prog, argv, environ); | |
266 | free(prog); | |
267 | } else | |
268 | errno = ENOENT; | |
269 | ||
270 | free_path_split(path); | |
271 | } | |
272 | ||
ea9e98c3 JS |
273 | #undef rename |
274 | int mingw_rename(const char *pold, const char *pnew) | |
275 | { | |
276 | /* | |
277 | * Try native rename() first to get errno right. | |
278 | * It is based on MoveFile(), which cannot overwrite existing files. | |
279 | */ | |
280 | if (!rename(pold, pnew)) | |
281 | return 0; | |
282 | if (errno != EEXIST) | |
283 | return -1; | |
284 | if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING)) | |
285 | return 0; | |
286 | /* TODO: translate more errors */ | |
287 | if (GetLastError() == ERROR_ACCESS_DENIED) { | |
288 | DWORD attrs = GetFileAttributes(pnew); | |
289 | if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) { | |
290 | errno = EISDIR; | |
291 | return -1; | |
292 | } | |
293 | } | |
294 | errno = EACCES; | |
295 | return -1; | |
296 | } | |
297 | ||
f4626df5 JS |
298 | struct passwd *getpwuid(int uid) |
299 | { | |
f7597aca | 300 | static char user_name[100]; |
f4626df5 | 301 | static struct passwd p; |
f7597aca JS |
302 | |
303 | DWORD len = sizeof(user_name); | |
304 | if (!GetUserName(user_name, &len)) | |
305 | return NULL; | |
306 | p.pw_name = user_name; | |
307 | p.pw_gecos = "unknown"; | |
308 | p.pw_dir = NULL; | |
f4626df5 JS |
309 | return &p; |
310 | } | |
311 | ||
6072fc31 JS |
312 | static HANDLE timer_event; |
313 | static HANDLE timer_thread; | |
314 | static int timer_interval; | |
315 | static int one_shot; | |
316 | static sig_handler_t timer_fn = SIG_DFL; | |
317 | ||
318 | /* The timer works like this: | |
319 | * The thread, ticktack(), is a trivial routine that most of the time | |
320 | * only waits to receive the signal to terminate. The main thread tells | |
321 | * the thread to terminate by setting the timer_event to the signalled | |
322 | * state. | |
323 | * But ticktack() interrupts the wait state after the timer's interval | |
324 | * length to call the signal handler. | |
325 | */ | |
326 | ||
327 | static __stdcall unsigned ticktack(void *dummy) | |
328 | { | |
329 | while (WaitForSingleObject(timer_event, timer_interval) == WAIT_TIMEOUT) { | |
330 | if (timer_fn == SIG_DFL) | |
331 | die("Alarm"); | |
332 | if (timer_fn != SIG_IGN) | |
333 | timer_fn(SIGALRM); | |
334 | if (one_shot) | |
335 | break; | |
336 | } | |
337 | return 0; | |
338 | } | |
339 | ||
340 | static int start_timer_thread(void) | |
341 | { | |
342 | timer_event = CreateEvent(NULL, FALSE, FALSE, NULL); | |
343 | if (timer_event) { | |
344 | timer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL); | |
345 | if (!timer_thread ) | |
346 | return errno = ENOMEM, | |
347 | error("cannot start timer thread"); | |
348 | } else | |
349 | return errno = ENOMEM, | |
350 | error("cannot allocate resources for timer"); | |
351 | return 0; | |
352 | } | |
353 | ||
354 | static void stop_timer_thread(void) | |
355 | { | |
356 | if (timer_event) | |
357 | SetEvent(timer_event); /* tell thread to terminate */ | |
358 | if (timer_thread) { | |
359 | int rc = WaitForSingleObject(timer_thread, 1000); | |
360 | if (rc == WAIT_TIMEOUT) | |
361 | error("timer thread did not terminate timely"); | |
362 | else if (rc != WAIT_OBJECT_0) | |
363 | error("waiting for timer thread failed: %lu", | |
364 | GetLastError()); | |
365 | CloseHandle(timer_thread); | |
366 | } | |
367 | if (timer_event) | |
368 | CloseHandle(timer_event); | |
369 | timer_event = NULL; | |
370 | timer_thread = NULL; | |
371 | } | |
372 | ||
373 | static inline int is_timeval_eq(const struct timeval *i1, const struct timeval *i2) | |
374 | { | |
375 | return i1->tv_sec == i2->tv_sec && i1->tv_usec == i2->tv_usec; | |
376 | } | |
377 | ||
f4626df5 JS |
378 | int setitimer(int type, struct itimerval *in, struct itimerval *out) |
379 | { | |
6072fc31 JS |
380 | static const struct timeval zero; |
381 | static int atexit_done; | |
382 | ||
383 | if (out != NULL) | |
384 | return errno = EINVAL, | |
385 | error("setitimer param 3 != NULL not implemented"); | |
386 | if (!is_timeval_eq(&in->it_interval, &zero) && | |
387 | !is_timeval_eq(&in->it_interval, &in->it_value)) | |
388 | return errno = EINVAL, | |
389 | error("setitimer: it_interval must be zero or eq it_value"); | |
390 | ||
391 | if (timer_thread) | |
392 | stop_timer_thread(); | |
393 | ||
394 | if (is_timeval_eq(&in->it_value, &zero) && | |
395 | is_timeval_eq(&in->it_interval, &zero)) | |
396 | return 0; | |
397 | ||
398 | timer_interval = in->it_value.tv_sec * 1000 + in->it_value.tv_usec / 1000; | |
399 | one_shot = is_timeval_eq(&in->it_interval, &zero); | |
400 | if (!atexit_done) { | |
401 | atexit(stop_timer_thread); | |
402 | atexit_done = 1; | |
403 | } | |
404 | return start_timer_thread(); | |
f4626df5 JS |
405 | } |
406 | ||
407 | int sigaction(int sig, struct sigaction *in, struct sigaction *out) | |
408 | { | |
6072fc31 JS |
409 | if (sig != SIGALRM) |
410 | return errno = EINVAL, | |
411 | error("sigaction only implemented for SIGALRM"); | |
412 | if (out != NULL) | |
413 | return errno = EINVAL, | |
414 | error("sigaction: param 3 != NULL not implemented"); | |
415 | ||
416 | timer_fn = in->sa_handler; | |
417 | return 0; | |
418 | } | |
419 | ||
420 | #undef signal | |
421 | sig_handler_t mingw_signal(int sig, sig_handler_t handler) | |
422 | { | |
423 | if (sig != SIGALRM) | |
424 | return signal(sig, handler); | |
425 | sig_handler_t old = timer_fn; | |
426 | timer_fn = handler; | |
427 | return old; | |
f4626df5 | 428 | } |