Mercurial > hg-old > index.cgi
comparison lwar/main.c @ 172:47427342e41d
lwar now creates, lists, and adds to archive files
author | lost |
---|---|
date | Sun, 01 Mar 2009 22:59:52 +0000 |
parents | d610b8aef91b |
children | cc41ccee8f64 |
comparison
equal
deleted
inserted
replaced
171:d610b8aef91b | 172:47427342e41d |
---|---|
28 | 28 |
29 #include <argp.h> | 29 #include <argp.h> |
30 #include <errno.h> | 30 #include <errno.h> |
31 #include <stdio.h> | 31 #include <stdio.h> |
32 #include <stdlib.h> | 32 #include <stdlib.h> |
33 #include <sys/types.h> | |
34 #include <sys/stat.h> | |
35 #include <unistd.h> | |
33 | 36 |
34 #include "lwar.h" | 37 #include "lwar.h" |
35 | 38 |
36 // command line option handling | 39 // command line option handling |
37 const char *argp_program_version = "LWAR from " PACKAGE_STRING; | 40 const char *argp_program_version = "LWAR from " PACKAGE_STRING; |
44 case 'd': | 47 case 'd': |
45 // debug | 48 // debug |
46 debug_level++; | 49 debug_level++; |
47 break; | 50 break; |
48 | 51 |
49 // case ARGP_KEY_ARG: | 52 case 'a': |
50 // | 53 // add members |
54 operation = LWAR_OP_ADD; | |
55 break; | |
56 | |
57 case 'c': | |
58 // create archive | |
59 operation = LWAR_OP_CREATE; | |
60 break; | |
61 | |
62 // case 'r': | |
63 // // remove members | |
64 // operation = LWAR_OP_REMOVE; | |
51 // break; | 65 // break; |
66 | |
67 case 'l': | |
68 // list members | |
69 operation = LWAR_OP_LIST; | |
70 break; | |
71 | |
72 case ARGP_KEY_ARG: | |
73 if (archive_file) | |
74 { | |
75 // add archive member to list | |
76 add_file_name(arg); | |
77 } | |
78 else | |
79 archive_file = arg; | |
80 break; | |
52 | 81 |
53 default: | 82 default: |
54 return ARGP_ERR_UNKNOWN; | 83 return ARGP_ERR_UNKNOWN; |
55 } | 84 } |
56 return 0; | 85 return 0; |
57 } | 86 } |
58 | 87 |
59 static struct argp_option options[] = | 88 static struct argp_option options[] = |
60 { | 89 { |
90 // { "remove", 'r', 0, 0, | |
91 // "Remove members from the archive" }, | |
92 { "add", 'a', 0, 0, | |
93 "Add members to the archive" }, | |
94 { "list", 'l', 0, 0, | |
95 "List the contents of the archive" }, | |
96 { "create", 'c', 0, 0, | |
97 "Create new archive (or truncate existing one)" }, | |
61 { "debug", 'd', 0, 0, | 98 { "debug", 'd', 0, 0, |
62 "Set debug mode"}, | 99 "Set debug mode"}, |
63 { 0 } | 100 { 0 } |
64 }; | 101 }; |
65 | 102 |
66 static struct argp argp = | 103 static struct argp argp = |
67 { | 104 { |
68 options, | 105 options, |
69 parse_opts, | 106 parse_opts, |
70 "<input file> ...", | 107 "<archive> [<file> ...]", |
71 "LWLINK, a HD6309 and MC6809 cross-linker" | 108 "LWAR, a library file manager for LWLINK" |
72 }; | 109 }; |
110 | |
111 extern void do_list(void); | |
112 extern void do_add(void); | |
113 extern void do_remove(void); | |
73 | 114 |
74 // main function; parse command line, set up assembler state, and run the | 115 // main function; parse command line, set up assembler state, and run the |
75 // assembler on the first file | 116 // assembler on the first file |
76 int main(int argc, char **argv) | 117 int main(int argc, char **argv) |
77 { | 118 { |
78 argp_parse(&argp, argc, argv, 0, 0, NULL); | 119 argp_parse(&argp, argc, argv, 0, 0, NULL); |
120 if (archive_file == NULL) | |
121 { | |
122 fprintf(stderr, "You must specify an archive file.\n"); | |
123 exit(1); | |
124 } | |
125 | |
126 if (operation == 0) | |
127 { | |
128 fprintf(stderr, "You must specify an operation.\n"); | |
129 exit(1); | |
130 } | |
131 | |
132 if (operation == LWAR_OP_LIST || operation == LWAR_OP_REMOVE) | |
133 { | |
134 struct stat stbuf; | |
135 // make sure the archive exists | |
136 if (stat(archive_file, &stbuf) < 0) | |
137 { | |
138 fprintf(stderr, "Cannot open archive file %s:\n", archive_file); | |
139 perror(""); | |
140 exit(2); | |
141 } | |
142 } | |
143 if (operation == LWAR_OP_CREATE) | |
144 { | |
145 struct stat stbuf; | |
146 // check if the archive exists | |
147 if (stat(archive_file, &stbuf) < 0) | |
148 { | |
149 if (errno != ENOENT) | |
150 { | |
151 fprintf(stderr, "Cannot create archive file %s:\n", archive_file); | |
152 perror(""); | |
153 exit(2); | |
154 } | |
155 } | |
156 else | |
157 { | |
158 if (unlink(archive_file) < 0) | |
159 { | |
160 fprintf(stderr, "Cannot create archive file %s:\n", archive_file); | |
161 perror(""); | |
162 exit(2); | |
163 } | |
164 | |
165 } | |
166 } | |
79 | 167 |
168 switch (operation) | |
169 { | |
170 case LWAR_OP_LIST: | |
171 do_list(); | |
172 break; | |
173 | |
174 case LWAR_OP_ADD: | |
175 case LWAR_OP_CREATE: | |
176 do_add(); | |
177 break; | |
178 | |
179 case LWAR_OP_REMOVE: | |
180 do_remove(); | |
181 break; | |
182 | |
183 } | |
184 | |
80 exit(0); | 185 exit(0); |
81 } | 186 } |