1 /* vi: set sw=4 ts=4: */
3 * Mini ar implementation for busybox
5 * Copyright (C) 2000 by Glenn McGrath
6 * Written by Glenn McGrath <bug1@netconnect.com.au> 1 June 2000
8 * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
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.
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.
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
24 * Last modified 9 September 2000
35 #include <sys/types.h>
41 #define PRESERVE_DATE 1 /* preserve original dates */
42 #define VERBOSE 2 /* be verbose */
43 #define DISPLAY 4 /* display contents */
44 #define EXT_TO_FILE 8 /* extract contents of archive */
45 #define EXT_TO_STDOUT 16 /* extract to stdout */
47 #define MAX_NAME_LENGTH 100
49 //#define BB_DECLARE_EXTERN
50 //#define bb_need_io_error
51 //#include "messages.c"
53 typedef struct rawArHeader { /* Byte Offset */
54 char name[16]; /* 0-15 */
55 char date[12]; /* 16-27 */
56 char uid[6], gid[6]; /* 28-39 */
57 char mode[8]; /* 40-47 */
58 char size[10]; /* 48-57 */
59 char fmag[2]; /* 58-59 */
62 typedef struct headerL {
63 char name[MAX_NAME_LENGTH];
74 * identify Ar header (magic) and set srcFd to first header entry
76 static int checkArMagic(int srcFd)
79 if (fullRead(srcFd, arMagic, 8) != 8)
82 if (strncmp(arMagic,"!<arch>",7) != 0)
88 * read, convert and check the raw ar header
89 * srcFd should be pointing to the start of header prior to entry
90 * srcFd will be pointing at the start of data after successful exit
91 * if returns FALSE srcFd is reset to initial position
93 static int readRawArHeader(int srcFd, headerL_t *header)
95 rawArHeader_t rawArHeader;
99 initialOffset = lseek(srcFd, 0, SEEK_CUR);
100 if (fullRead(srcFd, (char *) &rawArHeader, 60) != 60) {
101 lseek(srcFd, initialOffset, SEEK_SET);
104 if ((rawArHeader.fmag[0]!='`') || (rawArHeader.fmag[1]!='\n')) {
105 lseek(srcFd, initialOffset, SEEK_SET);
109 strncpy(header->name, rawArHeader.name, 16);
110 nameLength=strcspn(header->name, " \\");
111 header->name[nameLength]='\0';
112 parse_mode(rawArHeader.mode, &header->mode);
113 header->mtime = atoi(rawArHeader.date);
114 header->uid = atoi(rawArHeader.uid);
115 header->gid = atoi(rawArHeader.gid);
116 header->size = (size_t) atoi(rawArHeader.size);
117 header->offset = initialOffset + (off_t) 60;
122 * get, check and correct the converted header
124 static int readArEntry(int srcFd, headerL_t *newEntry)
128 if(readRawArHeader(srcFd, newEntry)==FALSE)
131 nameLength = strcspn(newEntry->name, "/");
133 /* handle GNU style short filenames, strip trailing '/' */
135 newEntry->name[nameLength]='\0';
137 /* handle GNU style long filenames */
138 if (nameLength == 0) {
139 /* escape from recursive call */
140 if (newEntry->name[1]=='0')
143 /* the data section contains the real filename */
144 if (newEntry->name[1]=='/') {
145 char tempName[MAX_NAME_LENGTH];
147 if (newEntry->size > MAX_NAME_LENGTH)
148 newEntry->size = MAX_NAME_LENGTH;
149 fullRead(srcFd, tempName, newEntry->size);
150 tempName[newEntry->size-3]='\0';
152 /* read the second header for this entry */
153 /* be carefull, this is recursive */
154 if (readArEntry(srcFd, newEntry)==FALSE)
157 if ((newEntry->name[0]='/') && (newEntry->name[1]='0'))
158 strcpy(newEntry->name, tempName);
160 errorMsg("Invalid long filename\n");
169 * return the headerL_t struct for the specified filename
171 static headerL_t *getHeaders(int srcFd, headerL_t *head)
175 list = (headerL_t *) malloc(sizeof(headerL_t));
177 if (checkArMagic(srcFd)==TRUE)
180 errorMsg("isnt an ar archive\n");
183 while(readArEntry(srcFd, list) == TRUE) {
184 list->next = (headerL_t *) malloc(sizeof(headerL_t));
188 /* recursive check for sub-archives */
189 lseek(srcFd, list->size, SEEK_CUR);
190 /* printf("checking for sub headers\n");
191 if ((subList = getHeaders(srcFd, list->next)) != NULL) {
192 printf("found a sub archive !\n");
195 printf("didnt find a sub header\n"); */
203 * find an entry in the linked list matching the filename
205 static headerL_t *findEntry(headerL_t *head, const char *filename)
207 while(head->next != NULL) {
208 if (strcmp(filename, head->name)==0)
216 * populate linked list with all ar file entries and offset
218 static int displayEntry(headerL_t *head, int funct)
220 if ((funct & VERBOSE) == VERBOSE) {
221 printf("%s %d/%d %8d %s ", modeString(head->mode), head->uid, head->gid, head->size, timeString(head->mtime));
223 printf("%s\n", head->name);
228 static int extractAr(int srcFd, int dstFd, headerL_t *file)
230 lseek(srcFd, file->offset, SEEK_SET);
231 if (copySubFile(srcFd, dstFd, (size_t) file->size) == TRUE)
236 extern int ar_main(int argc, char **argv)
238 int funct = 0, opt=0;
239 int srcFd=0, dstFd=0;
240 headerL_t *header, *entry, *extractList;
242 while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
245 funct = funct | PRESERVE_DATE;
248 funct = funct | VERBOSE;
251 funct = funct | DISPLAY;
254 funct = funct | EXT_TO_FILE;
257 funct = funct | EXT_TO_STDOUT;
264 /* check the src filename was specified */
265 if (optind == argc) {
270 if ( (srcFd = open(argv[optind], O_RDONLY)) < 0) {
271 errorMsg("Cannot read %s\n", optarg);
275 entry = (headerL_t *) malloc(sizeof(headerL_t));
276 header = (headerL_t *) malloc(sizeof(headerL_t));
277 extractList = (headerL_t *) malloc(sizeof(headerL_t));
279 header = getHeaders(srcFd, header);
281 /* find files to extract or display */
283 /* only handle specified files */
284 while(optind < argc) {
285 if ( (entry = findEntry(header, argv[optind])) != NULL) {
286 entry->next = (headerL_t *) malloc(sizeof(headerL_t));
287 *entry->next = *extractList;
288 *extractList = *entry;
294 /* extract everything */
295 extractList = header;
297 while(extractList->next != NULL) {
298 if ( (funct & EXT_TO_FILE) == EXT_TO_FILE) {
299 dstFd = open(extractList->name, O_WRONLY | O_CREAT, extractList->mode);
301 extractAr(srcFd, dstFd, extractList);
303 if ( (funct & EXT_TO_STDOUT) == EXT_TO_STDOUT)
304 extractAr(srcFd, fileno(stdout), extractList);
305 if ( (funct & DISPLAY) == DISPLAY)
306 displayEntry(extractList, funct);
307 extractList=extractList->next;