Revert "Tune test scripts to use <<\EOF where possible"
[git/pclouds.git] / builtin-box.c
blob9a861f1ac6da0e35cf130207b634af7a8052e97a
1 #include <ctype.h>
2 #include "cache.h"
4 int gitbox_main(int argc, char **argv);
6 static void prepend_to_path(const char *dir, int len)
8 const char *old_path = getenv("PATH");
9 char *path;
10 int path_len = len;
12 if (!old_path)
13 old_path = "/usr/local/bin:/usr/bin:/bin";
15 path_len = len + strlen(old_path) + 1;
17 path = xmalloc(path_len + 1);
19 memcpy(path, dir, len);
20 #ifdef __MINGW32__
21 path[len] = ';';
22 #else
23 path[len] = ':';
24 #endif
25 memcpy(path + len + 1, old_path, path_len - len);
27 setenv("PATH", path, 1);
29 free(path);
32 int cmd_box(int argc, const char **argv, const char *prefix)
34 const char *cmd = argv[0];
35 char *slash = strrchr(cmd, '/');
36 const char *exec_path = NULL;
39 * Take the basename of argv[0] as the command
40 * name, and the dirname as the default exec_path
41 * if it's an absolute path and we don't have
42 * anything better.
44 if (slash) {
45 *slash++ = 0;
46 if (*cmd == '/')
47 exec_path = cmd;
48 cmd = slash;
51 #ifdef __MINGW32__
52 slash = strrchr(cmd, '\\');
53 if (slash) {
54 *slash++ = 0;
55 if (cmd[1] == ':')
56 exec_path = cmd;
57 cmd = slash;
59 #endif
62 * We search for git commands in the following order:
63 * - git_exec_path()
64 * - the path of the "git" command if we could find it
65 * in $0
66 * - the regular PATH.
68 if (exec_path)
69 prepend_to_path(exec_path, strlen(exec_path));
70 exec_path = git_exec_path();
71 prepend_to_path(exec_path, strlen(exec_path));
73 return gitbox_main(argc, argv);