3 * Implementation of git-cheetah compatibility functions for POSIX
5 * Copyright (C) Heiko Voigt, 2009
8 #include "../common/git-compat-util.h"
13 #include "../common/strbuf.h"
14 #include "../common/exec.h"
15 #include "../common/strbuf.h"
16 #include "../common/debug.h"
17 #include "../common/systeminfo.h"
21 int is_directory(const char *path
)
24 return !lstat(path
, &st
) && S_ISDIR(st
.st_mode
);
27 pid_t
fork_process(const char *cmd
, const char **args
, const char *wd
)
39 debug_git("chdir failed: %s", strerror(errno
));
41 path_variable
= getenv("PATH");
42 line_max
= strlen(path_variable
)+MAX_PATH
+2;
43 line
= xmalloc(line_max
*sizeof(char));
44 snprintf(line
, line_max
, "%s%s%s", path_variable
,
45 *(git_path()) ? ":" : "", git_path());
46 setenv("PATH", line
, 1);
48 snprintf(line
, line_max
, "%s", cmd
);
49 for (i
=1; args
[i
] && i
<MAX_ARGS
; i
++) {
50 strncat(line
, " ", 1024);
51 strncat(line
, args
[i
], 1024);
53 line
[line_max
-1] = '\0';
54 debug_git("starting child, wd: '%s' call: '%s'", wd
, line
);
56 /* We are already forked here and we usually never return from
57 * the next call so it does not matter wether we cast to (char**)
58 * to suppress warnings because we can not modify the callers
61 execvp(cmd
,(char **)args
);
63 /* here this code is done, if not something went wrong */
64 debug_git("execv failed: %s, Error: %s\n", cmd
, strerror(errno
));
65 exit(-ERR_RUN_COMMAND_FORK
);
68 int wait_for_process(pid_t pid
, int max_time
, int *errcode
)
71 if (waitpid(pid
, &stat_loc
, 0) < 0) {
72 debug_git("waitpid failed (%i): %s",errno
, strerror(errno
));
73 *errcode
= -ERR_RUN_COMMAND_WAITPID_NOEXIT
;
77 if (WIFEXITED(stat_loc
)) {
78 *errcode
= WEXITSTATUS(stat_loc
);
79 debug_git("Exit status: 0x%x", *errcode
);
84 if (WIFSIGNALED(stat_loc
) && WCOREDUMP(stat_loc
))
85 sprintf(errmsg
,"coredump");
86 else if (WIFSIGNALED(stat_loc
))
87 sprintf(errmsg
,"terminated due to signal %i", WTERMSIG(stat_loc
));
88 else if (WIFSTOPPED(stat_loc
))
89 sprintf(errmsg
,"stopped due to signal %i", WSTOPSIG(stat_loc
));
90 debug_git("[ERROR] child process failed: %s", errmsg
);