Blame contrib/splitfont.c

Packit Service 50ad14
/* splitfont: extract characters from font */
Packit Service 50ad14
/* this is for iso fonts, no psf header, just 256 characters */
Packit Service 50ad14
Packit Service 50ad14
#include <stdio.h>
Packit Service 50ad14
#include <fcntl.h>
Packit Service 50ad14
#include <unistd.h>
Packit Service 50ad14
#include <sys/stat.h>
Packit Service 50ad14
Packit Service 50ad14
void dosplit(int from, int to, char *fontbuf, int size, char *fontfile)
Packit Service 50ad14
{
Packit Service 50ad14
	int itemsize = size / 256;
Packit Service 50ad14
	int i, fd;
Packit Service 50ad14
	char *p, *q, s;
Packit Service 50ad14
	char filename[4096];
Packit Service 50ad14
Packit Service 50ad14
	if (from < 0 || from > 255 || to < 0 || to > 255) {
Packit Service 50ad14
		fprintf(stderr, "splitfont: bad argument %s,%s\n",
Packit Service 50ad14
		        from, to);
Packit Service 50ad14
		exit(1);
Packit Service 50ad14
	}
Packit Service 50ad14
	if (strlen(fontfile) >= sizeof(filename) - 4) {
Packit Service 50ad14
		fprintf(stderr, "splitfont: ridiculously long name\n");
Packit Service 50ad14
		exit(1);
Packit Service 50ad14
	}
Packit Service 50ad14
	while (from <= to) {
Packit Service 50ad14
		sprintf(filename, "%s.%02x", fontfile, from);
Packit Service 50ad14
		if ((fd = open(filename, O_WRONLY | O_CREAT, 0666)) < 0) {
Packit Service 50ad14
			perror("splitfont");
Packit Service 50ad14
			fprintf(stderr, "cannot open %s for writing\n", filename);
Packit Service 50ad14
		}
Packit Service 50ad14
		p = &fontbuf[from * itemsize];
Packit Service 50ad14
		if (write(fd, p, itemsize) != itemsize) {
Packit Service 50ad14
			perror("splitfont");
Packit Service 50ad14
			fprintf(stderr, "error writing %s\n", filename);
Packit Service 50ad14
		}
Packit Service 50ad14
		close(fd);
Packit Service 50ad14
		from++;
Packit Service 50ad14
	}
Packit Service 50ad14
}
Packit Service 50ad14
Packit Service 50ad14
int main(int argc, char **argv)
Packit Service 50ad14
{
Packit Service 50ad14
	struct stat statbuf;
Packit Service 50ad14
	char fontbuf[4096];
Packit Service 50ad14
	int fd;
Packit Service 50ad14
	char *p, *q;
Packit Service 50ad14
	int from, to;
Packit Service 50ad14
Packit Service 50ad14
	if (argc != 3) {
Packit Service 50ad14
		fprintf(stderr, "call: splitfont fontfile 17,23-30,...\n");
Packit Service 50ad14
		exit(1);
Packit Service 50ad14
	}
Packit Service 50ad14
	if (stat(argv[1], &statbuf)) {
Packit Service 50ad14
		perror("splitfont");
Packit Service 50ad14
		fprintf(stderr, "cannot stat fontfile %s", argv[1]);
Packit Service 50ad14
		exit(1);
Packit Service 50ad14
	}
Packit Service 50ad14
	if (statbuf.st_size > 4096) {
Packit Service 50ad14
		fprintf(stderr, "splitfont: file unexpectedly large\n");
Packit Service 50ad14
		exit(1);
Packit Service 50ad14
	}
Packit Service 50ad14
	if (statbuf.st_size % 256) {
Packit Service 50ad14
		fprintf(stderr, "splitfont: file size not a multiple of 256\n");
Packit Service 50ad14
		exit(1);
Packit Service 50ad14
	}
Packit Service 50ad14
	if ((fd = open(argv[1], O_RDONLY)) < 0) {
Packit Service 50ad14
		perror("splitfont");
Packit Service 50ad14
		fprintf(stderr, "cannot open fontfile %s", argv[1]);
Packit Service 50ad14
		exit(1);
Packit Service 50ad14
	}
Packit Service 50ad14
	if (read(fd, fontbuf, statbuf.st_size) != statbuf.st_size) {
Packit Service 50ad14
		perror("splitfont");
Packit Service 50ad14
		fprintf(stderr, "error reading fontfile %s", argv[1]);
Packit Service 50ad14
		exit(1);
Packit Service 50ad14
	}
Packit Service 50ad14
Packit Service 50ad14
	p = argv[2];
Packit Service 50ad14
	while (1) {
Packit Service 50ad14
		to = from = strtoul(p, &q, 0);
Packit Service 50ad14
		if (*q == '-') {
Packit Service 50ad14
			p  = q + 1;
Packit Service 50ad14
			to = strtoul(p, &q, 0);
Packit Service 50ad14
		}
Packit Service 50ad14
		if (*q && *q != ',') {
Packit Service 50ad14
			fprintf(stderr, "splitfont: garbage in %s\n", p);
Packit Service 50ad14
			exit(1);
Packit Service 50ad14
		}
Packit Service 50ad14
		dosplit(from, to, fontbuf, statbuf.st_size, argv[1]);
Packit Service 50ad14
		if (!*q)
Packit Service 50ad14
			break;
Packit Service 50ad14
		p = q + 1;
Packit Service 50ad14
	}
Packit Service 50ad14
	return 0;
Packit Service 50ad14
}