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