Mercurial > hg-old > index.cgi
comparison lwar/extract.c @ 188:bb2665c7005c
Added --extract and --replace to lwar
author | lost |
---|---|
date | Sun, 22 Mar 2009 06:51:48 +0000 |
parents | |
children | bae1e3ecdce1 |
comparison
equal
deleted
inserted
replaced
187:857cb407229e | 188:bb2665c7005c |
---|---|
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 #ifdef HAVE_CONFIG_H | |
23 #include "config.h" | |
24 #endif | |
25 | |
26 #include <errno.h> | |
27 #include <stdio.h> | |
28 #include <stdlib.h> | |
29 #include <string.h> | |
30 | |
31 #include "lwar.h" | |
32 | |
33 void do_extract(void) | |
34 { | |
35 FILE *f; | |
36 char buf[8]; | |
37 long l; | |
38 int c; | |
39 char fnbuf[1024]; | |
40 int i; | |
41 FILE *nf; | |
42 | |
43 f = fopen(archive_file, "r"); | |
44 if (!f) | |
45 { | |
46 perror("Opening archive file"); | |
47 exit(1); | |
48 } | |
49 | |
50 fread(buf, 1, 6, f); | |
51 if (memcmp("LWAR1V", buf, 6)) | |
52 { | |
53 fprintf(stderr, "%s is not a valid archive file.\n", archive_file); | |
54 exit(1); | |
55 } | |
56 | |
57 for (;;) | |
58 { | |
59 c = fgetc(f); | |
60 if (ferror(f)) | |
61 { | |
62 perror("Reading archive file"); | |
63 exit(1); | |
64 } | |
65 if (c == EOF) | |
66 return; | |
67 | |
68 | |
69 // find the end of the file name | |
70 if (!c) | |
71 return; | |
72 | |
73 i = 0; | |
74 while (c) | |
75 { | |
76 fnbuf[i++] = c; | |
77 c = fgetc(f); | |
78 if (c == EOF || ferror(f)) | |
79 { | |
80 fprintf(stderr, "Bad archive file\n"); | |
81 exit(1); | |
82 } | |
83 } | |
84 fnbuf[i] = 0; | |
85 | |
86 // get length of archive member | |
87 l = 0; | |
88 c = fgetc(f); | |
89 l = c << 24; | |
90 c = fgetc(f); | |
91 l |= c << 16; | |
92 c = fgetc(f); | |
93 l |= c << 8; | |
94 c = fgetc(f); | |
95 l |= c; | |
96 | |
97 for (i = 0; i < nfiles; i++) | |
98 { | |
99 if (!strcmp(files[i], fnbuf)) | |
100 break; | |
101 } | |
102 if (i < nfiles || nfiles == 0) | |
103 { | |
104 // extract the file | |
105 nf = fopen(fnbuf, "w"); | |
106 if (!nf) | |
107 { | |
108 fprintf(stderr, "Cannot extract '%s': %s\n", fnbuf, strerror(errno)); | |
109 exit(1); | |
110 } | |
111 while (l) | |
112 { | |
113 c = fgetc(f); | |
114 fputc(c, nf); | |
115 l--; | |
116 } | |
117 fclose(nf); | |
118 } | |
119 else | |
120 { | |
121 // skip the file | |
122 fseek(f, l, SEEK_CUR); | |
123 } | |
124 } | |
125 } |