newterm.c (817B)
1 extern char* argv0; 2 3 static char* 4 getcwd_by_pid(pid_t pid) { 5 static char cwd[32]; 6 snprintf(cwd, sizeof cwd, "/proc/%d/cwd", pid); 7 return cwd; 8 } 9 10 void 11 newterm(const Arg* a) 12 { 13 switch (fork()) { 14 case -1: 15 die("fork failed: %s\n", strerror(errno)); 16 break; 17 case 0: 18 switch (fork()) { 19 case -1: 20 die("fork failed: %s\n", strerror(errno)); 21 break; 22 case 0: 23 #if OSC7_PATCH 24 if (term.cwd) { 25 if (chdir(term.cwd) == 0) { 26 /* We need to put the working directory also in PWD, so that 27 * the shell starts in the right directory if `cwd` is a 28 * symlink. */ 29 setenv("PWD", term.cwd, 1); 30 } 31 } else { 32 chdir(getcwd_by_pid(pid)); 33 } 34 #else 35 chdir(getcwd_by_pid(pid)); 36 #endif // OSC7_PATCH 37 38 execl("/proc/self/exe", argv0, NULL); 39 exit(1); 40 default: 41 exit(0); 42 } 43 } 44 }