Skip to content

Commit

Permalink
tools: change prototype of the builddir lookup function
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
whot committed Jul 17, 2018
1 parent 8362031 commit ad5d2fe
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
6 changes: 1 addition & 5 deletions tools/libinput-quirks.c
Expand Up @@ -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;
Expand Down
48 changes: 31 additions & 17 deletions tools/shared.c
Expand Up @@ -467,56 +467,70 @@ is_touchpad_device(const char *devnode)
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
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
Expand Down
5 changes: 3 additions & 2 deletions tools/shared.h
Expand Up @@ -25,6 +25,7 @@
#define _SHARED_H_

#include <stdbool.h>
#include <limits.h>

#include <quirks.h>
#include <libinput.h>
Expand Down Expand Up @@ -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
15 changes: 9 additions & 6 deletions tools/test-builddir-lookup.c
Expand Up @@ -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();
}
Expand Down

0 comments on commit ad5d2fe

Please sign in to comment.