annotate lwar/add.c @ 172:47427342e41d

lwar now creates, lists, and adds to archive files
author lost
date Sun, 01 Mar 2009 22:59:52 +0000
parents
children cc41ccee8f64
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
172
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
1 /*
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
2 add.c
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
4
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
5 This file is part of LWAR.
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
6
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
7 LWAR is free software: you can redistribute it and/or modify it under the
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
10 version.
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
11
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
15 more details.
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
16
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
19
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
20
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
21 Implements the program startup code
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
22
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
23 */
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
24
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
26 #include "config.h"
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
27 #endif
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
28
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
29 #include <errno.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
30 #include <stdio.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
31 #include <stdlib.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
32
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
33 #include "lwar.h"
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
34
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
35 void do_add(void)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
36 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
37 FILE *f;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
38 unsigned char buf[8];
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
39 long l;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
40 int c;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
41 FILE *f2;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
42 int i;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
43
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
44 f = fopen(archive_file, "r+");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
45 if (!f)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
46 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
47 if (errno == ENOENT)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
48 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
49 f = fopen(archive_file, "w");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
50 if (f)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
51 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
52 fputs("LWAR1V", f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
53 goto doadd;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
54 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
55 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
56 perror("Cannot open archive file");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
57 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
58
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
59 fread(buf, 1, 6, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
60 if (memcmp("LWAR1V", buf, 6))
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
61 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
62 fprintf(stderr, "%s is not a valid archive file.\n", archive_file);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
63 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
64 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
65
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
66 for (;;)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
67 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
68 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
69 if (c == EOF && ferror(f))
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
70 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
71 perror("Reading archive file");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
72 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
73 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
74 if (c == EOF)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
75 goto doadd;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
76
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
77 if (!c)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
78 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
79 fseek(f, -1, SEEK_CUR);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
80 goto doadd;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
81 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
82
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
83 // find the end of the file name
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
84 while (c)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
85 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
86 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
87 if (c == EOF || ferror(f))
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
88 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
89 fprintf(stderr, "Bad archive file\n");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
90 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
91 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
92 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
93
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
94 // get length of archive member
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
95 l = 0;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
96 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
97 l = c << 24;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
98 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
99 l |= c << 16;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
100 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
101 l |= c << 8;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
102 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
103 l |= c;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
104
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
105 fseek(f, l, SEEK_CUR);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
106 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
107 // back up to the NUL byte at the end of the file
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
108 fseek(f, -1, SEEK_CUR);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
109 doadd:
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
110 for (i = 0; i < nfiles; i++)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
111 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
112 f2 = fopen(files[i], "r");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
113 if (!f2)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
114 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
115 fprintf(stderr, "Cannot open file %s:", files[i]);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
116 perror("");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
117 exit(1);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
118 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
119 fseek(f2, 0, SEEK_END);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
120 l = ftell(f2);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
121 fseek(f2, 0, SEEK_SET);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
122 fputs(files[i], f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
123 fputc(0, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
124 fputc(l >> 24, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
125 fputc((l >> 16) & 0xff, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
126 fputc((l >> 8) & 0xff, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
127 fputc(l & 0xff, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
128 while (l)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
129 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
130 c = fgetc(f2);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
131 fputc(c, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
132 l--;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
133 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
134 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
135
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
136 // flag end of file
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
137 fputc(0, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
138 }