summaryrefslogtreecommitdiff
path: root/xembed.c
blob: cbb0e97a715f7ee3ed642fbded6ac2c24156c0f4 (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
45
/*
 * See LICENSE file for copyright and license details.
 */

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
	char *xembed;
	int tty;
	pid_t pgrp, tcpgrp;

	if (argc < 3) {
		fprintf(stderr, "usage: %s flag cmd ...\n", argv[0]);
		return 2;
	}

	if (!(xembed = getenv("XEMBED")))
		goto noembed;

	if ((tty = open("/dev/tty", O_RDONLY)) < 0)
		goto noembed;

	pgrp = getpgrp();
	tcpgrp = tcgetpgrp(tty);

	close(tty);

	if (pgrp == tcpgrp) { /* in foreground of tty */
		argv[0] = argv[2];
		argv[2] = xembed;
	} else {
noembed:
		argv += 2;
	}

	execvp(argv[0], argv);

	perror(argv[0]); /* failed to execute */
	return 1;
}