1c598fa164654feea3e3d0104d34d99affb57545
[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@netconnect.com.au> 1 June 2000
7  *              
8  * Modified 8 August 2000 by Glenn McGrath 
9  *  - now uses getopt
10  *  - moved copySubFile function to utilities.c
11  *  - creates linked list of all headers 
12  *  - easily accessable to other busybox functions  
13  *
14  * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24  * General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  *
30  * Last modified 25 August 2000
31  */
32 #include <stdio.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <ctype.h>
36 #include <time.h>
37 #include <utime.h>
38 #include <sys/types.h>
39 #include "internal.h"
40
41 #define BLOCK_SIZE 60
42 #define PRESERVE_DATE 1 /* preserve original dates */
43 #define VERBOSE       2 /* be verbose */
44 #define DISPLAY       4 /* display contents */
45 #define EXT_TO_FILE   8 /* extract contents of archive */
46 #define EXT_TO_STDOUT 16        /* extract to stdout */
47
48 #define BB_DECLARE_EXTERN
49 #define bb_need_io_error
50 #include "messages.c"
51
52 typedef struct rawArHeader {            /* Byte Offset */
53         char name[16];          /*  0-15 */
54         char date[12];          /* 16-27 */
55         char uid[6], gid[6];    /* 28-39 */
56         char mode[8];           /* 40-47 */
57         char size[10];          /* 48-57 */
58         char fmag[2];           /* 58-59 */
59 } rawArHeader_t;
60
61 typedef struct headerL {
62         char name[16];
63         size_t size;
64         uid_t uid;
65         gid_t gid;
66         mode_t mode;
67         time_t mtime;
68         off_t offset;
69         struct headerL *next;
70 } headerL_t;
71
72 /*
73  * populate linked list with all ar file entries and offset 
74  */
75 static int parseArArchive(int srcFd, headerL_t *current)
76 {
77         off_t lastOffset=0, thisOffset=0;
78         char arVersion[8];
79         rawArHeader_t rawArHeader;
80         
81         lseek(srcFd, 0, SEEK_SET);
82         if (fullRead(srcFd, arVersion, 8) <= 0) {
83                 errorMsg("Invalid header magic\n");
84                 return (FALSE);
85         }
86         if (strncmp(arVersion,"!<arch>",7) != 0) {
87                 errorMsg("This doesnt appear to be an ar archive\n");
88                 return(FALSE);
89         }
90         while (fullRead(srcFd, (char *) &rawArHeader, 60) == 60) {
91               if ( (rawArHeader.fmag[0] == '`') &&
92                         (rawArHeader.fmag[1] == '\n')) {
93                         sscanf(rawArHeader.name, "%s", current->name);
94                         parse_mode(rawArHeader.mode, &current->mode);
95                         current->mtime = atoi(rawArHeader.date);
96                         current->uid = atoi(rawArHeader.uid);
97                         current->gid = atoi(rawArHeader.gid);
98                         current->size = (size_t) atoi(rawArHeader.size);
99                         current->offset = lseek(srcFd, 0 , SEEK_CUR);
100                         current->next = (headerL_t *) xmalloc(sizeof(headerL_t));
101                         lastOffset = lseek(srcFd, (off_t) current->size, SEEK_CUR);
102                         current = current->next;
103                 }
104                 else {  /* GNU ar has an extra char after data */
105                         thisOffset=lseek(srcFd, 0, SEEK_CUR);
106                         if ( (thisOffset - lastOffset) > ((off_t) 61) ) 
107                                 return(FALSE);
108                         lseek(srcFd, thisOffset - 59, SEEK_SET);
109                 }
110         }
111         return(FALSE);
112 }
113
114 /*
115  * return the headerL_t struct for the specified filename
116  */
117 static headerL_t *getSubFileHeader(int srcFd, const char filename[16])
118 {
119         headerL_t *list;
120         list = xmalloc(sizeof(headerL_t)); 
121
122         parseArArchive(srcFd, list);
123         while (list->next != NULL) {
124                 if (strncmp(list->name, filename, strlen(filename))==0)
125                         return(list);
126                 list=list->next;
127         }
128         return(NULL);
129 }
130
131 /*
132  * populate linked list with all ar file entries and offset 
133  */
134 static int displayEntry(int srcFd, const char filename[16], int funct)
135 {
136         headerL_t *file;
137
138         if ((file = getSubFileHeader(srcFd, filename)) == NULL)
139                 return(FALSE);
140         if ((funct & VERBOSE) == VERBOSE) {
141                 printf("%s %d/%d %8d %s ", modeString(file->mode), file->uid, file->gid, file->size, timeString(file->mtime));
142         }
143         printf("%s\n", file->name);
144         return(TRUE);
145 }
146
147 static int extractAr(int srcFd, int dstFd, const char filename[16])
148 {
149         headerL_t *file;
150  
151         if ( (file = getSubFileHeader(srcFd, filename)) == NULL)
152                 return(FALSE);
153         lseek(srcFd, file->offset, SEEK_SET);
154         if (copySubFile(srcFd, dstFd, (size_t) file->size) == TRUE)
155                 return(TRUE);   
156         return(FALSE);
157 }
158
159 extern int ar_main(int argc, char **argv)
160 {
161         int funct = 0, opt=0;
162         int srcFd=0, dstFd=0;
163  
164         while ((opt = getopt(argc, argv, "ovt:p:x:")) != -1) {
165                 switch (opt) {
166                 case 'o':
167                         funct = funct | PRESERVE_DATE;
168                         break;
169                 case 'v':
170                         funct = funct | VERBOSE;
171                         break;
172                 case 't':
173                         funct = funct | DISPLAY;
174                 case 'x':
175                         if (opt=='x') {
176                                 funct = funct | EXT_TO_FILE;
177                         }
178                 case 'p':
179                         if (opt=='p') {
180                                 funct = funct | EXT_TO_STDOUT;
181                         }
182                         /* following is common to 't','x' and 'p' */
183                         if (optarg == NULL) {
184                                 printf("expected a filename\n");
185                                 return(FALSE);
186                         }
187                         if ( (srcFd = open(optarg, O_RDONLY)) < 0) {
188                                 errorMsg("Cannot read %s\n", optarg);
189                                 return (FALSE);
190                         }
191                         break;
192                 default:
193                         usage(ar_usage);
194                 }
195         }
196  
197         /* check options not just preserve_dates and/or verbose */  
198         if (funct < 4) {
199                 usage(ar_usage);
200                 return(FALSE);
201         }
202         
203         /* find files to extract */
204         if (optind<argc)
205                 for(; optind < argc; optind++) {
206                         if ( (funct & EXT_TO_FILE) == EXT_TO_FILE) {
207                                 dstFd = open(argv[optind], O_WRONLY | O_CREAT);
208                                 extractAr(srcFd, dstFd, argv[optind]);
209                         }
210                         if ( (funct & EXT_TO_STDOUT) == EXT_TO_STDOUT)  
211                                 extractAr(srcFd, fileno(stdout), argv[optind]); 
212                         if ( (funct & DISPLAY) == DISPLAY)
213                                 displayEntry(srcFd, argv[optind], funct);
214                 }
215         return (TRUE);
216 }