-
Notifications
You must be signed in to change notification settings - Fork 2
/
trdos-list.c
75 lines (55 loc) · 1.33 KB
/
trdos-list.c
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
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "formats/scl.c"
#include "formats/trd.c"
#include "printer.c"
#include "trdos.h"
void usage(char *program) {
printf(
"TR-DOS List is an utility to display the TR-DOS image file "
"listing.\n\n");
printf("Usage:\n");
printf(" %s <file.trd>\n", program);
printf(" %s <file.scl>\n", program);
}
extern int errno;
int has_ext(char *filename, const char *ext) {
const char *pos = strrchr(filename, '.');
if (pos == NULL) {
return 1;
}
return strncasecmp(pos + 1, ext, strlen(ext));
}
int list(char *filename) {
FILE *fp;
int result;
img_list img_list;
if (has_ext(filename, "trd") == 0) {
img_list = &trd_list;
} else if (has_ext(filename, "scl") == 0) {
img_list = &scl_list;
} else {
printf("Unsupported file type\n");
return EXIT_FAILURE;
}
fp = fopen(filename, "r");
if (fp == NULL) {
perror("Unable to open file");
return EXIT_FAILURE;
}
result = img_list(fp);
fclose(fp);
if (!result) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
char *filename;
if (argc < 2) {
usage(argv[0]);
return EXIT_FAILURE;
}
return list(argv[1]);
}