summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/consoletype.c')
-rw-r--r--src/consoletype.c59
1 files changed, 45 insertions, 14 deletions
diff --git a/src/consoletype.c b/src/consoletype.c
index 87dbf07..c8b8dd6 100644
--- a/src/consoletype.c
+++ b/src/consoletype.c
@@ -3,36 +3,67 @@
* simple app to figure out whether the current terminal
* is serial, console (vt), or remote (pty).
*
- * Copyright 1999-2004 Gentoo Foundation
+ * Copyright 1999-2006 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
- * $Header$
*/
#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include "headers.h"
-int main(int argc, char *argv[])
+enum { 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(__linux__)
+#if defined(TIOCLINUX)
unsigned char twelve = 12;
- if (ioctl (0, TIOCLINUX, &twelve) < 0) {
- printf("serial\n");
- return 1;
- }
+ if (ioctl (0, TIOCLINUX, &twelve) < 0)
+ return IS_SERIAL;
#endif
- printf("vt\n");
- return 0;
- } else {
- printf("pty\n");
- return 2;
- }
+ return IS_VT;
+ } else
+ return IS_PTY;
+#endif
+ return IS_UNK;
+}
+
+int main(int argc, char *argv[])
+{
+ int type = check_ttyname();
+ if (type == IS_UNK)
+ type = check_devnode();
+ puts(tty_names[type]);
+ return type;
}