From: "Chris West (Faux)" <g...@goeswhere.com> This helps when a command is running as a different user, it is common to be able to see its parents, but not its cwd. It may make sense, in this situation, to present the parent's path. e.g. if a user runs a set of commands like:
$ cd /foo/bar $ sudo hang-for-a-bit .. it could be expected that get_cwd returns "/foo/bar", not NULL, as previously. sudo's cwd is not available, as the process itself is running as root, but its parent process is, and its parent (the shell)'s cwd is available to us. --- osdep-linux.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/osdep-linux.c b/osdep-linux.c index ccac267..91d0e5d 100644 --- a/osdep-linux.c +++ b/osdep-linux.c @@ -60,6 +60,26 @@ osdep_get_name(int fd, unused char *tty) return (buf); } +// 0 on failure +pid_t +parent_pid_of(pid_t pid) { + char *path; + FILE *f; + pid_t ret = 0; + + xasprintf(&path, "/proc/%lld/stat", (long long) pid); + f = fopen(path, "r"); + if (!f) + return 0; + + if (1 != fscanf(f, "%*d %*s %*c %d", &ret)) { + ret = 0; + } + + fclose(f); + return ret; +} + char * osdep_get_cwd(int fd) { @@ -71,13 +91,17 @@ osdep_get_cwd(int fd) if ((pgrp = tcgetpgrp(fd)) == -1) return (NULL); - xasprintf(&path, "/proc/%lld/cwd", (long long) pgrp); - n = readlink(path, target, MAXPATHLEN); - free(path); - if (n > 0) { - target[n] = '\0'; - return (target); - } + do { + xasprintf(&path, "/proc/%lld/cwd", (long long) pgrp); + n = readlink(path, target, MAXPATHLEN); + free(path); + if (n > 0) { + target[n] = '\0'; + return (target); + } + pgrp = parent_pid_of(pgrp); + } while (pgrp); + return (NULL); } -- 1.8.5.3 ------------------------------------------------------------------------------ Learn Graph Databases - Download FREE O'Reilly Book "Graph Databases" is the definitive new guide to graph databases and their applications. Written by three acclaimed leaders in the field, this first edition is now available. Download your free book today! http://p.sf.net/sfu/13534_NeoTech _______________________________________________ tmux-users mailing list tmux-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/tmux-users