]> icculus.org git repositories - btb/d2x.git/blob - utilities/mvlextract.c
load builtin mission correctly
[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 [filename]\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                 if (argc == 2 || !strcmp(argv[2], filename[i]))
52                         printf("Filename: %s \tLength: %i\n", filename[i], len[i]);
53         }
54
55         if (!v) {
56                 for (i = 0; i < nfiles; i++) {
57                         if (argc > 2 && strcmp(argv[2], filename[i]))
58                                 fseek(mvlfile, len[i], SEEK_CUR);
59                         else {
60                                 if (ftell(mvlfile) > statbuf.st_size) {
61                                         printf("Error, end of file\n");
62                                         exit(1);
63                                 }
64                                 buf = (char *)malloc(len[i]);
65                                 if (buf == NULL) {
66                                         printf("Unable to allocate memory\n");
67                                 } else {
68                                         fread(buf, len[i], 1, mvlfile);
69                                         writefile = fopen(filename[i], "w");
70                                         fwrite(buf, len[i], 1, writefile);
71                                         fclose(writefile);
72                                         free(buf);
73                                 }
74                         }
75                 }
76         }
77         fclose(mvlfile);
78
79         return 0;
80 }