]>
Dogcows Code - chaz/tar/blob - src/list.c
2 Copyright (C) 1988, 1992 Free Software Foundation
4 This file is part of GNU Tar.
6 GNU Tar is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Tar is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Tar; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 * Also includes support routines for reading a tar archive.
25 * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
30 #include <sys/types.h>
45 #define isodigit(c) ( ((c) >= '0') && ((c) <= '7') )
50 extern FILE *msg_file
;
52 long from_oct(); /* Decode octal number */
53 void demode(); /* Print file mode */
55 union record
*head
; /* Points to current archive header */
56 struct stat hstat
; /* Stat struct corresponding */
57 int head_standard
; /* Tape header is in ANSI format */
66 void names_notfound();
72 void skip_extended_headers();
74 extern char *quote_copy_string();
78 * Main loop for reading an archive.
81 read_and(do_something
)
82 void (*do_something
)();
84 int status
= 3; /* Initial status at start of archive */
86 extern time_t new_time
;
89 name_gather(); /* Gather all the names */
90 open_archive(1); /* Open for reading */
94 status
= read_header();
97 case 1: /* Valid header */
98 /* We should decode next field (mode) first... */
99 /* Ensure incoming names are null terminated. */
101 if ( !name_match(current_file_name
)
102 || (f_new_files
&& hstat
.st_mtime
<new_time
)
103 || (f_exclude
&& check_exclude(current_file_name
))) {
107 if( head
->header
.linkflag
==LF_VOLHDR
108 || head
->header
.linkflag
==LF_MULTIVOL
109 || head
->header
.linkflag
==LF_NAMES
) {
113 if (f_show_omitted_dirs
114 && head
->header
.linkflag
== LF_DIR
)
115 msg ("Omitting %s\n", current_file_name
);
116 /* Skip past it in the archive */
117 if (head
->header
.isextended
)
119 save_linkflag
= head
->header
.linkflag
;
122 /* register union record *exhdr;
126 if (!exhdr->ext_hdr.isextended) {
132 skip_extended_headers();
134 /* Skip to the next header on the archive */
135 if(save_linkflag
!= LF_DIR
)
136 skip_file((long)hstat
.st_size
);
145 * If the previous header was good, tell them
146 * that we are skipping bad ones.
148 case 0: /* Invalid header */
150 switch (prev_status
) {
151 case 3: /* Error on first record */
152 msg("Hmm, this doesn't look like a tar archive.");
154 case 2: /* Error after record of zeroes */
155 case 1: /* Error after header rec */
156 msg("Skipping to next file header...");
157 case 0: /* Error after error */
162 case 2: /* Record of zeroes */
164 status
= prev_status
; /* If error after 0's */
168 case EOF
: /* End of archive */
174 restore_saved_dir_info ();
176 names_notfound(); /* Print names not found */
181 * Print a header record, based on tar options.
186 extern char *save_name
;
187 int isextended
= 0; /* Flag to remember if head is extended */
189 /* Save the record */
192 /* Print the header record */
195 decode_header(head
, &hstat
, &head_standard
, 0);
199 if(f_gnudump
&& head
->header
.linkflag
==LF_DUMPDIR
) {
200 size_t size
, written
, check
;
202 extern long save_totsize
;
203 extern long save_sizeleft
;
207 save_name
= current_file_name
;
208 save_totsize
=hstat
.st_size
;
210 for(size
= hstat
.st_size
;size
>0;size
-=written
) {
213 data
= findrec()->charptr
;
215 msg("EOF in archive file?");
218 written
= endofrecs()->charptr
- data
;
222 check
=fwrite(data
,sizeof(char), written
, msg_file
);
223 userec((union record
*)(data
+written
- 1));
225 msg_perror("only wrote %ld of %ld bytes to file %s",check
, written
,current_file_name
);
226 skip_file((long)(size
)-written
);
232 saverec((union record
**) 0); /* Unsave it */
233 fputc('\n',msg_file
);
238 saverec((union record
**) 0); /* Unsave it */
239 /* Check to see if we have an extended header to skip over also */
240 if (head
->header
.isextended
)
243 /* Skip past the header in the archive */
247 * If we needed to skip any extended headers, do so now, by
248 * reading extended headers and skipping past them in the
252 /* register union record *exhdr;
257 if (!exhdr->ext_hdr.isextended) {
263 skip_extended_headers();
267 save_name
=current_file_name
;
268 /* Skip to the next header on the archive */
270 skip_file((long) hstat
.st_size
);
278 * Read a record that's supposed to be a header record.
279 * Return its address in "head", and if it is good, the file's
280 * size in hstat.st_size.
282 * Return 1 for success, 0 if the checksum is bad, EOF on eof,
283 * 2 for a record full of zeros (EOF marker).
285 * You must always userec(head) to skip past the header which this
292 register long sum
, recsum
;
294 register union record
*header
;
299 static char *next_long_name
, *next_long_link
;
305 head
= header
; /* This is our current header */
309 recsum
= from_oct(8, header
->header
.chksum
);
313 for (i
= sizeof(*header
); --i
>= 0;) {
315 * We can't use unsigned char here because of old compilers,
321 /* Adjust checksum to count the "chksum" field as blanks. */
322 for (i
= sizeof(header
->header
.chksum
); --i
>= 0;)
323 sum
-= 0xFF & header
->header
.chksum
[i
];
324 sum
+= ' '* sizeof header
->header
.chksum
;
328 * This is a zeroed record...whole record is 0's except
329 * for the 8 blanks we faked for the checksum field.
338 * Good record. Decode file size and return.
340 if (header
->header
.linkflag
== LF_LINK
)
341 hstat
.st_size
= 0; /* Links 0 size on tape */
343 hstat
.st_size
= from_oct(1+12, header
->header
.size
);
345 header
->header
.arch_name
[NAMSIZ
-1] = '\0';
346 if (header
->header
.linkflag
== LF_LONGNAME
347 || header
->header
.linkflag
== LF_LONGLINK
)
349 longp
= ((header
->header
.linkflag
== LF_LONGNAME
)
356 bp
= *longp
= (char *) ck_malloc (hstat
.st_size
);
358 for (size
= hstat
.st_size
;
362 data
= findrec ()->charptr
;
365 msg ("Unexpected EOF on archive file");
368 written
= endofrecs () ->charptr
- data
;
372 bcopy (data
, bp
, written
);
374 userec ((union record
*) (data
+ written
- 1));
380 name
= (next_long_name
382 : head
->header
.arch_name
);
383 if (current_file_name
)
384 free (current_file_name
);
385 current_file_name
= malloc (strlen (name
) + 1);
386 strcpy (current_file_name
, name
);
388 name
= (next_long_link
390 : head
->header
.arch_linkname
);
391 if (current_link_name
)
392 free (current_link_name
);
393 current_link_name
= malloc (strlen (name
) + 1);
394 strcpy (current_link_name
, name
);
396 next_long_link
= next_long_name
= 0;
403 * Decode things from a file header record into a "struct stat".
404 * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
405 * Standard" tar format or regular old tar format.
407 * read_header() has already decoded the checksum and length, so we don't.
409 * If wantug != 0, we want the uid/group info decoded from Unix Standard
410 * tapes (for extraction). If == 0, we are just printing anyway, so save time.
412 * decode_header should NOT be called twice for the same record, since the
413 * two calls might use different "wantug" values and thus might end up with
414 * different uid/gid for the two calls. If anybody wants the uid/gid they
415 * should decode it first, and other callers should decode it without uid/gid
416 * before calling a routine, e.g. print_header, that assumes decoded data.
419 decode_header(header
, st
, stdp
, wantug
)
420 register union record
*header
;
421 register struct stat
*st
;
430 st
->st_mode
= from_oct(8, header
->header
.mode
);
431 st
->st_mtime
= from_oct(1+12, header
->header
.mtime
);
433 st
->st_atime
= from_oct(1+12, header
->header
.atime
);
434 st
->st_ctime
= from_oct(1+12, header
->header
.ctime
);
437 if (0==strcmp(header
->header
.magic
, TMAGIC
)) {
438 /* Unix Standard tar archive */
442 st
->st_uid
= from_oct(8, header
->header
.uid
);
443 st
->st_gid
= from_oct(8, header
->header
.gid
);
446 (*header
->header
.uname
447 ? finduid (header
->header
.uname
)
448 : from_oct (8, header
->header
.uid
));
450 (*header
->header
.gname
451 ? findgid (header
->header
.gname
)
452 : from_oct (8, header
->header
.gid
));
455 #if defined(S_IFBLK) || defined(S_IFCHR)
456 switch (header
->header
.linkflag
) {
457 case LF_BLK
: case LF_CHR
:
458 st
->st_rdev
= makedev(from_oct(8, header
->header
.devmajor
),
459 from_oct(8, header
->header
.devminor
));
463 /* Old fashioned tar archive */
465 st
->st_uid
= from_oct(8, header
->header
.uid
);
466 st
->st_gid
= from_oct(8, header
->header
.gid
);
473 * Quick and dirty octal conversion.
475 * Result is -1 if the field is invalid (all blank, or nonoctal).
478 from_oct(digs
, where
)
480 register char *where
;
484 while (isspace(*where
)) { /* Skip spaces */
487 return -1; /* All blank field */
490 while (digs
> 0 && isodigit(*where
)) { /* Scan til nonoctal */
491 value
= (value
<< 3) | (*where
++ - '0');
495 if (digs
> 0 && *where
&& !isspace(*where
))
496 return -1; /* Ended on non-space/nul */
505 * Plain and fancy file header block logging.
506 * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
507 * This should just contain file names, so it can be fed back into tar
508 * with xargs or the "-T" option. The verbose option can give a bunch
509 * of info, one line per file. I doubt anybody tries to parse its
510 * format, or if they do, they shouldn't. Unix tar is pretty random here
513 * Note that print_header uses the globals <head>, <hstat>, and
514 * <head_standard>, which must be set up in advance. This is not very clean
515 * and should be cleaned up. FIXME.
517 #define UGSWIDTH 18 /* min width of User, group, size */
518 /* UGSWIDTH of 18 means that with user and group names <= 8 chars the columns
519 never shift during the listing. */
520 #define DATEWIDTH 19 /* Last mod date */
521 static int ugswidth
= UGSWIDTH
; /* Max width encountered so far */
528 char uform
[11], gform
[11]; /* These hold formatted ints */
530 char size
[24]; /* Holds a formatted long or maj, min */
531 time_t longie
; /* To make ctime() call portable */
535 static char *longname
;
536 static char *longlink
;
540 fprintf(msg_file
,"rec %10d: ",baserec
+ (ar_record
- ar_block
));
541 /* annofile(msg_file, (char *)NULL); */
543 if (f_verbose
<= 1) {
544 /* Just the fax, mam. */
547 name
=quote_copy_string(current_file_name
);
549 name
=current_file_name
;
550 fprintf(msg_file
, "%s\n", name
);
551 if(name
!=current_file_name
)
554 /* File type and modes */
556 switch (head
->header
.linkflag
) {
571 msg ("Visible longname error\n");
579 if ('/' == current_file_name
[strlen(current_file_name
)-1])
582 case LF_DUMPDIR
:modes
[0] = 'd'; break;
583 case LF_DIR
: modes
[0] = 'd'; break;
584 case LF_SYMLINK
:modes
[0] = 'l'; break;
585 case LF_BLK
: modes
[0] = 'b'; break;
586 case LF_CHR
: modes
[0] = 'c'; break;
587 case LF_FIFO
: modes
[0] = 'p'; break;
588 case LF_CONTIG
: modes
[0] = 'C'; break;
591 demode((unsigned)hstat
.st_mode
, modes
+1);
594 longie
= hstat
.st_mtime
;
595 timestamp
= ctime(&longie
);
596 timestamp
[16] = '\0';
597 timestamp
[24] = '\0';
599 /* User and group names */
600 if (*head
->header
.uname
&& head_standard
) {
601 user
= head
->header
.uname
;
604 (void)sprintf(uform
, "%d",
605 from_oct (8, head
->header
.uid
));
607 if (*head
->header
.gname
&& head_standard
) {
608 group
= head
->header
.gname
;
611 (void)sprintf(gform
, "%d",
612 from_oct (8, head
->header
.gid
));
615 /* Format the file size or major/minor device numbers */
616 switch (head
->header
.linkflag
) {
617 #if defined(S_IFBLK) || defined(S_IFCHR)
620 (void)sprintf(size
, "%d,%d",
621 major(hstat
.st_rdev
),
622 minor(hstat
.st_rdev
));
626 (void)sprintf(size
, "%ld",
627 from_oct(1+12, head
->header
.realsize
));
630 (void)sprintf(size
, "%ld", (long)hstat
.st_size
);
633 /* Figure out padding and print the whole line. */
634 pad
= strlen(user
) + strlen(group
) + strlen(size
) + 1;
635 if (pad
> ugswidth
) ugswidth
= pad
;
637 name
= quote_copy_string(current_file_name
);
639 name
=current_file_name
;
640 fprintf(msg_file
, "%s %s/%s %*s%s %s %s %s",
647 timestamp
+4, timestamp
+20,
650 if(name
!=current_file_name
)
652 switch (head
->header
.linkflag
) {
654 name
=quote_copy_string(current_link_name
);
656 name
=current_link_name
;
657 fprintf(msg_file
, " -> %s\n", name
);
658 if(name
!=current_link_name
)
663 name
=quote_copy_string(current_link_name
);
665 name
=current_link_name
;
666 fprintf(msg_file
, " link to %s\n", current_link_name
);
667 if(name
!=current_link_name
)
672 fprintf(msg_file
, " unknown file type '%c'\n",
673 head
->header
.linkflag
);
685 putc('\n', msg_file
);
689 fprintf(msg_file
, "--Volume Header--\n");
693 fprintf(msg_file
, "--Continued at byte %ld--\n",from_oct(1+12,head
->header
.offset
));
697 fprintf(msg_file
,"--Mangled file names--\n");
705 * Print a similar line when we make a directory automatically.
708 pr_mkdir(pathname
, length
, mode
)
718 /* File type and modes */
720 demode((unsigned)mode
, modes
+1);
723 fprintf(msg_file
,"rec %10d: ",baserec
+ (ar_record
- ar_block
));
724 /* annofile(msg_file, (char *)NULL); */
725 name
=quote_copy_string(pathname
);
728 fprintf(msg_file
, "%s %*s %.*s\n",
731 "Creating directory:",
741 * Skip over <size> bytes of data in records in the archive.
748 extern long save_totsize
;
749 extern long save_sizeleft
;
758 if (x
== NULL
) { /* Check it... */
759 msg("Unexpected EOF on archive file");
765 save_sizeleft
-=RECORDSIZE
;
770 skip_extended_headers()
772 register union record
*exhdr
;
776 if (!exhdr
->ext_hdr
.isextended
) {
785 * Decode the mode string from a stat entry into a 9-char string and a null.
789 register unsigned mode
;
790 register char *string
;
792 register unsigned mask
;
793 register char *rwx
= "rwxrwxrwx";
795 for (mask
= 0400; mask
!= 0; mask
>>= 1) {
805 if (string
[-7] == 'x')
810 if (string
[-4] == 'x')
815 if (string
[-1] == 'x')
This page took 0.071218 seconds and 5 git commands to generate.