diff options
author | Matt Kraai <kraai@debian.org> | 2001-01-22 20:49:00 +0000 |
---|---|---|
committer | Matt Kraai <kraai@debian.org> | 2001-01-22 20:49:00 +0000 |
commit | 3b3f5c364a486dc2c081f0684a4315740f349be1 (patch) | |
tree | edfa51886ecd329e393fc33eb8871d5787e36560 | |
parent | 8f8dab94e579958e5702d535b7b023bb548f5b64 (diff) | |
download | busybox-w32-3b3f5c364a486dc2c081f0684a4315740f349be1.tar.gz busybox-w32-3b3f5c364a486dc2c081f0684a4315740f349be1.tar.bz2 busybox-w32-3b3f5c364a486dc2c081f0684a4315740f349be1.zip |
Use getopt (or getopt_long).
-rw-r--r-- | archival/tar.c | 157 | ||||
-rw-r--r-- | tar.c | 157 | ||||
-rw-r--r-- | utility.c | 2 |
3 files changed, 153 insertions, 163 deletions
diff --git a/archival/tar.c b/archival/tar.c index bf8f9b813..2699550ec 100644 --- a/archival/tar.c +++ b/archival/tar.c | |||
@@ -163,7 +163,7 @@ extern int tar_unzip_init(int tarFd) | |||
163 | 163 | ||
164 | if (pipe(unzip_pipe)!=0) | 164 | if (pipe(unzip_pipe)!=0) |
165 | error_msg_and_die("pipe error\n"); | 165 | error_msg_and_die("pipe error\n"); |
166 | 166 | ||
167 | if ( (child_pid = fork()) == -1) | 167 | if ( (child_pid = fork()) == -1) |
168 | error_msg_and_die("fork failure\n"); | 168 | error_msg_and_die("fork failure\n"); |
169 | 169 | ||
@@ -182,6 +182,13 @@ extern int tar_unzip_init(int tarFd) | |||
182 | } | 182 | } |
183 | #endif | 183 | #endif |
184 | 184 | ||
185 | #if defined BB_FEATURE_TAR_EXCLUDE | ||
186 | struct option longopts[] = { | ||
187 | { "exclude", 1, NULL, 'e' }, | ||
188 | { NULL, 0, NULL, 0 } | ||
189 | }; | ||
190 | #endif | ||
191 | |||
185 | extern int tar_main(int argc, char **argv) | 192 | extern int tar_main(int argc, char **argv) |
186 | { | 193 | { |
187 | char** excludeList=NULL; | 194 | char** excludeList=NULL; |
@@ -189,7 +196,6 @@ extern int tar_main(int argc, char **argv) | |||
189 | const char *tarName="-"; | 196 | const char *tarName="-"; |
190 | #if defined BB_FEATURE_TAR_EXCLUDE | 197 | #if defined BB_FEATURE_TAR_EXCLUDE |
191 | int excludeListSize=0; | 198 | int excludeListSize=0; |
192 | char *excludeFileName ="-"; | ||
193 | FILE *fileList; | 199 | FILE *fileList; |
194 | char file[256]; | 200 | char file[256]; |
195 | #endif | 201 | #endif |
@@ -202,95 +208,84 @@ extern int tar_main(int argc, char **argv) | |||
202 | int verboseFlag = FALSE; | 208 | int verboseFlag = FALSE; |
203 | int tostdoutFlag = FALSE; | 209 | int tostdoutFlag = FALSE; |
204 | int status = FALSE; | 210 | int status = FALSE; |
205 | int firstOpt = TRUE; | 211 | int opt; |
206 | int stopIt; | ||
207 | 212 | ||
208 | if (argc <= 1) | 213 | if (argc <= 1) |
209 | usage(tar_usage); | 214 | usage(tar_usage); |
210 | 215 | ||
211 | while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) { | 216 | if (argv[1][0] != '-') { |
212 | firstOpt=FALSE; | 217 | char *tmp = xmalloc(strlen(argv[1]) + 2); |
213 | stopIt=FALSE; | 218 | tmp[0] = '-'; |
214 | while (stopIt==FALSE && **argv) { | 219 | strcpy(tmp + 1, argv[1]); |
215 | switch (*((*argv)++)) { | 220 | argv[1] = tmp; |
216 | case 'c': | 221 | } |
217 | if (extractFlag == TRUE || listFlag == TRUE) | 222 | |
218 | goto flagError; | 223 | while ( |
219 | createFlag = TRUE; | 224 | #ifndef BB_FEATURE_TAR_EXCLUDE |
220 | break; | 225 | (opt = getopt(argc, argv, "cxtzvOf:")) |
221 | case 'x': | 226 | #else |
222 | if (listFlag == TRUE || createFlag == TRUE) | 227 | (opt = getopt_long(argc, argv, "cxtzvOf:X:", longopts, NULL)) |
223 | goto flagError; | 228 | #endif |
224 | extractFlag = TRUE; | 229 | > 0) { |
225 | break; | 230 | switch (opt) { |
226 | case 't': | 231 | case 'c': |
227 | if (extractFlag == TRUE || createFlag == TRUE) | 232 | if (extractFlag == TRUE || listFlag == TRUE) |
228 | goto flagError; | 233 | goto flagError; |
229 | listFlag = TRUE; | 234 | createFlag = TRUE; |
230 | break; | 235 | break; |
236 | case 'x': | ||
237 | if (listFlag == TRUE || createFlag == TRUE) | ||
238 | goto flagError; | ||
239 | extractFlag = TRUE; | ||
240 | break; | ||
241 | case 't': | ||
242 | if (extractFlag == TRUE || createFlag == TRUE) | ||
243 | goto flagError; | ||
244 | listFlag = TRUE; | ||
245 | break; | ||
231 | #ifdef BB_FEATURE_TAR_GZIP | 246 | #ifdef BB_FEATURE_TAR_GZIP |
232 | case 'z': | 247 | case 'z': |
233 | unzipFlag = TRUE; | 248 | unzipFlag = TRUE; |
234 | break; | 249 | break; |
235 | #endif | 250 | #endif |
236 | case 'v': | 251 | case 'v': |
237 | verboseFlag = TRUE; | 252 | verboseFlag = TRUE; |
238 | break; | 253 | break; |
239 | case 'O': | 254 | case 'O': |
240 | tostdoutFlag = TRUE; | 255 | tostdoutFlag = TRUE; |
241 | break; | 256 | break; |
242 | case 'f': | 257 | case 'f': |
243 | if (*tarName != '-') | 258 | if (*tarName != '-') |
244 | error_msg_and_die( "Only one 'f' option allowed\n"); | 259 | error_msg_and_die( "Only one 'f' option allowed\n"); |
245 | tarName = *(++argv); | 260 | tarName = optarg; |
246 | if (tarName == NULL) | 261 | break; |
247 | error_msg_and_die( "Option requires an argument: No file specified\n"); | ||
248 | stopIt=TRUE; | ||
249 | break; | ||
250 | #if defined BB_FEATURE_TAR_EXCLUDE | 262 | #if defined BB_FEATURE_TAR_EXCLUDE |
251 | case 'e': | 263 | case 'e': |
252 | if (strcmp(*argv, "xclude")==0) { | 264 | excludeList=xrealloc( excludeList, |
253 | excludeList=xrealloc( excludeList, | 265 | sizeof(char *) * (excludeListSize+2)); |
254 | sizeof(char *) * (excludeListSize+2)); | 266 | excludeList[excludeListSize] = optarg; |
255 | excludeList[excludeListSize] = *(++argv); | 267 | /* Tack a NULL onto the end of the list */ |
256 | if (excludeList[excludeListSize] == NULL) | 268 | excludeList[++excludeListSize] = NULL; |
257 | error_msg_and_die( "Option requires an argument: No file specified\n"); | 269 | case 'X': |
258 | /* Tack a NULL onto the end of the list */ | 270 | fileList = xfopen(optarg, "r"); |
259 | excludeList[++excludeListSize] = NULL; | 271 | while (fgets(file, sizeof(file), fileList) != NULL) { |
260 | stopIt=TRUE; | 272 | excludeList = xrealloc(excludeList, |
261 | break; | 273 | sizeof(char *) * (excludeListSize+2)); |
262 | } | 274 | if (file[strlen(file)-1] == '\n') |
263 | case 'X': | 275 | file[strlen(file)-1] = '\0'; |
264 | if (*excludeFileName != '-') | 276 | excludeList[excludeListSize] = xstrdup(file); |
265 | error_msg_and_die("Only one 'X' option allowed\n"); | 277 | /* Tack a NULL onto the end of the list */ |
266 | excludeFileName = *(++argv); | 278 | excludeList[++excludeListSize] = NULL; |
267 | if (excludeFileName == NULL) | 279 | } |
268 | error_msg_and_die("Option requires an argument: No file specified\n"); | 280 | fclose(fileList); |
269 | fileList = fopen (excludeFileName, "r"); | 281 | break; |
270 | if (! fileList) | ||
271 | error_msg_and_die("Exclude file: file not found\n"); | ||
272 | while (fgets(file, sizeof(file), fileList) != NULL) { | ||
273 | excludeList = xrealloc(excludeList, | ||
274 | sizeof(char *) * (excludeListSize+2)); | ||
275 | if (file[strlen(file)-1] == '\n') | ||
276 | file[strlen(file)-1] = '\0'; | ||
277 | excludeList[excludeListSize] = xstrdup(file); | ||
278 | /* Tack a NULL onto the end of the list */ | ||
279 | excludeList[++excludeListSize] = NULL; | ||
280 | } | ||
281 | fclose(fileList); | ||
282 | stopIt=TRUE; | ||
283 | break; | ||
284 | #endif | 282 | #endif |
285 | case '-': | ||
286 | break; | ||
287 | default: | 283 | default: |
288 | usage(tar_usage); | 284 | usage(tar_usage); |
289 | } | ||
290 | } | 285 | } |
291 | } | 286 | } |
292 | 287 | ||
293 | /* | 288 | /* |
294 | * Do the correct type of action supplying the rest of the | 289 | * Do the correct type of action supplying the rest of the |
295 | * command line arguments as the list of files to process. | 290 | * command line arguments as the list of files to process. |
296 | */ | 291 | */ |
@@ -302,13 +297,13 @@ extern int tar_main(int argc, char **argv) | |||
302 | if (unzipFlag==TRUE) | 297 | if (unzipFlag==TRUE) |
303 | error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip\n"); | 298 | error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip\n"); |
304 | #endif | 299 | #endif |
305 | status = writeTarFile(tarName, verboseFlag, argv, excludeList); | 300 | status = writeTarFile(tarName, verboseFlag, argv + optind, excludeList); |
306 | #endif | 301 | #endif |
307 | } | 302 | } |
308 | if (listFlag == TRUE || extractFlag == TRUE) { | 303 | if (listFlag == TRUE || extractFlag == TRUE) { |
309 | int tarFd; | 304 | int tarFd; |
310 | if (*argv) | 305 | if (argv[optind]) |
311 | extractList = argv; | 306 | extractList = argv + optind; |
312 | /* Open the tar file for reading. */ | 307 | /* Open the tar file for reading. */ |
313 | if (!strcmp(tarName, "-")) | 308 | if (!strcmp(tarName, "-")) |
314 | tarFd = fileno(stdin); | 309 | tarFd = fileno(stdin); |
@@ -163,7 +163,7 @@ extern int tar_unzip_init(int tarFd) | |||
163 | 163 | ||
164 | if (pipe(unzip_pipe)!=0) | 164 | if (pipe(unzip_pipe)!=0) |
165 | error_msg_and_die("pipe error\n"); | 165 | error_msg_and_die("pipe error\n"); |
166 | 166 | ||
167 | if ( (child_pid = fork()) == -1) | 167 | if ( (child_pid = fork()) == -1) |
168 | error_msg_and_die("fork failure\n"); | 168 | error_msg_and_die("fork failure\n"); |
169 | 169 | ||
@@ -182,6 +182,13 @@ extern int tar_unzip_init(int tarFd) | |||
182 | } | 182 | } |
183 | #endif | 183 | #endif |
184 | 184 | ||
185 | #if defined BB_FEATURE_TAR_EXCLUDE | ||
186 | struct option longopts[] = { | ||
187 | { "exclude", 1, NULL, 'e' }, | ||
188 | { NULL, 0, NULL, 0 } | ||
189 | }; | ||
190 | #endif | ||
191 | |||
185 | extern int tar_main(int argc, char **argv) | 192 | extern int tar_main(int argc, char **argv) |
186 | { | 193 | { |
187 | char** excludeList=NULL; | 194 | char** excludeList=NULL; |
@@ -189,7 +196,6 @@ extern int tar_main(int argc, char **argv) | |||
189 | const char *tarName="-"; | 196 | const char *tarName="-"; |
190 | #if defined BB_FEATURE_TAR_EXCLUDE | 197 | #if defined BB_FEATURE_TAR_EXCLUDE |
191 | int excludeListSize=0; | 198 | int excludeListSize=0; |
192 | char *excludeFileName ="-"; | ||
193 | FILE *fileList; | 199 | FILE *fileList; |
194 | char file[256]; | 200 | char file[256]; |
195 | #endif | 201 | #endif |
@@ -202,95 +208,84 @@ extern int tar_main(int argc, char **argv) | |||
202 | int verboseFlag = FALSE; | 208 | int verboseFlag = FALSE; |
203 | int tostdoutFlag = FALSE; | 209 | int tostdoutFlag = FALSE; |
204 | int status = FALSE; | 210 | int status = FALSE; |
205 | int firstOpt = TRUE; | 211 | int opt; |
206 | int stopIt; | ||
207 | 212 | ||
208 | if (argc <= 1) | 213 | if (argc <= 1) |
209 | usage(tar_usage); | 214 | usage(tar_usage); |
210 | 215 | ||
211 | while (*(++argv) && (**argv == '-' || firstOpt == TRUE)) { | 216 | if (argv[1][0] != '-') { |
212 | firstOpt=FALSE; | 217 | char *tmp = xmalloc(strlen(argv[1]) + 2); |
213 | stopIt=FALSE; | 218 | tmp[0] = '-'; |
214 | while (stopIt==FALSE && **argv) { | 219 | strcpy(tmp + 1, argv[1]); |
215 | switch (*((*argv)++)) { | 220 | argv[1] = tmp; |
216 | case 'c': | 221 | } |
217 | if (extractFlag == TRUE || listFlag == TRUE) | 222 | |
218 | goto flagError; | 223 | while ( |
219 | createFlag = TRUE; | 224 | #ifndef BB_FEATURE_TAR_EXCLUDE |
220 | break; | 225 | (opt = getopt(argc, argv, "cxtzvOf:")) |
221 | case 'x': | 226 | #else |
222 | if (listFlag == TRUE || createFlag == TRUE) | 227 | (opt = getopt_long(argc, argv, "cxtzvOf:X:", longopts, NULL)) |
223 | goto flagError; | 228 | #endif |
224 | extractFlag = TRUE; | 229 | > 0) { |
225 | break; | 230 | switch (opt) { |
226 | case 't': | 231 | case 'c': |
227 | if (extractFlag == TRUE || createFlag == TRUE) | 232 | if (extractFlag == TRUE || listFlag == TRUE) |
228 | goto flagError; | 233 | goto flagError; |
229 | listFlag = TRUE; | 234 | createFlag = TRUE; |
230 | break; | 235 | break; |
236 | case 'x': | ||
237 | if (listFlag == TRUE || createFlag == TRUE) | ||
238 | goto flagError; | ||
239 | extractFlag = TRUE; | ||
240 | break; | ||
241 | case 't': | ||
242 | if (extractFlag == TRUE || createFlag == TRUE) | ||
243 | goto flagError; | ||
244 | listFlag = TRUE; | ||
245 | break; | ||
231 | #ifdef BB_FEATURE_TAR_GZIP | 246 | #ifdef BB_FEATURE_TAR_GZIP |
232 | case 'z': | 247 | case 'z': |
233 | unzipFlag = TRUE; | 248 | unzipFlag = TRUE; |
234 | break; | 249 | break; |
235 | #endif | 250 | #endif |
236 | case 'v': | 251 | case 'v': |
237 | verboseFlag = TRUE; | 252 | verboseFlag = TRUE; |
238 | break; | 253 | break; |
239 | case 'O': | 254 | case 'O': |
240 | tostdoutFlag = TRUE; | 255 | tostdoutFlag = TRUE; |
241 | break; | 256 | break; |
242 | case 'f': | 257 | case 'f': |
243 | if (*tarName != '-') | 258 | if (*tarName != '-') |
244 | error_msg_and_die( "Only one 'f' option allowed\n"); | 259 | error_msg_and_die( "Only one 'f' option allowed\n"); |
245 | tarName = *(++argv); | 260 | tarName = optarg; |
246 | if (tarName == NULL) | 261 | break; |
247 | error_msg_and_die( "Option requires an argument: No file specified\n"); | ||
248 | stopIt=TRUE; | ||
249 | break; | ||
250 | #if defined BB_FEATURE_TAR_EXCLUDE | 262 | #if defined BB_FEATURE_TAR_EXCLUDE |
251 | case 'e': | 263 | case 'e': |
252 | if (strcmp(*argv, "xclude")==0) { | 264 | excludeList=xrealloc( excludeList, |
253 | excludeList=xrealloc( excludeList, | 265 | sizeof(char *) * (excludeListSize+2)); |
254 | sizeof(char *) * (excludeListSize+2)); | 266 | excludeList[excludeListSize] = optarg; |
255 | excludeList[excludeListSize] = *(++argv); | 267 | /* Tack a NULL onto the end of the list */ |
256 | if (excludeList[excludeListSize] == NULL) | 268 | excludeList[++excludeListSize] = NULL; |
257 | error_msg_and_die( "Option requires an argument: No file specified\n"); | 269 | case 'X': |
258 | /* Tack a NULL onto the end of the list */ | 270 | fileList = xfopen(optarg, "r"); |
259 | excludeList[++excludeListSize] = NULL; | 271 | while (fgets(file, sizeof(file), fileList) != NULL) { |
260 | stopIt=TRUE; | 272 | excludeList = xrealloc(excludeList, |
261 | break; | 273 | sizeof(char *) * (excludeListSize+2)); |
262 | } | 274 | if (file[strlen(file)-1] == '\n') |
263 | case 'X': | 275 | file[strlen(file)-1] = '\0'; |
264 | if (*excludeFileName != '-') | 276 | excludeList[excludeListSize] = xstrdup(file); |
265 | error_msg_and_die("Only one 'X' option allowed\n"); | 277 | /* Tack a NULL onto the end of the list */ |
266 | excludeFileName = *(++argv); | 278 | excludeList[++excludeListSize] = NULL; |
267 | if (excludeFileName == NULL) | 279 | } |
268 | error_msg_and_die("Option requires an argument: No file specified\n"); | 280 | fclose(fileList); |
269 | fileList = fopen (excludeFileName, "r"); | 281 | break; |
270 | if (! fileList) | ||
271 | error_msg_and_die("Exclude file: file not found\n"); | ||
272 | while (fgets(file, sizeof(file), fileList) != NULL) { | ||
273 | excludeList = xrealloc(excludeList, | ||
274 | sizeof(char *) * (excludeListSize+2)); | ||
275 | if (file[strlen(file)-1] == '\n') | ||
276 | file[strlen(file)-1] = '\0'; | ||
277 | excludeList[excludeListSize] = xstrdup(file); | ||
278 | /* Tack a NULL onto the end of the list */ | ||
279 | excludeList[++excludeListSize] = NULL; | ||
280 | } | ||
281 | fclose(fileList); | ||
282 | stopIt=TRUE; | ||
283 | break; | ||
284 | #endif | 282 | #endif |
285 | case '-': | ||
286 | break; | ||
287 | default: | 283 | default: |
288 | usage(tar_usage); | 284 | usage(tar_usage); |
289 | } | ||
290 | } | 285 | } |
291 | } | 286 | } |
292 | 287 | ||
293 | /* | 288 | /* |
294 | * Do the correct type of action supplying the rest of the | 289 | * Do the correct type of action supplying the rest of the |
295 | * command line arguments as the list of files to process. | 290 | * command line arguments as the list of files to process. |
296 | */ | 291 | */ |
@@ -302,13 +297,13 @@ extern int tar_main(int argc, char **argv) | |||
302 | if (unzipFlag==TRUE) | 297 | if (unzipFlag==TRUE) |
303 | error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip\n"); | 298 | error_msg_and_die("Creation of compressed not internally support by tar, pipe to busybox gunzip\n"); |
304 | #endif | 299 | #endif |
305 | status = writeTarFile(tarName, verboseFlag, argv, excludeList); | 300 | status = writeTarFile(tarName, verboseFlag, argv + optind, excludeList); |
306 | #endif | 301 | #endif |
307 | } | 302 | } |
308 | if (listFlag == TRUE || extractFlag == TRUE) { | 303 | if (listFlag == TRUE || extractFlag == TRUE) { |
309 | int tarFd; | 304 | int tarFd; |
310 | if (*argv) | 305 | if (argv[optind]) |
311 | extractList = argv; | 306 | extractList = argv + optind; |
312 | /* Open the tar file for reading. */ | 307 | /* Open the tar file for reading. */ |
313 | if (!strcmp(tarName, "-")) | 308 | if (!strcmp(tarName, "-")) |
314 | tarFd = fileno(stdin); | 309 | tarFd = fileno(stdin); |
@@ -1711,7 +1711,7 @@ FILE *wfopen(const char *path, const char *mode) | |||
1711 | #endif | 1711 | #endif |
1712 | 1712 | ||
1713 | #if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \ | 1713 | #if defined BB_HOSTNAME || defined BB_LOADACM || defined BB_MORE \ |
1714 | || defined BB_SED || defined BB_SH || defined BB_UNIQ \ | 1714 | || defined BB_SED || defined BB_SH || defined BB_TAR || defined BB_UNIQ \ |
1715 | || defined BB_WC || defined BB_CMP | 1715 | || defined BB_WC || defined BB_CMP |
1716 | FILE *xfopen(const char *path, const char *mode) | 1716 | FILE *xfopen(const char *path, const char *mode) |
1717 | { | 1717 | { |