Reorganise unarchiving functions, more code re-use, only does single pass(no more...
[oweals/busybox.git] / archival / ar.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini ar implementation for busybox 
4  *
5  * Copyright (C) 2000 by Glenn McGrath
6  * Written by Glenn McGrath <bug1@optushome.com.au> 1 June 2000
7  *              
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  */
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <getopt.h>
29 #include <unistd.h>
30 #include "busybox.h"
31
32 extern int ar_main(int argc, char **argv)
33 {
34         FILE *src_stream = NULL;
35         char **extract_names = NULL;
36         char ar_magic[8];
37         int extract_function = 0;
38         int opt;
39         int num_of_entries = 0;
40         extern off_t archive_offset;
41
42         while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
43                 switch (opt) {
44                 case 'o':
45                         extract_function |= extract_preserve_date;
46                         break;
47                 case 'v':
48                         extract_function |= extract_verbose_list;
49                         break;
50                 case 't':
51                         extract_function |= extract_list;
52                         break;
53                 case 'p':
54                         extract_function |= extract_to_stdout;
55                         break;
56                 case 'x':
57                         extract_function |= extract_all_to_fs;
58                         break;
59                 default:
60                         show_usage();
61                 }
62         }
63  
64         /* check the src filename was specified */
65         if (optind == argc) {
66                 show_usage();
67         }
68
69         src_stream = xfopen(argv[optind++], "r");
70
71         /* check ar magic */
72         fread(ar_magic, 1, 8, src_stream);
73         if (strncmp(ar_magic,"!<arch>",7) != 0) {
74                 error_msg_and_die("invalid magic");
75         }
76         archive_offset = 8;
77
78         extract_names = malloc(sizeof(char *));
79         extract_names[0] = NULL;
80         while (optind < argc) {
81                 num_of_entries++;
82                 *extract_names = realloc(*extract_names, num_of_entries);
83                 extract_names[num_of_entries - 1] = xstrdup(argv[optind]);
84                 optind++;
85         }
86
87         unarchive(src_stream, &get_header_ar, extract_function, "./", extract_names);
88         return EXIT_SUCCESS;
89 }