aboutsummaryrefslogtreecommitdiff
path: root/archival/unzip.c
blob: ae0d7c1dc56f324e192327d10526823db01b94e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* vi: set sw=4 ts=4: */
/*
 * Mini unzip implementation for busybox
 *
 * Copyright (C) 2001 by Laurence Anderson
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <fcntl.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "unarchive.h"
#include "busybox.h"

extern int unzip_main(int argc, char **argv)
{
	FILE *src_stream;
	int extract_function = extract_all_to_fs | extract_create_leading_dirs;
	char **extract_names = NULL;
	char **exclude_names = NULL;
	int opt = 0;
	int num_of_entries = 0;
	int exclude = 0;
	char *outdir = "./";
	FILE *msgout = stdout;

	while ((opt = getopt(argc, argv, "lnopqxd:")) != -1) {
		switch (opt) {
			case 'l':
				extract_function |= extract_verbose_list;
				extract_function ^= extract_all_to_fs;
				break;
			case 'n':
				break;
			case 'o':
				extract_function |= extract_unconditional;
				break;
			case 'p':
				extract_function |= extract_to_stdout;
				extract_function ^= extract_all_to_fs;
				/* FALLTHROUGH */
			case 'q':
				msgout = xfopen("/dev/null", "w");
				break;
			case 'd':
				outdir = xstrdup(optarg);
				strcat(outdir, "/");
				break;
			case 'x':
				exclude = 1;
				break;
		}
	}

	if (optind == argc) {
		show_usage();
	}

	if (*argv[optind] == '-') src_stream = stdin;
	else src_stream = xfopen(argv[optind++], "r");

	while (optind < argc) {
		if (exclude) {
			exclude_names = xrealloc(exclude_names, sizeof(char *) * (num_of_entries + 2));
			exclude_names[num_of_entries] = xstrdup(argv[optind]);
		} else {
			extract_names = xrealloc(extract_names, sizeof(char *) * (num_of_entries + 2));
			extract_names[num_of_entries] = xstrdup(argv[optind]);
		}
		num_of_entries++;
		if (exclude) exclude_names[num_of_entries] = NULL;
		else extract_names[num_of_entries] = NULL;
		optind++;
	}

	unarchive(src_stream, msgout, &get_header_zip, extract_function, outdir, extract_names, exclude_names);
	return EXIT_SUCCESS;
}