moved utility to utilities to avoid conflict with c++ std lib <utility>
[btb/d2x.git] / utilities / hogextract.c
1 /*
2  * Written 1999 Jan 29 by Josh Cogliati
3  * I grant this program to public domain.
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11
12 int
13 main(int argc, char *argv[])
14 {
15         FILE *hogfile, *writefile;
16         int len;
17         char filename[13];
18         char *buf;
19         struct stat statbuf;
20
21         if (argc != 2) {
22                 printf("Usage: hogextract hogfile\n"
23                        "extracts all the files in hogfile into the current directory\n");
24                 exit(0);
25         }
26         hogfile = fopen(argv[1], "r");
27         stat(argv[1], &statbuf);
28         printf("%i\n", (int)statbuf.st_size);
29         buf = (char *)malloc(3);
30         fread(buf, 3, 1, hogfile);
31         printf("Extracting from: %s\n", argv[1]);
32         free(buf);
33         while(ftell(hogfile)<statbuf.st_size) {
34                 fread(filename, 13, 1, hogfile);
35                 fread(&len, sizeof(int), 1, hogfile);
36                 printf("Filename: %s \tLength: %i\n", filename, len);
37                 buf = (char *)malloc(len);
38                 if (buf == NULL) {
39                         printf("Unable to allocate memory\n");
40                 } else {
41                         fread(buf, len, 1, hogfile);
42                         writefile = fopen(filename, "w");
43                         fwrite(buf, len, 1, writefile);
44                         fclose(writefile);
45                         free(buf);
46                 }
47         }
48         fclose(hogfile);
49
50         return 0;
51 }