From: Michael Stapelberg Date: Mon, 13 Aug 2012 11:38:04 +0000 (+0200) Subject: i3 --moreversion: use readlink /proc/$pid/exe instead of realpath(argv[0]) X-Git-Tag: 4.3~74 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=d7b11bde283855633bd1b3e695d88d024a0abcd8;p=i3%2Fi3 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. --- 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);