diff options
author | Josh Poimboeuf <jpoimboe@redhat.com> | 2015-12-15 09:39:38 -0600 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2015-12-17 14:27:10 -0300 |
commit | 2f4ce5ec1d447beb42143a9653716a2ab025161e (patch) | |
tree | 97c7c5342f217383a7fefc579ad16f8da62bb6a2 /tools/perf/util/exec_cmd.c | |
parent | 46113a54be53aea50a4f5926b87e86e2e66c4266 (diff) | |
download | linux-2f4ce5ec1d447beb42143a9653716a2ab025161e.tar.gz linux-2f4ce5ec1d447beb42143a9653716a2ab025161e.tar.bz2 linux-2f4ce5ec1d447beb42143a9653716a2ab025161e.zip |
perf tools: Finalize subcmd independence
For the files that will be moved to the subcmd library, remove all their
perf-specific includes and duplicate any needed functionality.
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/6e12946f0f26ce4d543d34db68d9dae3c8551cb9.1450193761.git.jpoimboe@redhat.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/exec_cmd.c')
-rw-r--r-- | tools/perf/util/exec_cmd.c | 64 |
1 files changed, 57 insertions, 7 deletions
diff --git a/tools/perf/util/exec_cmd.c b/tools/perf/util/exec_cmd.c index 701111ac7699..e7f9ed7943e3 100644 --- a/tools/perf/util/exec_cmd.c +++ b/tools/perf/util/exec_cmd.c @@ -1,12 +1,17 @@ -#include "cache.h" -#include "exec_cmd.h" -#include "quote.h" -#include "subcmd-config.h" - +#include <linux/compiler.h> +#include <linux/string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> #include <string.h> +#include <stdlib.h> +#include <stdio.h> #include "subcmd-util.h" +#include "exec_cmd.h" +#include "subcmd-config.h" #define MAX_ARGS 32 +#define PATH_MAX 4096 static const char *argv_exec_path; static const char *argv0_path; @@ -20,6 +25,49 @@ void exec_cmd_init(const char *exec_name, const char *prefix, subcmd_config.exec_path_env = exec_path_env; } +#define is_dir_sep(c) ((c) == '/') + +static int is_absolute_path(const char *path) +{ + return path[0] == '/'; +} + +static const char *get_pwd_cwd(void) +{ + static char cwd[PATH_MAX + 1]; + char *pwd; + struct stat cwd_stat, pwd_stat; + if (getcwd(cwd, PATH_MAX) == NULL) + return NULL; + pwd = getenv("PWD"); + if (pwd && strcmp(pwd, cwd)) { + stat(cwd, &cwd_stat); + if (!stat(pwd, &pwd_stat) && + pwd_stat.st_dev == cwd_stat.st_dev && + pwd_stat.st_ino == cwd_stat.st_ino) { + strlcpy(cwd, pwd, PATH_MAX); + } + } + return cwd; +} + +static const char *make_nonrelative_path(const char *path) +{ + static char buf[PATH_MAX + 1]; + + if (is_absolute_path(path)) { + if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) + die("Too long path: %.*s", 60, path); + } else { + const char *cwd = get_pwd_cwd(); + if (!cwd) + die("Cannot determine the current working directory"); + if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX) + die("Too long path: %.*s", 60, path); + } + return buf; +} + char *system_path(const char *path) { char *buf = NULL; @@ -151,8 +199,10 @@ int execl_cmd(const char *cmd,...) break; } va_end(param); - if (MAX_ARGS <= argc) - return error("too many args to run %s", cmd); + if (MAX_ARGS <= argc) { + fprintf(stderr, " Error: too many args to run %s\n", cmd); + return -1; + } argv[argc] = NULL; return execv_cmd(argv); |