1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/string.h>
10 #include "subcmd-util.h"
12 #include "subcmd-config.h"
17 static const char *argv_exec_path;
18 static const char *argv0_path;
20 void exec_cmd_init(const char *exec_name, const char *prefix,
21 const char *exec_path, const char *exec_path_env)
23 subcmd_config.exec_name = exec_name;
24 subcmd_config.prefix = prefix;
25 subcmd_config.exec_path = exec_path;
26 subcmd_config.exec_path_env = exec_path_env;
28 /* Setup environment variable for invoked shell script. */
29 setenv("PREFIX", prefix, 1);
32 #define is_dir_sep(c) ((c) == '/')
34 static int is_absolute_path(const char *path)
36 return path[0] == '/';
39 static const char *get_pwd_cwd(void)
41 static char cwd[PATH_MAX + 1];
43 struct stat cwd_stat, pwd_stat;
44 if (getcwd(cwd, PATH_MAX) == NULL)
47 if (pwd && strcmp(pwd, cwd)) {
49 if (!stat(pwd, &pwd_stat) &&
50 pwd_stat.st_dev == cwd_stat.st_dev &&
51 pwd_stat.st_ino == cwd_stat.st_ino) {
52 strlcpy(cwd, pwd, PATH_MAX);
58 static const char *make_nonrelative_path(const char *path)
60 static char buf[PATH_MAX + 1];
62 if (is_absolute_path(path)) {
63 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
64 die("Too long path: %.*s", 60, path);
66 const char *cwd = get_pwd_cwd();
68 die("Cannot determine the current working directory");
69 if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
70 die("Too long path: %.*s", 60, path);
75 char *system_path(const char *path)
79 if (is_absolute_path(path))
82 astrcatf(&buf, "%s/%s", subcmd_config.prefix, path);
87 const char *extract_argv0_path(const char *argv0)
91 if (!argv0 || !*argv0)
93 slash = argv0 + strlen(argv0);
95 while (argv0 <= slash && !is_dir_sep(*slash))
99 argv0_path = strndup(argv0, slash - argv0);
100 return argv0_path ? slash + 1 : NULL;
106 void set_argv_exec_path(const char *exec_path)
108 argv_exec_path = exec_path;
110 * Propagate this setting to external programs.
112 setenv(subcmd_config.exec_path_env, exec_path, 1);
116 /* Returns the highest-priority location to look for subprograms. */
117 char *get_argv_exec_path(void)
122 return strdup(argv_exec_path);
124 env = getenv(subcmd_config.exec_path_env);
128 return system_path(subcmd_config.exec_path);
131 static void add_path(char **out, const char *path)
134 if (is_absolute_path(path))
137 astrcat(out, make_nonrelative_path(path));
143 void setup_path(void)
145 const char *old_path = getenv("PATH");
146 char *new_path = NULL;
147 char *tmp = get_argv_exec_path();
149 add_path(&new_path, tmp);
150 add_path(&new_path, argv0_path);
154 astrcat(&new_path, old_path);
156 astrcat(&new_path, "/usr/local/bin:/usr/bin:/bin");
158 setenv("PATH", new_path, 1);
163 static const char **prepare_exec_cmd(const char **argv)
168 for (argc = 0; argv[argc]; argc++)
169 ; /* just counting */
170 nargv = malloc(sizeof(*nargv) * (argc + 2));
172 nargv[0] = subcmd_config.exec_name;
173 for (argc = 0; argv[argc]; argc++)
174 nargv[argc + 1] = argv[argc];
175 nargv[argc + 1] = NULL;
179 int execv_cmd(const char **argv) {
180 const char **nargv = prepare_exec_cmd(argv);
182 /* execvp() can only ever return if it fails */
183 execvp(subcmd_config.exec_name, (char **)nargv);
190 int execl_cmd(const char *cmd,...)
193 const char *argv[MAX_ARGS + 1];
197 va_start(param, cmd);
200 while (argc < MAX_ARGS) {
201 arg = argv[argc++] = va_arg(param, char *);
206 if (MAX_ARGS <= argc) {
207 fprintf(stderr, " Error: too many args to run %s\n", cmd);
212 return execv_cmd(argv);