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 | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 33 | const int preserve_date = 1; /* preserve original dates */ |
| 34 | const int verbose = 2; /* be verbose */ |
| 35 | const int display = 4; /* display contents */ |
| 36 | const int extract_to_file = 8; /* extract contents of archive */ |
| 37 | const int extract_to_stdout = 16; /* extract to stdout */ |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 38 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 39 | FILE *src_file = NULL, *dst_file = NULL; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 40 | int funct = 0, opt=0; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 41 | |
Glenn L McGrath | 47fd219 | 2001-04-12 16:37:13 +0000 | [diff] [blame] | 42 | ar_headers_t *head, *ar_extract_list=NULL; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 43 | |
Glenn L McGrath | 47fd219 | 2001-04-12 16:37:13 +0000 | [diff] [blame] | 44 | ar_extract_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t)); |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 45 | head = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t)); |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 46 | |
| 47 | while ((opt = getopt(argc, argv, "ovtpx")) != -1) { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 48 | switch (opt) { |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 49 | case 'o': |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 50 | funct |= preserve_date; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 51 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 52 | case 'v': |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 53 | funct |= verbose; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 54 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 55 | case 't': |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 56 | funct |= display; |
Glenn L McGrath | 437bf72 | 2000-09-09 13:38:26 +0000 | [diff] [blame] | 57 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 58 | case 'p': |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 59 | funct |= extract_to_stdout; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 60 | break; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 61 | case 'x': |
| 62 | funct |= extract_to_file; |
Glenn L McGrath | e2b345a | 2000-09-09 14:50:04 +0000 | [diff] [blame] | 63 | break; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 64 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 65 | show_usage(); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 68 | |
Matt Kraai | d995493 | 2000-09-22 03:36:27 +0000 | [diff] [blame] | 69 | /* check the src filename was specified */ |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 70 | if (optind == argc) { |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 71 | show_usage(); |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if ( (src_file = wfopen(argv[optind], "r")) < 0) { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 75 | error_msg_and_die("Cannot read %s", argv[optind]); |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 76 | } |
Matt Kraai | d995493 | 2000-09-22 03:36:27 +0000 | [diff] [blame] | 77 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 78 | optind++; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 79 | head = get_ar_headers(src_file); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 80 | /* find files to extract or display */ |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 81 | /* search through argv and build extract list */ |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 82 | for (;optind < argc; optind++) { |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 83 | ar_headers_t *ar_entry; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 84 | ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t)); |
| 85 | ar_entry = head; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 86 | while (ar_entry->next != NULL) { |
| 87 | if (strcmp(argv[optind], ar_entry->name) == 0) { |
| 88 | ar_headers_t *tmp; |
| 89 | tmp = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); |
Glenn L McGrath | 47fd219 | 2001-04-12 16:37:13 +0000 | [diff] [blame] | 90 | *tmp = *ar_extract_list; |
| 91 | *ar_extract_list = *ar_entry; |
| 92 | ar_extract_list->next = tmp; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 93 | break; |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 94 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 95 | ar_entry=ar_entry->next; |
| 96 | } |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 97 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 98 | |
| 99 | /* if individual files not found extract all files */ |
Glenn L McGrath | 47fd219 | 2001-04-12 16:37:13 +0000 | [diff] [blame] | 100 | if (ar_extract_list->next==NULL) { |
| 101 | ar_extract_list = head; |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 102 | } |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 103 | |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 104 | /* find files to extract or display */ |
Glenn L McGrath | 47fd219 | 2001-04-12 16:37:13 +0000 | [diff] [blame] | 105 | while (ar_extract_list->next != NULL) { |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 106 | if (funct & extract_to_file) { |
Glenn L McGrath | 47fd219 | 2001-04-12 16:37:13 +0000 | [diff] [blame] | 107 | dst_file = wfopen(ar_extract_list->name, "w"); |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 108 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 109 | else if (funct & extract_to_stdout) { |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 110 | dst_file = stdout; |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame] | 111 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 112 | if ((funct & extract_to_file) || (funct & extract_to_stdout)) { |
Glenn L McGrath | 47fd219 | 2001-04-12 16:37:13 +0000 | [diff] [blame] | 113 | fseek(src_file, ar_extract_list->offset, SEEK_SET); |
| 114 | copy_file_chunk(src_file, dst_file, ar_extract_list->size); |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame] | 115 | } |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 116 | if (funct & verbose) { |
Glenn L McGrath | 47fd219 | 2001-04-12 16:37:13 +0000 | [diff] [blame] | 117 | printf("%s %d/%d %8d %s ", mode_string(ar_extract_list->mode), |
| 118 | ar_extract_list->uid, ar_extract_list->gid, |
| 119 | (int) ar_extract_list->size, time_string(ar_extract_list->mtime)); |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 120 | } |
| 121 | if ((funct & display) || (funct & verbose)){ |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 122 | puts(ar_extract_list->name); |
Glenn L McGrath | 4f1b012 | 2000-12-15 06:50:09 +0000 | [diff] [blame] | 123 | } |
Glenn L McGrath | 47fd219 | 2001-04-12 16:37:13 +0000 | [diff] [blame] | 124 | ar_extract_list = ar_extract_list->next; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 125 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 126 | return EXIT_SUCCESS; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 127 | } |