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