annotate lwar/extract.c @ 200:3f9d299d2477

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