annotate lwar/list.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 bae1e3ecdce1
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 list.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 #include <string.h>
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
33
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
34 #include "lwar.h"
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
35
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
36 void do_list(void)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
37 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
38 FILE *f;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
39 char buf[8];
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
40 long l;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
41 int c;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
42
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
43 f = fopen(archive_file, "r");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
44 if (!f)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
45 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
46 perror("Opening archive file");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
47 exit(1);
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
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
50 fread(buf, 1, 6, f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
51 if (memcmp("LWAR1V", buf, 6))
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
52 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
53 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
54 exit(1);
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
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
57 for (;;)
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 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
60 if (ferror(f))
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 perror("Reading 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 if (c == EOF)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
66 return;
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
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
69 // find the end of the file name
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
70 if (!c)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
71 return;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
72
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
73 while (c)
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
74 {
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
75 putchar(c);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
76 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
77 if (c == EOF || ferror(f))
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 fprintf(stderr, "Bad archive file\n");
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
80 exit(1);
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
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
84 // get length of archive member
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
85 l = 0;
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 l = c << 24;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
88 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
89 l |= c << 16;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
90 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
91 l |= c << 8;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
92 c = fgetc(f);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
93 l |= c;
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
94 printf(": %04lx bytes\n", l);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
95 fseek(f, l, SEEK_CUR);
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
96 }
47427342e41d lwar now creates, lists, and adds to archive files
lost
parents:
diff changeset
97 }