Add pancyrillic font
[platform/upstream/kbd.git] / src / getfd.c
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 "nls.h"
9 #include "getfd.h"
10
11 static char *conspath[] = {
12         "/proc/self/fd/0",
13         "/dev/tty",
14         "/dev/tty0",
15         "/dev/vc/0",
16         "/dev/systty",
17         "/dev/console",
18         NULL
19 };
20
21 /*
22  * getfd.c
23  *
24  * Get an fd for use with kbd/console ioctls.
25  * We try several things because opening /dev/console will fail
26  * if someone else used X (which does a chown on /dev/console).
27  */
28
29 static int
30 is_a_console(int fd) {
31         char arg;
32
33         arg = 0;
34         return (isatty (fd)
35                 && ioctl(fd, KDGKBTYPE, &arg) == 0
36                 && ((arg == KB_101) || (arg == KB_84)));
37 }
38
39 static int
40 open_a_console(const char *fnam) {
41         int fd;
42
43         /*
44          * For ioctl purposes we only need some fd and permissions
45          * do not matter. But setfont:activatemap() does a write.
46          */
47         fd = open(fnam, O_RDWR);
48         if (fd < 0)
49                 fd = open(fnam, O_WRONLY);
50         if (fd < 0)
51                 fd = open(fnam, O_RDONLY);
52         if (fd < 0)
53                 return -1;
54         return fd;
55 }
56
57 int
58 getfd(const char *fnam) {
59         int fd, i;
60
61         if (fnam) {
62                 if ((fd = open_a_console(fnam)) >= 0) {
63                         if (is_a_console(fd))
64                                 return fd;
65                         close(fd);
66                 }
67                 fprintf(stderr, _("Couldn't open %s\n"), fnam);
68                 exit(1);
69         }
70
71         for (i = 0; conspath[i]; i++) {
72                 if ((fd = open_a_console(conspath[i])) >= 0) {
73                         if (is_a_console(fd))
74                                 return fd;
75                         close(fd);
76                 }
77         }
78
79         for (fd = 0; fd < 3; fd++)
80                 if (is_a_console(fd))
81                         return fd;
82
83         fprintf(stderr,
84                 _("Couldn't get a file descriptor referring to the console\n"));
85
86         /* total failure */
87         exit(1);
88 }