1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) tons of folks. Tracking down who wrote what
6 * isn't something I'm going to worry about... If you wrote something
7 * here, please feel free to acknowledge your work.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
24 * Permission has been granted to redistribute this code under the GPL.
32 #include <sys/ioctl.h>
39 /* From <linux/kd.h> */
40 static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
41 static const int KB_84 = 0x01;
42 static const int KB_101 = 0x02; /* this is what we always answer */
44 int is_a_console(int fd)
49 return (ioctl(fd, KDGKBTYPE, &arg) == 0
50 && ((arg == KB_101) || (arg == KB_84)));
53 static int open_a_console(char *fnam)
58 fd = open(fnam, O_RDWR);
60 /* if failed, try read-only */
61 if (fd < 0 && errno == EACCES)
62 fd = open(fnam, O_RDONLY);
64 /* if failed, try write-only */
65 if (fd < 0 && errno == EACCES)
66 fd = open(fnam, O_WRONLY);
72 /* if not a console, fail */
73 if (!is_a_console(fd)) {
83 * Get an fd for use with kbd/console ioctls.
84 * We try several things because opening /dev/console will fail
85 * if someone else used X (which does a chown on /dev/console).
87 * if tty_name is non-NULL, try this one instead.
90 int get_console_fd(char *tty_name)
95 if (-1 == (fd = open_a_console(tty_name)))
101 fd = open_a_console("/dev/tty");
105 fd = open_a_console("/dev/tty0");
109 fd = open_a_console("/dev/console");
113 for (fd = 0; fd < 3; fd++)
114 if (is_a_console(fd))
117 error_msg("Couldnt get a file descriptor referring to the console");
118 return -1; /* total failure */
125 c-file-style: "linux"