]> icculus.org git repositories - btb/d2x.git/blob - utilities/mvlextract.c
allow specifying file to extract
[btb/d2x.git] / utilities / mvlextract.c
1 /*
2  * Written 1999 Jan 29 by Josh Cogliati
3  * I grant this program to public domain.
4  *
5  * Modified for mvl by Bradley Bell, 2002
6  * All modifications under GPL, version 2 or later
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14
15 #define MAX_FILES 256
16
17 int
18 main(int argc, char *argv[])
19 {
20         FILE *mvlfile, *writefile;
21         int i, nfiles, len[MAX_FILES];
22         char filename[MAX_FILES][13];
23         char *buf;
24         struct stat statbuf;
25         int v = 0;
26
27         if (argc > 1 && !strcmp(argv[1], "v")) {
28                 v = 1;
29                 argc--;
30                 argv++;
31         }
32
33         if (argc != 2) {
34                 printf("Usage: mvlextract [v] mvlfile\n"
35                        "extracts all the files in mvlfile into the current directory\n"
36                            "Options:\n"
37                            "  v    View files, don't extract\n");
38                 exit(0);
39         }
40         mvlfile = fopen(argv[1], "r");
41         stat(argv[1], &statbuf);
42         printf("%i\n", (int)statbuf.st_size);
43         buf = (char *)malloc(4);
44         fread(buf, 4, 1, mvlfile);
45         fread(&nfiles, 4, 1, mvlfile);
46         printf("Extracting from: %s\n", argv[1]);
47         free(buf);
48         for (i = 0; i < nfiles; i++) {
49                 fread(filename[i], 13, 1, mvlfile);
50                 fread(&len[i], 4, 1, mvlfile);
51                 printf("Filename: %s \tLength: %i\n", filename[i], len[i]);
52         }
53
54         if (!v) {
55                 for (i = 0; i < nfiles; i++) {
56                         if (ftell(mvlfile) > statbuf.st_size) {
57                                 printf("Error, end of file\n");
58                                 exit(1);
59                         }
60                         buf = (char *)malloc(len[i]);
61                         if (buf == NULL) {
62                                 printf("Unable to allocate memory\n");
63                         } else {
64                                 fread(buf, len[i], 1, mvlfile);
65                                 writefile = fopen(filename[i], "w");
66                                 fwrite(buf, len[i], 1, writefile);
67                                 fclose(writefile);
68                                 free(buf);
69                         }
70                 }
71         }
72         fclose(mvlfile);
73
74         return 0;
75 }