summaryrefslogtreecommitdiff
blob: 42495043afdf6049c0a0432ff594474b47259933 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * consoletype.c
 * simple app to figure out whether the current terminal
 * is serial, console (vt), or remote (pty).
 *
 * Copyright 1999-2020 Gentoo Authors
 * Distributed under the terms of the GNU General Public License v2
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#if defined(__linux__)
#include <sys/sysmacros.h>
#endif

enum termtype {
	IS_VT = 0,
	IS_SERIAL = 1,
	IS_PTY = 2,
	IS_UNK = 3
};

const char * const tty_names[] = {
	"vt",
	"serial",
	"pty",
	"unknown"
};

static inline int check_ttyname(void)
{
	char *tty = ttyname(0);

	if (tty == NULL)
		return IS_UNK;

	if (strncmp(tty, "/dev/", 5) == 0)
		tty += 5;

	if (!strncmp (tty, "ttyS", 4) || !strncmp (tty, "cuaa", 4))
		return IS_SERIAL;
	else if (!strncmp (tty, "pts/", 4) || !strncmp (tty, "ttyp", 4))
		return IS_PTY;
	else if (!strncmp (tty, "tty", 3))
		return IS_VT;
	else
		return IS_UNK;
}

static inline int check_devnode(void)
{
#if defined(__linux__)
	int maj;
	struct stat sb;

	fstat(0, &sb);
	maj = major(sb.st_rdev);
	if (maj != 3 && (maj < 136 || maj > 143)) {
#if defined(TIOCLINUX)
		unsigned char twelve = 12;
		if (ioctl (0, TIOCLINUX, &twelve) < 0)
			return IS_SERIAL;
#endif
		return IS_VT;
	} else
		return IS_PTY;
#endif
	return IS_UNK;
}

int main(int argc, char *argv[])
{
	int rc;
	int type = check_ttyname();
	if (type == IS_UNK)
		type = check_devnode();
	puts(tty_names[type]);
	if (argc > 1 && strcmp(argv[1], "stdout") == 0)
		rc = 0;
	else
		rc = type;
	return rc;
}