0003-open-in-cwd.patch (3354B)
1 From ca8264b42f5d523f1e9bc9fec2f6060096a6c430 Mon Sep 17 00:00:00 2001 2 From: Randy Palamar <palamar@ualberta.ca> 3 Date: Tue, 24 Oct 2023 06:02:54 -0600 4 Subject: [PATCH] open in cwd 5 6 --- 7 config.def.h | 1 + 8 st.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 9 st.h | 1 + 10 x.c | 3 +++ 11 4 files changed, 56 insertions(+), 1 deletion(-) 12 13 diff --git a/config.def.h b/config.def.h 14 index 2cd740a..897e12a 100644 15 --- a/config.def.h 16 +++ b/config.def.h 17 @@ -201,6 +201,7 @@ static Shortcut shortcuts[] = { 18 { TERMMOD, XK_Y, selpaste, {.i = 0} }, 19 { ShiftMask, XK_Insert, selpaste, {.i = 0} }, 20 { TERMMOD, XK_Num_Lock, numlock, {.i = 0} }, 21 + { TERMMOD, XK_Return, newterm, {.i = 0} }, 22 }; 23 24 /* 25 diff --git a/st.c b/st.c 26 index 57c6e96..bf4edfd 100644 27 --- a/st.c 28 +++ b/st.c 29 @@ -20,6 +20,9 @@ 30 #include "st.h" 31 #include "win.h" 32 33 +extern char **g_argv; 34 +extern char *argv0; 35 + 36 #if defined(__linux) 37 #include <pty.h> 38 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) 39 @@ -28,6 +31,10 @@ 40 #include <libutil.h> 41 #endif 42 43 +#if defined(__OpenBSD__) 44 + #include <sys/sysctl.h> 45 +#endif 46 + 47 /* Arbitrary sizes */ 48 #define UTF_INVALID 0xFFFD 49 #define UTF_SIZ 4 50 @@ -796,7 +803,7 @@ ttynew(const char *line, char *cmd, const char *out, char **args) 51 if (s > 2) 52 close(s); 53 #ifdef __OpenBSD__ 54 - if (pledge("stdio getpw proc exec", NULL) == -1) 55 + if (pledge("stdio getpw proc ps exec", NULL) == -1) 56 die("pledge\n"); 57 #endif 58 execsh(cmd, args); 59 @@ -806,6 +813,7 @@ ttynew(const char *line, char *cmd, const char *out, char **args) 60 if (pledge("stdio rpath tty proc", NULL) == -1) 61 die("pledge\n"); 62 #endif 63 + fcntl(m, F_SETFD, FD_CLOEXEC); 64 close(s); 65 cmdfd = m; 66 signal(SIGCHLD, sigchld); 67 @@ -1054,6 +1062,48 @@ tswapscreen(void) 68 tfulldirt(); 69 } 70 71 +static int 72 +chdir_by_pid(pid_t pid) 73 +{ 74 + char buf[32]; 75 + 76 +#if defined(__linux) 77 + snprintf(buf, sizeof(buf), "/proc/%ld/cwd", (long)pid); 78 +#elif defined(__OpenBSD__) 79 + size_t sz = 32; 80 + int name[3] = {CTL_KERN, KERN_PROC_CWD, pid}; 81 + if (sysctl(name, 3, buf, &sz, 0, 0) == -1) 82 + return -1; 83 +#endif 84 + return chdir(buf); 85 +} 86 + 87 +void 88 +newterm(const Arg* a) 89 +{ 90 + int res; 91 + switch (fork()) { 92 + case -1: 93 + die("fork failed: %s\n", strerror(errno)); 94 + break; 95 + case 0: 96 + switch (fork()) { 97 + case -1: 98 + die("fork failed: %s\n", strerror(errno)); 99 + break; 100 + case 0: 101 + chdir_by_pid(pid); 102 + execvp("/proc/self/exe", g_argv); 103 + exit(1); 104 + break; 105 + default: 106 + exit(0); 107 + } 108 + default: 109 + wait(NULL); 110 + } 111 +} 112 + 113 void 114 tscrolldown(int orig, int n) 115 { 116 diff --git a/st.h b/st.h 117 index fd3b0d8..f2b03b0 100644 118 --- a/st.h 119 +++ b/st.h 120 @@ -81,6 +81,7 @@ void die(const char *, ...); 121 void redraw(void); 122 void draw(void); 123 124 +void newterm(const Arg *); 125 void printscreen(const Arg *); 126 void printsel(const Arg *); 127 void sendbreak(const Arg *); 128 diff --git a/x.c b/x.c 129 index 2407c7e..0f0d28c 100644 130 --- a/x.c 131 +++ b/x.c 132 @@ -16,6 +16,7 @@ 133 #include <X11/Xft/Xft.h> 134 #include <X11/XKBlib.h> 135 136 +char **g_argv; 137 char *argv0; 138 #include "arg.h" 139 #include "st.h" 140 @@ -2053,6 +2054,8 @@ main(int argc, char *argv[]) 141 xw.isfixed = False; 142 xsetcursor(cursorshape); 143 144 + g_argv = argv; 145 + 146 ARGBEGIN { 147 case 'a': 148 allowaltscreen = 0; 149 -- 150 2.44.0 151