formatting
[btb/d2x.git] / utility / 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 <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10
11 void
12 main(int argc, char *argv[])
13 {
14         FILE *hogfile, *writefile;
15         int fp, len, fseekret=0;
16         char filename[13];
17         char *buf;
18         struct stat statbuf;
19
20         if (argc != 2) {
21                 printf("Usage: hogextract hogfile\n"
22                        "extracts all the files in hogfile into the current directory\n");
23                 exit(0);
24         }
25         hogfile = fopen(argv[1], "r");
26         stat(argv[1], &statbuf);
27         printf("%i\n", statbuf.st_size);
28         buf = (char *)malloc(3);
29         fread(buf, 3, 1, hogfile);
30         printf("Extracting from: %s\n", argv[1]);
31         free(buf);
32         while(ftell(hogfile)<statbuf.st_size) {
33                 fread(filename, 13, 1, hogfile);
34                 fread(&len, sizeof(int), 1, hogfile);
35                 printf("Filename: %s \tLength: %i\n", filename, len);
36                 buf = (char *)malloc(len);
37                 if (buf == NULL) {
38                         printf("Unable to allocate memory\n");
39                 } else {
40                         fread(buf, len, 1, hogfile);
41                         writefile = fopen(filename, "w");
42                         fwrite(buf, len, 1, writefile);
43                         fclose(writefile);
44                         free(buf);
45                 }
46         }
47         fclose(hogfile);
48 }