From d7b11bde283855633bd1b3e695d88d024a0abcd8 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Mon, 13 Aug 2012 13:38:04 +0200 Subject: [PATCH] i3 --moreversion: use readlink /proc/$pid/exe instead of realpath(argv[0]) The latter is actually wrong. For example, when running i3 --moreversion, it will print $(pwd)/i3 instead of $(which i3). In my previous tests, this coincidentally was the same. --- src/display_version.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/display_version.c b/src/display_version.c index 7460a1a2..ac1a622c 100644 --- a/src/display_version.c +++ b/src/display_version.c @@ -123,18 +123,25 @@ void display_running_version(void) { errx(EXIT_FAILURE, "Could not parse my own reply. That's weird. reply is %.*s", (int)reply_length, reply); printf("\rRunning i3 version: %s (pid %s)\n", human_readable_version, pid_from_atom); - printf("\n"); - char resolved_path[PATH_MAX]; - if (realpath(start_argv[0], resolved_path) == NULL) - err(EXIT_FAILURE, "realpath(%s)", start_argv[0]); - printf("The i3 binary you just called: %s\n", resolved_path); #ifdef __linux__ char exepath[PATH_MAX], destpath[PATH_MAX]; + ssize_t linksize; + + snprintf(exepath, sizeof(exepath), "/proc/%d/exe", getpid()); + + if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1) + err(EXIT_FAILURE, "readlink(%s)", exepath); + + /* readlink() does not NULL-terminate strings, so we have to. */ + destpath[linksize] = '\0'; + + printf("\n"); + printf("The i3 binary you just called: %s\n", destpath); + snprintf(exepath, sizeof(exepath), "/proc/%s/exe", pid_from_atom); - ssize_t linksize; if ((linksize = readlink(exepath, destpath, sizeof(destpath))) == -1) err(EXIT_FAILURE, "readlink(%s)", exepath); -- 2.39.5