Remove references to PATH_MAX macro
Since the macro PATH_MAX is not defined on every system (GNU/Hurd being one of those who do not define it), we remove all references to this macro. Instead, we use a buffer of arbitraty size and grow it when needed to contain paths.
This commit is contained in:
parent
4f5e0e794f
commit
f22995393a
@ -159,8 +159,9 @@ static void handle_button_release(xcb_connection_t *conn, xcb_button_release_eve
|
|||||||
fclose(script);
|
fclose(script);
|
||||||
|
|
||||||
char *link_path;
|
char *link_path;
|
||||||
|
char *exe_path = get_exe_path(argv0);
|
||||||
sasprintf(&link_path, "%s.nagbar_cmd", script_path);
|
sasprintf(&link_path, "%s.nagbar_cmd", script_path);
|
||||||
symlink(get_exe_path(argv0), link_path);
|
symlink(exe_path, link_path);
|
||||||
|
|
||||||
char *terminal_cmd;
|
char *terminal_cmd;
|
||||||
sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
|
sasprintf(&terminal_cmd, "i3-sensible-terminal -e %s", link_path);
|
||||||
@ -172,6 +173,7 @@ static void handle_button_release(xcb_connection_t *conn, xcb_button_release_eve
|
|||||||
free(link_path);
|
free(link_path);
|
||||||
free(terminal_cmd);
|
free(terminal_cmd);
|
||||||
free(script_path);
|
free(script_path);
|
||||||
|
free(exe_path);
|
||||||
|
|
||||||
/* TODO: unset flag, re-render */
|
/* TODO: unset flag, re-render */
|
||||||
}
|
}
|
||||||
|
@ -372,7 +372,8 @@ char *get_process_filename(const char *prefix);
|
|||||||
*
|
*
|
||||||
* The implementation follows http://stackoverflow.com/a/933996/712014
|
* The implementation follows http://stackoverflow.com/a/933996/712014
|
||||||
*
|
*
|
||||||
|
* Returned value must be freed by the caller.
|
||||||
*/
|
*/
|
||||||
const char *get_exe_path(const char *argv0);
|
char *get_exe_path(const char *argv0);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#include "libi3.h"
|
#include "libi3.h"
|
||||||
|
|
||||||
@ -11,10 +12,14 @@
|
|||||||
*
|
*
|
||||||
* The implementation follows http://stackoverflow.com/a/933996/712014
|
* The implementation follows http://stackoverflow.com/a/933996/712014
|
||||||
*
|
*
|
||||||
|
* Returned value must be freed by the caller.
|
||||||
*/
|
*/
|
||||||
const char *get_exe_path(const char *argv0) {
|
char *get_exe_path(const char *argv0) {
|
||||||
static char destpath[PATH_MAX];
|
size_t destpath_size = 1024;
|
||||||
char tmp[PATH_MAX];
|
size_t tmp_size = 1024;
|
||||||
|
char *destpath = smalloc(destpath_size);
|
||||||
|
char *tmp = smalloc(tmp_size);
|
||||||
|
|
||||||
|
|
||||||
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
#if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
||||||
/* Linux and Debian/kFreeBSD provide /proc/self/exe */
|
/* Linux and Debian/kFreeBSD provide /proc/self/exe */
|
||||||
@ -25,30 +30,48 @@ const char *get_exe_path(const char *argv0) {
|
|||||||
#endif
|
#endif
|
||||||
ssize_t linksize;
|
ssize_t linksize;
|
||||||
|
|
||||||
if ((linksize = readlink(exepath, destpath, sizeof(destpath) - 1)) != -1) {
|
while ((linksize = readlink(exepath, destpath, destpath_size)) == 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. */
|
/* readlink() does not NULL-terminate strings, so we have to. */
|
||||||
destpath[linksize] = '\0';
|
destpath[linksize] = '\0';
|
||||||
|
free(tmp);
|
||||||
return destpath;
|
return destpath;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* argv[0] is most likely a full path if it starts with a slash. */
|
/* argv[0] is most likely a full path if it starts with a slash. */
|
||||||
if (argv0[0] == '/')
|
if (argv0[0] == '/') {
|
||||||
return argv0;
|
free(tmp);
|
||||||
|
free(destpath);
|
||||||
|
return sstrdup(argv0);
|
||||||
|
}
|
||||||
|
|
||||||
/* if argv[0] contains a /, prepend the working directory */
|
/* if argv[0] contains a /, prepend the working directory */
|
||||||
if (strchr(argv0, '/') != NULL &&
|
if (strchr(argv0, '/') != NULL) {
|
||||||
getcwd(tmp, sizeof(tmp)) != NULL) {
|
char *retgcwd;
|
||||||
snprintf(destpath, sizeof(destpath), "%s/%s", tmp, argv0);
|
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;
|
return destpath;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Fall back to searching $PATH (or _CS_PATH in absence of $PATH). */
|
/* Fall back to searching $PATH (or _CS_PATH in absence of $PATH). */
|
||||||
char *path = getenv("PATH");
|
char *path = getenv("PATH");
|
||||||
if (path == NULL) {
|
if (path == NULL) {
|
||||||
/* _CS_PATH is typically something like "/bin:/usr/bin" */
|
/* _CS_PATH is typically something like "/bin:/usr/bin" */
|
||||||
confstr(_CS_PATH, tmp, sizeof(tmp));
|
while (confstr(_CS_PATH, tmp, tmp_size) > tmp_size) {
|
||||||
|
tmp_size = tmp_size * 2;
|
||||||
|
tmp = srealloc(tmp, tmp_size);
|
||||||
|
}
|
||||||
sasprintf(&path, ":%s", tmp);
|
sasprintf(&path, ":%s", tmp);
|
||||||
} else {
|
} else {
|
||||||
path = strdup(path);
|
path = strdup(path);
|
||||||
@ -59,16 +82,20 @@ const char *get_exe_path(const char *argv0) {
|
|||||||
if ((component = strtok(str, ":")) == NULL)
|
if ((component = strtok(str, ":")) == NULL)
|
||||||
break;
|
break;
|
||||||
str = NULL;
|
str = NULL;
|
||||||
snprintf(destpath, sizeof(destpath), "%s/%s", component, argv0);
|
free(destpath);
|
||||||
|
sasprintf(&destpath, "%s/%s", component, argv0);
|
||||||
/* Of course this is not 100% equivalent to actually exec()ing the
|
/* Of course this is not 100% equivalent to actually exec()ing the
|
||||||
* binary, but meh. */
|
* binary, but meh. */
|
||||||
if (access(destpath, X_OK) == 0) {
|
if (access(destpath, X_OK) == 0) {
|
||||||
free(path);
|
free(path);
|
||||||
|
free(tmp);
|
||||||
return destpath;
|
return destpath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
free(destpath);
|
||||||
free(path);
|
free(path);
|
||||||
|
free(tmp);
|
||||||
|
|
||||||
/* Last resort: maybe it’s in /usr/bin? */
|
/* Last resort: maybe it’s in /usr/bin? */
|
||||||
return "/usr/bin/i3-nagbar";
|
return sstrdup("/usr/bin/i3-nagbar");
|
||||||
}
|
}
|
||||||
|
@ -128,13 +128,18 @@ void display_running_version(void) {
|
|||||||
printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
|
printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom);
|
||||||
|
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
char exepath[PATH_MAX],
|
size_t destpath_size = 1024;
|
||||||
destpath[PATH_MAX];
|
|
||||||
ssize_t linksize;
|
ssize_t linksize;
|
||||||
|
char *exepath;
|
||||||
|
char *destpath = smalloc(destpath_size);
|
||||||
|
|
||||||
snprintf(exepath, sizeof(exepath), "/proc/%d/exe", getpid());
|
sasprintf(&exepath, "/proc/%d/exe", getpid());
|
||||||
|
|
||||||
if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1)
|
while ((linksize = readlink(exepath, destpath, destpath_size)) == destpath_size) {
|
||||||
|
destpath_size = destpath_size * 2;
|
||||||
|
destpath = srealloc(destpath, destpath_size);
|
||||||
|
}
|
||||||
|
if (linksize == -1)
|
||||||
err(EXIT_FAILURE, "readlink(%s)", exepath);
|
err(EXIT_FAILURE, "readlink(%s)", exepath);
|
||||||
|
|
||||||
/* readlink() does not NULL-terminate strings, so we have to. */
|
/* readlink() does not NULL-terminate strings, so we have to. */
|
||||||
@ -143,9 +148,14 @@ void display_running_version(void) {
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
printf("The i3 binary you just called: %s\n", destpath);
|
printf("The i3 binary you just called: %s\n", destpath);
|
||||||
|
|
||||||
snprintf(exepath, sizeof(exepath), "/proc/%s/exe", pid_from_atom);
|
free(exepath);
|
||||||
|
sasprintf(&exepath, "/proc/%s/exe", pid_from_atom);
|
||||||
|
|
||||||
if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1)
|
while ((linksize = readlink(exepath, destpath, destpath_size)) == destpath_size) {
|
||||||
|
destpath_size = destpath_size * 2;
|
||||||
|
destpath = srealloc(destpath, destpath_size);
|
||||||
|
}
|
||||||
|
if (linksize == -1)
|
||||||
err(EXIT_FAILURE, "readlink(%s)", exepath);
|
err(EXIT_FAILURE, "readlink(%s)", exepath);
|
||||||
|
|
||||||
/* readlink() does not NULL-terminate strings, so we have to. */
|
/* readlink() does not NULL-terminate strings, so we have to. */
|
||||||
@ -159,7 +169,8 @@ void display_running_version(void) {
|
|||||||
/* Since readlink() might put a "(deleted)" somewhere in the buffer and
|
/* Since readlink() might put a "(deleted)" somewhere in the buffer and
|
||||||
* stripping that out seems hackish and ugly, we read the process’s argv[0]
|
* stripping that out seems hackish and ugly, we read the process’s argv[0]
|
||||||
* instead. */
|
* instead. */
|
||||||
snprintf(exepath, sizeof(exepath), "/proc/%s/cmdline", pid_from_atom);
|
free(exepath);
|
||||||
|
sasprintf(&exepath, "/proc/%s/cmdline", pid_from_atom);
|
||||||
|
|
||||||
int fd;
|
int fd;
|
||||||
if ((fd = open(exepath, O_RDONLY)) == -1)
|
if ((fd = open(exepath, O_RDONLY)) == -1)
|
||||||
@ -169,6 +180,9 @@ void display_running_version(void) {
|
|||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
printf("The i3 binary you are running: %s\n", destpath);
|
printf("The i3 binary you are running: %s\n", destpath);
|
||||||
|
|
||||||
|
free(exepath);
|
||||||
|
free(destpath);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
yajl_free(handle);
|
yajl_free(handle);
|
||||||
|
15
src/main.c
15
src/main.c
@ -488,18 +488,25 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
/* The following code is helpful, but not required. We thus don’t pay
|
/* The following code is helpful, but not required. We thus don’t pay
|
||||||
* much attention to error handling, non-linux or other edge cases. */
|
* much attention to error handling, non-linux or other edge cases. */
|
||||||
char cwd[PATH_MAX];
|
|
||||||
LOG("CORE DUMPS: You are running a development version of i3, so coredumps were automatically enabled (ulimit -c unlimited).\n");
|
LOG("CORE DUMPS: You are running a development version of i3, so coredumps were automatically enabled (ulimit -c unlimited).\n");
|
||||||
if (getcwd(cwd, sizeof(cwd)) != NULL)
|
size_t cwd_size = 1024;
|
||||||
|
char *cwd = smalloc(cwd_size);
|
||||||
|
char *cwd_ret;
|
||||||
|
while ((cwd_ret = getcwd(cwd, cwd_size)) == NULL && errno == ERANGE) {
|
||||||
|
cwd_size = cwd_size * 2;
|
||||||
|
cwd = srealloc(cwd, cwd_size);
|
||||||
|
}
|
||||||
|
if (cwd_ret != NULL)
|
||||||
LOG("CORE DUMPS: Your current working directory is \"%s\".\n", cwd);
|
LOG("CORE DUMPS: Your current working directory is \"%s\".\n", cwd);
|
||||||
int patternfd;
|
int patternfd;
|
||||||
if ((patternfd = open("/proc/sys/kernel/core_pattern", O_RDONLY)) >= 0) {
|
if ((patternfd = open("/proc/sys/kernel/core_pattern", O_RDONLY)) >= 0) {
|
||||||
memset(cwd, '\0', sizeof(cwd));
|
memset(cwd, '\0', cwd_size);
|
||||||
if (read(patternfd, cwd, sizeof(cwd)) > 0)
|
if (read(patternfd, cwd, cwd_size) > 0)
|
||||||
/* a trailing newline is included in cwd */
|
/* a trailing newline is included in cwd */
|
||||||
LOG("CORE DUMPS: Your core_pattern is: %s", cwd);
|
LOG("CORE DUMPS: Your core_pattern is: %s", cwd);
|
||||||
close(patternfd);
|
close(patternfd);
|
||||||
}
|
}
|
||||||
|
free(cwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG("i3 " I3_VERSION " starting\n");
|
LOG("i3 " I3_VERSION " starting\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user