diff --git a/src/builddir.h b/src/builddir.h index 84aa8f68..a927b084 100644 --- a/src/builddir.h +++ b/src/builddir.h @@ -30,10 +30,10 @@ /** * Try to figure out the directory we're executing from and if it matches - * the builddir, return that directory. Otherwise, return NULL. + * the builddir true and set builddir (if not NULL) to the given builddir. */ -static inline char * -builddir_lookup(void) +static inline bool +builddir_lookup(char **builddir) { char execdir[PATH_MAX]; char *pathsep; @@ -42,11 +42,11 @@ builddir_lookup(void) /* In the case of release builds, the builddir is the empty string */ if (streq(MESON_BUILD_ROOT, "")) - return NULL; + return false; nread = readlink("/proc/self/exe", execdir, sizeof(execdir) - 1); if (nread <= 0 || nread == sizeof(execdir) - 1) - return NULL; + return false; /* readlink doesn't terminate the string and readlink says anything past sz is undefined */ @@ -54,11 +54,14 @@ builddir_lookup(void) pathsep = strrchr(execdir, '/'); if (!pathsep) - return NULL; + return false; *pathsep = '\0'; if (!streq(execdir, MESON_BUILD_ROOT)) - return NULL; + return false; - return safe_strdup(execdir); + if (builddir) + *builddir = safe_strdup(execdir); + + return true; } diff --git a/test/litest.c b/test/litest.c index 51ccb457..2fcca29c 100644 --- a/test/litest.c +++ b/test/litest.c @@ -4961,16 +4961,13 @@ litest_parse_argv(int argc, char **argv, int *njobs_out) JOBS_SINGLE, JOBS_CUSTOM } want_jobs = JOBS_DEFAULT; - char *builddir; char *jobs_env; int jobs = 0; /* If we are not running from the builddir, we assume we're running * against the system as installed */ - builddir = builddir_lookup(); - if (!builddir) + if (!builddir_lookup(NULL)) use_system_rules_quirks = true; - free(builddir); if (in_debugger) want_jobs = JOBS_SINGLE; diff --git a/test/test-builddir-lookup.c b/test/test-builddir-lookup.c index 348daaa8..4817fed6 100644 --- a/test/test-builddir-lookup.c +++ b/test/test-builddir-lookup.c @@ -28,21 +28,24 @@ int main(int argc, char **argv) { - char *builddir; + char *builddir = NULL; char *mode; assert(argc == 2); mode = argv[1]; - builddir = builddir_lookup(); + bool is_builddir = builddir_lookup(&builddir); if (streq(mode, "--builddir-is-null")) { + assert(!is_builddir); assert(builddir == NULL); } else if (streq(mode, "--builddir-is-set")) { /* In the case of release builds, the builddir is the empty string */ if (streq(MESON_BUILD_ROOT, "")) { + assert(!is_builddir); assert(builddir == NULL); } else { + assert(is_builddir); assert(builddir); assert(streq(MESON_BUILD_ROOT, builddir)); } diff --git a/tools/libinput-quirks.c b/tools/libinput-quirks.c index cc5de8bc..a41fc3b5 100644 --- a/tools/libinput-quirks.c +++ b/tools/libinput-quirks.c @@ -166,10 +166,8 @@ main(int argc, char **argv) /* Overriding the data dir means no custom override file */ if (!data_path) { - char *builddir = builddir_lookup(); - if (builddir) { + if (builddir_lookup(NULL)) { data_path = LIBINPUT_QUIRKS_SRCDIR; - free(builddir); } else { data_path = LIBINPUT_QUIRKS_DIR; override_file = LIBINPUT_QUIRKS_OVERRIDE_FILE; diff --git a/tools/libinput-record.c b/tools/libinput-record.c index de4c8481..cbb5b4cb 100644 --- a/tools/libinput-record.c +++ b/tools/libinput-record.c @@ -1799,19 +1799,16 @@ print_device_quirks(struct record_device *dev) struct quirks_context *quirks; const char *data_path = LIBINPUT_QUIRKS_DIR; const char *override_file = LIBINPUT_QUIRKS_OVERRIDE_FILE; - char *builddir = NULL; if (stat(dev->devnode, &st) < 0) return; - if ((builddir = builddir_lookup())) { + if (builddir_lookup(NULL)) { setenv("LIBINPUT_QUIRKS_DIR", LIBINPUT_QUIRKS_SRCDIR, 0); data_path = LIBINPUT_QUIRKS_SRCDIR; override_file = NULL; } - free(builddir); - quirks = quirks_init_subsystem(data_path, override_file, quirks_log_handler, diff --git a/tools/shared.c b/tools/shared.c index e5a4dfe7..22c522e8 100644 --- a/tools/shared.c +++ b/tools/shared.c @@ -538,10 +538,8 @@ tools_open_device(const char **paths, bool verbose, bool *grab) static void tools_setenv_quirks_dir(void) { - char *builddir = builddir_lookup(); - if (builddir) { + if (builddir_lookup(NULL)) { setenv("LIBINPUT_QUIRKS_DIR", LIBINPUT_QUIRKS_SRCDIR, 0); - free(builddir); } } @@ -765,8 +763,9 @@ setup_path(void) const char *path = getenv("PATH"); char new_path[PATH_MAX]; const char *extra_path = LIBINPUT_TOOL_PATH; - char *builddir = builddir_lookup(); + char *builddir = NULL; + builddir_lookup(&builddir); snprintf(new_path, sizeof(new_path), "%s:%s",