comparison lwar/extract.c @ 0:2c24602be78f

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