aboutsummaryrefslogtreecommitdiff
path: root/keep/manual-programs/suckless/st-flexipatch/patch/newterm.c
blob: a7430ceb32fd55c467c2d7b8d3e2cf5d7071e218 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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);
		}
	}
}