home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 7
/
FreshFishVol7.bin
/
bbs
/
gnu
/
cpio-2.3-src.lha
/
GNU
/
src
/
amiga
/
cpio-2.3
/
copyin.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-05-04
|
37KB
|
1,273 lines
/* copyin.c - extract or list a cpio archive
Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "filetypes.h"
#include "system.h"
#include "cpiohdr.h"
#include "dstring.h"
#include "extern.h"
#include "defer.h"
#include "rmt.h"
#ifndef FNM_PATHNAME
#include <fnmatch.h>
#endif
#ifndef HAVE_LCHOWN
#define lchown chown
#endif
static void read_pattern_file ();
static void skip_padding ();
static void defer_copyin ();
static void create_defered_links ();
static void create_final_defers ();
/* Return 16-bit integer I with the bytes swapped. */
#define swab_short(i) ((((i) << 8) & 0xff00) | (((i) >> 8) & 0x00ff))
/* Read the header, including the name of the file, from file
descriptor IN_DES into FILE_HDR. */
void
read_in_header (file_hdr, in_des)
struct new_cpio_header *file_hdr;
int in_des;
{
long bytes_skipped = 0; /* Bytes of junk found before magic number. */
/* Search for a valid magic number. */
if (archive_format == arf_unknown)
{
char tmpbuf[512];
int check_tar;
int peeked_bytes;
while (archive_format == arf_unknown)
{
peeked_bytes = peek_in_buf (tmpbuf, in_des, 512);
if (peeked_bytes < 6)
error (1, 0, "premature end of archive");
if (!strncmp (tmpbuf, "070701", 6))
archive_format = arf_newascii;
else if (!strncmp (tmpbuf, "070707", 6))
archive_format = arf_oldascii;
else if (!strncmp (tmpbuf, "070702", 6))
{
archive_format = arf_crcascii;
crc_i_flag = TRUE;
}
else if ((*((unsigned short *) tmpbuf) == 070707) ||
(*((unsigned short *) tmpbuf) == swab_short ((unsigned short) 070707)))
archive_format = arf_binary;
else if (peeked_bytes >= 512
&& (check_tar = is_tar_header (tmpbuf)))
{
if (check_tar == 2)
archive_format = arf_ustar;
else
archive_format = arf_tar;
}
else
{
copy_in_buf ((char *) tmpbuf, in_des, 1L);
++bytes_skipped;
}
}
}
if (archive_format == arf_tar || archive_format == arf_ustar)
{
if (append_flag)
last_header_start = input_bytes - io_block_size +
(in_buff - input_buffer);
if (bytes_skipped > 0)
error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
read_in_tar_header (file_hdr, in_des);
return;
}
file_hdr->c_tar_linkname = NULL;
copy_in_buf ((char *) file_hdr, in_des, 6L);
while (1)
{
if (append_flag)
last_header_start = input_bytes - io_block_size
+ (in_buff - input_buffer) - 6;
if (archive_format == arf_newascii
&& !strncmp ((char *) file_hdr, "070701", 6))
{
if (bytes_skipped > 0)
error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
read_in_new_ascii (file_hdr, in_des);
break;
}
if (archive_format == arf_crcascii
&& !strncmp ((char *) file_hdr, "070702", 6))
{
if (bytes_skipped > 0)
error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
read_in_new_ascii (file_hdr, in_des);
break;
}
if ( (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
&& !strncmp ((char *) file_hdr, "070707", 6))
{
if (bytes_skipped > 0)
error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
read_in_old_ascii (file_hdr, in_des);
break;
}
if ( (archive_format == arf_binary || archive_format == arf_hpbinary)
&& (file_hdr->c_magic == 070707
|| file_hdr->c_magic == swab_short ((unsigned short) 070707)))
{
/* Having to skip 1 byte because of word alignment is normal. */
if (bytes_skipped > 0)
error (0, 0, "warning: skipped %ld bytes of junk", bytes_skipped);
read_in_binary (file_hdr, in_des);
break;
}
bytes_skipped++;
bcopy ((char *) file_hdr + 1, (char *) file_hdr, 5);
copy_in_buf ((char *) file_hdr + 5, in_des, 1L);
}
}
/* Fill in FILE_HDR by reading an old-format ASCII format cpio header from
file descriptor IN_DES, except for the magic number, which is
already filled in. */
void
read_in_old_ascii (file_hdr, in_des)
struct new_cpio_header *file_hdr;
int in_des;
{
char ascii_header[78];
unsigned long dev;
unsigned long rdev;
copy_in_buf (ascii_header, in_des, 70L);
ascii_header[70] = '\0';
sscanf (ascii_header,
"%6lo%6lo%6lo%6lo%6lo%6lo%6lo%11lo%6lo%11lo",
&dev, &file_hdr->c_ino,
&file_hdr->c_mode, &file_hdr->c_uid, &file_hdr->c_gid,
&file_hdr->c_nlink, &rdev, &file_hdr->c_mtime,
&file_hdr->c_namesize, &file_hdr->c_filesize);
file_hdr->c_dev_maj = major (dev);
file_hdr->c_dev_min = minor (dev);
file_hdr->c_rdev_maj = major (rdev);
file_hdr->c_rdev_min = minor (rdev);
/* Read file name from input. */
if (file_hdr->c_name != NULL)
free (file_hdr->c_name);
file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize + 1);
copy_in_buf (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
#ifndef __MSDOS__
/* HP/UX cpio creates archives that look just like ordinary archives,
but for devices it sets major = 0, minor = 1, and puts the
actual major/minor number in the filesize field. See if this
is an HP/UX cpio archive, and if so fix it. We have to do this
here because process_copy_in() assumes filesize is always 0
for devices. */
switch (file_hdr->c_mode & CP_IFMT)
{
case CP_IFCHR:
case CP_IFBLK:
#ifdef CP_IFSOCK
case CP_IFSOCK:
#endif
#ifdef CP_IFIFO
case CP_IFIFO:
#endif
if (file_hdr->c_filesize != 0
&& file_hdr->c_rdev_maj == 0
&& file_hdr->c_rdev_min == 1)
{
file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
file_hdr->c_filesize = 0;
}
break;
default:
break;
}
#endif /* __MSDOS__ */
}
/* Fill in FILE_HDR by reading a new-format ASCII format cpio header from
file descriptor IN_DES, except for the magic number, which is
already filled in. */
void
read_in_new_ascii (file_hdr, in_des)
struct new_cpio_header *file_hdr;
int in_des;
{
char ascii_header[112];
copy_in_buf (ascii_header, in_des, 104L);
ascii_header[104] = '\0';
sscanf (ascii_header,
"%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx%8lx",
&file_hdr->c_ino, &file_hdr->c_mode, &file_hdr->c_uid,
&file_hdr->c_gid, &file_hdr->c_nlink, &file_hdr->c_mtime,
&file_hdr->c_filesize, &file_hdr->c_dev_maj, &file_hdr->c_dev_min,
&file_hdr->c_rdev_maj, &file_hdr->c_rdev_min, &file_hdr->c_namesize,
&file_hdr->c_chksum);
/* Read file name from input. */
if (file_hdr->c_name != NULL)
free (file_hdr->c_name);
file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
copy_in_buf (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
/* In SVR4 ASCII format, the amount of space allocated for the header
is rounded up to the next long-word, so we might need to drop
1-3 bytes. */
skip_padding (in_des, file_hdr->c_namesize + 110);
}
/* Fill in FILE_HDR by reading a binary format cpio header from
file descriptor IN_DES, except for the first 6 bytes (the magic
number, device, and inode number), which are already filled in. */
void
read_in_binary (file_hdr, in_des)
struct new_cpio_header *file_hdr;
int in_des;
{
struct old_cpio_header short_hdr;
/* Copy the data into the short header, then later transfer
it into the argument long header. */
short_hdr.c_dev = ((struct old_cpio_header *)