Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 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 |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 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 | * |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 24 | * Last modified 9 September 2000 |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 25 | */ |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 27 | #include <string.h> |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 28 | #include <fcntl.h> |
| 29 | #include <errno.h> |
| 30 | #include <ctype.h> |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 31 | #include <time.h> |
| 32 | #include <utime.h> |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | #include <stdlib.h> |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 35 | #include <sys/types.h> |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 36 | #include <sys/stat.h> |
| 37 | #include <malloc.h> |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 38 | #include "internal.h" |
| 39 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 40 | #define BLOCK_SIZE 60 |
| 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 */ |
Glenn L McGrath | e2b345a | 2000-09-09 14:50:04 +0000 | [diff] [blame] | 46 | #define RECURSIVE 32 |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 47 | |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 48 | #define MAX_NAME_LENGTH 100 |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 49 | |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 50 | //#define BB_DECLARE_EXTERN |
| 51 | //#define bb_need_io_error |
| 52 | //#include "messages.c" |
| 53 | |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 54 | //#define BB_AR_EXPERIMENTAL_UNTAR |
| 55 | |
| 56 | #if defined BB_AR_EXPERIMENTAL_UNTAR |
| 57 | typedef struct rawTarHeader { |
| 58 | char name[100]; /* 0-99 */ |
| 59 | char mode[8]; /* 100-107 */ |
| 60 | char uid[8]; /* 108-115 */ |
| 61 | char gid[8]; /* 116-123 */ |
| 62 | char size[12]; /* 124-135 */ |
| 63 | char mtime[12]; /* 136-147 */ |
| 64 | char chksum[8]; /* 148-155 */ |
| 65 | char typeflag; /* 156-156 */ |
| 66 | char linkname[100]; /* 157-256 */ |
| 67 | char magic[6]; /* 257-262 */ |
| 68 | char version[2]; /* 263-264 */ |
| 69 | char uname[32]; /* 265-296 */ |
| 70 | char gname[32]; /* 297-328 */ |
| 71 | char devmajor[8]; /* 329-336 */ |
| 72 | char devminor[8]; /* 337-344 */ |
| 73 | char prefix[155]; /* 345-499 */ |
| 74 | char padding[12]; /* 500-512 */ |
| 75 | } rawTarHeader_t; |
| 76 | #endif |
| 77 | |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 78 | typedef struct rawArHeader { /* Byte Offset */ |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 79 | char name[16]; /* 0-15 */ |
| 80 | char date[12]; /* 16-27 */ |
| 81 | char uid[6], gid[6]; /* 28-39 */ |
| 82 | char mode[8]; /* 40-47 */ |
| 83 | char size[10]; /* 48-57 */ |
| 84 | char fmag[2]; /* 58-59 */ |
| 85 | } rawArHeader_t; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 86 | |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 87 | typedef struct headerL { |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 88 | char name[MAX_NAME_LENGTH]; |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 89 | size_t size; |
| 90 | uid_t uid; |
| 91 | gid_t gid; |
| 92 | mode_t mode; |
| 93 | time_t mtime; |
| 94 | off_t offset; |
| 95 | struct headerL *next; |
| 96 | } headerL_t; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 97 | |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 98 | #if defined BB_AR_EXPERIMENTAL_UNTAR |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 99 | /* |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 100 | * identify Tar header (magic field) and reset srcFd to entry position |
| 101 | */ |
| 102 | static int checkTarMagic(int srcFd) |
| 103 | { |
| 104 | off_t headerStart; |
| 105 | char magic[6]; |
| 106 | |
| 107 | headerStart = lseek(srcFd, 0, SEEK_CUR); |
| 108 | lseek(srcFd, (off_t) 257, SEEK_CUR); |
| 109 | fullRead(srcFd, magic, 6); |
| 110 | lseek(srcFd, headerStart, SEEK_SET); |
| 111 | if (strncmp(magic, "ustar", 5)!=0) |
| 112 | return(FALSE); |
| 113 | return(TRUE); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static int readTarHeader(int srcFd, headerL_t *current) |
| 118 | { |
| 119 | rawTarHeader_t rawTarHeader; |
| 120 | unsigned char *temp = (unsigned char *) &rawTarHeader; |
| 121 | long sum = 0; |
| 122 | int i; |
| 123 | off_t initialOffset; |
| 124 | |
| 125 | initialOffset = lseek(srcFd, 0, SEEK_CUR); |
| 126 | if (fullRead(srcFd, (char *) &rawTarHeader, 512) != 512) { |
| 127 | lseek(srcFd, initialOffset, SEEK_SET); |
| 128 | return(FALSE); |
| 129 | } |
| 130 | for (i = 0; i < 148 ; i++) |
| 131 | sum += temp[i]; |
| 132 | sum += ' ' * 8; |
| 133 | for (i = 156; i < 512 ; i++) |
| 134 | sum += temp[i]; |
| 135 | if (sum!= strtol(rawTarHeader.chksum, NULL, 8)) |
| 136 | return(FALSE); |
| 137 | sscanf(rawTarHeader.name, "%s", current->name); |
| 138 | current->size = strtol(rawTarHeader.size, NULL, 8); |
| 139 | current->uid = strtol(rawTarHeader.uid, NULL, 8); |
| 140 | current->gid = strtol(rawTarHeader.gid, NULL, 8); |
| 141 | current->mode = strtol(rawTarHeader.mode, NULL, 8); |
| 142 | current->mtime = strtol(rawTarHeader.mtime, NULL, 8); |
| 143 | current->offset = lseek(srcFd, 0 , SEEK_CUR); |
| 144 | |
| 145 | current->next = (headerL_t *) xmalloc(sizeof(headerL_t)); |
| 146 | current = current->next; |
| 147 | return(TRUE); |
| 148 | } |
| 149 | #endif |
| 150 | |
| 151 | /* |
| 152 | * identify Ar header (magic) and reset srcFd to entry position |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 153 | */ |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 154 | static int checkArMagic(int srcFd) |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 155 | { |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 156 | off_t headerStart; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 157 | char arMagic[8]; |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 158 | |
| 159 | headerStart = lseek(srcFd, 0, SEEK_CUR); |
| 160 | if (fullRead(srcFd, arMagic, 8) != 8) { |
| 161 | printf("fatal error/n"); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 162 | return (FALSE); |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 163 | } |
| 164 | lseek(srcFd, headerStart, SEEK_SET); |
| 165 | |
| 166 | if (strncmp(arMagic,"!<arch>",7) != 0) |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 167 | return(FALSE); |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 168 | return(TRUE); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * get, check and correct the converted header |
| 173 | */ |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 174 | static int readArEntry(int srcFd, headerL_t *entry) |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 175 | { |
| 176 | size_t nameLength; |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 177 | rawArHeader_t rawArHeader; |
| 178 | off_t initialOffset; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 179 | |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 180 | initialOffset = lseek(srcFd, 0, SEEK_CUR); |
| 181 | if (fullRead(srcFd, (char *) &rawArHeader, 60) != 60) { |
| 182 | lseek(srcFd, initialOffset, SEEK_SET); |
| 183 | return(FALSE); |
| 184 | } |
| 185 | if ((rawArHeader.fmag[0]!='`') || (rawArHeader.fmag[1]!='\n')) { |
| 186 | lseek(srcFd, initialOffset, SEEK_SET); |
| 187 | return(FALSE); |
| 188 | } |
| 189 | |
| 190 | strncpy(entry->name, rawArHeader.name, 16); |
| 191 | nameLength=strcspn(entry->name, " \\"); |
| 192 | entry->name[nameLength]='\0'; |
| 193 | parse_mode(rawArHeader.mode, &entry->mode); |
| 194 | entry->mtime = atoi(rawArHeader.date); |
| 195 | entry->uid = atoi(rawArHeader.uid); |
| 196 | entry->gid = atoi(rawArHeader.gid); |
| 197 | entry->size = (size_t) atoi(rawArHeader.size); |
| 198 | entry->offset = initialOffset + (off_t) 60; |
| 199 | |
| 200 | nameLength = strcspn(entry->name, "/"); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 201 | |
| 202 | /* handle GNU style short filenames, strip trailing '/' */ |
| 203 | if (nameLength > 0) |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 204 | entry->name[nameLength]='\0'; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 205 | |
| 206 | /* handle GNU style long filenames */ |
| 207 | if (nameLength == 0) { |
| 208 | /* escape from recursive call */ |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 209 | if (entry->name[1]=='0') |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 210 | return(TRUE); |
| 211 | |
| 212 | /* the data section contains the real filename */ |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 213 | if (entry->name[1]=='/') { |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 214 | char tempName[MAX_NAME_LENGTH]; |
| 215 | |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 216 | if (entry->size > MAX_NAME_LENGTH) |
| 217 | entry->size = MAX_NAME_LENGTH; |
| 218 | fullRead(srcFd, tempName, entry->size); |
| 219 | tempName[entry->size-3]='\0'; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 220 | |
| 221 | /* read the second header for this entry */ |
| 222 | /* be carefull, this is recursive */ |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 223 | if (readArEntry(srcFd, entry)==FALSE) |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 224 | return(FALSE); |
| 225 | |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 226 | if ((entry->name[0]='/') && (entry->name[1]='0')) |
| 227 | strcpy(entry->name, tempName); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 228 | else { |
| 229 | errorMsg("Invalid long filename\n"); |
| 230 | return(FALSE); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | return(TRUE); |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 238 | * return the headerL_t struct for the specified filename |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 239 | */ |
Glenn L McGrath | e2b345a | 2000-09-09 14:50:04 +0000 | [diff] [blame] | 240 | static headerL_t *getHeaders(int srcFd, headerL_t *head, int funct) |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 241 | { |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 242 | #if defined BB_AR_EXPERIMENTAL_UNTAR |
| 243 | int tar=FALSE; |
| 244 | #endif |
| 245 | int ar=FALSE; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 246 | headerL_t *list; |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 247 | off_t initialOffset; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 248 | |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 249 | list = (headerL_t *) malloc(sizeof(headerL_t)); |
| 250 | initialOffset=lseek(srcFd, 0, SEEK_CUR); |
| 251 | if (checkArMagic(srcFd)==TRUE) |
| 252 | ar=TRUE; |
| 253 | |
| 254 | #if defined BB_AR_EXPERIMENTAL_UNTAR |
| 255 | if (checkTarMagic(srcFd)==TRUE) |
| 256 | tar=TRUE; |
| 257 | |
| 258 | if (tar==TRUE) { |
| 259 | while(readTarHeader(srcFd, list)==TRUE) { |
| 260 | off_t tarOffset; |
| 261 | list->next = (headerL_t *) malloc(sizeof(headerL_t)); |
| 262 | *list->next = *head; |
| 263 | *head = *list; |
| 264 | |
| 265 | /* recursive check for sub-archives */ |
| 266 | if ((funct & RECURSIVE) == RECURSIVE) |
| 267 | head = getHeaders(srcFd, head, funct); |
| 268 | tarOffset = (off_t) head->size/512; |
| 269 | if ( head->size % 512 > 0) |
| 270 | tarOffset++; |
| 271 | tarOffset=tarOffset*512; |
| 272 | lseek(srcFd, head->offset + tarOffset, SEEK_SET); |
| 273 | } |
| 274 | } |
| 275 | #endif |
| 276 | |
| 277 | if (ar==TRUE) { |
| 278 | lseek(srcFd, 8, SEEK_CUR); |
| 279 | while(1) { |
| 280 | if (readArEntry(srcFd, list) == FALSE) { |
| 281 | lseek(srcFd, ++initialOffset, SEEK_CUR); |
| 282 | if (readArEntry(srcFd, list) == FALSE) |
| 283 | return(head); |
| 284 | } |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 285 | list->next = (headerL_t *) malloc(sizeof(headerL_t)); |
| 286 | *list->next = *head; |
| 287 | *head = *list; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 288 | /* recursive check for sub-archives */ |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 289 | if (funct & RECURSIVE) |
Glenn L McGrath | e2b345a | 2000-09-09 14:50:04 +0000 | [diff] [blame] | 290 | head = getHeaders(srcFd, head, funct); |
| 291 | lseek(srcFd, head->offset + head->size, SEEK_SET); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 294 | return(head); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * find an entry in the linked list matching the filename |
| 299 | */ |
| 300 | static headerL_t *findEntry(headerL_t *head, const char *filename) |
| 301 | { |
| 302 | while(head->next != NULL) { |
| 303 | if (strcmp(filename, head->name)==0) |
| 304 | return(head); |
| 305 | head=head->next; |
| 306 | } |
| 307 | return(NULL); |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 308 | } |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 309 | |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 310 | extern int ar_main(int argc, char **argv) |
| 311 | { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 312 | int funct = 0, opt=0; |
| 313 | int srcFd=0, dstFd=0; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 314 | headerL_t *header, *entry, *extractList; |
| 315 | |
Glenn L McGrath | e2b345a | 2000-09-09 14:50:04 +0000 | [diff] [blame] | 316 | while ((opt = getopt(argc, argv, "ovtpxR")) != -1) { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 317 | switch (opt) { |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 318 | case 'o': |
Eric Andersen | 49352ad | 2000-09-11 04:55:31 +0000 | [diff] [blame] | 319 | funct |= PRESERVE_DATE; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 320 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 321 | case 'v': |
Eric Andersen | 49352ad | 2000-09-11 04:55:31 +0000 | [diff] [blame] | 322 | funct |= VERBOSE; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 323 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 324 | case 't': |
Eric Andersen | 49352ad | 2000-09-11 04:55:31 +0000 | [diff] [blame] | 325 | funct |= DISPLAY; |
Glenn L McGrath | 437bf72 | 2000-09-09 13:38:26 +0000 | [diff] [blame] | 326 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 327 | case 'x': |
Eric Andersen | 49352ad | 2000-09-11 04:55:31 +0000 | [diff] [blame] | 328 | funct |= EXT_TO_FILE; |
Glenn L McGrath | 437bf72 | 2000-09-09 13:38:26 +0000 | [diff] [blame] | 329 | break; |
Eric Andersen | 57ebebf | 2000-07-05 17:21:58 +0000 | [diff] [blame] | 330 | case 'p': |
Eric Andersen | 49352ad | 2000-09-11 04:55:31 +0000 | [diff] [blame] | 331 | funct |= EXT_TO_STDOUT; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 332 | break; |
Glenn L McGrath | e2b345a | 2000-09-09 14:50:04 +0000 | [diff] [blame] | 333 | case 'R': |
Eric Andersen | 49352ad | 2000-09-11 04:55:31 +0000 | [diff] [blame] | 334 | funct |= RECURSIVE; |
Glenn L McGrath | e2b345a | 2000-09-09 14:50:04 +0000 | [diff] [blame] | 335 | break; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 336 | default: |
| 337 | usage(ar_usage); |
| 338 | } |
| 339 | } |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 340 | |
Glenn L McGrath | 437bf72 | 2000-09-09 13:38:26 +0000 | [diff] [blame] | 341 | /* check the src filename was specified */ |
| 342 | if (optind == argc) { |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 343 | usage(ar_usage); |
| 344 | return(FALSE); |
| 345 | } |
Eric Andersen | 852ff13 | 2000-06-16 04:56:40 +0000 | [diff] [blame] | 346 | |
Glenn L McGrath | 437bf72 | 2000-09-09 13:38:26 +0000 | [diff] [blame] | 347 | if ( (srcFd = open(argv[optind], O_RDONLY)) < 0) { |
| 348 | errorMsg("Cannot read %s\n", optarg); |
| 349 | return (FALSE); |
| 350 | } |
| 351 | optind++; |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 352 | entry = (headerL_t *) malloc(sizeof(headerL_t)); |
| 353 | header = (headerL_t *) malloc(sizeof(headerL_t)); |
| 354 | extractList = (headerL_t *) malloc(sizeof(headerL_t)); |
| 355 | |
Glenn L McGrath | e2b345a | 2000-09-09 14:50:04 +0000 | [diff] [blame] | 356 | header = getHeaders(srcFd, header, funct); |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 357 | /* find files to extract or display */ |
| 358 | if (optind<argc) { |
| 359 | /* only handle specified files */ |
| 360 | while(optind < argc) { |
| 361 | if ( (entry = findEntry(header, argv[optind])) != NULL) { |
| 362 | entry->next = (headerL_t *) malloc(sizeof(headerL_t)); |
| 363 | *entry->next = *extractList; |
| 364 | *extractList = *entry; |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 365 | } |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 366 | optind++; |
| 367 | } |
| 368 | } |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 369 | else |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 370 | extractList = header; |
| 371 | |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 372 | while(extractList->next != NULL) { |
| 373 | if (funct & EXT_TO_FILE) { |
| 374 | if (isDirectory(extractList->name, TRUE, NULL)==FALSE) |
| 375 | createPath(extractList->name, 0666); |
Glenn L McGrath | 6fb88e7 | 2000-09-09 12:48:40 +0000 | [diff] [blame] | 376 | dstFd = open(extractList->name, O_WRONLY | O_CREAT, extractList->mode); |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 377 | lseek(srcFd, extractList->offset, SEEK_SET); |
| 378 | copySubFile(srcFd, dstFd, (size_t) extractList->size); |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 379 | } |
Glenn L McGrath | fca8050 | 2000-09-11 05:25:39 +0000 | [diff] [blame^] | 380 | if (funct & EXT_TO_STDOUT) { |
| 381 | lseek(srcFd, extractList->offset, SEEK_SET); |
| 382 | copySubFile(srcFd, fileno(stdout), (size_t) extractList->size); |
| 383 | } |
| 384 | if ( (funct & DISPLAY) || (funct & VERBOSE)) { |
| 385 | if (funct & VERBOSE) |
| 386 | printf("%s %d/%d %8d %s ", modeString(extractList->mode), |
| 387 | extractList->uid, extractList->gid, |
| 388 | extractList->size, timeString(extractList->mtime)); |
| 389 | printf("%s\n", extractList->name); |
| 390 | } |
Glenn L McGrath | 8324b9f | 2000-09-09 08:35:45 +0000 | [diff] [blame] | 391 | extractList=extractList->next; |
| 392 | } |
Glenn L McGrath | 06aeb6c | 2000-08-25 03:50:10 +0000 | [diff] [blame] | 393 | return (TRUE); |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 394 | } |