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
|
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <f65.h>
#include "unalf.h"
FILE *in_file, *out_file;
char *in_filename, *self;
opts_t opts;
const char *exclude_globs[MAX_EXCLUDES];
int exclude_count;
char * const *include_globs;
static void create_outdir(void);
static void set_self(char *argv0) {
char *p;
self = argv0;
p = strrchr(self, '/');
if(!p) p = strrchr(self, '\\'); // windows exe needs this
if(p) self = p + 1;
}
/* like "mkdir -p" (no error if dir already exists),
followed by "cd" (which will error if the existing
"directory" turns out to be a file or broken symlink */
static void create_outdir(void) {
int r;
r =
#if defined(__MINGW32__) || defined(__MINGW64__)
mkdir(opts.outdir);
#else
mkdir(opts.outdir, 0777);
#endif
if(r < 0 && errno != EEXIST) {
fprintf(stderr, "%s: ", self);
perror(opts.outdir);
exit(1);
}
if(chdir(opts.outdir) < 0) {
fprintf(stderr, "%s: ", self);
perror(opts.outdir);
exit(1);
}
}
void usage(void) {
extern char *usage_msg[];
char **line;
puts("unalf (ALF extractor) v" VERSION " by B. Watson. WTFPL.");
printf("Usage: %s -[options] <file> [wildcard ...]\n", self);
puts("Options:");
puts(" wildcards: extract only matching files.");
for(line = usage_msg; *line; line++)
puts(*line);
exit(0);
}
int main(int argc, char **argv) {
set_self(argv[0]);
if(argc < 2 || !strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")) usage();
if(!strcmp(argv[1], "--version")) {
puts(VERSION);
exit(0);
}
parse_opts(argc, argv);
if(!(in_file = fopen(in_filename, "rb"))) {
fprintf(stderr, "%s: ", self);
perror(in_filename);
exit(1);
}
if(opts.outdir) create_outdir();
if(opts.listonly)
list_alf();
else
extract_alf();
exit(bad_checksum_count ? 2 : 0);
}
|