- /*fprintf(stderr, __FUNCTION__ " goin home\n");*/
-
- return(buffer);
-}
-#endif
-
-#ifdef L_get_header_ar
-file_header_t *get_header_ar(FILE *src_stream)
-{
- file_header_t *typed;
- union {
- char raw[60];
- struct {
- char name[16];
- char date[12];
- char uid[6];
- char gid[6];
- char mode[8];
- char size[10];
- char magic[2];
- } formated;
- } ar;
- static char *ar_long_names;
-
- if (fread(ar.raw, 1, 60, src_stream) != 60) {
- return(NULL);
- }
- archive_offset += 60;
- /* align the headers based on the header magic */
- if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
- /* some version of ar, have an extra '\n' after each data entry,
- * this puts the next header out by 1 */
- if (ar.formated.magic[1] != '`') {
- error_msg("Invalid magic");
- return(NULL);
- }
- /* read the next char out of what would be the data section,
- * if its a '\n' then it is a valid header offset by 1*/
- archive_offset++;
- if (fgetc(src_stream) != '\n') {
- error_msg("Invalid magic");
- return(NULL);
- }
- /* fix up the header, we started reading 1 byte too early */
- /* raw_header[60] wont be '\n' as it should, but it doesnt matter */
- memmove(ar.raw, &ar.raw[1], 59);
- }
-
- typed = (file_header_t *) xcalloc(1, sizeof(file_header_t));
-
- typed->size = (size_t) atoi(ar.formated.size);
- /* long filenames have '/' as the first character */
- if (ar.formated.name[0] == '/') {
- if (ar.formated.name[1] == '/') {
- /* If the second char is a '/' then this entries data section
- * stores long filename for multiple entries, they are stored
- * in static variable long_names for use in future entries */
- ar_long_names = (char *) xrealloc(ar_long_names, typed->size);
- fread(ar_long_names, 1, typed->size, src_stream);
- archive_offset += typed->size;
- /* This ar entries data section only contained filenames for other records
- * they are stored in the static ar_long_names for future reference */
- return (get_header_ar(src_stream)); /* Return next header */
- } else if (ar.formated.name[1] == ' ') {
- /* This is the index of symbols in the file for compilers */
- seek_sub_file(src_stream, typed->size);
- return (get_header_ar(src_stream)); /* Return next header */
- } else {
- /* The number after the '/' indicates the offset in the ar data section
- (saved in variable long_name) that conatains the real filename */
- if (!ar_long_names) {
- error_msg("Cannot resolve long file name");
- return (NULL);
- }
- typed->name = xstrdup(ar_long_names + atoi(&ar.formated.name[1]));
- }
- } else {
- /* short filenames */
- typed->name = xcalloc(1, 16);
- strncpy(typed->name, ar.formated.name, 16);
- }
- typed->name[strcspn(typed->name, " /")]='\0';
-
- /* convert the rest of the now valid char header to its typed struct */
- parse_mode(ar.formated.mode, &typed->mode);
- typed->mtime = atoi(ar.formated.date);
- typed->uid = atoi(ar.formated.uid);
- typed->gid = atoi(ar.formated.gid);
-
- return(typed);
-}
-
-void free_header_ar(file_header_t *ar_entry)
-{
- if (ar_entry == NULL) {
- return;
- }
-
- free(ar_entry->name);
- free(ar_entry->link_name);
-
- free(ar_entry);
-}
-#endif
-
-#ifdef L_get_header_cpio
-struct hardlinks {
- file_header_t *entry;
- int inode;
- struct hardlinks *next;
-};
-
-file_header_t *get_header_cpio(FILE *src_stream)
-{
- file_header_t *cpio_entry = NULL;
- char cpio_header[110];
- int namesize;
- char dummy[16];
- int major, minor, nlink, inode;
- static struct hardlinks *saved_hardlinks = NULL;
- static int pending_hardlinks = 0;
-
-
- if (pending_hardlinks) { /* Deal with any pending hardlinks */
- struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
- while (tmp) {
- if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
- cpio_entry = tmp->entry;
- if (oldtmp) oldtmp->next = tmp->next; /* Remove item from linked list */
- else saved_hardlinks = tmp->next;
- free(tmp);
- return (cpio_entry);
- }
- oldtmp = tmp;
- tmp = tmp->next;
- }
- pending_hardlinks = 0; /* No more pending hardlinks, read next file entry */
- }
-
- /* There can be padding before archive header */
- seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
- if (fread(cpio_header, 1, 110, src_stream) == 110) {
- archive_offset += 110;
- if (strncmp(cpio_header, "07070", 5) != 0) {
- error_msg("Unsupported format or invalid magic");
- return(NULL);
- }
- switch (cpio_header[5]) {
- case '2': /* "crc" header format */
- /* Doesnt do the crc check yet */
- case '1': /* "newc" header format */
- cpio_entry = (file_header_t *) xcalloc(1, sizeof(file_header_t));
- sscanf(cpio_header, "%6c%8x%8x%8x%8x%8x%8lx%8lx%16c%8x%8x%8x%8c",
- dummy, &inode, (unsigned int*)&cpio_entry->mode,
- (unsigned int*)&cpio_entry->uid, (unsigned int*)&cpio_entry->gid,
- &nlink, &cpio_entry->mtime, (unsigned long*)&cpio_entry->size,
- dummy, &major, &minor, &namesize, dummy);
-
- cpio_entry->name = (char *) xcalloc(1, namesize);
- fread(cpio_entry->name, 1, namesize, src_stream); /* Read in filename */
- archive_offset += namesize;
- /* Skip padding before file contents */
- seek_sub_file(src_stream, (4 - (archive_offset % 4)) % 4);
- if (strcmp(cpio_entry->name, "TRAILER!!!") == 0) {
- printf("%d blocks\n", (int) (archive_offset % 512 ? (archive_offset / 512) + 1 : archive_offset / 512)); /* Always round up */
- if (saved_hardlinks) { /* Bummer - we still have unresolved hardlinks */
- struct hardlinks *tmp = saved_hardlinks, *oldtmp = NULL;
- while (tmp) {
- error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
- oldtmp = tmp;
- tmp = tmp->next;
- free (oldtmp->entry->name);
- free (oldtmp->entry);
- free (oldtmp);
- }
- saved_hardlinks = NULL;
- pending_hardlinks = 0;
- }
- return(NULL);
- }