/* * header.h * Dirty little file to include header files w/out autotools. * * Copyright 1999-2004 Gentoo Foundation * Distributed under the terms of the GNU General Public License v2 * $Header$ */ /* Common includes */ #define HAVE_TIOCNOTTY #define HAVE_SETSID /* OS-specific includes */ #if defined(__GLIBC__) # define HAVE_SYS_SYSMACROS_H # define HAVE_ERROR_H #endif /* Now we actually include crap ;) */ #ifdef HAVE_ERROR_H # include #endif #ifdef HAVE_SYS_SYSMACROS_H # include #endif /* * consoletype.c * simple app to figure out whether the current terminal * is serial, console (vt), or remote (pty). * * Copyright 1999-2006 Gentoo Foundation * Distributed under the terms of the GNU General Public License v2 */ #include #include #include #include #include #include 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(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 type = check_ttyname(); if (type == IS_UNK) type = check_devnode(); puts(tty_names[type]); return type; }