]> git.draconx.ca Git - liblbx.git/blob - src/lbxtool.c
liblbx: Rename lbx_stat to lbx_file_stat.
[liblbx.git] / src / lbxtool.c
1 /*
2  *  2ooM: The Master of Orion II Reverse Engineering Project
3  *  Simple command-line tool to extract LBX archive files.
4  *  Copyright (C) 2006-2010 Nick Bowler
5  *
6  *  This program is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <getopt.h>
25 #include <fnmatch.h>
26
27 #include "tools.h"
28 #include "lbx.h"
29
30
31 static void printusage(void)
32 {
33         puts("usage: lbxtool [-l|-x] [-v] [-f path] [file ...]");
34 }
35
36 static void printhelp(void)
37 {
38         printusage();
39         puts("For now, see the man page for detailed help.");
40 }
41
42 static const char *progname;
43 #define errmsg(fmt, ...) (\
44         fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
45 )
46
47 enum {
48         MODE_NONE,
49         MODE_LIST,
50         MODE_EXTRACT,
51 };
52
53 int filematch(char **argv, const char *name)
54 {
55         int i;
56
57         for (i = 0; argv[i]; i++) {
58                 switch(fnmatch(argv[i], name, 0)) {
59                 case 0:
60                         return 0;
61                 case FNM_NOMATCH:
62                         break;
63                 default:
64                         errmsg("error matching glob: %s.\n", argv[i]);
65                         return 1;
66                 }
67         }
68
69         return i ? -1: 0;
70 }
71
72 int list(LBX *lbx, int verbose, char **argv) {
73         size_t nfiles;
74         unsigned int i;
75
76         nfiles = lbx_numfiles(lbx);
77         if (verbose) {
78                 printf("Files in archive: %zu\n", nfiles);
79         }
80
81         for (i = 0; i < nfiles; i++) {
82                 struct lbx_statbuf stat;
83
84                 lbx_file_stat(lbx, i, &stat);
85
86                 switch (filematch(argv, stat.name)) {
87                 case -1: continue;
88                 case  0: break;
89                 default: return EXIT_FAILURE;
90                 }
91
92                 printf("%s", stat.name);
93                 if (verbose) {
94                         printf(" size=%zu bytes", stat.size);
95                 }
96
97                 putchar('\n');
98         }
99
100         return EXIT_SUCCESS;
101 }
102
103 int extract_file(LBXfile *f, const struct lbx_statbuf *stat)
104 {
105         int ret = -1;
106         size_t rc;
107         FILE *of;
108
109         of = fopen(stat->name, "wb");
110         if (!of) {
111                 errmsg("%s: fopen: %s\n",
112                         stat->name, strerror(errno));
113                 return -1;
114         }
115
116         while (1) {
117                 unsigned char buf[1024];
118
119                 rc = lbx_file_read(f, buf, sizeof buf);
120                 if (rc == 0) {
121                         if (lbx_file_eof(f))
122                                 ret = 0;
123                         break;
124                 }
125
126                 if (fwrite(buf, rc, 1, of) != 1) {
127                         errmsg("%s: fwrite: %s\n", stat->name, strerror(errno));
128                         break;
129                 }
130
131                 if (rc < sizeof buf) {
132                         if (lbx_file_eof(f))
133                                 ret = 0;
134                         break;
135                 }
136         }
137
138         if (fclose(of) == EOF) {
139                 errmsg("%s: fclose: %s\n", stat->name, strerror(errno));
140                 return -1;
141         }
142
143         return ret;
144 }
145
146 int extract(LBX *lbx, int verbose, char **argv) {
147         size_t nfiles;
148         unsigned int i;
149
150         nfiles = lbx_numfiles(lbx);
151         if (verbose) {
152                 printf("Files in archive: %zu\n", nfiles);
153         }
154
155         for (i = 0; i < nfiles; i++) {
156                 struct lbx_statbuf stat;
157                 LBXfile *file;
158
159                 lbx_file_stat(lbx, i, &stat);
160
161                 switch (filematch(argv, stat.name)) {
162                 case -1: continue;
163                 case  0: break;
164                 default: return EXIT_FAILURE;
165                 }
166
167                 file = lbx_file_open(lbx, i);
168                 if (!file) {
169                         errmsg("failed to open archive member %s: %s.\n",
170                                 stat.name, lbx_strerror());
171                         continue;
172                 }
173
174                 if (extract_file(file, &stat) == -1)
175                         return EXIT_FAILURE;
176                 lbx_file_close(file);
177         }
178
179         return EXIT_SUCCESS;
180 }
181
182 int main(int argc, char **argv)
183 {
184         int mode = MODE_NONE, verbose = 0, opt, rc = EXIT_FAILURE;
185         struct lbx_pipe_state stdin_handle = { .f = stdin };
186         const char *file = NULL;
187         LBX *lbx;
188
189         static const char         *sopts   = "lxf:i:vV";
190         static const struct option lopts[] = {
191                 { "list",    0, NULL, 'l' },
192                 { "extract", 0, NULL, 'x' },
193
194                 { "file",    1, NULL, 'f' },
195                 { "index",   1, NULL, 'i' },
196
197                 { "verbose", 0, NULL, 'v' },
198
199                 { "version", 0, NULL, 'V' },
200                 { "usage",   0, NULL, 'U' },
201                 { "help",    0, NULL, 'H' },
202
203                 { 0 }
204         };
205
206         progname = "lbxtool"; /* argv[0]; */
207         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
208                 switch(opt) {
209                 case 'l':
210                         mode = MODE_LIST;
211                         break;
212                 case 'x':
213                         mode = MODE_EXTRACT;
214                         break;
215                 case 'f':
216                         file = optarg;
217                         break;
218                 case 'i':
219                         /* FIXME: Add index file support. */
220                         break;
221                 case 'v':
222                         verbose = 1;
223                         break;
224                 case 'V':
225                         puts(VERSION_BOILERPLATE("lbxtool"));
226                         return EXIT_SUCCESS;
227                 case 'U':
228                         printusage();
229                         return EXIT_SUCCESS;
230                 case 'H':
231                         printhelp();
232                         return EXIT_SUCCESS;
233                 default:
234                         return EXIT_FAILURE;
235                 }
236         }
237
238         if (file)
239                 lbx = lbx_fopen(file);
240         else
241                 lbx = lbx_open(&stdin_handle, &lbx_pipe_fops, NULL, "stdin");
242
243         if (!lbx) {
244                 errmsg("failed to open archive: %s.\n", lbx_strerror());
245                 return EXIT_FAILURE;
246         }
247
248         switch (mode) {
249         case MODE_LIST:
250                 rc = list(lbx, verbose, &argv[optind]);
251                 break;
252         case MODE_EXTRACT:
253                 rc = extract(lbx, verbose, &argv[optind]);
254                 break;
255         default:
256                 fprintf(stderr, "%s: you must specify a mode.\n", progname);
257         }
258
259         lbx_close(lbx);
260         return rc;
261 }