From b4c56fb4b5f0c496b544080c5dd38d3cf93c10e8 Mon Sep 17 00:00:00 2001 From: Michael Biebl Date: Tue, 14 Jul 2009 17:40:43 +0200 Subject: Imported Upstream version 0.2.1 --- src/getfd.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/getfd.c (limited to 'src/getfd.c') diff --git a/src/getfd.c b/src/getfd.c new file mode 100644 index 0000000..69c37a3 --- /dev/null +++ b/src/getfd.c @@ -0,0 +1,75 @@ +/* + * Adapted from kbd-1.12 + * License: GPL + * + */ + +#include "config.h" + +#include +#include +#include +#include +#include + +#ifdef HAVE_PATHS_H +#include +#endif /* HAVE_PATHS_H */ + +/* + * getfd.c + * + * Get an fd for use with kbd/console ioctls. + * We try several things because opening /dev/console will fail + * if someone else used X (which does a chown on /dev/console). + */ + +static int +is_a_console (int fd) +{ + char arg; + + arg = 0; + return (ioctl (fd, KDGKBTYPE, &arg) == 0 + && ((arg == KB_101) || (arg == KB_84))); +} + +static int +open_a_console (char *fnam) +{ + int fd; + + fd = open (fnam, O_RDONLY); + if (fd < 0 && errno == EACCES) + fd = open(fnam, O_WRONLY); + if (fd < 0 || ! is_a_console (fd)) + return -1; + return fd; +} + +int getfd (void) +{ + int fd; + + fd = open_a_console (_PATH_TTY); + if (fd >= 0) + return fd; + + fd = open_a_console ("/dev/tty"); + if (fd >= 0) + return fd; + + fd = open_a_console (_PATH_CONSOLE); + if (fd >= 0) + return fd; + + fd = open_a_console ("/dev/console"); + if (fd >= 0) + return fd; + + for (fd = 0; fd < 3; fd++) + if (is_a_console (fd)) + return fd; + + return -1; +} -- cgit v1.2.3