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
33 #define AR_BLOCK_SIZE 60
34 #define BB_DECLARE_EXTERN
35 #define bb_need_io_error
38 struct ArHeader { /* Byte Offset */
39 char ar_name[16]; /* 0-15 */
40 char ar_date[12]; /* 16-27 */
41 char ar_uid[6], ar_gid[6]; /* 28-39 */
42 char ar_mode[8]; /* 40-47 */
43 char ar_size[10]; /* 48-57 */
44 char ar_fmag[2]; /* 58-59 */
46 typedef struct ArHeader ArHeader;
49 char name[17]; /* File name */
50 time_t date; /* long int, No of seconds since epoch */
51 uid_t uid; /* unsigned int, Numeric UID */
52 gid_t gid; /* unsigned int, Numeric GID */
53 mode_t mode; /* unsigned int, Unix mode */
54 size_t size; /* int, Size of the file */
56 typedef struct ArInfo ArInfo;
58 static const char ar_usage[] = "ar [optxvV] archive [filenames] \n"
59 #ifndef BB_FEATURE_TRIVIAL_HELP
60 "\nExtract or list files from an ar archive.\n\n"
62 "\to\t\tpreserve original dates\n"
63 "\tp\t\textract to stdout\n"
66 "\tv\t\tverbosely list files processed\n"
70 static void displayContents(struct ArInfo *entry, int funct)
72 /* TODO convert mode to string */
74 printf("%i %i/%i %6i %s ", entry->mode, entry->uid, entry->gid,
75 entry->size, timeString(entry->date));
76 printf("%s\n", entry->name);
79 /* Converts to new typed struct */
80 static int readArHeader(struct ArHeader *rawHeader, struct ArInfo *header)
85 for (count = 0; count < 16; count++) {
86 if (rawHeader->ar_name[count] == ' ') {
87 for (count2 = count; count2 < 16; count2++)
88 if (!isspace(rawHeader->ar_name[count2]))
93 if (rawHeader->ar_name[count] == '/')
95 header->name[count] = rawHeader->ar_name[count];
97 header->name[count] = '\0';
98 header->date = atoi(rawHeader->ar_date);
99 header->uid = atoi(rawHeader->ar_uid);
100 header->gid = atoi(rawHeader->ar_gid);
101 header->mode = atoi(rawHeader->ar_mode);
102 header->size = atoi(rawHeader->ar_size);
107 * Copy size bytes from current position if srcFd to current position in dstFd
108 * taken from tarExtractRegularFile in tar.c
109 * could be used for ar, tar and copyFile .
111 static int copySubFile(int srcFd, int dstFd, int copySize)
113 int readSize, writeSize, doneSize;
116 while (copySize > 0) {
117 if (copySize > BUFSIZ)
121 writeSize = fullRead(srcFd, buffer, readSize);
122 if (writeSize <= 0) {
123 errorMsg(io_error, "copySubFile :", strerror(errno));
126 doneSize = fullWrite(dstFd, buffer, writeSize);
128 errorMsg(io_error, "copySubFile :", strerror(errno));
131 copySize -= doneSize;
137 * Need to add checks, stat newfile before creation,change mode from 0777
138 * extract to current dir
139 * dstStat.st_size copied from current position of file pointed to by srcFd
141 static int extractToFile(int srcFd, const char *path, const char *name,
146 char *pathname = NULL;
148 if ((temp = isDirectory(path, TRUE, &tmpStat)) != TRUE) {
149 if (!createPath(path, 0777)) {
150 fatalError("Cannot extract to specified path");
154 temp = (strlen(path) + 16);
155 pathname = (char *) xmalloc(temp);
156 pathname = strcpy(pathname, path);
157 pathname = strcat(pathname, &name[0]);
158 dstFd = device_open(pathname, O_WRONLY | O_CREAT);
160 temp = copySubFile(srcFd, dstFd, size);
165 /* Step through the ar file entries */
166 static int readArFile(char **fileNames, int argc, int funct)
176 ArHeader rawArHeader;
180 char *selName[argc - 2];
183 if ((funct & 1) == 1)
185 if ((funct & 2) == 2)
187 if ((funct & 4) == 4)
189 if ((funct & 16) == 16) { /* extract to stdout */
193 if ((funct & 8) == 8) { /* extract to file */
196 arName = fileNames[2];
197 for (i = 0; i < (argc - 3); i++)
198 selName[i] = fileNames[i + 3];
199 arFd = open(arName, O_RDONLY);
201 errorMsg("Error opening '%s': %s\n", arName, strerror(errno));
204 if (fullRead(arFd, arVersion, 8) <= 0) {
205 errorMsg("ar: Unexpected EOF in archive\n");
208 if (strncmp(arVersion, "!<arch>", 7) != 0) {
209 errorMsg("ar header fails check ");
212 while ((status = fullRead(arFd, (char *) &rawArHeader, AR_BLOCK_SIZE))
214 readArHeader(&rawArHeader, &arEntry);
217 displayContents(&arEntry, funct);
223 for (i = 0; i < (argc - 3); i++) {
224 if ((status = (strcmp(selName[i], arEntry.name))) == 0)
228 if ((extract == 1) && (found == 1)) {
229 if (extToStdout == 1) {
230 copySubFile(arFd, fileno(stdout), arEntry.size);
232 extractToFile(arFd, "./", arEntry.name, arEntry.size);
235 lseek(arFd, arEntry.size, SEEK_CUR);
240 extern int ar_main(int argc, char **argv)
253 while ((c = *opt_ptr++) != '\0') {
255 case 'o': /* preserver original dates */
258 case 'p': /* extract to stdout */
261 case 't': /* display contents */
264 case 'x': /* extract contents of archive */
267 case 'v': /* be verbose */
275 ret = readArFile(argv, argc, funct);