2016-10-11 09:13:35 +02:00
|
|
|
|
/*
|
|
|
|
|
* vim:ts=4:sw=4:expandtab
|
|
|
|
|
*
|
|
|
|
|
* i3 - an improved dynamic tiling window manager
|
|
|
|
|
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
#include "libi3.h"
|
|
|
|
|
|
2020-04-19 09:43:48 +02:00
|
|
|
|
#include <errno.h>
|
2013-06-10 22:55:39 +02:00
|
|
|
|
#include <stdlib.h>
|
2020-04-19 09:43:48 +02:00
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
2013-06-10 22:55:39 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This function returns the absolute path to the executable it is running in.
|
|
|
|
|
*
|
2017-09-24 10:19:07 +02:00
|
|
|
|
* The implementation follows https://stackoverflow.com/a/933996/712014
|
2013-06-10 22:55:39 +02:00
|
|
|
|
*
|
2013-11-21 22:03:49 +01:00
|
|
|
|
* Returned value must be freed by the caller.
|
2013-06-10 22:55:39 +02:00
|
|
|
|
*/
|
2013-11-21 22:03:49 +01:00
|
|
|
|
char *get_exe_path(const char *argv0) {
|
2014-06-15 19:07:02 +02:00
|
|
|
|
size_t destpath_size = 1024;
|
|
|
|
|
size_t tmp_size = 1024;
|
|
|
|
|
char *destpath = smalloc(destpath_size);
|
|
|
|
|
char *tmp = smalloc(tmp_size);
|
2013-06-10 22:55:39 +02:00
|
|
|
|
|
|
|
|
|
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2014-06-15 19:07:02 +02:00
|
|
|
|
/* Linux and Debian/kFreeBSD provide /proc/self/exe */
|
2013-06-10 22:55:39 +02:00
|
|
|
|
#if defined(__linux__) || defined(__FreeBSD_kernel__)
|
2014-06-15 19:07:02 +02:00
|
|
|
|
const char *exepath = "/proc/self/exe";
|
2013-06-10 22:55:39 +02:00
|
|
|
|
#elif defined(__FreeBSD__)
|
2014-06-15 19:07:02 +02:00
|
|
|
|
const char *exepath = "/proc/curproc/file";
|
2013-06-10 22:55:39 +02:00
|
|
|
|
#endif
|
2014-06-15 19:07:02 +02:00
|
|
|
|
ssize_t linksize;
|
2013-06-10 22:55:39 +02:00
|
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
|
while ((linksize = readlink(exepath, destpath, destpath_size)) == (ssize_t)destpath_size) {
|
|
|
|
|
destpath_size = destpath_size * 2;
|
|
|
|
|
destpath = srealloc(destpath, destpath_size);
|
|
|
|
|
}
|
|
|
|
|
if (linksize != -1) {
|
|
|
|
|
/* readlink() does not NULL-terminate strings, so we have to. */
|
|
|
|
|
destpath[linksize] = '\0';
|
|
|
|
|
free(tmp);
|
|
|
|
|
return destpath;
|
|
|
|
|
}
|
2013-06-10 22:55:39 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
|
/* argv[0] is most likely a full path if it starts with a slash. */
|
|
|
|
|
if (argv0[0] == '/') {
|
|
|
|
|
free(tmp);
|
|
|
|
|
free(destpath);
|
|
|
|
|
return sstrdup(argv0);
|
|
|
|
|
}
|
2013-06-10 22:55:39 +02:00
|
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
|
/* if argv[0] contains a /, prepend the working directory */
|
|
|
|
|
if (strchr(argv0, '/') != NULL) {
|
|
|
|
|
char *retgcwd;
|
|
|
|
|
while ((retgcwd = getcwd(tmp, tmp_size)) == NULL && errno == ERANGE) {
|
|
|
|
|
tmp_size = tmp_size * 2;
|
|
|
|
|
tmp = srealloc(tmp, tmp_size);
|
|
|
|
|
}
|
|
|
|
|
if (retgcwd != NULL) {
|
|
|
|
|
free(destpath);
|
|
|
|
|
sasprintf(&destpath, "%s/%s", tmp, argv0);
|
|
|
|
|
free(tmp);
|
|
|
|
|
return destpath;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-10 22:55:39 +02:00
|
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
|
/* Fall back to searching $PATH (or _CS_PATH in absence of $PATH). */
|
|
|
|
|
char *path = getenv("PATH");
|
|
|
|
|
if (path == NULL) {
|
|
|
|
|
/* _CS_PATH is typically something like "/bin:/usr/bin" */
|
|
|
|
|
while (confstr(_CS_PATH, tmp, tmp_size) > tmp_size) {
|
|
|
|
|
tmp_size = tmp_size * 2;
|
|
|
|
|
tmp = srealloc(tmp, tmp_size);
|
|
|
|
|
}
|
|
|
|
|
sasprintf(&path, ":%s", tmp);
|
|
|
|
|
} else {
|
2015-08-03 11:50:50 +02:00
|
|
|
|
path = sstrdup(path);
|
2014-06-15 19:07:02 +02:00
|
|
|
|
}
|
|
|
|
|
const char *component;
|
|
|
|
|
char *str = path;
|
|
|
|
|
while (1) {
|
|
|
|
|
if ((component = strtok(str, ":")) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
str = NULL;
|
|
|
|
|
free(destpath);
|
|
|
|
|
sasprintf(&destpath, "%s/%s", component, argv0);
|
|
|
|
|
/* Of course this is not 100% equivalent to actually exec()ing the
|
2020-02-21 03:06:48 +01:00
|
|
|
|
* binary, but meh. */
|
2014-06-15 19:07:02 +02:00
|
|
|
|
if (access(destpath, X_OK) == 0) {
|
|
|
|
|
free(path);
|
|
|
|
|
free(tmp);
|
|
|
|
|
return destpath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free(destpath);
|
|
|
|
|
free(path);
|
|
|
|
|
free(tmp);
|
2013-06-10 22:55:39 +02:00
|
|
|
|
|
2014-06-15 19:07:02 +02:00
|
|
|
|
/* Last resort: maybe it’s in /usr/bin? */
|
|
|
|
|
return sstrdup("/usr/bin/i3-nagbar");
|
2013-06-10 22:55:39 +02:00
|
|
|
|
}
|