summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2018-07-17 10:12:16 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2018-07-17 10:36:08 +1000
commitad5d2fef7202e07581c1a3464ff54ada8565282b (patch)
tree6984efe77a05615cbaa7aeaa929c17ce3bbdddd5
parent8362031064e8c788b1645488002e0a556ea0466b (diff)
tools: change prototype of the builddir lookup function
Only one place really needs the return argument, so we might as well just pass the memory to be returned in. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
-rw-r--r--tools/libinput-quirks.c6
-rw-r--r--tools/shared.c48
-rw-r--r--tools/shared.h5
-rw-r--r--tools/test-builddir-lookup.c15
4 files changed, 44 insertions, 30 deletions
diff --git a/tools/libinput-quirks.c b/tools/libinput-quirks.c
index b9c38142..7952d351 100644
--- a/tools/libinput-quirks.c
+++ b/tools/libinput-quirks.c
@@ -162,12 +162,8 @@ main(int argc, char **argv)
/* Overriding the data dir means no custom override file */
if (!data_path) {
- char *builddir;
-
- builddir = tools_execdir_is_builddir();
- if (builddir) {
+ if (tools_execdir_is_builddir(NULL, 0)) {
data_path = LIBINPUT_QUIRKS_SRCDIR;
- free(builddir);
} else {
data_path = LIBINPUT_QUIRKS_DIR;
override_file = LIBINPUT_QUIRKS_OVERRIDE_FILE;
diff --git a/tools/shared.c b/tools/shared.c
index b2f895cb..c36ee136 100644
--- a/tools/shared.c
+++ b/tools/shared.c
@@ -467,38 +467,51 @@ out:
return is_touchpad;
}
-/* Try to read the directory we're executing from and if it matches the
- * builddir, return it as path. Otherwise, return NULL.
+/**
+ * Try to read the directory we're executing from and if it matches the
+ * builddir, return it.
+ *
+ * @param execdir_out If not NULL, set to the exec directory
+ * @param sz Size of execdir_out
+ *
+ * @return true if the execdir is the builddir, false otherwise.
+ *
+ * If execdir_out is NULL and szt is 0, it merely returns true/false.
*/
-char *
-tools_execdir_is_builddir(void)
+bool
+tools_execdir_is_builddir(char *execdir_out, size_t sz)
{
char execdir[PATH_MAX] = {0};
char *pathsep;
- ssize_t sz;
+ ssize_t nread;
/* In the case of release builds, the builddir is
the empty string */
if (streq(MESON_BUILD_ROOT, ""))
- return NULL;
+ return false;
- sz = readlink("/proc/self/exe", execdir, sizeof(execdir) - 1);
- if (sz <= 0 || sz == sizeof(execdir) - 1)
- return NULL;
+ nread = readlink("/proc/self/exe", execdir, sizeof(execdir) - 1);
+ if (nread <= 0 || nread == sizeof(execdir) - 1)
+ return false;
/* readlink doesn't terminate the string and readlink says
anything past sz is undefined */
- execdir[sz + 1] = '\0';
+ execdir[++nread] = '\0';
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 (sz > 0) {
+ assert(execdir_out != NULL);
+ assert(sz >= (size_t)nread);
+ snprintf(execdir_out, nread, "%s", execdir);
+ }
+ return true;
}
static inline void
@@ -506,17 +519,18 @@ setup_path(void)
{
const char *path = getenv("PATH");
char new_path[PATH_MAX];
- char *builddir;
+ char builddir[PATH_MAX];
+ const char *extra_path = LIBINPUT_TOOL_PATH;
- builddir = tools_execdir_is_builddir();
+ if (tools_execdir_is_builddir(builddir, sizeof(builddir)))
+ extra_path = builddir;
snprintf(new_path,
sizeof(new_path),
"%s:%s",
- builddir ? builddir : LIBINPUT_TOOL_PATH,
+ extra_path,
path ? path : "");
setenv("PATH", new_path, 1);
- free(builddir);
}
int
diff --git a/tools/shared.h b/tools/shared.h
index 364babe6..7888d265 100644
--- a/tools/shared.h
+++ b/tools/shared.h
@@ -25,6 +25,7 @@
#define _SHARED_H_
#include <stdbool.h>
+#include <limits.h>
#include <quirks.h>
#include <libinput.h>
@@ -119,7 +120,7 @@ tools_list_device_quirks(struct quirks_context *ctx,
void (*callback)(void *userdata, const char *str),
void *userdata);
-char *
-tools_execdir_is_builddir(void);
+bool
+tools_execdir_is_builddir(char *execdir_out, size_t sz);
#endif
diff --git a/tools/test-builddir-lookup.c b/tools/test-builddir-lookup.c
index 821ca70f..db746f5c 100644
--- a/tools/test-builddir-lookup.c
+++ b/tools/test-builddir-lookup.c
@@ -26,25 +26,28 @@
#include "shared.h"
int main(int argc, char **argv) {
- char *builddir;
+ char builddir[PATH_MAX];
char *mode;
+ bool rc, rc2;
assert(argc == 2);
mode = argv[1];
- builddir = tools_execdir_is_builddir();
+ rc = tools_execdir_is_builddir(builddir, sizeof(builddir));
+ rc2 = tools_execdir_is_builddir(NULL, 0);
if (streq(mode, "--builddir-is-null")) {
- assert(builddir == NULL);
+ assert(rc == false);
+ assert(rc == rc2);
} 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(builddir == NULL);
+ assert(rc == false);
} else {
- assert(builddir != NULL);
+ assert(rc == true);
assert(streq(MESON_BUILD_ROOT, builddir));
- free(builddir);
}
+ assert(rc == rc2);
} else {
abort();
}