annotate lwar/main.c @ 429:33c5bc04ea67

Fixed unitialized memory in lw_stringlist_create()
author lost@l-w.ca
date Sun, 19 Sep 2010 12:32:52 -0600
parents 1c31e9005ff7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 main.c
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWAR.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWAR is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21 Implements the program startup code
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 #include <argp.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
28 #include <errno.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29 #include <stdio.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
30 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 #include <sys/types.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 #include <sys/stat.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33 #include <unistd.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 #include "lwar.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 // command line option handling
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 const char *argp_program_version = "LWAR from " PACKAGE_STRING;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40 char *program_name;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 static error_t parse_opts(int key, char *arg, struct argp_state *state)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 switch (key)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 case 'd':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 // debug
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 debug_level++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 case 'a':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52 // add members
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
53 operation = LWAR_OP_ADD;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 case 'c':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 // create archive
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 operation = LWAR_OP_CREATE;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61 case 'm':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 mergeflag = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 case 'r':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66 // replace members
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 operation = LWAR_OP_REPLACE;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70 case 'l':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71 // list members
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 operation = LWAR_OP_LIST;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 case 'x':
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 // extract members
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 operation = LWAR_OP_EXTRACT;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 case ARGP_KEY_ARG:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 if (archive_file)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 // add archive member to list
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 add_file_name(arg);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 archive_file = arg;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 default:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 return ARGP_ERR_UNKNOWN;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 return 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 static struct argp_option options[] =
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98 { "replace", 'r', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 "Add or replace archive members" },
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 { "extract", 'x', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 "Extract members from the archive" },
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 { "add", 'a', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 "Add members to the archive" },
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104 { "list", 'l', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 "List the contents of the archive" },
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106 { "create", 'c', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 "Create new archive (or truncate existing one)" },
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108 { "merge", 'm', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 "Add the contents of archive arguments instead of the archives themselves" },
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110 { "debug", 'd', 0, 0,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 "Set debug mode"},
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112 { 0 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113 };
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115 static struct argp argp =
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 options,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118 parse_opts,
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 "<archive> [<file> ...]",
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 "LWAR, a library file manager for LWLINK"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 };
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
123 extern void do_list(void);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124 extern void do_add(void);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
125 extern void do_remove(void);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 extern void do_replace(void);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 extern void do_extract(void);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 // main function; parse command line, set up assembler state, and run the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
130 // assembler on the first file
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
131 int main(int argc, char **argv)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
132 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
133 program_name = argv[0];
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
134 argp_parse(&argp, argc, argv, 0, 0, NULL);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
135 if (archive_file == NULL)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
136 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
137 fprintf(stderr, "You must specify an archive file.\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
139 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
140
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141 if (operation == 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
142 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
143 fprintf(stderr, "You must specify an operation.\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
144 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
145 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
146
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147 if (operation == LWAR_OP_LIST || operation == LWAR_OP_REMOVE || operation == LWAR_OP_EXTRACT)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
148 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
149 struct stat stbuf;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
150 // make sure the archive exists
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
151 if (stat(archive_file, &stbuf) < 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
152 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
153 fprintf(stderr, "Cannot open archive file %s:\n", archive_file);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
154 perror("");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
155 exit(2);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
156 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
157 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
158 if (operation == LWAR_OP_CREATE)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
159 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
160 struct stat stbuf;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
161 // check if the archive exists
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
162 if (stat(archive_file, &stbuf) < 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
163 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
164 if (errno != ENOENT)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
165 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
166 fprintf(stderr, "Cannot create archive file %s:\n", archive_file);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
167 perror("");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
168 exit(2);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
169 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
170 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
171 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
172 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
173 if (unlink(archive_file) < 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
174 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
175 fprintf(stderr, "Cannot create archive file %s:\n", archive_file);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
176 perror("");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
177 exit(2);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
178 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
179
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
180 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
181 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
182
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
183 switch (operation)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
184 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
185 case LWAR_OP_LIST:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
186 do_list();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
187 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
188
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
189 case LWAR_OP_ADD:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
190 case LWAR_OP_CREATE:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
191 do_add();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
192 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
193
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
194 case LWAR_OP_REMOVE:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
195 do_remove();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
196 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
197
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
198 case LWAR_OP_REPLACE:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
199 do_replace();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
200 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
201
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
202 case LWAR_OP_EXTRACT:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
203 do_extract();
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
204 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
205 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
206
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
207 exit(0);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
208 }