Rename variable that shadows global
[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         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 */
38
39         FILE *src_file = NULL, *dst_file = NULL;
40         int funct = 0, opt=0;
41
42         ar_headers_t *head, *ar_extract_list=NULL;
43
44         ar_extract_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
45         head = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
46
47         while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
48                 switch (opt) {
49                 case 'o':
50                         funct |= preserve_date;
51                         break;
52                 case 'v':
53                         funct |= verbose;
54                         break;
55                 case 't':
56                         funct |= display;
57                         break;
58                 case 'p':
59                         funct |= extract_to_stdout;
60                         break;
61                 case 'x':
62                         funct |= extract_to_file;
63                         break;
64                 default:
65                         show_usage();
66                 }
67         }
68  
69         /* check the src filename was specified */
70         if (optind == argc) {
71                 show_usage();
72         }
73
74         if ( (src_file = wfopen(argv[optind], "r")) < 0) {
75                 error_msg_and_die("Cannot read %s", argv[optind]);
76         }
77
78         optind++;       
79         head = get_ar_headers(src_file);
80         /* find files to extract or display */
81         /* search through argv and build extract list */
82         for (;optind < argc; optind++) {
83                 ar_headers_t *ar_entry;
84                 ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
85                 ar_entry = head;
86                 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));
90                                 *tmp = *ar_extract_list;
91                                 *ar_extract_list = *ar_entry;
92                                 ar_extract_list->next = tmp;
93                                 break;                                  
94                         }
95                         ar_entry=ar_entry->next;
96                 }
97         }
98
99         /* if individual files not found extract all files */   
100         if (ar_extract_list->next==NULL) {
101                 ar_extract_list = head;
102         }
103
104         /* find files to extract or display */  
105         while (ar_extract_list->next != NULL) {
106                 if (funct & extract_to_file) {
107                         dst_file = wfopen(ar_extract_list->name, "w");                          
108                 }
109                 else if (funct & extract_to_stdout) {
110                         dst_file = stdout;
111                 }
112                 if ((funct & extract_to_file) || (funct & extract_to_stdout)) {
113                         fseek(src_file, ar_extract_list->offset, SEEK_SET);
114                         copy_file_chunk(src_file, dst_file, ar_extract_list->size);                     
115                 }
116                 if (funct & verbose) {
117                         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));
120                 }
121                 if ((funct & display) || (funct & verbose)){
122                         printf("%s\n", ar_extract_list->name);
123                 }
124                 ar_extract_list = ar_extract_list->next;
125         }
126         return EXIT_SUCCESS;
127 }