core: remove reference to a non-existing field of the theme struct
[fbsplash.git] / misc / kbd / getfd.c
blob34a0624ced6759aa9650f0ca7c9363418d6427e2
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <sys/ioctl.h>
7 #include <linux/kd.h>
8 #include "getfd.h"
11 * getfd.c
13 * Get an fd for use with kbd/console ioctls.
14 * We try several things because opening /dev/console will fail
15 * if someone else used X (which does a chown on /dev/console).
18 static int
19 is_a_console(int fd) {
20 char arg;
22 arg = 0;
23 return (ioctl(fd, KDGKBTYPE, &arg) == 0
24 && ((arg == KB_101) || (arg == KB_84)));
27 static int
28 open_a_console(const char *fnam) {
29 int fd;
32 * For ioctl purposes we only need some fd and permissions
33 * do not matter. But setfont:activatemap() does a write.
35 fd = open(fnam, O_RDWR);
36 if (fd < 0 && errno == EACCES)
37 fd = open(fnam, O_WRONLY);
38 if (fd < 0 && errno == EACCES)
39 fd = open(fnam, O_RDONLY);
40 if (fd < 0)
41 return -1;
42 if (!is_a_console(fd)) {
43 close(fd);
44 return -1;
46 return fd;
49 int getfd(const char *fnam) {
50 int fd;
52 if (fnam) {
53 fd = open_a_console(fnam);
54 if (fd >= 0)
55 return fd;
56 fprintf(stderr, "Couldnt open %s\n", fnam);
57 exit(1);
60 fd = open_a_console("/dev/tty");
61 if (fd >= 0)
62 return fd;
64 fd = open_a_console("/dev/tty0");
65 if (fd >= 0)
66 return fd;
68 fd = open_a_console("/dev/vc/0");
69 if (fd >= 0)
70 return fd;
72 fd = open_a_console("/dev/console");
73 if (fd >= 0)
74 return fd;
76 for (fd = 0; fd < 3; fd++)
77 if (is_a_console(fd))
78 return fd;
80 fprintf(stderr, "Couldnt get a file descriptor referring to the console\n");
81 exit(1); /* total failure */