4 #include "subcmd-config.h"
10 static const char *argv_exec_path
;
11 static const char *argv0_path
;
13 void exec_cmd_init(const char *exec_name
, const char *prefix
,
14 const char *exec_path
, const char *exec_path_env
)
16 subcmd_config
.exec_name
= exec_name
;
17 subcmd_config
.prefix
= prefix
;
18 subcmd_config
.exec_path
= exec_path
;
19 subcmd_config
.exec_path_env
= exec_path_env
;
22 char *system_path(const char *path
)
24 struct strbuf d
= STRBUF_INIT
;
26 if (is_absolute_path(path
))
29 strbuf_addf(&d
, "%s/%s", subcmd_config
.prefix
, path
);
30 path
= strbuf_detach(&d
, NULL
);
34 const char *perf_extract_argv0_path(const char *argv0
)
38 if (!argv0
|| !*argv0
)
40 slash
= argv0
+ strlen(argv0
);
42 while (argv0
<= slash
&& !is_dir_sep(*slash
))
46 argv0_path
= strndup(argv0
, slash
- argv0
);
47 return argv0_path
? slash
+ 1 : NULL
;
53 void perf_set_argv_exec_path(const char *exec_path
)
55 argv_exec_path
= exec_path
;
57 * Propagate this setting to external programs.
59 setenv(subcmd_config
.exec_path_env
, exec_path
, 1);
63 /* Returns the highest-priority, location to look for perf programs. */
64 char *perf_exec_path(void)
69 return strdup(argv_exec_path
);
71 env
= getenv(subcmd_config
.exec_path_env
);
75 return system_path(subcmd_config
.exec_path
);
78 static void add_path(struct strbuf
*out
, const char *path
)
81 if (is_absolute_path(path
))
82 strbuf_addstr(out
, path
);
84 strbuf_addstr(out
, make_nonrelative_path(path
));
86 strbuf_addch(out
, PATH_SEP
);
92 const char *old_path
= getenv("PATH");
93 struct strbuf new_path
= STRBUF_INIT
;
94 char *tmp
= perf_exec_path();
96 add_path(&new_path
, tmp
);
97 add_path(&new_path
, argv0_path
);
101 strbuf_addstr(&new_path
, old_path
);
103 strbuf_addstr(&new_path
, "/usr/local/bin:/usr/bin:/bin");
105 setenv("PATH", new_path
.buf
, 1);
107 strbuf_release(&new_path
);
110 static const char **prepare_perf_cmd(const char **argv
)
115 for (argc
= 0; argv
[argc
]; argc
++)
116 ; /* just counting */
117 nargv
= malloc(sizeof(*nargv
) * (argc
+ 2));
119 nargv
[0] = subcmd_config
.exec_name
;
120 for (argc
= 0; argv
[argc
]; argc
++)
121 nargv
[argc
+ 1] = argv
[argc
];
122 nargv
[argc
+ 1] = NULL
;
126 int execv_perf_cmd(const char **argv
) {
127 const char **nargv
= prepare_perf_cmd(argv
);
129 /* execvp() can only ever return if it fails */
130 execvp(subcmd_config
.exec_name
, (char **)nargv
);
137 int execl_perf_cmd(const char *cmd
,...)
140 const char *argv
[MAX_ARGS
+ 1];
144 va_start(param
, cmd
);
147 while (argc
< MAX_ARGS
) {
148 arg
= argv
[argc
++] = va_arg(param
, char *);
153 if (MAX_ARGS
<= argc
)
154 return error("too many args to run %s", cmd
);
157 return execv_perf_cmd(argv
);
This page took 0.053966 seconds and 5 git commands to generate.