aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/get_console.c72
1 files changed, 27 insertions, 45 deletions
diff --git a/libbb/get_console.c b/libbb/get_console.c
index 9eb4190a9..0dc24cb78 100644
--- a/libbb/get_console.c
+++ b/libbb/get_console.c
@@ -2,8 +2,8 @@
/*
* Utility routines.
*
- * Copyright (C) many different people.
- * If you wrote this, please acknowledge your work.
+ * Copyright (C) many different people. If you wrote this, please
+ * acknowledge your work.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -29,27 +29,15 @@
-
-
/* From <linux/kd.h> */
static const int KDGKBTYPE = 0x4B33; /* get keyboard type */
-static const int KB_84 = 0x01;
-static const int KB_101 = 0x02; /* this is what we always answer */
-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)
+static int open_a_console(const char *fnam)
{
int fd;
- /* try read-only */
+ /* try read-write */
fd = open(fnam, O_RDWR);
/* if failed, try read-only */
@@ -60,17 +48,6 @@ static int open_a_console(char *fnam)
if (fd < 0 && errno == EACCES)
fd = open(fnam, O_WRONLY);
- /* if failed, fail */
- if (fd < 0)
- return -1;
-
- /* if not a console, fail */
- if (!is_a_console(fd)) {
- close(fd);
- return -1;
- }
-
- /* success */
return fd;
}
@@ -78,32 +55,37 @@ static int open_a_console(char *fnam)
* 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).
- *
- * if tty_name is non-NULL, try this one instead.
*/
int get_console_fd(void)
{
int fd;
- fd = open_a_console(CURRENT_TTY);
- if (fd >= 0)
- return fd;
-
- fd = open_a_console(CURRENT_VC);
- if (fd >= 0)
- return fd;
-
- fd = open_a_console(CONSOLE_DEV);
- if (fd >= 0)
- return fd;
-
- for (fd = 0; fd < 3; fd++)
- if (is_a_console(fd))
- return fd;
+ static const char * const choise_console_names[] = {
+ CONSOLE_DEV, CURRENT_VC, CURRENT_TTY
+ };
+
+ for (fd = 2; fd >= 0; fd--) {
+ int fd4name;
+ int choise_fd;
+ char arg;
+
+ fd4name = open_a_console(choise_console_names[fd]);
+ chk_std:
+ choise_fd = fd4name >= 0 ? fd4name : fd;
+
+ arg = 0;
+ if (ioctl(choise_fd, KDGKBTYPE, &arg) == 0)
+ return choise_fd;
+ if(fd4name >= 0) {
+ close(fd4name);
+ fd4name = -1;
+ goto chk_std;
+ }
+ }
bb_error_msg("Couldn't get a file descriptor referring to the console");
- return -1; /* total failure */
+ return fd; /* total failure */
}