Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini ar implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 2000 by Glenn McGrath |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 6 | * Written by Glenn McGrath <bug1@optushome.com.au> 1 June 2000 |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 8 | * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | */ |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <getopt.h> |
| 28 | #include <unistd.h> |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 29 | #include "busybox.h" |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 30 | |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 31 | extern int ar_main(int argc, char **argv) |
| 32 | { |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame^] | 33 | FILE *src_stream = NULL; |
| 34 | int extract_function = 0, opt = 0; |
| 35 | file_headers_t *head; |
| 36 | file_headers_t *ar_extract_list = NULL; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 37 | |
| 38 | while ((opt = getopt(argc, argv, "ovtpx")) != -1) { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 39 | switch (opt) { |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 40 | case 'o': |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame^] | 41 | extract_function |= extract_preserve_date; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 42 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 43 | case 'v': |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame^] | 44 | extract_function |= extract_verbose_list; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 45 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 46 | case 't': |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame^] | 47 | extract_function |= extract_list; |
Glenn L McGrath | 437bf72 | 2000-09-09 13:38:26 +0000 | [diff] [blame] | 48 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 49 | case 'p': |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame^] | 50 | extract_function |= extract_to_stdout; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 51 | break; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 52 | case 'x': |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame^] | 53 | extract_function |= extract_all_to_fs; |
Glenn L McGrath | e2b345a | 2000-09-09 14:50:04 +0000 | [diff] [blame] | 54 | break; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 55 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 56 | show_usage(); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 57 | } |
| 58 | } |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 59 | |
Matt Kraai | d995493 | 2000-09-22 03:36:27 +0000 | [diff] [blame] | 60 | /* check the src filename was specified */ |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 61 | if (optind == argc) { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 62 | show_usage(); |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame^] | 65 | src_stream = xfopen(argv[optind++], "r"); |
| 66 | head = get_ar_headers(src_stream); |
Matt Kraai | d995493 | 2000-09-22 03:36:27 +0000 | [diff] [blame] | 67 | |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 68 | /* find files to extract or display */ |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 69 | /* search through argv and build extract list */ |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame^] | 70 | ar_extract_list = (file_headers_t *) xcalloc(1, sizeof(file_headers_t)); |
| 71 | if (optind < argc) { |
| 72 | while (optind < argc) { |
| 73 | ar_extract_list = add_from_archive_list(head, ar_extract_list, argv[optind]); |
| 74 | optind++; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 75 | } |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame^] | 76 | } else { |
Glenn L McGrath | 47fd219 | 2001-04-12 16:37:13 +0000 | [diff] [blame] | 77 | ar_extract_list = head; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 78 | } |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 79 | |
Glenn L McGrath | 9aff903 | 2001-06-13 07:26:39 +0000 | [diff] [blame^] | 80 | /* If there isnt even one possible entry then abort */ |
| 81 | if (ar_extract_list->name == NULL) { |
| 82 | error_msg_and_die("No files to extract"); |
| 83 | } |
| 84 | |
| 85 | fseek(src_stream, 0, SEEK_SET); |
| 86 | extract_archive(src_stream, stdout, ar_extract_list, extract_function, "./"); |
| 87 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 88 | return EXIT_SUCCESS; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 89 | } |