Glenn L McGrath | 87ac702 | 2002-01-02 13:52:26 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini unzip implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2001 by Laurence Anderson |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <fcntl.h> |
| 24 | #include <getopt.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <unistd.h> |
| 28 | #include "unarchive.h" |
| 29 | #include "busybox.h" |
| 30 | |
| 31 | extern int unzip_main(int argc, char **argv) |
| 32 | { |
| 33 | FILE *src_stream; |
| 34 | int extract_function = extract_all_to_fs | extract_create_leading_dirs; |
| 35 | char **extract_names = NULL; |
| 36 | char **exclude_names = NULL; |
| 37 | int opt = 0; |
| 38 | int num_of_entries = 0; |
| 39 | int exclude = 0; |
| 40 | char *outdir = "./"; |
| 41 | FILE *msgout = stdout; |
| 42 | |
| 43 | while ((opt = getopt(argc, argv, "lnopqxd:")) != -1) { |
| 44 | switch (opt) { |
| 45 | case 'l': |
| 46 | extract_function |= extract_verbose_list; |
| 47 | extract_function ^= extract_all_to_fs; |
| 48 | break; |
| 49 | case 'n': |
| 50 | break; |
| 51 | case 'o': |
| 52 | extract_function |= extract_unconditional; |
| 53 | break; |
| 54 | case 'p': |
| 55 | extract_function |= extract_to_stdout; |
| 56 | extract_function ^= extract_all_to_fs; |
| 57 | /* FALLTHROUGH */ |
| 58 | case 'q': |
| 59 | msgout = xfopen("/dev/null", "w"); |
| 60 | break; |
| 61 | case 'd': |
| 62 | outdir = xstrdup(optarg); |
| 63 | strcat(outdir, "/"); |
| 64 | break; |
| 65 | case 'x': |
| 66 | exclude = 1; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (optind == argc) { |
| 72 | show_usage(); |
| 73 | } |
| 74 | |
| 75 | if (*argv[optind] == '-') src_stream = stdin; |
| 76 | else src_stream = xfopen(argv[optind++], "r"); |
| 77 | |
| 78 | while (optind < argc) { |
| 79 | if (exclude) { |
| 80 | exclude_names = xrealloc(exclude_names, sizeof(char *) * (num_of_entries + 2)); |
| 81 | exclude_names[num_of_entries] = xstrdup(argv[optind]); |
| 82 | } else { |
| 83 | extract_names = xrealloc(extract_names, sizeof(char *) * (num_of_entries + 2)); |
| 84 | extract_names[num_of_entries] = xstrdup(argv[optind]); |
| 85 | } |
| 86 | num_of_entries++; |
| 87 | if (exclude) exclude_names[num_of_entries] = NULL; |
| 88 | else extract_names[num_of_entries] = NULL; |
| 89 | optind++; |
| 90 | } |
| 91 | |
| 92 | unarchive(src_stream, msgout, &get_header_zip, extract_function, outdir, extract_names, exclude_names); |
| 93 | return EXIT_SUCCESS; |
| 94 | } |