]> icculus.org git repositories - btb/d2x.git/blob - utilities/mvlcreate.c
added mvl utilities
[btb/d2x.git] / utilities / mvlcreate.c
1 /*
2  * Written 1999 Jan 29 by Josh Cogliati
3  * I grant this program to public domain.
4  *
5  * Modified for mvl by Bradley Bell, 2002
6  * All modifications under GPL, version 2 or later
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <dirent.h>
16
17 #define MAX_FILES 256
18
19 int
20 main(int argc, char *argv[])
21 {
22         FILE *mvlfile, *readfile;
23         DIR *dp;
24         struct dirent *ep;
25         int i, nfiles = 0, len[MAX_FILES];
26         char filename[MAX_FILES][13];
27         char *buf;
28         struct stat statbuf;
29
30         if (argc != 2) {
31                 printf("Usage: mvlcreate mvlfile\n"
32                        "creates mvlfile using all the files in the current directory\n");
33                 exit(0);
34         }
35
36         dp = opendir("./");
37         if (dp != NULL) {
38                 while ((ep = readdir(dp))) {
39                         strcpy(filename[i], ep->d_name);
40                         stat(filename[i], &statbuf);
41                         if(! S_ISDIR(statbuf.st_mode)) {
42                                 nfiles++;
43                                 len[i] = (int)statbuf.st_size;
44                                 printf("Filename: %s \tLength: %i\n", filename[i], len[i]);
45                         }
46                 }
47         }
48         closedir(dp);
49
50         printf("Creating: %s\n", argv[1]);
51         mvlfile = fopen(argv[1], "w");
52         buf = (char *)malloc(4);
53         strncpy(buf, "DMVL", 4);
54         fwrite(buf, 4, 1, mvlfile);
55         free(buf);
56
57         fwrite(&nfiles, 4, 1, mvlfile);
58
59         for (i = 0; i < nfiles; i++) {
60                 fwrite(filename[i], 13, 1, mvlfile);
61                 fwrite(&len[i], 4, 1, mvlfile);
62         }
63
64         for (i = 0; i < nfiles; i++) {
65                 readfile = fopen(filename[i], "r");
66                 buf = (char *)malloc(len[i]);
67                 if (buf == NULL) {
68                         printf("Unable to allocate memory\n");
69                 } else {
70                         fread(buf, statbuf.st_size, 1, readfile);
71                         fwrite(buf, statbuf.st_size, 1, mvlfile);
72                 }
73                 fclose(readfile);
74         }
75
76         fclose(mvlfile);
77
78         return 0;
79 }