aboutsummaryrefslogtreecommitdiff
path: root/src/sanity.c
blob: df3cbe2770bcbc5fa67fe09bf63d58afbdc9bc86 (plain)
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
#include <stdio.h>
#include <string.h>
#include "u816.h"

extern const char *self;

void bad_atari_filename(const char *fname, const char *msg) {
	char fn[50] = "";
	char xbuf[5];
	int i;
	u8 c;

	for(i = 0; (c = (u8)fname[i]) && i < 12; i++) {
		if(c < ' ' || c > '|') {
			/* not printable, insert a hex escape */
			sprintf(xbuf, "$%02X", c);
			strcat(fn, xbuf);
		} else {
			strncat(fn, (char *)&c, 1);
		}
	}

	fprintf(stderr, "%s: warning: bad Atari filename \"%s\": %s\n", self, fn, msg);
}

/* note to self: it's tempting to use isalpha(), isprint(), etc
	from ctype.h... but those are locale-aware. we want ASCII-only
	versions. */
void sanity_check_filename(const char *fname) {
	u8 c;
	unsigned int i, bad = 0, dots = 0, uscore = 0;

	c = fname[0];
	if(!c) {
		bad_atari_filename(fname, "empty!");
		return;
	} else if(c < 'A' || c > 'Z') {
		bad_atari_filename(fname, "does not begin with A-Z");
	}

	for(i = 0; (i < 12) && (c = fname[i]); i++) {
		if(c >= 'A' && c <= 'Z')
			continue;
		if(c >= '0' && c <= '9')
			continue;
		if(c == '_') {
			uscore++;
			continue;
		}
		if(c == '.') {
			dots++;
			continue;
		}
		bad++;
	}

	if(!dots) {
		bad_atari_filename(fname, "no \".\"");
	} else if(dots > 1) {
		bad_atari_filename(fname, "more than one \".\"");
	}

	if(bad)
		bad_atari_filename(fname, "invalid characters.");
	else if(uscore)
		fprintf(stderr, "%s: filename has underscore, OK on Sparta/MyDOS, not Atari DOS 2.x\n", self);
}