Cleanup patch from Vladimir N. Oleynik.
[oweals/busybox.git] / 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 <string.h>
27 #include <stdlib.h>
28 #include <getopt.h>
29 #include <unistd.h>
30 #include "busybox.h"
31
32 typedef struct ar_headers_s {
33         char *name;
34         size_t size;
35         uid_t uid;
36         gid_t gid;
37         mode_t mode;
38         time_t mtime;
39         off_t offset;
40         struct ar_headers_s *next;
41 } ar_headers_t;
42
43 /*
44  * return the headerL_t struct for the filename descriptor
45  */
46 extern ar_headers_t get_headers(int srcFd)
47 {
48         typedef struct raw_ar_header_s {        /* Byte Offset */
49                 char name[16];  /*  0-15 */
50                 char date[12];  /* 16-27 */
51                 char uid[6];    
52                 char gid[6];    /* 28-39 */
53                 char mode[8];   /* 40-47 */
54                 char size[10];  /* 48-57 */
55                 char fmag[2];   /* 58-59 */
56         } raw_ar_header_t;
57         raw_ar_header_t raw_ar_header;
58
59         ar_headers_t *head, *entry;
60         char ar_magic[8];
61         char *long_name=NULL;
62         
63         head = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
64         entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
65         
66         /* check ar magic */
67         if (full_read(srcFd, ar_magic, 8) != 8)
68                 error_msg_and_die("cannot read magic\n");
69         if (strncmp(ar_magic,"!<arch>",7) != 0)
70                 error_msg_and_die("invalid magic\n");
71
72         while (full_read(srcFd, (char *) &raw_ar_header, 60)==60) {
73                 /* check the end of header markers are valid */
74                 if ((raw_ar_header.fmag[0]!='`') || (raw_ar_header.fmag[1]!='\n')) {
75                         char newline[1];
76                         if (raw_ar_header.fmag[1]!='`') {
77                                 break;
78                         }
79                         /* some version of ar, have an extra '\n' after each entry */
80                         read(srcFd, newline, 1);
81                         if (newline[0]!='\n') {
82                                 break;
83                         }
84                         /* fix up the header, we started reading 1 byte too early due to a '\n' */
85                         memmove((char *) &raw_ar_header, (char *)&raw_ar_header+1, 59);
86                         /* dont worry about adding the last '\n', we dont need it now */
87                 }
88                 
89                 entry->size = (size_t) atoi(raw_ar_header.size);
90                 /* long filenames have '/' as the first character */
91                 if (raw_ar_header.name[0] == '/') {
92                         if (raw_ar_header.name[1] == '/') {
93                                 /* multiple long filenames are stored as data in one entry */
94                                 long_name = (char *) xrealloc(long_name, entry->size);
95                                 full_read(srcFd, long_name, entry->size);
96                                 continue;
97                         }
98                         else {
99                                 /* The number after the '/' indicates the offset in the ar data section
100                                         (saved in variable long_name) that conatains the real filename */
101                                 const int long_name_offset = (int) atoi((char *) &raw_ar_header.name[1]);
102                                 entry->name = xmalloc(strlen(&long_name[long_name_offset]));
103                                 strcpy(entry->name, &long_name[long_name_offset]);
104                         }
105                 }
106                 else {
107                         /* short filenames */
108                         entry->name = xmalloc(16);
109                         strncpy(entry->name, raw_ar_header.name, 16);
110                 }
111                 entry->name[strcspn(entry->name, " /")]='\0';
112
113                 /* convert the rest of the now valid char header to its typed struct */ 
114                 parse_mode(raw_ar_header.mode, &entry->mode);
115                 entry->mtime = atoi(raw_ar_header.date);
116                 entry->uid = atoi(raw_ar_header.uid);
117                 entry->gid = atoi(raw_ar_header.gid);
118                 entry->offset = lseek(srcFd, 0, SEEK_CUR);
119
120                 /* add this entries header to our combined list */
121                 entry->next = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
122                 *entry->next = *head;
123                 *head = *entry;
124                 lseek(srcFd, (off_t) entry->size, SEEK_CUR);
125         }
126         return(*head);
127 }
128
129 extern int ar_main(int argc, char **argv)
130 {
131         const int preserve_date = 1;    /* preserve original dates */
132         const int verbose = 2;          /* be verbose */
133         const int display = 4;          /* display contents */
134         const int extract_to_file = 8;  /* extract contents of archive */
135         const int extract_to_stdout = 16;       /* extract to stdout */
136
137         int funct = 0, opt=0;
138         int srcFd=0, dstFd=0;
139
140         ar_headers_t head, *extract_list=NULL;
141
142         extract_list = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
143
144         while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
145                 switch (opt) {
146                 case 'o':
147                         funct |= preserve_date;
148                         break;
149                 case 'v':
150                         funct |= verbose;
151                         break;
152                 case 't':
153                         funct |= display;
154                         break;
155                 case 'p':
156                         funct |= extract_to_stdout;
157                         break;
158                 case 'x':
159                         funct |= extract_to_file;
160                         break;
161                 default:
162                         usage(ar_usage);
163                 }
164         }
165  
166         /* check the src filename was specified */
167         if (optind == argc)
168                 usage(ar_usage);
169         
170         if ( (srcFd = open(argv[optind], O_RDONLY)) < 0)
171                 error_msg_and_die("Cannot read %s\n", argv[optind]);
172
173         optind++;       
174         head = get_headers(srcFd);
175
176         /* find files to extract or display */
177         /* search through argv and build extract list */
178         for (;optind<argc; optind++) {
179                 ar_headers_t *ar_entry;
180                 ar_entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
181                 ar_entry = &head;
182                 while (ar_entry->next != NULL) {
183                         if (strcmp(argv[optind], ar_entry->name) == 0) {
184                                 ar_headers_t *tmp;
185                                 tmp = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
186                                 *tmp = *extract_list;
187                                 *extract_list = *ar_entry;
188                                 extract_list->next = tmp;
189                                 break;                                  
190                         }
191                         ar_entry=ar_entry->next;
192                 }
193         }
194
195         /* if individual files not found extract all files */   
196         if (extract_list->next==NULL) {
197                 extract_list = &head;
198         }
199         
200         /* find files to extract or display */  
201         while (extract_list->next != NULL) {
202                 if (funct & extract_to_file) {
203                         dstFd = open(extract_list->name, O_WRONLY | O_CREAT, extract_list->mode);                               
204                 }
205                 else if (funct & extract_to_stdout) {
206                         dstFd = fileno(stdout);
207                 }
208                 if ((funct & extract_to_file) || (funct & extract_to_stdout)) {
209                         lseek(srcFd, extract_list->offset, SEEK_SET);
210                         copy_file_chunk(srcFd, dstFd, (size_t) extract_list->size);                     
211                 }
212                 if (funct & verbose) {
213                         printf("%s %d/%d %8d %s ", mode_string(extract_list->mode), 
214                                 extract_list->uid, extract_list->gid,
215                                 extract_list->size, time_string(extract_list->mtime));
216                 }
217                 if ((funct & display) || (funct & verbose)){
218                         printf("%s\n", extract_list->name);
219                 }
220                 extract_list=extract_list->next;
221         }
222         return EXIT_SUCCESS;
223 }