]> icculus.org git repositories - crow/jumpnbump.git/blob - modify/jnbunpack.c
Removed unnecessary comment from license text which got there by a copy&paste error...
[crow/jumpnbump.git] / modify / jnbunpack.c
1 /*
2  * unpack.c
3  * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
4  * 
5  * Copyright (C) 2001 "timecop" <timecop@japan.co.jp>
6  *
7  * Copyright (C) 2002 Florian Schulze <crow@icculus.org>
8  *
9  * This file is part of Jump'n'Bump.
10  *
11  * Jump'n'Bump is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * Jump'n'Bump is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #ifndef _MSC_VER
32 #include <unistd.h>
33 #else
34 #include <io.h>
35 #endif
36
37 typedef struct {
38     char filename[12];
39     unsigned int offset;
40     unsigned int size;
41 } DirEntry;
42
43 #ifndef O_BINARY
44 #define O_BINARY 0
45 #endif
46
47 int main(int argc, char **argv)
48 {
49     int fd;
50     DirEntry *datafile;
51     int num_entries, i;
52
53     if (argc < 2) {
54         printf("dumbass, specify filename to unpack\n");
55         exit(1);
56     }
57
58     fd = open(argv[1], O_RDONLY | O_BINARY);
59     if (fd == -1) {
60         perror("open datafile");
61         exit(1);
62     }
63     /* get number of entries */
64     read(fd, &num_entries, 4);
65
66     printf("%d entries in datafile\n", num_entries);
67
68     datafile = calloc(num_entries, sizeof(DirEntry));
69     read(fd, datafile, num_entries * sizeof(DirEntry));
70     printf("Directory Listing:\n");
71     for (i = 0; i < num_entries; i++) {
72         char filename[14];
73         memset(filename, 0, sizeof(filename));
74         strncpy(filename, datafile[i].filename, 12);
75         printf("%02d:\t%s (%u bytes)\n", i, filename,
76                 datafile[i].size);
77     }
78
79     for (i = 0; i < num_entries; i++) {
80         int outfd;
81         char filename[14];
82         char *buf;
83         memset(filename, 0, sizeof(filename));
84         strncpy(filename, datafile[i].filename, 12);
85         printf("Extracting %s ", filename);
86
87         outfd = open(filename, O_RDWR | O_CREAT | O_BINARY, 0644);
88         if (!outfd) {
89             perror("cant open file");
90             exit(1);
91         }
92         lseek(fd, datafile[i].offset, SEEK_SET);
93         buf = calloc(1, datafile[i].size + 16);
94         read(fd, buf, datafile[i].size);
95         write(outfd, buf, datafile[i].size);
96         close(outfd);
97         free(buf);
98         printf("OK\n");
99     }
100     close(fd);
101     return 0;
102 }