Commit 20032376 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo
Browse files

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: default avatarIan 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: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent f50b8357
Loading
Loading
Loading
Loading
+20 −15
Original line number Diff line number Diff line
@@ -36,38 +36,40 @@ static int is_absolute_path(const char *path)
	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;
	struct stat cwd_stat, pwd_stat;
	if (getcwd(cwd, PATH_MAX) == NULL)
	if (getcwd(buf, sz) == NULL)
		return NULL;
	pwd = getenv("PWD");
	if (pwd && strcmp(pwd, cwd)) {
		stat(cwd, &cwd_stat);
	if (pwd && strcmp(pwd, buf)) {
		stat(buf, &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);
			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 (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
		if (strlcpy(buf, path, sz) >= sz)
			die("Too long path: %.*s", 60, path);
	} else {
		const char *cwd = get_pwd_cwd();
		const char *cwd = get_pwd_cwd(buf, sz);

		if (!cwd)
			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);

		strcat(buf, "/");
		strcat(buf, path);
	}
	return buf;
}
@@ -133,8 +135,11 @@ static void add_path(char **out, const char *path)
	if (path && *path) {
		if (is_absolute_path(path))
			astrcat(out, path);
		else
			astrcat(out, make_nonrelative_path(path));
		else {
			char buf[PATH_MAX];

			astrcat(out, make_nonrelative_path(buf, sizeof(buf), path));
		}

		astrcat(out, ":");
	}