blob: 151caafe1e88020f6fe4284ecd7f545e030b3021 [file] [log] [blame]
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +00001/* 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 McGrath4949faf2001-04-11 16:23:35 +000033extern ar_headers_t *get_ar_headers(FILE *in_file)
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000034{
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 McGrath4949faf2001-04-11 16:23:35 +000047 ar_headers_t *ar_list, *ar_tmp, *ar_entry;
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000048 char ar_magic[8];
49 char *long_name=NULL;
50
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000051 /* check ar magic */
Glenn L McGrath4949faf2001-04-11 16:23:35 +000052 if (fread(ar_magic, 1, 8, in_file) != 8) {
53 error_msg("cannot read magic");
54 return(NULL);
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000055 }
56
57 if (strncmp(ar_magic,"!<arch>",7) != 0) {
Glenn L McGrath4949faf2001-04-11 16:23:35 +000058 error_msg("invalid magic");
59 return(NULL);
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000060 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +000061
62 ar_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000063
Glenn L McGrath4949faf2001-04-11 16:23:35 +000064 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 McGrath9d53c8a2001-04-11 01:37:03 +000066 /* check the end of header markers are valid */
Glenn L McGrath4949faf2001-04-11 16:23:35 +000067 if ((raw_ar_header.fmag[0] != '`') || (raw_ar_header.fmag[1] != '\n')) {
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000068 char newline;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000069 if (raw_ar_header.fmag[1] != '`') {
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000070 break;
71 }
72 /* some version of ar, have an extra '\n' after each entry */
Glenn L McGrath4949faf2001-04-11 16:23:35 +000073 fread(&newline, 1, 1, in_file);
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000074 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 McGrath4949faf2001-04-11 16:23:35 +000082 ar_entry->size = (size_t) atoi(raw_ar_header.size);
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000083 /* 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 McGrath4949faf2001-04-11 16:23:35 +000087 long_name = (char *) xrealloc(long_name, ar_entry->size);
88 fread(long_name, 1, ar_entry->size, in_file);
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000089 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 McGrath4949faf2001-04-11 16:23:35 +000095 ar_entry->name = xmalloc(strlen(&long_name[long_name_offset]));
96 strcpy(ar_entry->name, &long_name[long_name_offset]);
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +000097 }
98 }
99 else {
100 /* short filenames */
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000101 ar_entry->name = xmalloc(16);
102 ar_entry->name = strncpy(ar_entry->name, raw_ar_header.name, 16);
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +0000103 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000104 ar_entry->name[strcspn(ar_entry->name, " /")]='\0';
Glenn L McGrath9d53c8a2001-04-11 01:37:03 +0000105
106 /* convert the rest of the now valid char header to its typed struct */
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000107 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 McGrath9d53c8a2001-04-11 01:37:03 +0000113
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000114 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 McGrath9d53c8a2001-04-11 01:37:03 +0000121 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000122
123 return(ar_list);
124}