Copyright (C) 1988 Free Software Foundation
This file is part of GNU Tar.
GNU Tar 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 1, or (at your option)
GNU Tar 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 GNU Tar; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
* Also includes support routines for reading a tar archive.
* this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
* @(#)list.c 1.31 11/5/87 - gnu
#include <sys/sysmacros.h> /* major() and minor() defined here */
char *ctime(); /* From libc.a */
#define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
long from_oct(); /* Decode octal number */
void demode(); /* Print file mode */
union record
*head
; /* Points to current archive header */
struct stat hstat
; /* Stat struct corresponding */
int head_standard
; /* Tape header is in ANSI format */
void skip_extended_headers();
extern char *quote_copy_string();
* Main loop for reading an archive.
int status
= 3; /* Initial status at start of archive */
name_gather(); /* Gather all the names */
open_archive(1); /* Open for reading */
case 1: /* Valid header */
/* We should decode next field (mode) first... */
/* Ensure incoming names are null terminated. */
head
->header
.name
[NAMSIZ
-1] = '\0';
if ( !name_match(head
->header
.name
)
|| (f_new_files
&& hstat
.st_mtime
<new_time
)
|| (f_exclude
&& check_exclude(head
->header
.name
))) {
if( head
->header
.linkflag
==LF_VOLHDR
|| head
->header
.linkflag
==LF_MULTIVOL
|| head
->header
.linkflag
==LF_NAMES
) {
/* Skip past it in the archive */
if (head
->header
.isextended
)
save_linkflag
= head
->header
.linkflag
;
/* register union record *exhdr;
if (!exhdr->ext_hdr.isextended) {
/* Skip to the next header on the archive */
if(save_linkflag
!= LF_DIR
)
skip_file((long)hstat
.st_size
);
* If the previous header was good, tell them
* that we are skipping bad ones.
case 0: /* Invalid header */
case 3: /* Error on first record */
msg("Hmm, this doesn't look like a tar archive.");
case 2: /* Error after record of zeroes */
case 1: /* Error after header rec */
msg("Skipping to next file header...");
case 0: /* Error after error */
case 2: /* Record of zeroes */
status
= prev_status
; /* If error after 0's */
case EOF
: /* End of archive */
names_notfound(); /* Print names not found */
* Print a header record, based on tar options.
int isextended
= 0; /* Flag to remember if head is extended */
/* Print the header record */
decode_header(head
, &hstat
, &head_standard
, 0);
if(f_gnudump
&& head
->header
.linkflag
==LF_DUMPDIR
) {
size_t size
, written
, check
;
extern long save_totsize
;
extern long save_sizeleft
;
save_name
= head
->header
.name
;
save_totsize
=hstat
.st_size
;
for(size
= hstat
.st_size
;size
>0;size
-=written
) {
data
= findrec()->charptr
;
msg("EOF in archive file?");
written
= endofrecs()->charptr
- data
;
check
=fwrite(data
,sizeof(char), written
, msg_file
);
userec((union record
*)(data
+written
- 1));
msg_perror("only wrote %ld of %ld bytes to file %s",check
, written
,head
->header
.name
);
skip_file((long)(size
)-written
);
saverec((union record
**) 0); /* Unsave it */
saverec((union record
**) 0); /* Unsave it */
/* Check to see if we have an extended header to skip over also */
if (head
->header
.isextended
)
/* Skip past the header in the archive */
* If we needed to skip any extended headers, do so now, by
* reading extended headers and skipping past them in the
/* register union record *exhdr;
if (!exhdr->ext_hdr.isextended) {
save_name
=head
->header
.name
;
/* Skip to the next header on the archive */
skip_file((long) hstat
.st_size
);
* Read a record that's supposed to be a header record.
* Return its address in "head", and if it is good, the file's
* Return 1 for success, 0 if the checksum is bad, EOF on eof,
* 2 for a record full of zeros (EOF marker).
* You must always userec(head) to skip past the header which this
register long sum
, recsum
;
register union record
*header
;
head
= header
; /* This is our current header */
recsum
= from_oct(8, header
->header
.chksum
);
for (i
= sizeof(*header
); --i
>= 0;) {
* We can't use unsigned char here because of old compilers,
/* Adjust checksum to count the "chksum" field as blanks. */
for (i
= sizeof(header
->header
.chksum
); --i
>= 0;)
sum
-= 0xFF & header
->header
.chksum
[i
];
sum
+= ' '* sizeof header
->header
.chksum
;
* Good record. Decode file size and return.
if (header
->header
.linkflag
== LF_LINK
)
hstat
.st_size
= 0; /* Links 0 size on tape */
hstat
.st_size
= from_oct(1+12, header
->header
.size
);
* This is a zeroed record...whole record is 0's except
* for the 8 blanks we faked for the checksum field.
* Decode things from a file header record into a "struct stat".
* Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
* Standard" tar format or regular old tar format.
* read_header() has already decoded the checksum and length, so we don't.
* If wantug != 0, we want the uid/group info decoded from Unix Standard
* tapes (for extraction). If == 0, we are just printing anyway, so save time.
* decode_header should NOT be called twice for the same record, since the
* two calls might use different "wantug" values and thus might end up with
* different uid/gid for the two calls. If anybody wants the uid/gid they
* should decode it first, and other callers should decode it without uid/gid
* before calling a routine, e.g. print_header, that assumes decoded data.
decode_header(header
, st
, stdp
, wantug
)
register union record
*header
;
register struct stat
*st
;
st
->st_mode
= from_oct(8, header
->header
.mode
);
st
->st_mtime
= from_oct(1+12, header
->header
.mtime
);
st
->st_atime
= from_oct(1+12, header
->header
.atime
);
st
->st_ctime
= from_oct(1+12, header
->header
.ctime
);
if (0==strcmp(header
->header
.magic
, TMAGIC
)) {
/* Unix Standard tar archive */
st
->st_uid
= from_oct(8, header
->header
.uid
);
st
->st_gid
= from_oct(8, header
->header
.gid
);
st
->st_uid
= finduid(header
->header
.uname
);
st
->st_gid
= findgid(header
->header
.gname
);
switch (header
->header
.linkflag
) {
case LF_BLK
: case LF_CHR
:
st
->st_rdev
= makedev(from_oct(8, header
->header
.devmajor
),
from_oct(8, header
->header
.devminor
));
/* Old fashioned tar archive */
st
->st_uid
= from_oct(8, header
->header
.uid
);
st
->st_gid
= from_oct(8, header
->header
.gid
);
* Quick and dirty octal conversion.
* Result is -1 if the field is invalid (all blank, or nonoctal).
while (isspace(*where
)) { /* Skip spaces */
return -1; /* All blank field */
while (digs
> 0 && isodigit(*where
)) { /* Scan til nonoctal */
value
= (value
<< 3) | (*where
++ - '0');
if (digs
> 0 && *where
&& !isspace(*where
))
return -1; /* Ended on non-space/nul */
* Plain and fancy file header block logging.
* Non-verbose just prints the name, e.g. for "tar t" or "tar x".
* This should just contain file names, so it can be fed back into tar
* with xargs or the "-T" option. The verbose option can give a bunch
* of info, one line per file. I doubt anybody tries to parse its
* format, or if they do, they shouldn't. Unix tar is pretty random here
* Note that print_header uses the globals <head>, <hstat>, and
* <head_standard>, which must be set up in advance. This is not very clean
* and should be cleaned up. FIXME.
#define UGSWIDTH 11 /* min width of User, group, size */
#define DATEWIDTH 19 /* Last mod date */
static int ugswidth
= UGSWIDTH
; /* Max width encountered so far */
char uform
[11], gform
[11]; /* These hold formatted ints */
char size
[24]; /* Holds a formatted long or maj, min */
long longie
; /* To make ctime() call portable */
fprintf(msg_file
,"rec %10d: ",baserec
+ (ar_record
- ar_block
));
/* annofile(msg_file, (char *)NULL); */
name
=quote_copy_string(head
->header
.name
);
fprintf(msg_file
, "%s\n", name
);
if(name
!=head
->header
.name
)
/* File type and modes */
switch (head
->header
.linkflag
) {
if ('/' == head
->header
.name
[strlen(head
->header
.name
)-1])
case LF_DUMPDIR
:modes
[0] = 'd'; break;
case LF_DIR
: modes
[0] = 'd'; break;
case LF_SYMLINK
:modes
[0] = 'l'; break;
case LF_BLK
: modes
[0] = 'b'; break;
case LF_CHR
: modes
[0] = 'c'; break;
case LF_FIFO
: modes
[0] = 'p'; break;
case LF_CONTIG
: modes
[0] = 'C'; break;
demode((unsigned)hstat
.st_mode
, modes
+1);
timestamp
= ctime(&longie
);
/* User and group names */
if (*head
->header
.uname
&& head_standard
) {
user
= head
->header
.uname
;
(void)sprintf(uform
, "%d", (int)hstat
.st_uid
);
if (*head
->header
.gname
&& head_standard
) {
group
= head
->header
.gname
;
(void)sprintf(gform
, "%d", (int)hstat
.st_gid
);
/* Format the file size or major/minor device numbers */
switch (head
->header
.linkflag
) {
(void)sprintf(size
, "%d,%d",
(void)sprintf(size
, "%ld",
from_oct(1+12, head
->header
.realsize
));
(void)sprintf(size
, "%ld", (long)hstat
.st_size
);
/* Figure out padding and print the whole line. */
pad
= strlen(user
) + strlen(group
) + strlen(size
) + 1;
if (pad
> ugswidth
) ugswidth
= pad
;
name
= quote_copy_string(head
->header
.name
);
fprintf(msg_file
, "%s %s/%s %*s%s %s %s %.*s",
timestamp
+4, timestamp
+20,
sizeof(head
->header
.name
),
if(name
!=head
->header
.name
)
switch (head
->header
.linkflag
) {
name
=quote_copy_string(head
->header
.linkname
);
name
=head
->header
.linkname
;
fprintf(msg_file
, " -> %s\n", name
);
if(name
!=head
->header
.linkname
)
name
=quote_copy_string(head
->header
.linkname
);
name
=head
->header
.linkname
;
fprintf(msg_file
, " link to %s\n", head
->header
.linkname
);
if(name
!=head
->header
.linkname
)
fprintf(msg_file
, " unknown file type '%c'\n",
fprintf(msg_file
, "--Volume Header--\n");
fprintf(msg_file
, "--Continued at byte %ld--\n",from_oct(1+12,head
->header
.offset
));
fprintf(msg_file
,"--Mangled file names--\n");
* Print a similar line when we make a directory automatically.
pr_mkdir(pathname
, length
, mode
)
/* File type and modes */
demode((unsigned)mode
, modes
+1);
fprintf(msg_file
,"rec %10d: ",baserec
+ (ar_record
- ar_block
));
/* annofile(msg_file, (char *)NULL); */
name
=quote_copy_string(pathname
);
fprintf(msg_file
, "%s %*s %.*s\n",
* Skip over <size> bytes of data in records in the archive.
extern long save_totsize
;
extern long save_sizeleft
;
if (x
== NULL
) { /* Check it... */
msg("Unexpected EOF on archive file");
save_sizeleft
-=RECORDSIZE
;
register union record
*exhdr
;
if (!exhdr
->ext_hdr
.isextended
) {
* Decode the mode string from a stat entry into a 9-char string and a null.
register char *rwx
= "rwxrwxrwx";
for (mask
= 0400; mask
!= 0; mask
>>= 1) {