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