Debian Codegraph @

d2x-rebirth/utilities/mvlcreate.c

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 /*
8 * Written 1999 Jan 29 by Josh Cogliati
9 * Modified by Bradley Bell, 2002, 2003
10 * This program is licensed under the terms of the GPL, version 2 or later
11 */
12
13 #ifdef HAVE_CONFIG_H
14 #include <conf.h>
15 #endif
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <dirent.h>
24
25 #define MAX_FILES 256
26
27 #define SWAPINT(x) (((x)<<24) | (((unsigned)(x)) >> 24) | (((x) &0x0000ff00) << 8) | (((x) & 0x00ff0000) >> 8))
28
29 int
30 main(int argc, char *argv[])
31 {
32 FILE *mvlfile, *readfile;
33 DIR *dp;
34 struct dirent *ep;
35 int i, nfiles = 0, len[MAX_FILES], tmp;
36 char filename[MAX_FILES][13];
37 char *buf;
38 struct stat statbuf;
39
40 if (argc != 2) {
41 printf("Usage: mvlcreate mvlfile\n"
42 "creates mvlfile using all the files in the current directory\n");
43 exit(0);
44 }
45
46 dp = opendir("./");
47 if (dp != NULL) {
48 while ((ep = readdir(dp))) {
49 if (strlen(ep->d_name) > 12) {
50 fprintf(stderr, "error: filename %s too long! (12 chars max!)\n", ep->d_name);
51 return 1;
52 }
53 memset(filename[nfiles], 0, 13);
54 strcpy(filename[nfiles], ep->d_name);
55 stat(filename[nfiles], &statbuf);
56 if(! S_ISDIR(statbuf.st_mode)) {
57 len[nfiles] = (int)statbuf.st_size;
58 printf("Filename: %s \tLength: %i\n", filename[nfiles], len[nfiles]);
59 nfiles++;
60 }
61 }
62 }
63 closedir(dp);
64
65 printf("Creating: %s\n", argv[1]);
66 mvlfile = fopen(argv[1], "wb");
67 buf = (char *)malloc(4);
68 strncpy(buf, "DMVL", 4);
69 fwrite(buf, 4, 1, mvlfile);
70 free(buf);
71
72 tmp = nfiles;
73 #ifdef WORDS_BIGENDIAN
74 tmp = SWAPINT(tmp);
75 #endif
76 fwrite(&tmp, 4, 1, mvlfile);
77
78 for (i = 0; i < nfiles; i++) {
79 fwrite(filename[i], 13, 1, mvlfile);
80 tmp = len[i];
81 #ifdef WORDS_BIGENDIAN
82 tmp = SWAPINT(tmp);
83 #endif
84 fwrite(&tmp, 4, 1, mvlfile);
85 }
86
87 for (i = 0; i < nfiles; i++) {
88 readfile = fopen(filename[i], "rb");
89 buf = (char *)malloc(len[i]);
90 if (buf == NULL) {
91 printf("Unable to allocate memory\n");
92 } else {
93 fread(buf, len[i], 1, readfile);
94 fwrite(buf, len[i], 1, mvlfile);
95 free(buf);
96 }
97 fclose(readfile);
98 }
99
100 fclose(mvlfile);
101
102 return 0;
103 }