aboutsummaryrefslogtreecommitdiffstats
path: root/sleeping-getty.c
blob: 9f7d09a2154ecc3e4aa080549a7e966c7b4228c2 (plain) (blame)
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
46
47
48
49
/* See LICENSE file for copyright and license details. */
#include <linux/vt.h>
#include <sys/ioctl.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


static char *argv0;

static void
usage(void)
{
	fprintf(stderr, "usage: %s vtno getty-command [arguments] ...\n", argv0);
	exit(1);
}

int
main(int argc, char *argv[])
{
	struct vt_stat state;
	long int tmp;
	int tty;
	char *end;

	argv0 = argv[0];
	if (argc < 3)
		usage();

	if (!isdigit(argv[1][0]))
		usage();
	errno = 0;
	tmp = strtol(argv[1], &end, 10);
	if (errno || *end || tmp < 0 || tmp > INT_MAX)
		usage();
	tty = (int)tmp;

	if (!ioctl(STDIN_FILENO, (long)VT_GETSTATE, &state))
		if (state.v_active != tty)
			ioctl(STDIN_FILENO, (long)VT_WAITACTIVE, tty);

	execvp(argv[2], &argv[2]);
	fprintf(stderr, "%s: execvp %s: %s\n", argv0, argv[2], strerror(errno));
	return 1;
}