annotate lwar/extract.c @ 276:23034db7dd8a 2.5

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