aboutsummaryrefslogtreecommitdiff
path: root/axe.c
blob: 838a73c1ab0207566b7432ca7544c89c893a0bff (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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* axe - manipulate atari disk images (dos 2.0s only for now)
 * 
 * Usage: ataridir [-a] [-x filename] [-l] [-v] <disk_image.atr>
 * 
 * (-a means list all directory entries, regardless of status byte)
 * -l means dump sector link info for all filenumbers on disk
 * -x means extract file from image
 * -v means hex dump of VTOC sector
 * 
 */

/* indented with: indent -kr -nsai -nsaw -ts3 -i3 -br -brf -brs -cdw -ce */

#include "axe.h"

int all = 0, total_sec, translate = 0;


int main(int argc, char *argv[]) {
#ifdef DEBUG
	int vtoc_dump = 0;
#endif
	int extract = 0, del = 0, dump_sec = 0, print = 1,
		 opt, write = 0, filenum = 0, i, j,
		 lng = 0, tar = 0, blank = 0, create = 0;
	unsigned char buf[256], diskbuf[720 * 128 + 16];
	char newblank[13];
	unsigned char *debuf;
	char filename[13] = { "\0" };
	char fnbuf[13] = "           \0";
	DIR *credir;
	struct dirent *nfile;

	printf("axe (the ATR/XFD Editor) v%s, (c) B. Watson.\n", VERSION);
	printf("Released under the WTFPL.\n\n");
	if(argc < 2)
		usage(argv[0]);
	while((opt = getopt(argc, argv, "ab:c:lvd:D:x:tuw:")) != EOF) {
		switch (opt) {
		case 'a':
			all = 1;
			break;
		case 'b':
			/*write_blank_disk(optarg); */
			blank = 1;
			strcpy(newblank, optarg);
			break;
		case 'c':
			print = 0;
			create = 1;
			strcpy(filename, optarg);
			break;
		case 'd':
			dump_sec = atoi(optarg);
			break;
		case 'l':
			lng = 1;
			break;
		case 'v':
			dump_sec = 360;
			break;
		case 'D':
			del = 1;
			print = 0;
			strcpy(filename, optarg);
			break;
		case 'x':
			strcpy(filename, optarg);
			extract = 1;
			print = 0;
			break;
		case 'w':
			write = 1;
			print = 0;
			strcpy(filename, optarg);
			break;
		case 't':
			tar = 1;
			break;
		case 'u':
			translate = 1;
			break;
		case '?':
		case ':':
			exit(1);
		default:
			printf("How'd I get here?\n");
			exit(255);
		}
	}

	if((create + write + extract + del + tar) > 1) {
		printf("Only one of -t, -w, -c, -x, -D may be specified.\n");
		exit(1);
	}
	if(blank)
		write_blank_disk(newblank);
	if(optind == argc) {
		if(!blank)
			usage(argv[0]);
		exit(!blank);
	}
	printf("Using image file %s\n\n", argv[argc - 1]);
#ifdef DEBUG
	printf("-a:%d  -l:%d\n  -v:%d", all, lng, vtoc_dump);
#endif
	if(filename[0] && (!create)) {
		parse_filename(filename, fnbuf);
	}
#ifdef DEBUG
	printf("Passed filename %s, parsed as %s\n", filename, fnbuf);
#endif
	if(create)
		write_blank_disk(argv[argc - 1]);
	load_disk(diskbuf, argv[argc - 1]);
/*   if(write) {
      if(strcmp(filename,argv[argc-1])==0) {
	 printf("No disk image name given - aborting\n");
	 exit(1);
      }
      write_file(diskbuf,filename);
      write_disk(diskbuf,argv[argc-1]);
      exit(0);
   } */
#ifdef DEBUG
	printf("-t=%d\n", tar);
#endif
	if(create) {
		char msgbuf[256];
		if((credir = opendir(filename)) == NULL) {
			sprintf(msgbuf, "can't open directory %s", filename);
			perror(msgbuf);
			exit(1);
		}
		while((nfile = readdir(credir)) != NULL) {
			if(nfile->d_name[0] == '.')
				continue;
			write_file(diskbuf, nfile->d_name);
		}
		printf("Done.\n");
		write_disk(diskbuf, argv[argc - 1]);
		exit(0);
	}


	if(tar) {
		make_dir(argv[argc - 1]);
	}

	for (i = 361; i < 369; i++) {
		read_sector(diskbuf, i, buf);
		for (j = 0; j < 128; j += 16) {
			debuf = buf + j;
			if((tar) && (debuf[0] & 64) && !(debuf[0] & 128)) {
				strncpy(fnbuf, (char *)(debuf + 5), 11);
				make_filename(fnbuf, filename);
				dump_file(diskbuf, debuf, filename);
			}
			if(print && !(debuf[0] & 128))
				print_entry(debuf);
#ifdef DEBUG
			printf("strncmp: %s, %s\n", fnbuf, debuf + 5);
#endif
			if((strncmp(fnbuf, (char *)(debuf + 5), 11) == 0) && !(debuf[0] & 128)) {
#ifdef DEBUG
				printf("matched filename\n");
#endif
				if(extract)
					dump_file(diskbuf, debuf, filename);
				if(del || write) {
					traverse_file(diskbuf, debuf, TF_DELETE);
					debuf[0] |= 128;
					write_sector(diskbuf, i, buf);
					write_disk(diskbuf, argv[argc - 1]);
				}
			}

			if(lng)
				traverse_file(diskbuf, debuf, TF_PRINT);

			filenum++;
		}
	}
	if(print) {
		read_sector(diskbuf, 360, buf);
		i = buf[3] + 256 * buf[4];
		printf("%03d FREE SECTORS\n", i);
/*      if(i!=(707-total_sec)) printf("Warning: VTOC free sectors != (707 - total_sec), %d vs. %d\n",i,707-total_sec);*/
	}
	if(dump_sec)
		dump_sector(diskbuf, dump_sec);
	if(write) {
		write_file(diskbuf, filename);
		write_disk(diskbuf, argv[argc - 1]);
	}
	exit(0);
}