From 526f247237ac70910b4974349418bbcbe7d9a123 Mon Sep 17 00:00:00 2001 From: lurchi Date: Thu, 27 Jun 2019 17:39:03 +0200 Subject: introduce GNUNET_OS_installation_get_path (useful for custom helper paths) --- src/include/gnunet_os_lib.h | 13 +++++++++++++ src/util/os_installation.c | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/include/gnunet_os_lib.h b/src/include/gnunet_os_lib.h index 285dfb68d..82203267c 100644 --- a/src/include/gnunet_os_lib.h +++ b/src/include/gnunet_os_lib.h @@ -305,6 +305,19 @@ char * GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind); +/** + * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon binary and + * a prefix, construct the full path to the binary. + * + * @param progname name of the binary + * @param prefix the path to use as a prefix (if NULL this function is + * equivalent to GNUNET_OS_get_libexec_binary_path) + * @return full path to the binary, if possible, otherwise copy of 'progname' + */ +char * +GNUNET_OS_get_binary_path (const char *progname, const char* prefix); + + /** * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon * binary, try to prefix it with the libexec/-directory to get the diff --git a/src/util/os_installation.c b/src/util/os_installation.c index f2d24f85e..973cda6b1 100644 --- a/src/util/os_installation.c +++ b/src/util/os_installation.c @@ -769,18 +769,19 @@ GNUNET_OS_installation_get_path (enum GNUNET_OS_InstallationPathKind dirkind) /** - * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon - * binary, try to prefix it with the libexec/-directory to get the - * full path. + * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon binary and + * a prefix, construct the full path to the binary. * * @param progname name of the binary + * @param prefix the path to use as a prefix (if NULL this function is + * equivalent to GNUNET_OS_get_libexec_binary_path) * @return full path to the binary, if possible, otherwise copy of 'progname' */ char * -GNUNET_OS_get_libexec_binary_path (const char *progname) +GNUNET_OS_get_binary_path (const char *progname, const char* prefix) { - static char *cache; - char *libexecdir; + static const char *cache; + const char *path; char *binary; if ( (DIR_SEPARATOR == progname[0]) || @@ -789,21 +790,38 @@ GNUNET_OS_get_libexec_binary_path (const char *progname) GNUNET_NO, NULL, NULL)) ) return GNUNET_strdup (progname); - if (NULL != cache) - libexecdir = cache; + if (NULL != prefix) + path = prefix; + else if (NULL != cache) + path = cache; else - libexecdir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBEXECDIR); - if (NULL == libexecdir) + path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBEXECDIR); + if (NULL == path) return GNUNET_strdup (progname); GNUNET_asprintf (&binary, "%s%s", - libexecdir, + path, progname); - cache = libexecdir; + cache = path; return binary; } +/** + * Given the name of a gnunet-helper, gnunet-service or gnunet-daemon + * binary, try to prefix it with the libexec/-directory to get the + * full path. + * + * @param progname name of the binary + * @return full path to the binary, if possible, otherwise copy of 'progname' + */ +char * +GNUNET_OS_get_libexec_binary_path (const char *progname) +{ + return GNUNET_OS_get_binary_path (progname, NULL); +} + + /** * Check whether an executable exists and possibly if the suid bit is * set on the file. Attempts to find the file using the current PATH -- cgit v1.2.3