1 #include "../git-compat-util.h"
3 unsigned int _CRT_fmode = _O_BINARY;
6 int mingw_open (const char *filename, int oflags, ...)
10 va_start(args, oflags);
11 mode = va_arg(args, int);
14 if (!strcmp(filename, "/dev/null"))
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))
25 unsigned int sleep (unsigned int seconds)
31 int mkstemp(char *template)
33 char *filename = mktemp(template);
36 return open(filename, O_RDWR | O_CREAT, 0600);
39 int gettimeofday(struct timeval *tv, void *tz)
44 tm.tm_year = st.wYear-1900;
45 tm.tm_mon = st.wMonth-1;
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);
53 tv->tv_usec = st.wMilliseconds*1000;
57 int pipe(int filedes[2])
62 if (_pipe(filedes, 8192, 0) < 0)
65 parent = GetCurrentProcess();
67 if (!DuplicateHandle (parent, (HANDLE)_get_osfhandle(filedes[0]),
68 parent, &h[0], 0, FALSE, DUPLICATE_SAME_ACCESS)) {
73 if (!DuplicateHandle (parent, (HANDLE)_get_osfhandle(filedes[1]),
74 parent, &h[1], 0, FALSE, DUPLICATE_SAME_ACCESS)) {
80 fd = _open_osfhandle((int)h[0], O_NOINHERIT);
90 fd = _open_osfhandle((int)h[1], O_NOINHERIT);
102 int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
107 struct tm *gmtime_r(const time_t *timep, struct tm *result)
109 /* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */
110 memcpy(result, gmtime(timep), sizeof(struct tm));
114 struct tm *localtime_r(const time_t *timep, struct tm *result)
116 /* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */
117 memcpy(result, localtime(timep), sizeof(struct tm));
122 char *mingw_getcwd(char *pointer, int len)
125 char *ret = getcwd(pointer, len);
128 for (i = 0; pointer[i]; i++)
129 if (pointer[i] == '\\')
134 static const char *parse_interpreter(const char *cmd)
136 static char buf[100];
140 /* don't even try a .exe */
142 if (n >= 4 && !strcasecmp(cmd+n-4, ".exe"))
145 fd = open(cmd, O_RDONLY);
148 n = read(fd, buf, sizeof(buf)-1);
150 if (n < 4) /* at least '#!/x' and not error */
153 if (buf[0] != '#' || buf[1] != '!')
156 p = strchr(buf, '\n');
161 if (!(p = strrchr(buf+2, '/')) && !(p = strrchr(buf+2, '\\')))
164 if ((opt = strchr(p+1, ' ')))
170 * Splits the PATH into parts.
172 static char **get_path_split(void)
174 char *p, **path, *envpath = getenv("PATH");
177 if (!envpath || !*envpath)
180 envpath = xstrdup(envpath);
186 if (*dir) { /* not earlier, catches series of ; */
193 path = xmalloc((n+1)*sizeof(char*));
198 path[i++] = xstrdup(p);
208 static void free_path_split(char **path)
220 * exe_only means that we only want to detect .exe files, but not scripts
221 * (which do not have an extension)
223 static char *lookup_prog(const char *dir, const char *cmd, int isexe, int exe_only)
226 snprintf(path, sizeof(path), "%s/%s.exe", dir, cmd);
228 if (!isexe && access(path, F_OK) == 0)
229 return xstrdup(path);
230 path[strlen(path)-4] = '\0';
231 if ((!exe_only || isexe) && access(path, F_OK) == 0)
232 return xstrdup(path);
237 * Determines the absolute path of cmd using the the split path in path.
238 * If cmd contains a slash or backslash, no lookup is performed.
240 static char *path_lookup(const char *cmd, char **path, int exe_only)
243 int len = strlen(cmd);
244 int isexe = len >= 4 && !strcasecmp(cmd+len-4, ".exe");
246 if (strchr(cmd, '/') || strchr(cmd, '\\'))
249 while (!prog && *path)
250 prog = lookup_prog(*path++, cmd, isexe, exe_only);
255 static int try_shell_exec(const char *cmd, char *const *argv, char **env)
257 const char *interpr = parse_interpreter(cmd);
264 path = get_path_split();
265 prog = path_lookup(interpr, path, 1);
269 while (argv[argc]) argc++;
270 argv2 = xmalloc(sizeof(*argv) * (argc+2));
271 argv2[0] = (char *)interpr;
272 argv2[1] = (char *)cmd; /* full path to the script file */
273 memcpy(&argv2[2], &argv[1], sizeof(*argv) * argc);
274 pid = spawnve(_P_NOWAIT, prog, argv2, (const char **)env);
277 if (waitpid(pid, &status, 0) < 0)
281 pid = 1; /* indicate that we tried but failed */
285 free_path_split(path);
289 static void mingw_execve(const char *cmd, char *const *argv, char *const *env)
291 /* check if git_command is a shell script */
292 if (!try_shell_exec(cmd, argv, (char **)env)) {
295 pid = spawnve(_P_NOWAIT, cmd, (const char **)argv, (const char **)env);
298 if (waitpid(pid, &status, 0) < 0)
304 void mingw_execvp(const char *cmd, char *const *argv)
306 char **path = get_path_split();
307 char *prog = path_lookup(cmd, path, 0);
310 mingw_execve(prog, argv, environ);
315 free_path_split(path);
319 int mingw_rename(const char *pold, const char *pnew)
322 * Try native rename() first to get errno right.
323 * It is based on MoveFile(), which cannot overwrite existing files.
325 if (!rename(pold, pnew))
329 if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
331 /* TODO: translate more errors */
332 if (GetLastError() == ERROR_ACCESS_DENIED) {
333 DWORD attrs = GetFileAttributes(pnew);
334 if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) {
343 struct passwd *getpwuid(int uid)
345 static char user_name[100];
346 static struct passwd p;
348 DWORD len = sizeof(user_name);
349 if (!GetUserName(user_name, &len))
351 p.pw_name = user_name;
352 p.pw_gecos = "unknown";
357 static HANDLE timer_event;
358 static HANDLE timer_thread;
359 static int timer_interval;
361 static sig_handler_t timer_fn = SIG_DFL;
363 /* The timer works like this:
364 * The thread, ticktack(), is a trivial routine that most of the time
365 * only waits to receive the signal to terminate. The main thread tells
366 * the thread to terminate by setting the timer_event to the signalled
368 * But ticktack() interrupts the wait state after the timer's interval
369 * length to call the signal handler.
372 static __stdcall unsigned ticktack(void *dummy)
374 while (WaitForSingleObject(timer_event, timer_interval) == WAIT_TIMEOUT) {
375 if (timer_fn == SIG_DFL)
377 if (timer_fn != SIG_IGN)
385 static int start_timer_thread(void)
387 timer_event = CreateEvent(NULL, FALSE, FALSE, NULL);
389 timer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);
391 return errno = ENOMEM,
392 error("cannot start timer thread");
394 return errno = ENOMEM,
395 error("cannot allocate resources for timer");
399 static void stop_timer_thread(void)
402 SetEvent(timer_event); /* tell thread to terminate */
404 int rc = WaitForSingleObject(timer_thread, 1000);
405 if (rc == WAIT_TIMEOUT)
406 error("timer thread did not terminate timely");
407 else if (rc != WAIT_OBJECT_0)
408 error("waiting for timer thread failed: %lu",
410 CloseHandle(timer_thread);
413 CloseHandle(timer_event);
418 static inline int is_timeval_eq(const struct timeval *i1, const struct timeval *i2)
420 return i1->tv_sec == i2->tv_sec && i1->tv_usec == i2->tv_usec;
423 int setitimer(int type, struct itimerval *in, struct itimerval *out)
425 static const struct timeval zero;
426 static int atexit_done;
429 return errno = EINVAL,
430 error("setitimer param 3 != NULL not implemented");
431 if (!is_timeval_eq(&in->it_interval, &zero) &&
432 !is_timeval_eq(&in->it_interval, &in->it_value))
433 return errno = EINVAL,
434 error("setitimer: it_interval must be zero or eq it_value");
439 if (is_timeval_eq(&in->it_value, &zero) &&
440 is_timeval_eq(&in->it_interval, &zero))
443 timer_interval = in->it_value.tv_sec * 1000 + in->it_value.tv_usec / 1000;
444 one_shot = is_timeval_eq(&in->it_interval, &zero);
446 atexit(stop_timer_thread);
449 return start_timer_thread();
452 int sigaction(int sig, struct sigaction *in, struct sigaction *out)
455 return errno = EINVAL,
456 error("sigaction only implemented for SIGALRM");
458 return errno = EINVAL,
459 error("sigaction: param 3 != NULL not implemented");
461 timer_fn = in->sa_handler;
466 sig_handler_t mingw_signal(int sig, sig_handler_t handler)
469 return signal(sig, handler);
470 sig_handler_t old = timer_fn;