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:
parent
3f1a5ed034
commit
c4c5113fbd
9 changed files with 136 additions and 11 deletions
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue