Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 1 | /* 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 McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 24 | * |
| 25 | */ |
| 26 | #include <fcntl.h> |
| 27 | #include <getopt.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
| 31 | #include "busybox.h" |
| 32 | |
| 33 | extern int cpio_main(int argc, char **argv) |
| 34 | { |
| 35 | FILE *src_stream = stdin; |
| 36 | char **extract_names = NULL; |
| 37 | int extract_function = 0; |
| 38 | int num_of_entries = 0; |
| 39 | int opt = 0; |
| 40 | mode_t oldmask = 0; |
| 41 | |
| 42 | while ((opt = getopt(argc, argv, "idmuvtF:")) != -1) { |
| 43 | switch (opt) { |
| 44 | case 'i': // extract |
| 45 | extract_function |= extract_all_to_fs; |
| 46 | break; |
Glenn L McGrath | 2e6d3cf | 2001-06-24 12:36:54 +0000 | [diff] [blame] | 47 | case 'd': // create _leading_ directories |
| 48 | extract_function |= extract_create_leading_dirs; |
| 49 | oldmask = umask(077); /* Make make_directory act like GNU cpio */ |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 50 | break; |
| 51 | case 'm': // preserve modification time |
| 52 | extract_function |= extract_preserve_date; |
| 53 | break; |
| 54 | case 'v': // verbosly list files |
| 55 | extract_function |= extract_verbose_list; |
| 56 | break; |
| 57 | case 'u': // unconditional |
| 58 | extract_function |= extract_unconditional; |
| 59 | break; |
| 60 | case 't': // list files |
| 61 | extract_function |= extract_list; |
| 62 | break; |
| 63 | case 'F': |
| 64 | src_stream = xfopen(optarg, "r"); |
| 65 | break; |
| 66 | default: |
| 67 | show_usage(); |
| 68 | } |
| 69 | } |
| 70 | |
Glenn L McGrath | a868ec8 | 2001-07-14 08:49:53 +0000 | [diff] [blame] | 71 | if ((extract_function & extract_all_to_fs) && (extract_function & extract_list)) { |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 72 | extract_function ^= extract_all_to_fs; /* If specify t, don't extract*/ |
| 73 | } |
| 74 | |
Glenn L McGrath | a868ec8 | 2001-07-14 08:49:53 +0000 | [diff] [blame] | 75 | if ((extract_function & extract_all_to_fs) && (extract_function & extract_verbose_list)) { |
| 76 | /* The meaning of v changes on extract */ |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 77 | extract_function ^= extract_verbose_list; |
| 78 | extract_function |= extract_list; |
| 79 | } |
| 80 | |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 81 | while (optind < argc) { |
Glenn L McGrath | a868ec8 | 2001-07-14 08:49:53 +0000 | [diff] [blame] | 82 | extract_names = xrealloc(extract_names, sizeof(char *) * (num_of_entries + 2)); |
| 83 | extract_names[num_of_entries] = xstrdup(argv[optind]); |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 84 | num_of_entries++; |
Glenn L McGrath | a868ec8 | 2001-07-14 08:49:53 +0000 | [diff] [blame] | 85 | extract_names[num_of_entries] = NULL; |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 86 | optind++; |
| 87 | } |
| 88 | |
Glenn L McGrath | 4bef7b4 | 2001-10-13 19:43:46 +0000 | [diff] [blame^] | 89 | unarchive(src_stream, stdout, &get_header_cpio, extract_function, "./", extract_names, NULL); |
Glenn L McGrath | a868ec8 | 2001-07-14 08:49:53 +0000 | [diff] [blame] | 90 | if (oldmask) { |
| 91 | umask(oldmask); /* Restore umask if we changed it */ |
| 92 | } |
Glenn L McGrath | 8f5b63e | 2001-06-22 09:22:06 +0000 | [diff] [blame] | 93 | return EXIT_SUCCESS; |
| 94 | } |
| 95 | |