st-flexipatch/patch/newterm.c
Utkarsh Verma c4c5113fbd
osc7: initial patch implementation (#154)
* osc7: initial patch implementation

Closes #153

* osc7: avoid redundant use of realpath()

* osc7: fix styling

* Changing position of the OSC7_PATCH toggle in patches.def.h

---------

Co-authored-by: Bakkeby <bakkeby@gmail.com>
2024-10-15 09:54:45 +02:00

46 lines
841 B
C

extern char* argv0;
static char*
getcwd_by_pid(pid_t pid) {
static char cwd[32];
snprintf(cwd, sizeof cwd, "/proc/%d/cwd", pid);
return cwd;
}
void
newterm(const Arg* a)
{
switch (fork()) {
case -1:
die("fork failed: %s\n", strerror(errno));
break;
case 0:
switch (fork()) {
case -1:
die("fork failed: %s\n", strerror(errno));
break;
case 0:
#if OSC7_PATCH
if (term.cwd) {
if (chdir(term.cwd) == 0) {
/* We need to put the working directory also in PWD, so that
* the shell starts in the right directory if `cwd` is a
* symlink. */
setenv("PWD", term.cwd, 1);
}
} else {
chdir(getcwd_by_pid(pid));
}
#else
chdir(getcwd_by_pid(pid));
#endif // OSC7_PATCH
execl("/proc/self/exe", argv0, NULL);
exit(1);
default:
exit(0);
}
default:
wait(NULL);
}
}