blob: ac8c41f966ca61ee2164ce3c7cf725c1d1a0b47c [file] [log] [blame]
Eric Andersen86ab8a32000-06-02 03:21:42 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini ar implementation for busybox
4 *
5 * Copyright (C) 2000 by Glenn McGrath
Glenn L McGrath4f1b0122000-12-15 06:50:09 +00006 * Written by Glenn McGrath <bug1@optushome.com.au> 1 June 2000
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +00007 *
Eric Andersen86ab8a32000-06-02 03:21:42 +00008 * 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 Andersen86ab8a32000-06-02 03:21:42 +000025#include <fcntl.h>
Glenn L McGrathef0eab52001-10-25 14:49:48 +000026#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000027#include <stdlib.h>
Glenn L McGratheb1c9402001-06-20 07:48:00 +000028#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <getopt.h>
Glenn L McGrathef0eab52001-10-25 14:49:48 +000030#include "unarchive.h"
Eric Andersen3570a342000-09-25 21:45:58 +000031#include "busybox.h"
Eric Andersen86ab8a32000-06-02 03:21:42 +000032
Eric Andersen86ab8a32000-06-02 03:21:42 +000033extern int ar_main(int argc, char **argv)
34{
Glenn L McGrath9aff9032001-06-13 07:26:39 +000035 FILE *src_stream = NULL;
Glenn L McGratheb1c9402001-06-20 07:48:00 +000036 char **extract_names = NULL;
37 char ar_magic[8];
Glenn L McGrath8f5b63e2001-06-22 09:22:06 +000038 int extract_function = extract_unconditional;
Glenn L McGratheb1c9402001-06-20 07:48:00 +000039 int opt;
40 int num_of_entries = 0;
41 extern off_t archive_offset;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000042
43 while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000044 switch (opt) {
Eric Andersen57ebebf2000-07-05 17:21:58 +000045 case 'o':
Glenn L McGrath9aff9032001-06-13 07:26:39 +000046 extract_function |= extract_preserve_date;
Eric Andersen86ab8a32000-06-02 03:21:42 +000047 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +000048 case 'v':
Glenn L McGrath9aff9032001-06-13 07:26:39 +000049 extract_function |= extract_verbose_list;
Eric Andersen86ab8a32000-06-02 03:21:42 +000050 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +000051 case 't':
Glenn L McGrath9aff9032001-06-13 07:26:39 +000052 extract_function |= extract_list;
Glenn L McGrath437bf722000-09-09 13:38:26 +000053 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +000054 case 'p':
Glenn L McGrath9aff9032001-06-13 07:26:39 +000055 extract_function |= extract_to_stdout;
Eric Andersen86ab8a32000-06-02 03:21:42 +000056 break;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000057 case 'x':
Glenn L McGrath9aff9032001-06-13 07:26:39 +000058 extract_function |= extract_all_to_fs;
Glenn L McGrathe2b345a2000-09-09 14:50:04 +000059 break;
Eric Andersen86ab8a32000-06-02 03:21:42 +000060 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000061 show_usage();
Eric Andersen86ab8a32000-06-02 03:21:42 +000062 }
63 }
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000064
Matt Kraaid9954932000-09-22 03:36:27 +000065 /* check the src filename was specified */
Glenn L McGrath4949faf2001-04-11 16:23:35 +000066 if (optind == argc) {
Eric Andersen67991cf2001-02-14 21:23:06 +000067 show_usage();
Glenn L McGrath4949faf2001-04-11 16:23:35 +000068 }
69
Glenn L McGrath9aff9032001-06-13 07:26:39 +000070 src_stream = xfopen(argv[optind++], "r");
Matt Kraaid9954932000-09-22 03:36:27 +000071
Glenn L McGratheb1c9402001-06-20 07:48:00 +000072 /* check ar magic */
73 fread(ar_magic, 1, 8, src_stream);
Glenn L McGratha868ec82001-07-14 08:49:53 +000074 archive_offset = 8;
Glenn L McGratheb1c9402001-06-20 07:48:00 +000075 if (strncmp(ar_magic,"!<arch>",7) != 0) {
76 error_msg_and_die("invalid magic");
77 }
Glenn L McGratheb1c9402001-06-20 07:48:00 +000078
Glenn L McGratha868ec82001-07-14 08:49:53 +000079 /* Create a list of files to extract */
Glenn L McGratheb1c9402001-06-20 07:48:00 +000080 while (optind < argc) {
Glenn L McGratha868ec82001-07-14 08:49:53 +000081 extract_names = xrealloc(extract_names, sizeof(char *) * (num_of_entries + 2));
82 extract_names[num_of_entries] = xstrdup(argv[optind]);
Glenn L McGratheb1c9402001-06-20 07:48:00 +000083 num_of_entries++;
Glenn L McGratha868ec82001-07-14 08:49:53 +000084 extract_names[num_of_entries] = NULL;
Glenn L McGratheb1c9402001-06-20 07:48:00 +000085 optind++;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000086 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +000087
Glenn L McGrath4bef7b42001-10-13 19:43:46 +000088 unarchive(src_stream, stdout, &get_header_ar, extract_function, "./", extract_names, NULL);
Matt Kraai3e856ce2000-12-01 02:55:13 +000089 return EXIT_SUCCESS;
Eric Andersen86ab8a32000-06-02 03:21:42 +000090}