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>
This commit is contained in:
Peter Hutterer 2018-07-17 10:12:16 +10:00
parent 8362031064
commit ad5d2fef72
4 changed files with 44 additions and 30 deletions

View file

@ -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;

View file

@ -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

View file

@ -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

View file

@ -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();
}