Remove unused variable prefix
[git-cheetah/kirill.git] / compat / posix.c
blobf159d7e4ae336938c87ea43d8f580ade6023d0c3
1 /* posix.c
3 * Implementation of git-cheetah compatibility functions for POSIX
5 * Copyright (C) Heiko Voigt, 2009
7 */
8 #include "../common/git-compat-util.h"
9 #include <dirent.h>
10 #include <unistd.h>
11 #include <sys/wait.h>
12 #include <stdlib.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"
19 #define MAX_ARGS 32
21 int is_directory(const char *path)
23 struct stat st;
24 return !lstat(path, &st) && S_ISDIR(st.st_mode);
27 pid_t fork_process(const char *cmd, const char **args, const char *wd)
29 pid_t pid;
30 char *line;
31 char *path_variable;
32 int i, line_max;
34 pid = fork();
35 if (pid)
36 return pid;
38 if (chdir(wd) < 0)
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);
55 free(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
59 * values anyway.
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)
70 int stat_loc;
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;
74 return -1;
77 if (WIFEXITED(stat_loc)) {
78 *errcode = WEXITSTATUS(stat_loc);
79 debug_git("Exit status: 0x%x", *errcode);
80 return 1;
81 } else {
82 char errmsg[1024];
83 *errcode = -1;
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);
91 return -1;
93 return 0;