include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / tools / perf / util / exec_cmd.c
blob7adf4ad15d8fcfd4c90d1efbe72f2594e47a3575
1 #include "cache.h"
2 #include "exec_cmd.h"
3 #include "quote.h"
5 #include <string.h>
7 #define MAX_ARGS 32
9 static const char *argv_exec_path;
10 static const char *argv0_path;
12 const char *system_path(const char *path)
14 static const char *prefix = PREFIX;
15 struct strbuf d = STRBUF_INIT;
17 if (is_absolute_path(path))
18 return path;
20 strbuf_addf(&d, "%s/%s", prefix, path);
21 path = strbuf_detach(&d, NULL);
22 return path;
25 const char *perf_extract_argv0_path(const char *argv0)
27 const char *slash;
29 if (!argv0 || !*argv0)
30 return NULL;
31 slash = argv0 + strlen(argv0);
33 while (argv0 <= slash && !is_dir_sep(*slash))
34 slash--;
36 if (slash >= argv0) {
37 argv0_path = strndup(argv0, slash - argv0);
38 return argv0_path ? slash + 1 : NULL;
41 return argv0;
44 void perf_set_argv_exec_path(const char *exec_path)
46 argv_exec_path = exec_path;
48 * Propagate this setting to external programs.
50 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
54 /* Returns the highest-priority, location to look for perf programs. */
55 const char *perf_exec_path(void)
57 const char *env;
59 if (argv_exec_path)
60 return argv_exec_path;
62 env = getenv(EXEC_PATH_ENVIRONMENT);
63 if (env && *env) {
64 return env;
67 return system_path(PERF_EXEC_PATH);
70 static void add_path(struct strbuf *out, const char *path)
72 if (path && *path) {
73 if (is_absolute_path(path))
74 strbuf_addstr(out, path);
75 else
76 strbuf_addstr(out, make_nonrelative_path(path));
78 strbuf_addch(out, PATH_SEP);
82 void setup_path(void)
84 const char *old_path = getenv("PATH");
85 struct strbuf new_path = STRBUF_INIT;
87 add_path(&new_path, perf_exec_path());
88 add_path(&new_path, argv0_path);
90 if (old_path)
91 strbuf_addstr(&new_path, old_path);
92 else
93 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
95 setenv("PATH", new_path.buf, 1);
97 strbuf_release(&new_path);
100 static const char **prepare_perf_cmd(const char **argv)
102 int argc;
103 const char **nargv;
105 for (argc = 0; argv[argc]; argc++)
106 ; /* just counting */
107 nargv = malloc(sizeof(*nargv) * (argc + 2));
109 nargv[0] = "perf";
110 for (argc = 0; argv[argc]; argc++)
111 nargv[argc + 1] = argv[argc];
112 nargv[argc + 1] = NULL;
113 return nargv;
116 int execv_perf_cmd(const char **argv) {
117 const char **nargv = prepare_perf_cmd(argv);
119 /* execvp() can only ever return if it fails */
120 execvp("perf", (char **)nargv);
122 free(nargv);
123 return -1;
127 int execl_perf_cmd(const char *cmd,...)
129 int argc;
130 const char *argv[MAX_ARGS + 1];
131 const char *arg;
132 va_list param;
134 va_start(param, cmd);
135 argv[0] = cmd;
136 argc = 1;
137 while (argc < MAX_ARGS) {
138 arg = argv[argc++] = va_arg(param, char *);
139 if (!arg)
140 break;
142 va_end(param);
143 if (MAX_ARGS <= argc)
144 return error("too many args to run %s", cmd);
146 argv[argc] = NULL;
147 return execv_perf_cmd(argv);