blob: baaa72ea18a237b3f7edc2d38e6a5acf372ce24a [file] [log] [blame]
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini cpio implementation for busybox
4 *
5 * Copyright (C) 2001 by Glenn McGrath
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 * Limitations:
22 * Doesn't check CRC's
23 * Only supports new ASCII and CRC formats
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000024 *
25 */
26#include <fcntl.h>
27#include <getopt.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
Glenn L McGrathef0eab52001-10-25 14:49:48 +000031#include "unarchive.h"
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000032#include "busybox.h"
33
34extern int cpio_main(int argc, char **argv)
35{
36 FILE *src_stream = stdin;
37 char **extract_names = NULL;
38 int extract_function = 0;
39 int num_of_entries = 0;
40 int opt = 0;
41 mode_t oldmask = 0;
42
43 while ((opt = getopt(argc, argv, "idmuvtF:")) != -1) {
44 switch (opt) {
45 case 'i': // extract
46 extract_function |= extract_all_to_fs;
47 break;
Glenn L McGrath2e6d3cf2001-06-24 12:36:54 +000048 case 'd': // create _leading_ directories
49 extract_function |= extract_create_leading_dirs;
50 oldmask = umask(077); /* Make make_directory act like GNU cpio */
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000051 break;
52 case 'm': // preserve modification time
53 extract_function |= extract_preserve_date;
54 break;
55 case 'v': // verbosly list files
56 extract_function |= extract_verbose_list;
57 break;
58 case 'u': // unconditional
59 extract_function |= extract_unconditional;
60 break;
61 case 't': // list files
62 extract_function |= extract_list;
63 break;
64 case 'F':
65 src_stream = xfopen(optarg, "r");
66 break;
67 default:
68 show_usage();
69 }
70 }
71
Glenn L McGratha868ec82001-07-14 08:49:53 +000072 if ((extract_function & extract_all_to_fs) && (extract_function & extract_list)) {
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000073 extract_function ^= extract_all_to_fs; /* If specify t, don't extract*/
74 }
75
Glenn L McGratha868ec82001-07-14 08:49:53 +000076 if ((extract_function & extract_all_to_fs) && (extract_function & extract_verbose_list)) {
77 /* The meaning of v changes on extract */
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000078 extract_function ^= extract_verbose_list;
79 extract_function |= extract_list;
80 }
81
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000082 while (optind < argc) {
Glenn L McGratha868ec82001-07-14 08:49:53 +000083 extract_names = xrealloc(extract_names, sizeof(char *) * (num_of_entries + 2));
84 extract_names[num_of_entries] = xstrdup(argv[optind]);
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000085 num_of_entries++;
Glenn L McGratha868ec82001-07-14 08:49:53 +000086 extract_names[num_of_entries] = NULL;
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000087 optind++;
88 }
89
Glenn L McGrath4bef7b42001-10-13 19:43:46 +000090 unarchive(src_stream, stdout, &get_header_cpio, extract_function, "./", extract_names, NULL);
Glenn L McGratha868ec82001-07-14 08:49:53 +000091 if (oldmask) {
92 umask(oldmask); /* Restore umask if we changed it */
93 }
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000094 return EXIT_SUCCESS;
95}
96