Create SFFS library out of HGFS
[minix.git] / commands / loadfont / loadfont.c
blob45538e0217f861b62ff7dec574ef994066cecbbc
1 /* loadfont.c - Load custom font into EGA, VGA video card
3 * Author: Hrvoje Stipetic (hs@hck.hr) Jun-1995.
5 */
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <termios.h>
14 #include <sys/ioctl.h>
16 void tell(char *s)
18 write(2, s, strlen(s));
21 char *itoa(unsigned i)
23 static char a[3*sizeof(int)];
24 char *p = a+sizeof(a)-1;
26 do {
27 *--p = '0' + i%10;
28 } while ((i /= 10) > 0);
30 return p;
33 void report(char *say)
35 int err = errno;
36 tell("loadfont: ");
37 if (say != NULL) {
38 tell(say);
39 tell(": ");
41 tell(strerror(err));
42 tell("\n");
45 void usage(void)
47 tell("Usage: loadfont fontfile\n");
48 exit(1);
52 int main(int argc, char *argv[])
54 static u8_t font[256][32];
55 static u8_t font_file[256 * (16+14+8) + 1];
56 u8_t *ff;
57 int fd, size, tsize, ch, ln;
58 struct winsize ws;
61 if (argc != 2)
62 usage();
64 if ((fd = open(argv[1], O_RDONLY)) < 0) {
65 report(argv[1]);
66 exit(1);
69 switch (read(fd, font_file, sizeof(font_file))) {
70 case 256 * 8:
71 size = 8;
72 break;
73 case 256 * 14:
74 size = 14;
75 break;
76 case 256 * 16:
77 size = 16;
78 break;
79 case 256 * (16+14+8):
80 size = 0;
81 break;
82 case -1:
83 report(argv[1]);
84 exit(1);
85 default:
86 tell("loadfont: ");
87 tell(argv[1]);
88 tell(": fontfile is not an 8x8, 8x14, 8x16, or compound font\n");
89 exit(1);
91 close(fd);
93 if (ioctl(0, TIOCGWINSZ, &ws) < 0 || (errno= ENOTTY, ws.ws_row == 0)) {
94 report(NULL);
95 exit(1);
97 tsize = ws.ws_ypixel / ws.ws_row;
99 if (size == 0) {
100 if (tsize >= 16) {
101 ff = font_file + 256 * (0);
102 } else
103 if (tsize >= 14) {
104 ff = font_file + 256 * (16);
105 } else {
106 ff = font_file + 256 * (16 + 14);
108 size = tsize;
109 } else {
110 ff = font_file;
113 for (ch = 0; ch < 256; ch++) {
114 for (ln = 0; ln < size; ln++) font[ch][ln] = ff[ch * size + ln];
117 if (ioctl(0, TIOCSFON, font) < 0) {
118 report(NULL);
119 exit(1);
121 exit(0);