7359f49105f35ddeee2056bde4e8f3269f81f2a2
[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 <getopt.h>
28 #include <unistd.h>
29 #include "busybox.h"
30
31 extern int ar_main(int argc, char **argv)
32 {
33         FILE *src_stream = NULL;
34         int extract_function = 0, opt = 0;
35         file_headers_t *head;
36         file_headers_t *ar_extract_list = NULL;
37
38         while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
39                 switch (opt) {
40                 case 'o':
41                         extract_function |= extract_preserve_date;
42                         break;
43                 case 'v':
44                         extract_function |= extract_verbose_list;
45                         break;
46                 case 't':
47                         extract_function |= extract_list;
48                         break;
49                 case 'p':
50                         extract_function |= extract_to_stdout;
51                         break;
52                 case 'x':
53                         extract_function |= extract_all_to_fs;
54                         break;
55                 default:
56                         show_usage();
57                 }
58         }
59  
60         /* check the src filename was specified */
61         if (optind == argc) {
62                 show_usage();
63         }
64
65         src_stream = xfopen(argv[optind++], "r");
66         head = get_ar_headers(src_stream);
67
68         /* find files to extract or display */
69         /* search through argv and build extract list */
70         ar_extract_list = (file_headers_t *) xcalloc(1, sizeof(file_headers_t));
71         if (optind < argc) {
72                 while (optind < argc) {
73                         ar_extract_list = add_from_archive_list(head, ar_extract_list, argv[optind]);
74                         optind++;
75                 }
76         } else {
77                 ar_extract_list = head;
78         }
79
80         /* If there isnt even one possible entry then abort */
81         if (ar_extract_list->name == NULL) {
82                 error_msg_and_die("No files to extract");
83         }       
84
85         fseek(src_stream, 0, SEEK_SET);
86         extract_archive(src_stream, stdout, ar_extract_list, extract_function, "./");
87
88         return EXIT_SUCCESS;
89 }