Recovering from my previous commit
[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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <getopt.h>
30 #include "unarchive.h"
31 #include "busybox.h"
32
33 extern int ar_main(int argc, char **argv)
34 {
35         FILE *src_stream = NULL;
36         char **extract_names = NULL;
37         char ar_magic[8];
38         int extract_function =  extract_unconditional;
39         int opt;
40         int num_of_entries = 0;
41         extern off_t archive_offset;
42
43         while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
44                 switch (opt) {
45                 case 'o':
46                         extract_function |= extract_preserve_date;
47                         break;
48                 case 'v':
49                         extract_function |= extract_verbose_list;
50                         break;
51                 case 't':
52                         extract_function |= extract_list;
53                         break;
54                 case 'p':
55                         extract_function |= extract_to_stdout;
56                         break;
57                 case 'x':
58                         extract_function |= extract_all_to_fs;
59                         break;
60                 default:
61                         show_usage();
62                 }
63         }
64  
65         /* check the src filename was specified */
66         if (optind == argc) {
67                 show_usage();
68         }
69
70         src_stream = xfopen(argv[optind++], "r");
71
72         /* check ar magic */
73         fread(ar_magic, 1, 8, src_stream);
74         archive_offset = 8;
75         if (strncmp(ar_magic,"!<arch>",7) != 0) {
76                 error_msg_and_die("invalid magic");
77         }
78
79         /* Create a list of files to extract */
80         while (optind < argc) {
81                 extract_names = xrealloc(extract_names, sizeof(char *) * (num_of_entries + 2));
82                 extract_names[num_of_entries] = xstrdup(argv[optind]);
83                 num_of_entries++;
84                 extract_names[num_of_entries] = NULL;
85                 optind++;
86         }
87
88         unarchive(src_stream, stdout, &get_header_ar, extract_function, "./", extract_names, NULL);
89         return EXIT_SUCCESS;
90 }