blob: 4c3d0bbd5110e6c5974fcb6b9f65a883c5e1b266 [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>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <stdlib.h>
27#include <getopt.h>
28#include <unistd.h>
Eric Andersen3570a342000-09-25 21:45:58 +000029#include "busybox.h"
Eric Andersen86ab8a32000-06-02 03:21:42 +000030
Eric Andersen86ab8a32000-06-02 03:21:42 +000031extern int ar_main(int argc, char **argv)
32{
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000033 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 McGrath8324b9f2000-09-09 08:35:45 +000038
Glenn L McGrath4949faf2001-04-11 16:23:35 +000039 FILE *src_file = NULL, *dst_file = NULL;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000040 int funct = 0, opt=0;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000041
Glenn L McGrath47fd2192001-04-12 16:37:13 +000042 ar_headers_t *head, *ar_extract_list=NULL;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000043
Glenn L McGrath47fd2192001-04-12 16:37:13 +000044 ar_extract_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
Glenn L McGrath4949faf2001-04-11 16:23:35 +000045 head = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000046
47 while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000048 switch (opt) {
Eric Andersen57ebebf2000-07-05 17:21:58 +000049 case 'o':
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000050 funct |= preserve_date;
Eric Andersen86ab8a32000-06-02 03:21:42 +000051 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +000052 case 'v':
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000053 funct |= verbose;
Eric Andersen86ab8a32000-06-02 03:21:42 +000054 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +000055 case 't':
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000056 funct |= display;
Glenn L McGrath437bf722000-09-09 13:38:26 +000057 break;
Eric Andersen57ebebf2000-07-05 17:21:58 +000058 case 'p':
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000059 funct |= extract_to_stdout;
Eric Andersen86ab8a32000-06-02 03:21:42 +000060 break;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000061 case 'x':
62 funct |= extract_to_file;
Glenn L McGrathe2b345a2000-09-09 14:50:04 +000063 break;
Eric Andersen86ab8a32000-06-02 03:21:42 +000064 default:
Eric Andersen67991cf2001-02-14 21:23:06 +000065 show_usage();
Eric Andersen86ab8a32000-06-02 03:21:42 +000066 }
67 }
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000068
Matt Kraaid9954932000-09-22 03:36:27 +000069 /* check the src filename was specified */
Glenn L McGrath4949faf2001-04-11 16:23:35 +000070 if (optind == argc) {
Eric Andersen67991cf2001-02-14 21:23:06 +000071 show_usage();
Glenn L McGrath4949faf2001-04-11 16:23:35 +000072 }
73
74 if ( (src_file = wfopen(argv[optind], "r")) < 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +000075 error_msg_and_die("Cannot read %s", argv[optind]);
Glenn L McGrath4949faf2001-04-11 16:23:35 +000076 }
Matt Kraaid9954932000-09-22 03:36:27 +000077
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000078 optind++;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000079 head = get_ar_headers(src_file);
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000080 /* find files to extract or display */
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000081 /* search through argv and build extract list */
Glenn L McGrath4949faf2001-04-11 16:23:35 +000082 for (;optind < argc; optind++) {
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000083 ar_headers_t *ar_entry;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000084 ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
85 ar_entry = head;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000086 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 McGrath47fd2192001-04-12 16:37:13 +000090 *tmp = *ar_extract_list;
91 *ar_extract_list = *ar_entry;
92 ar_extract_list->next = tmp;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000093 break;
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +000094 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000095 ar_entry=ar_entry->next;
96 }
Glenn L McGrath8324b9f2000-09-09 08:35:45 +000097 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +000098
99 /* if individual files not found extract all files */
Glenn L McGrath47fd2192001-04-12 16:37:13 +0000100 if (ar_extract_list->next==NULL) {
101 ar_extract_list = head;
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000102 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000103
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000104 /* find files to extract or display */
Glenn L McGrath47fd2192001-04-12 16:37:13 +0000105 while (ar_extract_list->next != NULL) {
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000106 if (funct & extract_to_file) {
Glenn L McGrath47fd2192001-04-12 16:37:13 +0000107 dst_file = wfopen(ar_extract_list->name, "w");
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000108 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000109 else if (funct & extract_to_stdout) {
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000110 dst_file = stdout;
Glenn L McGrathfca80502000-09-11 05:25:39 +0000111 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000112 if ((funct & extract_to_file) || (funct & extract_to_stdout)) {
Glenn L McGrath47fd2192001-04-12 16:37:13 +0000113 fseek(src_file, ar_extract_list->offset, SEEK_SET);
114 copy_file_chunk(src_file, dst_file, ar_extract_list->size);
Glenn L McGrathfca80502000-09-11 05:25:39 +0000115 }
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000116 if (funct & verbose) {
Glenn L McGrath47fd2192001-04-12 16:37:13 +0000117 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 McGrath4f1b0122000-12-15 06:50:09 +0000120 }
121 if ((funct & display) || (funct & verbose)){
Glenn L McGrath47fd2192001-04-12 16:37:13 +0000122 printf("%s\n", ar_extract_list->name);
Glenn L McGrath4f1b0122000-12-15 06:50:09 +0000123 }
Glenn L McGrath47fd2192001-04-12 16:37:13 +0000124 ar_extract_list = ar_extract_list->next;
Glenn L McGrath8324b9f2000-09-09 08:35:45 +0000125 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000126 return EXIT_SUCCESS;
Eric Andersen86ab8a32000-06-02 03:21:42 +0000127}