]> icculus.org git repositories - btb/d2x.git/blob - utilities/hogcreate.c
fix a bug introduced with change of 2004-06-26
[btb/d2x.git] / utilities / hogcreate.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 #ifdef HAVE_CONFIG_H
8 #include <conf.h>
9 #endif
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <dirent.h>
18
19 #define SWAPINT(x)   (((x)<<24) | (((uint)(x)) >> 24) | (((x) &0x0000ff00) << 8) | (((x) & 0x00ff0000) >> 8))
20
21 int
22 main(int argc, char *argv[])
23 {
24         FILE *hogfile, *readfile;
25         DIR *dp;
26         struct dirent *ep;
27         char filename[13];
28         char *buf;
29         struct stat statbuf;
30         int tmp;
31
32         if (argc != 2) {
33                 printf("Usage: hogcreate hogfile\n"
34                        "creates hogfile using all the files in the current directory\n");
35                 exit(0);
36         }
37         hogfile = fopen(argv[1], "wb");
38         buf = (char *)malloc(3);
39         strncpy(buf, "DHF", 3);
40         fwrite(buf, 3, 1, hogfile);
41         printf("Creating: %s\n", argv[1]);
42         free(buf);
43         dp = opendir("./");
44         if (dp != NULL) {
45                 while ((ep = readdir(dp))) {
46                         if (strlen(ep->d_name) > 12) {
47                                 fprintf(stderr, "error: filename %s too long! (12 chars max!)\n", ep->d_name);
48                                 return 1;
49                         }
50                         memset(filename, 0, 13);
51                         strcpy(filename, ep->d_name);
52                         stat(filename, &statbuf);
53                         if(! S_ISDIR(statbuf.st_mode)) {
54                                 printf("Filename: %s \tLength: %i\n", filename, (int)statbuf.st_size);
55                                 readfile = fopen(filename, "rb");
56                                 buf = (char *)malloc(statbuf.st_size);
57                                 if (buf == NULL) {
58                                         printf("Unable to allocate memery\n");
59                                 } else {
60                                         fwrite(filename, 13, 1, hogfile);
61                                         tmp = (int)statbuf.st_size;
62 #ifdef WORDS_BIGENDIAN
63                                         tmp = SWAPINT(tmp);
64 #endif
65                                         fwrite(&tmp, 4, 1, hogfile);
66                                         fread(buf, statbuf.st_size, 1, readfile);
67                                         fwrite(buf, statbuf.st_size, 1, hogfile);
68                                 }
69                                 fclose(readfile);
70
71                         }
72                 }
73                 closedir(dp);
74         }
75         fclose(hogfile);
76
77         return 0;
78 }