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>
This commit is contained in:
Utkarsh Verma 2024-10-15 09:54:45 +02:00 committed by GitHub
parent 3f1a5ed034
commit c4c5113fbd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 136 additions and 11 deletions

View file

@ -1,7 +1,15 @@
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)
{
int res;
switch (fork()) {
case -1:
die("fork failed: %s\n", strerror(errno));
@ -12,9 +20,23 @@ newterm(const Arg* a)
die("fork failed: %s\n", strerror(errno));
break;
case 0:
res = chdir(getcwd_by_pid(pid));
execlp("st", "./st", NULL);
break;
#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);
}
@ -22,9 +44,3 @@ newterm(const Arg* a)
wait(NULL);
}
}
static char *getcwd_by_pid(pid_t pid) {
char buf[32];
snprintf(buf, sizeof buf, "/proc/%d/cwd", pid);
return realpath(buf, NULL);
}