diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2003-09-01 08:53:32 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2003-09-01 08:53:32 +0000 |
commit | 005f83adf511ab89296639abbef737ad4e5826f9 (patch) | |
tree | 0b6c4e7e592bb6f150be66ec072002d2e85fd4dc /libbb/get_console.c | |
parent | d9461f887ee967d3d9e55321b5eee5b88e1dc415 (diff) | |
download | busybox-w32-005f83adf511ab89296639abbef737ad4e5826f9.tar.gz busybox-w32-005f83adf511ab89296639abbef737ad4e5826f9.tar.bz2 busybox-w32-005f83adf511ab89296639abbef737ad4e5826f9.zip |
Fix compile error and reducing size for libbb/get_console.c to previous size.
Vodz last_patch106
Diffstat (limited to 'libbb/get_console.c')
-rw-r--r-- | libbb/get_console.c | 72 |
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 @@ | |||
2 | /* | 2 | /* |
3 | * Utility routines. | 3 | * Utility routines. |
4 | * | 4 | * |
5 | * Copyright (C) many different people. | 5 | * Copyright (C) many different people. If you wrote this, please |
6 | * If you wrote this, please acknowledge your work. | 6 | * acknowledge your work. |
7 | * | 7 | * |
8 | * This program is free software; you can redistribute it and/or modify | 8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by | 9 | * it under the terms of the GNU General Public License as published by |
@@ -29,27 +29,15 @@ | |||
29 | 29 | ||
30 | 30 | ||
31 | 31 | ||
32 | |||
33 | |||
34 | /* From <linux/kd.h> */ | 32 | /* From <linux/kd.h> */ |
35 | static const int KDGKBTYPE = 0x4B33; /* get keyboard type */ | 33 | static const int KDGKBTYPE = 0x4B33; /* get keyboard type */ |
36 | static const int KB_84 = 0x01; | ||
37 | static const int KB_101 = 0x02; /* this is what we always answer */ | ||
38 | 34 | ||
39 | static int is_a_console(int fd) | ||
40 | { | ||
41 | char arg; | ||
42 | 35 | ||
43 | arg = 0; | 36 | static int open_a_console(const char *fnam) |
44 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 | ||
45 | && ((arg == KB_101) || (arg == KB_84))); | ||
46 | } | ||
47 | |||
48 | static int open_a_console(char *fnam) | ||
49 | { | 37 | { |
50 | int fd; | 38 | int fd; |
51 | 39 | ||
52 | /* try read-only */ | 40 | /* try read-write */ |
53 | fd = open(fnam, O_RDWR); | 41 | fd = open(fnam, O_RDWR); |
54 | 42 | ||
55 | /* if failed, try read-only */ | 43 | /* if failed, try read-only */ |
@@ -60,17 +48,6 @@ static int open_a_console(char *fnam) | |||
60 | if (fd < 0 && errno == EACCES) | 48 | if (fd < 0 && errno == EACCES) |
61 | fd = open(fnam, O_WRONLY); | 49 | fd = open(fnam, O_WRONLY); |
62 | 50 | ||
63 | /* if failed, fail */ | ||
64 | if (fd < 0) | ||
65 | return -1; | ||
66 | |||
67 | /* if not a console, fail */ | ||
68 | if (!is_a_console(fd)) { | ||
69 | close(fd); | ||
70 | return -1; | ||
71 | } | ||
72 | |||
73 | /* success */ | ||
74 | return fd; | 51 | return fd; |
75 | } | 52 | } |
76 | 53 | ||
@@ -78,32 +55,37 @@ static int open_a_console(char *fnam) | |||
78 | * Get an fd for use with kbd/console ioctls. | 55 | * Get an fd for use with kbd/console ioctls. |
79 | * We try several things because opening /dev/console will fail | 56 | * We try several things because opening /dev/console will fail |
80 | * if someone else used X (which does a chown on /dev/console). | 57 | * if someone else used X (which does a chown on /dev/console). |
81 | * | ||
82 | * if tty_name is non-NULL, try this one instead. | ||
83 | */ | 58 | */ |
84 | 59 | ||
85 | int get_console_fd(void) | 60 | int get_console_fd(void) |
86 | { | 61 | { |
87 | int fd; | 62 | int fd; |
88 | 63 | ||
89 | fd = open_a_console(CURRENT_TTY); | 64 | static const char * const choise_console_names[] = { |
90 | if (fd >= 0) | 65 | CONSOLE_DEV, CURRENT_VC, CURRENT_TTY |
91 | return fd; | 66 | }; |
92 | 67 | ||
93 | fd = open_a_console(CURRENT_VC); | 68 | for (fd = 2; fd >= 0; fd--) { |
94 | if (fd >= 0) | 69 | int fd4name; |
95 | return fd; | 70 | int choise_fd; |
96 | 71 | char arg; | |
97 | fd = open_a_console(CONSOLE_DEV); | 72 | |
98 | if (fd >= 0) | 73 | fd4name = open_a_console(choise_console_names[fd]); |
99 | return fd; | 74 | chk_std: |
100 | 75 | choise_fd = fd4name >= 0 ? fd4name : fd; | |
101 | for (fd = 0; fd < 3; fd++) | 76 | |
102 | if (is_a_console(fd)) | 77 | arg = 0; |
103 | return fd; | 78 | if (ioctl(choise_fd, KDGKBTYPE, &arg) == 0) |
79 | return choise_fd; | ||
80 | if(fd4name >= 0) { | ||
81 | close(fd4name); | ||
82 | fd4name = -1; | ||
83 | goto chk_std; | ||
84 | } | ||
85 | } | ||
104 | 86 | ||
105 | bb_error_msg("Couldn't get a file descriptor referring to the console"); | 87 | bb_error_msg("Couldn't get a file descriptor referring to the console"); |
106 | return -1; /* total failure */ | 88 | return fd; /* total failure */ |
107 | } | 89 | } |
108 | 90 | ||
109 | 91 | ||