2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "unarchive.h"
23 file_header_t *get_header_ar(FILE *src_stream)
38 static char *ar_long_names;
40 if (fread(ar.raw, 1, 60, src_stream) != 60) {
44 /* align the headers based on the header magic */
45 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
46 /* some version of ar, have an extra '\n' after each data entry,
47 * this puts the next header out by 1 */
48 if (ar.formated.magic[1] != '`') {
49 error_msg("Invalid magic");
52 /* read the next char out of what would be the data section,
53 * if its a '\n' then it is a valid header offset by 1*/
55 if (fgetc(src_stream) != '\n') {
56 error_msg("Invalid magic");
59 /* fix up the header, we started reading 1 byte too early */
60 /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
61 memmove(ar.raw, &ar.raw[1], 59);
64 typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
66 typed->size = (size_t) atoi(ar.formated.size);
67 /* long filenames have '/' as the first character */
68 if (ar.formated.name[0] == '/') {
69 if (ar.formated.name[1] == '/') {
70 /* If the second char is a '/' then this entries data section
71 * stores long filename for multiple entries, they are stored
72 * in static variable long_names for use in future entries */
73 ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
74 fread(ar_long_names, 1, typed->size, src_stream);
75 archive_offset += typed->size;
76 /* This ar entries data section only contained filenames for other records
77 * they are stored in the static ar_long_names for future reference */
78 return (get_header_ar(src_stream)); /* Return next header */
79 } else if (ar.formated.name[1] == ' ') {
80 /* This is the index of symbols in the file for compilers */
81 seek_sub_file(src_stream, typed->size);
82 return (get_header_ar(src_stream)); /* Return next header */
84 /* The number after the '/' indicates the offset in the ar data section
85 (saved in variable long_name) that conatains the real filename */
87 error_msg("Cannot resolve long file name");
90 typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
94 typed->name = xcalloc(1, 16);
95 strncpy(typed->name, ar.formated.name, 16);
97 typed->name[strcspn(typed->name, " /")]='\0';
99 /* convert the rest of the now valid char header to its typed struct */
100 parse_mode(ar.formated.mode, &typed->mode);
101 typed->mtime = atoi(ar.formated.date);
102 typed->uid = atoi(ar.formated.uid);
103 typed->gid = atoi(ar.formated.gid);