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 * Modified 8 August 2000 by Glenn McGrath
10 * - moved copySubFile function to utilities.c
11 * - creates linked list of all headers
12 * - easily accessable to other busybox functions
14 * Based in part on BusyBox tar, Debian dpkg-deb and GNU ar.
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.
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.
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
30 * Last modified 25 August 2000
38 #include <sys/types.h>
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 */
48 #define BB_DECLARE_EXTERN
49 #define bb_need_io_error
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 */
61 typedef struct headerL {
73 * populate linked list with all ar file entries and offset
75 static int parseArArchive(int srcFd, headerL_t *current)
77 off_t lastOffset=0, thisOffset=0;
79 rawArHeader_t rawArHeader;
81 lseek(srcFd, 0, SEEK_SET);
82 if (fullRead(srcFd, arVersion, 8) <= 0) {
83 errorMsg("Invalid header magic\n");
86 if (strncmp(arVersion,"!<arch>",7) != 0) {
87 errorMsg("This doesnt appear to be an ar archive\n");
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, ¤t->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;
104 else { /* GNU ar has an extra char after data */
105 thisOffset=lseek(srcFd, 0, SEEK_CUR);
106 if ( (thisOffset - lastOffset) > ((off_t) 61) )
108 lseek(srcFd, thisOffset - 59, SEEK_SET);
115 * return the headerL_t struct for the specified filename
117 static headerL_t *getSubFileHeader(int srcFd, const char filename[16])
120 list = xmalloc(sizeof(headerL_t));
122 parseArArchive(srcFd, list);
123 while (list->next != NULL) {
124 if (strncmp(list->name, filename, strlen(filename))==0)
132 * populate linked list with all ar file entries and offset
134 static int displayEntry(int srcFd, const char filename[16], int funct)
138 if ((file = getSubFileHeader(srcFd, filename)) == NULL)
140 if ((funct & VERBOSE) == VERBOSE) {
141 printf("%s %d/%d %8d %s ", modeString(file->mode), file->uid, file->gid, file->size, timeString(file->mtime));
143 printf("%s\n", file->name);
147 static int extractAr(int srcFd, int dstFd, const char filename[16])
151 if ( (file = getSubFileHeader(srcFd, filename)) == NULL)
153 lseek(srcFd, file->offset, SEEK_SET);
154 if (copySubFile(srcFd, dstFd, (size_t) file->size) == TRUE)
159 extern int ar_main(int argc, char **argv)
161 int funct = 0, opt=0;
162 int srcFd=0, dstFd=0;
164 while ((opt = getopt(argc, argv, "ovt:p:x:")) != -1) {
167 funct = funct | PRESERVE_DATE;
170 funct = funct | VERBOSE;
173 funct = funct | DISPLAY;
176 funct = funct | EXT_TO_FILE;
180 funct = funct | EXT_TO_STDOUT;
182 /* following is common to 't','x' and 'p' */
183 if (optarg == NULL) {
184 printf("expected a filename\n");
187 if ( (srcFd = open(optarg, O_RDONLY)) < 0) {
188 errorMsg("Cannot read %s\n", optarg);
197 /* check options not just preserve_dates and/or verbose */
203 /* find files to extract */
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]);
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);