From 024846b4d82ad57e68b64cac2ac12b932a9042d2 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 17 Apr 2014 23:48:19 +0100 Subject: [PATCH] If pgrp fails in osdep_get_cwd, try sid. Fixes eg cat foo|less. From Balazs Kezes. --- osdep-linux.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/osdep-linux.c b/osdep-linux.c index ccac2670..46aea68e 100644 --- a/osdep-linux.c +++ b/osdep-linux.c @@ -65,7 +65,7 @@ osdep_get_cwd(int fd) { static char target[MAXPATHLEN + 1]; char *path; - pid_t pgrp; + pid_t pgrp, sid; ssize_t n; if ((pgrp = tcgetpgrp(fd)) == -1) @@ -74,6 +74,13 @@ osdep_get_cwd(int fd) xasprintf(&path, "/proc/%lld/cwd", (long long) pgrp); n = readlink(path, target, MAXPATHLEN); free(path); + + if (n == -1 && ioctl(fd, TIOCGSID, &sid) != -1) { + xasprintf(&path, "/proc/%lld/cwd", (long long) sid); + n = readlink(path, target, MAXPATHLEN); + free(path); + } + if (n > 0) { target[n] = '\0'; return (target);