3 * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
5 * Copyright (C) 2001 "timecop" <timecop@japan.co.jp>
7 * Copyright (C) 2002 Florian Schulze <crow@icculus.org>
9 * This file is part of Jump'n'Bump.
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.
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.
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
29 #include <sys/types.h>
51 int main(int argc
, char **argv
)
61 while ((c
= getopt(argc
, argv
, "o:")) != EOF
) {
65 outfile
= strdup(optarg
);
75 if (argv
[c
][0]=='-') {
76 if (argv
[c
][1]=='o') {
77 if ( ((c
+1)<argc
) && (argv
[c
+1]) ) {
78 outfile
= strdup(argv
[c
+1]);
91 if (outfile
== NULL
) {
92 printf("You must specify output filename with -o\n");
96 printf("You must specify some files to pack, duh\n");
101 /* prepare to pack things - get sizes and calculate offsets */
102 printf("%u files to pack\n", num_entries
);
103 datafile
= calloc(num_entries
, sizeof(DirEntry
));
105 /* skip past the directory structure */
106 offset
= 4 + (num_entries
* 20);
108 for (i
= 0; i
< num_entries
; i
++) {
110 if (stat(argv
[i
], &dummy
)) {
111 fprintf(stderr
, "%s is not accessible: ", argv
[i
]);
115 if (strlen(argv
[i
]) > 12) {
116 fprintf(stderr
, "filename %s is longer than 12 chars\n", argv
[i
]);
119 strncpy(datafile
[i
].filename
, argv
[i
], 12);
120 datafile
[i
].size
= dummy
.st_size
;
121 /* num_entries * (12 + 8) */
122 datafile
[i
].offset
= offset
;
123 offset
+= datafile
[i
].size
;
126 /* here, we checked that all files are ok, and ready to roll the packfile */
127 fd
= open(outfile
, O_RDWR
| O_CREAT
| O_BINARY
, 0644);
129 perror("opening packfile");
132 printf("Opened %s\n", outfile
);
134 /* write number of entries in this data file */
138 temp
= (num_entries
>> 0) & 0xff;
140 temp
= (num_entries
>> 8) & 0xff;
142 temp
= (num_entries
>> 16) & 0xff;
144 temp
= (num_entries
>> 24) & 0xff;
148 /* write the directory structure */
149 for (i
= 0; i
< num_entries
; i
++) {
152 write(fd
, &datafile
[i
].filename
, 12);
153 temp
= (datafile
[i
].offset
>> 0) & 0xff;
155 temp
= (datafile
[i
].offset
>> 8) & 0xff;
157 temp
= (datafile
[i
].offset
>> 16) & 0xff;
159 temp
= (datafile
[i
].offset
>> 24) & 0xff;
161 temp
= (datafile
[i
].size
>> 0) & 0xff;
163 temp
= (datafile
[i
].size
>> 8) & 0xff;
165 temp
= (datafile
[i
].size
>> 16) & 0xff;
167 temp
= (datafile
[i
].size
>> 24) & 0xff;
171 /* write in the actual files */
172 for (i
= 0; i
< num_entries
; i
++) {
176 printf("adding %s ", argv
[i
]);
178 infd
= open(argv
[i
], O_RDONLY
| O_BINARY
);
180 perror("opening file");
183 buf
= malloc(datafile
[i
].size
+ 16);
184 read(infd
, buf
, datafile
[i
].size
);
186 write(fd
, buf
, datafile
[i
].size
);