annotate old-trunk/lwar/extract.c @ 339:eb230fa7d28e

Prepare for migration to hg
author lost
date Fri, 19 Mar 2010 02:54:14 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 extract.c
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWAR.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWAR is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24 #include <errno.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 #include <stdio.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 #include <string.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
28
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29 #include "lwar.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
30
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 void do_extract(void)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33 FILE *f;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 char buf[8];
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 long l;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36 int c;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 char fnbuf[1024];
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 int i;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 FILE *nf;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 f = fopen(archive_file, "r");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 if (!f)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 perror("Opening archive file");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 fread(buf, 1, 6, f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 if (memcmp("LWAR1V", buf, 6))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 fprintf(stderr, "%s is not a valid archive file.\n", archive_file);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
53 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55 for (;;)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 c = fgetc(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 if (ferror(f))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60 perror("Reading archive file");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 if (c == EOF)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 // find the end of the file name
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 if (!c)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71 i = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 while (c)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74 fnbuf[i++] = c;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 c = fgetc(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 if (c == EOF || ferror(f))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78 fprintf(stderr, "Bad archive file\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 fnbuf[i] = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 // get length of archive member
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 l = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 c = fgetc(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 l = c << 24;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 c = fgetc(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 l |= c << 16;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 c = fgetc(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 l |= c << 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92 c = fgetc(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 l |= c;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 for (i = 0; i < nfiles; i++)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 if (!strcmp(files[i], fnbuf))
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 if (i < nfiles || nfiles == 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 // extract the file
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 nf = fopen(fnbuf, "w");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104 if (!nf)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106 fprintf(stderr, "Cannot extract '%s': %s\n", fnbuf, strerror(errno));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 while (l)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 c = fgetc(f);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112 fputc(c, nf);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113 l--;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115 fclose(nf);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 // skip the file
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 fseek(f, l, SEEK_CUR);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
123 }