File: splitfont.c

package info (click to toggle)
kbd 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,980 kB
  • sloc: ansic: 14,562; javascript: 3,038; sh: 1,154; pascal: 643; makefile: 614; lex: 542; yacc: 338; perl: 126; python: 96; sed: 6
file content (96 lines) | stat: -rw-r--r-- 2,221 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* splitfont: extract characters from font */
/* this is for iso fonts, no psf header, just 256 characters */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

void dosplit(int from, int to, char *fontbuf, int size, char *fontfile)
{
	int itemsize = size / 256;
	int i, fd;
	char *p, *q, s;
	char filename[4096];

	if (from < 0 || from > 255 || to < 0 || to > 255) {
		fprintf(stderr, "splitfont: bad argument %s,%s\n",
		        from, to);
		exit(1);
	}
	if (strlen(fontfile) >= sizeof(filename) - 4) {
		fprintf(stderr, "splitfont: ridiculously long name\n");
		exit(1);
	}
	while (from <= to) {
		sprintf(filename, "%s.%02x", fontfile, from);
		if ((fd = open(filename, O_WRONLY | O_CREAT, 0666)) < 0) {
			perror("splitfont");
			fprintf(stderr, "cannot open %s for writing\n", filename);
		}
		p = &fontbuf[from * itemsize];
		if (write(fd, p, itemsize) != itemsize) {
			perror("splitfont");
			fprintf(stderr, "error writing %s\n", filename);
		}
		close(fd);
		from++;
	}
}

int main(int argc, char **argv)
{
	struct stat statbuf;
	char fontbuf[4096];
	int fd;
	char *p, *q;
	int from, to;

	if (argc != 3) {
		fprintf(stderr, "call: splitfont fontfile 17,23-30,...\n");
		exit(1);
	}
	if (stat(argv[1], &statbuf)) {
		perror("splitfont");
		fprintf(stderr, "cannot stat fontfile %s", argv[1]);
		exit(1);
	}
	if (statbuf.st_size > 4096) {
		fprintf(stderr, "splitfont: file unexpectedly large\n");
		exit(1);
	}
	if (statbuf.st_size % 256) {
		fprintf(stderr, "splitfont: file size not a multiple of 256\n");
		exit(1);
	}
	if ((fd = open(argv[1], O_RDONLY)) < 0) {
		perror("splitfont");
		fprintf(stderr, "cannot open fontfile %s", argv[1]);
		exit(1);
	}
	if (read(fd, fontbuf, statbuf.st_size) != statbuf.st_size) {
		perror("splitfont");
		fprintf(stderr, "error reading fontfile %s", argv[1]);
		exit(1);
	}

	p = argv[2];
	while (1) {
		to = from = strtoul(p, &q, 0);
		if (*q == '-') {
			p  = q + 1;
			to = strtoul(p, &q, 0);
		}
		if (*q && *q != ',') {
			fprintf(stderr, "splitfont: garbage in %s\n", p);
			exit(1);
		}
		dosplit(from, to, fontbuf, statbuf.st_size, argv[1]);
		if (!*q)
			break;
		p = q + 1;
	}
	return 0;
}