mac data file loading
[btb/d2x.git] / utilities / hogcreate.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 <string.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <dirent.h>
13
14 int
15 main(int argc, char *argv[])
16 {
17         FILE *hogfile, *readfile;
18         DIR *dp;
19         struct dirent *ep;
20         char filename[13];
21         char *buf;
22         struct stat statbuf;
23
24         if (argc != 2) {
25                 printf("Usage: hogcreate hogfile\n"
26                        "creates hogfile using all the files in the current directory\n");
27                 exit(0);
28         }
29         hogfile = fopen(argv[1], "w");
30         buf = (char *)malloc(3);
31         strncpy(buf, "DHF", 3);
32         fwrite(buf, 3, 1, hogfile);
33         printf("Creating: %s\n", argv[1]);
34         free(buf);
35         dp = opendir("./");
36         if (dp != NULL) {
37                 while ((ep = readdir(dp))) {
38                         strcpy(filename, ep->d_name);
39                         stat(filename, &statbuf);
40                         if(! S_ISDIR(statbuf.st_mode)) {
41                                 printf("Filename: %s \tLength: %i\n", filename, (int)statbuf.st_size);
42                                 readfile = fopen(filename, "r");
43                                 buf = (char *)malloc(statbuf.st_size);
44                                 if (buf == NULL) {
45                                         printf("Unable to allocate memery\n");
46                                 } else {
47                                         fwrite(filename, 13, 1, hogfile);
48                                         fwrite(&statbuf.st_size, sizeof(int), 1, hogfile);
49                                         fread(buf, statbuf.st_size, 1, readfile);
50                                         fwrite(buf, statbuf.st_size, 1, hogfile);
51                                 }
52                                 fclose(readfile);
53
54                         }
55                 }
56                 closedir(dp);
57         }
58         fclose(hogfile);
59
60         return 0;
61 }