Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
| 5 | * Copyright (C) tons of folks. Tracking down who wrote what |
| 6 | * isn't something I'm going to worry about... If you wrote something |
| 7 | * here, please feel free to acknowledge your work. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell |
| 24 | * Permission has been granted to redistribute this code under the GPL. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
| 31 | #include "libbb.h" |
| 32 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 33 | extern ar_headers_t *get_ar_headers(FILE *in_file) |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 34 | { |
| 35 | typedef struct raw_ar_header_s { /* Byte Offset */ |
| 36 | char name[16]; /* 0-15 */ |
| 37 | char date[12]; /* 16-27 */ |
| 38 | char uid[6]; |
| 39 | char gid[6]; /* 28-39 */ |
| 40 | char mode[8]; /* 40-47 */ |
| 41 | char size[10]; /* 48-57 */ |
| 42 | char fmag[2]; /* 58-59 */ |
| 43 | } raw_ar_header_t; |
| 44 | |
| 45 | raw_ar_header_t raw_ar_header; |
| 46 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 47 | ar_headers_t *ar_list, *ar_tmp, *ar_entry; |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 48 | char ar_magic[8]; |
| 49 | char *long_name=NULL; |
| 50 | |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 51 | /* check ar magic */ |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 52 | if (fread(ar_magic, 1, 8, in_file) != 8) { |
| 53 | error_msg("cannot read magic"); |
| 54 | return(NULL); |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | if (strncmp(ar_magic,"!<arch>",7) != 0) { |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 58 | error_msg("invalid magic"); |
| 59 | return(NULL); |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 60 | } |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 61 | |
| 62 | ar_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t)); |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 63 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 64 | while (fread((char *) &raw_ar_header, 1, 60, in_file) == 60) { |
| 65 | ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t)); |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 66 | /* check the end of header markers are valid */ |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 67 | if ((raw_ar_header.fmag[0] != '`') || (raw_ar_header.fmag[1] != '\n')) { |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 68 | char newline; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 69 | if (raw_ar_header.fmag[1] != '`') { |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 70 | break; |
| 71 | } |
| 72 | /* some version of ar, have an extra '\n' after each entry */ |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 73 | fread(&newline, 1, 1, in_file); |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 74 | if (newline!='\n') { |
| 75 | break; |
| 76 | } |
| 77 | /* fix up the header, we started reading 1 byte too early due to a '\n' */ |
| 78 | memmove((char *) &raw_ar_header, (char *)&raw_ar_header+1, 59); |
| 79 | /* dont worry about adding the last '\n', we dont need it now */ |
| 80 | } |
| 81 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 82 | ar_entry->size = (size_t) atoi(raw_ar_header.size); |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 83 | /* long filenames have '/' as the first character */ |
| 84 | if (raw_ar_header.name[0] == '/') { |
| 85 | if (raw_ar_header.name[1] == '/') { |
| 86 | /* multiple long filenames are stored as data in one entry */ |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 87 | long_name = (char *) xrealloc(long_name, ar_entry->size); |
| 88 | fread(long_name, 1, ar_entry->size, in_file); |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 89 | continue; |
| 90 | } |
| 91 | else { |
| 92 | /* The number after the '/' indicates the offset in the ar data section |
| 93 | (saved in variable long_name) that conatains the real filename */ |
| 94 | const int long_name_offset = (int) atoi((char *) &raw_ar_header.name[1]); |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 95 | ar_entry->name = xmalloc(strlen(&long_name[long_name_offset])); |
| 96 | strcpy(ar_entry->name, &long_name[long_name_offset]); |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | else { |
| 100 | /* short filenames */ |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 101 | ar_entry->name = xmalloc(16); |
| 102 | ar_entry->name = strncpy(ar_entry->name, raw_ar_header.name, 16); |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 103 | } |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 104 | ar_entry->name[strcspn(ar_entry->name, " /")]='\0'; |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 105 | |
| 106 | /* convert the rest of the now valid char header to its typed struct */ |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 107 | parse_mode(raw_ar_header.mode, &ar_entry->mode); |
| 108 | ar_entry->mtime = atoi(raw_ar_header.date); |
| 109 | ar_entry->uid = atoi(raw_ar_header.uid); |
| 110 | ar_entry->gid = atoi(raw_ar_header.gid); |
| 111 | ar_entry->offset = ftell(in_file); |
| 112 | ar_entry->next = NULL; |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 113 | |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 114 | fseek(in_file, (off_t) ar_entry->size, SEEK_CUR); |
| 115 | |
| 116 | ar_tmp = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t)); |
| 117 | *ar_tmp = *ar_list; |
| 118 | *ar_list = *ar_entry; |
| 119 | free(ar_entry); |
| 120 | ar_list->next = ar_tmp; |
Glenn L McGrath | 9d53c8a | 2001-04-11 01:37:03 +0000 | [diff] [blame] | 121 | } |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame^] | 122 | |
| 123 | return(ar_list); |
| 124 | } |