libsubcmd: Avoid two path statics, removing 8192 bytes from .bss
Use a single stack allocated buffer and avoid 8,192 bytes in .bss. Signed-off-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: K Prateek Nayak <kprateek.nayak@amd.com> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Leo Yan <leo.yan@linaro.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Paolo Bonzini <pbonzini@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ravi Bangoria <ravi.bangoria@amd.com> Cc: Ross Zwisler <zwisler@chromium.org> Cc: Sean Christopherson <seanjc@google.com> Cc: Steven Rostedt (VMware) <rostedt@goodmis.org> Cc: Tiezhu Yang <yangtiezhu@loongson.cn> Cc: Yang Jihong <yangjihong1@huawei.com> Link: https://lore.kernel.org/r/20230526183401.2326121-17-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
parent
f50b8357f8
commit
2003237687
@ -36,38 +36,40 @@ static int is_absolute_path(const char *path)
|
|||||||
return path[0] == '/';
|
return path[0] == '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *get_pwd_cwd(void)
|
static const char *get_pwd_cwd(char *buf, size_t sz)
|
||||||
{
|
{
|
||||||
static char cwd[PATH_MAX + 1];
|
|
||||||
char *pwd;
|
char *pwd;
|
||||||
struct stat cwd_stat, pwd_stat;
|
struct stat cwd_stat, pwd_stat;
|
||||||
if (getcwd(cwd, PATH_MAX) == NULL)
|
if (getcwd(buf, sz) == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
pwd = getenv("PWD");
|
pwd = getenv("PWD");
|
||||||
if (pwd && strcmp(pwd, cwd)) {
|
if (pwd && strcmp(pwd, buf)) {
|
||||||
stat(cwd, &cwd_stat);
|
stat(buf, &cwd_stat);
|
||||||
if (!stat(pwd, &pwd_stat) &&
|
if (!stat(pwd, &pwd_stat) &&
|
||||||
pwd_stat.st_dev == cwd_stat.st_dev &&
|
pwd_stat.st_dev == cwd_stat.st_dev &&
|
||||||
pwd_stat.st_ino == cwd_stat.st_ino) {
|
pwd_stat.st_ino == cwd_stat.st_ino) {
|
||||||
strlcpy(cwd, pwd, PATH_MAX);
|
strlcpy(buf, pwd, sz);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return cwd;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *make_nonrelative_path(const char *path)
|
static const char *make_nonrelative_path(char *buf, size_t sz, const char *path)
|
||||||
{
|
{
|
||||||
static char buf[PATH_MAX + 1];
|
|
||||||
|
|
||||||
if (is_absolute_path(path)) {
|
if (is_absolute_path(path)) {
|
||||||
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
|
if (strlcpy(buf, path, sz) >= sz)
|
||||||
die("Too long path: %.*s", 60, path);
|
die("Too long path: %.*s", 60, path);
|
||||||
} else {
|
} else {
|
||||||
const char *cwd = get_pwd_cwd();
|
const char *cwd = get_pwd_cwd(buf, sz);
|
||||||
|
|
||||||
if (!cwd)
|
if (!cwd)
|
||||||
die("Cannot determine the current working directory");
|
die("Cannot determine the current working directory");
|
||||||
if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
|
|
||||||
|
if (strlen(cwd) + strlen(path) + 2 >= sz)
|
||||||
die("Too long path: %.*s", 60, path);
|
die("Too long path: %.*s", 60, path);
|
||||||
|
|
||||||
|
strcat(buf, "/");
|
||||||
|
strcat(buf, path);
|
||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
@ -133,8 +135,11 @@ static void add_path(char **out, const char *path)
|
|||||||
if (path && *path) {
|
if (path && *path) {
|
||||||
if (is_absolute_path(path))
|
if (is_absolute_path(path))
|
||||||
astrcat(out, path);
|
astrcat(out, path);
|
||||||
else
|
else {
|
||||||
astrcat(out, make_nonrelative_path(path));
|
char buf[PATH_MAX];
|
||||||
|
|
||||||
|
astrcat(out, make_nonrelative_path(buf, sizeof(buf), path));
|
||||||
|
}
|
||||||
|
|
||||||
astrcat(out, ":");
|
astrcat(out, ":");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user