The current implimintation of ProcGetCWD relies on procfs. The following
patch uses sysctl to instead retrieve the current working directory.

This enables the use of exec-formatted xterm function on a path relative
to the terminal's current working directory.


ig0r < ig0r [at] b3zdna [dot] net >


diff --git app/xterm/Makefile app/xterm/Makefile
index ee3383094..d99a02cb3 100644
--- app/xterm/Makefile
+++ app/xterm/Makefile
@@ -14,7 +14,7 @@ CPPFLAGS+=    -I. -I${.CURDIR} -I${X11BASE}/include \
                -DHAVE_CONFIG_H -DUTMP \
                -DDEF_ALLOW_FONT=False -DDEF_ALLOW_TCAP=False \
                -DDEF_ALLOW_WINDOW=False -DDEF_ALLOW_MOUSE=False \
-               -DOPT_PRINT_ON_EXIT=0
+               -DOPT_PRINT_ON_EXIT=0 -DOPT_EXEC_XTERM=True
 LDADD+=                -L${X11BASE}/lib -lXaw -lXpm -lXt -lSM -lICE -lXmu \
                -lXft -lXrender -lXinerama -lX11 -lxcb -lXext -lXau -lXdmcp \
                -lfontconfig -lexpat -lfreetype -lutil -ltermcap -lz
diff --git app/xterm/main.c app/xterm/main.c
index b6e4e8f58..95f6946a5 100644
--- app/xterm/main.c
+++ app/xterm/main.c
@@ -2908,7 +2908,7 @@ main(int argc, char *argv[]ENVP_ARG)
         if (data &&
             (strstr(data, "exec-formatted") || strstr(data, 
"exec-selectable"))) {
 
-            if (pledge("stdio rpath wpath id proc exec tty", NULL) == -1) {
+            if (pledge("stdio rpath wpath id proc exec tty ps", NULL) == -1) {
                 xtermWarning("pledge\n");
                 exit(1);
             }
@@ -2955,7 +2955,7 @@ main(int argc, char *argv[]ENVP_ARG)
             unveil(etc_utmp, "w");
            unveil(etc_wtmp, "w");
 
-            if (pledge("stdio rpath wpath id proc tty", NULL) == -1) {
+            if (pledge("stdio rpath wpath id proc tty ps", NULL) == -1) {
                xtermWarning("pledge\n");
                exit(1);
             }
diff --git app/xterm/misc.c app/xterm/misc.c
index 89eee4880..33ada0f98 100644
--- app/xterm/misc.c
+++ app/xterm/misc.c
@@ -99,6 +99,8 @@
 
 #include <assert.h>
 
+#include <sys/sysctl.h>
+
 #ifdef VMS
 #define XTERM_VMS_LOGFILE "SYS$SCRATCH:XTERM_LOG.TXT"
 #ifdef ALLOWLOGFILEEXEC
@@ -1108,14 +1110,12 @@ HandleStringEvent(Widget w GCC_UNUSED,
 char *
 ProcGetCWD(pid_t pid)
 {
-    char *child_cwd = NULL;
+       char child_cwd_link[BUFSIZ];
 
-    if (pid) {
-       char child_cwd_link[sizeof(PROCFS_ROOT) + 80];
-       sprintf(child_cwd_link, PROCFS_ROOT "/%lu/cwd", (unsigned long) pid);
-       child_cwd = Readlink(child_cwd_link);
-    }
-    return child_cwd;
+       return pid &&
+               !sysctl((int[]){CTL_KERN,KERN_PROC_CWD,pid}, 3, child_cwd_link, 
(size_t[]){BUFSIZ}, 0, 0)
+               ? strdup(child_cwd_link)
+               : NULL;
 }
 
 /* ARGSUSED */

Reply via email to