File: splitfont.c

package info (click to toggle)
console-tools 1%3A0.2.3-23.3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,228 kB
  • ctags: 1,134
  • sloc: ansic: 12,037; sh: 9,426; yacc: 984; makefile: 380; lex: 287; perl: 127; sed: 93
file content (93 lines) | stat: -rw-r--r-- 2,154 bytes parent folder | download | duplicates (11)
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
/* splitfont: extract characters from font */
/* this is for iso fonts, no psf header, just 256 characters */

#include <stdio.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;
}