1 /* List a tar archive, with support routines for reading a tar archive.
3 Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001, 2003 Free Software Foundation, Inc.
6 Written by John Gilmore, on 1985-08-26.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any later
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* Define to non-zero for forcing old ctime format instead of ISO format. */
30 #define max(a, b) ((a) < (b) ? (b) : (a))
32 union block *current_header; /* points to current archive header */
33 enum archive_format current_format; /* recognized format */
34 union block *recent_long_name; /* recent long name header and contents */
35 union block *recent_long_link; /* likewise, for long link */
36 size_t recent_long_name_blocks; /* number of blocks in recent_long_name */
37 size_t recent_long_link_blocks; /* likewise, for long link */
39 static uintmax_t from_header (const char *, size_t, const char *,
40 uintmax_t, uintmax_t);
42 /* Base 64 digits; see Internet RFC 2045 Table 1. */
43 static char const base_64_digits[64] =
45 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
46 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
47 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
48 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
49 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
52 /* Table of base-64 digit values indexed by unsigned chars.
53 The value is 64 for unsigned chars that are not base-64 digits. */
54 static char base64_map[UCHAR_MAX + 1];
60 memset (base64_map, 64, sizeof base64_map);
61 for (i = 0; i < 64; i++)
62 base64_map[(int) base_64_digits[i]] = i;
65 /* Main loop for reading an archive. */
67 read_and (void (*do_something) (void))
69 enum read_header status = HEADER_STILL_UNREAD;
70 enum read_header prev_status;
74 open_archive (ACCESS_READ);
79 status = read_header (false);
82 case HEADER_STILL_UNREAD:
83 case HEADER_SUCCESS_EXTENDED:
88 /* Valid header. We should decode next field (mode) first.
89 Ensure incoming names are null terminated. */
91 if (! name_match (current_stat_info.file_name)
92 || (newer_mtime_option != TYPE_MINIMUM (time_t)
93 /* FIXME: We get mtime now, and again later; this causes
94 duplicate diagnostics if header.mtime is bogus. */
95 && ((current_stat_info.stat.st_mtime
96 = TIME_FROM_HEADER (current_header->header.mtime))
97 < newer_mtime_option))
98 || excluded_name (current_stat_info.file_name))
100 switch (current_header->header.typeflag)
103 case GNUTYPE_MULTIVOL:
108 if (show_omitted_dirs_option)
109 WARN ((0, 0, _("%s: Omitting"),
110 quotearg_colon (current_stat_info.file_name)));
121 case HEADER_ZERO_BLOCK:
122 if (block_number_option)
124 char buf[UINTMAX_STRSIZE_BOUND];
125 fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
126 STRINGIFY_BIGINT (current_block_ordinal (), buf));
129 set_next_block_after (current_header);
130 status = prev_status;
131 if (ignore_zeros_option)
135 case HEADER_END_OF_FILE:
136 if (block_number_option)
138 char buf[UINTMAX_STRSIZE_BOUND];
139 fprintf (stdlis, _("block %s: ** End of File **\n"),
140 STRINGIFY_BIGINT (current_block_ordinal (), buf));
145 /* If the previous header was good, tell them that we are
146 skipping bad ones. */
147 set_next_block_after (current_header);
150 case HEADER_STILL_UNREAD:
151 ERROR ((0, 0, _("This does not look like a tar archive")));
154 case HEADER_ZERO_BLOCK:
156 ERROR ((0, 0, _("Skipping to next header")));
159 case HEADER_END_OF_FILE:
161 /* We are in the middle of a cascade of errors. */
164 case HEADER_SUCCESS_EXTENDED:
173 names_notfound (); /* print names not found */
176 /* Print a header block, based on tar options. */
180 /* Print the header block. */
182 decode_header (current_header, ¤t_stat_info, ¤t_format, 0);
186 if (incremental_option && current_header->header.typeflag == GNUTYPE_DUMPDIR)
189 size_t written, check;
190 union block *data_block;
192 set_next_block_after (current_header);
193 if (multi_volume_option)
195 assign_string (&save_name, current_stat_info.file_name);
196 save_totsize = current_stat_info.stat.st_size;
198 for (size = current_stat_info.stat.st_size; size > 0; size -= written)
200 if (multi_volume_option)
201 save_sizeleft = size;
202 data_block = find_next_block ();
205 ERROR ((0, 0, _("Unexpected EOF in archive")));
206 break; /* FIXME: What happens, then? */
208 written = available_space_after (data_block);
212 check = fwrite (data_block->buffer, sizeof (char), written, stdlis);
213 set_next_block_after ((union block *)
214 (data_block->buffer + written - 1));
215 if (check != written)
217 write_error_details (current_stat_info.file_name, check, written);
218 skip_file (size - written);
222 if (multi_volume_option)
223 assign_string (&save_name, 0);
224 fputc ('\n', stdlis);
230 if (multi_volume_option)
231 assign_string (&save_name, current_stat_info.file_name);
235 if (multi_volume_option)
236 assign_string (&save_name, 0);
239 /* Read a block that's supposed to be a header block. Return its
240 address in "current_header", and if it is good, the file's size in
241 current_stat_info.stat.st_size.
243 Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a
244 block full of zeros (EOF marker).
246 If RAW_EXTENDED_HEADERS is nonzero, do not automagically fold the
247 GNU long name and link headers into later headers.
249 You must always set_next_block_after(current_header) to skip past
250 the header which this routine reads. */
252 /* The standard BSD tar sources create the checksum by adding up the
253 bytes in the header as type char. I think the type char was unsigned
254 on the PDP-11, but it's signed on the Next and Sun. It looks like the
255 sources to BSD tar were never changed to compute the checksum
256 correctly, so both the Sun and Next add the bytes of the header as
257 signed chars. This doesn't cause a problem until you get a file with
258 a name containing characters with the high bit set. So read_header
259 computes two checksums -- signed and unsigned. */
262 read_header (bool raw_extended_headers)
265 int unsigned_sum; /* the POSIX one :-) */
266 int signed_sum; /* the Sun one :-( */
268 uintmax_t parsed_sum;
271 union block *header_copy;
273 union block *data_block;
274 size_t size, written;
275 union block *next_long_name = 0;
276 union block *next_long_link = 0;
277 size_t next_long_name_blocks;
278 size_t next_long_link_blocks;
282 header = find_next_block ();
283 current_header = header;
285 return HEADER_END_OF_FILE;
290 for (i = sizeof *header; i-- != 0;)
292 unsigned_sum += (unsigned char) *p;
293 signed_sum += (signed char) (*p++);
296 if (unsigned_sum == 0)
297 return HEADER_ZERO_BLOCK;
299 /* Adjust checksum to count the "chksum" field as blanks. */
301 for (i = sizeof header->header.chksum; i-- != 0;)
303 unsigned_sum -= (unsigned char) header->header.chksum[i];
304 signed_sum -= (signed char) (header->header.chksum[i]);
306 unsigned_sum += ' ' * sizeof header->header.chksum;
307 signed_sum += ' ' * sizeof header->header.chksum;
309 parsed_sum = from_header (header->header.chksum,
310 sizeof header->header.chksum, 0,
312 (uintmax_t) TYPE_MAXIMUM (int));
313 if (parsed_sum == (uintmax_t) -1)
314 return HEADER_FAILURE;
316 recorded_sum = parsed_sum;
318 if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
319 return HEADER_FAILURE;
321 /* Good block. Decode file size and return. */
323 if (header->header.typeflag == LNKTYPE)
324 current_stat_info.stat.st_size = 0; /* links 0 size on tape */
326 current_stat_info.stat.st_size = OFF_FROM_HEADER (header->header.size);
328 if (header->header.typeflag == GNUTYPE_LONGNAME
329 || header->header.typeflag == GNUTYPE_LONGLINK
330 || header->header.typeflag == XHDTYPE
331 || header->header.typeflag == XGLTYPE)
333 if (raw_extended_headers)
334 return HEADER_SUCCESS_EXTENDED;
335 else if (header->header.typeflag == GNUTYPE_LONGNAME
336 || header->header.typeflag == GNUTYPE_LONGLINK)
338 size_t name_size = current_stat_info.stat.st_size;
339 size = name_size - name_size % BLOCKSIZE + 2 * BLOCKSIZE;
340 if (name_size != current_stat_info.stat.st_size
344 header_copy = xmalloc (size + 1);
346 if (header->header.typeflag == GNUTYPE_LONGNAME)
349 free (next_long_name);
350 next_long_name = header_copy;
351 next_long_name_blocks = size / BLOCKSIZE;
356 free (next_long_link);
357 next_long_link = header_copy;
358 next_long_link_blocks = size / BLOCKSIZE;
361 set_next_block_after (header);
362 *header_copy = *header;
363 bp = header_copy->buffer + BLOCKSIZE;
365 for (size -= BLOCKSIZE; size > 0; size -= written)
367 data_block = find_next_block ();
370 ERROR ((0, 0, _("Unexpected EOF in archive")));
373 written = available_space_after (data_block);
377 memcpy (bp, data_block->buffer, written);
379 set_next_block_after ((union block *)
380 (data_block->buffer + written - 1));
385 else if (header->header.typeflag == XHDTYPE
386 || header->header.typeflag == XGLTYPE)
387 xheader_read (header, OFF_FROM_HEADER (header->header.size));
395 struct posix_header const *h = ¤t_header->header;
396 char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
398 if (recent_long_name)
399 free (recent_long_name);
403 name = next_long_name->buffer + BLOCKSIZE;
404 recent_long_name = next_long_name;
405 recent_long_name_blocks = next_long_name_blocks;
409 /* Accept file names as specified by POSIX.1-1996
413 if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0)
415 memcpy (np, h->prefix, sizeof h->prefix);
416 np[sizeof h->prefix] = '\0';
420 /* Prevent later references to current_header from
421 mistakenly treating this as an old GNU header.
422 This assignment invalidates h->prefix. */
423 current_header->oldgnu_header.isextended = 0;
425 memcpy (np, h->name, sizeof h->name);
426 np[sizeof h->name] = '\0';
428 recent_long_name = 0;
429 recent_long_name_blocks = 0;
431 assign_string (¤t_stat_info.orig_file_name, name);
432 assign_string (¤t_stat_info.file_name, name);
433 current_stat_info.had_trailing_slash = strip_trailing_slashes (current_stat_info.file_name);
435 if (recent_long_link)
436 free (recent_long_link);
440 name = next_long_link->buffer + BLOCKSIZE;
441 recent_long_link = next_long_link;
442 recent_long_link_blocks = next_long_link_blocks;
446 memcpy (namebuf, h->linkname, sizeof h->linkname);
447 namebuf[sizeof h->linkname] = '\0';
449 recent_long_link = 0;
450 recent_long_link_blocks = 0;
452 assign_string (¤t_stat_info.link_name, name);
454 return HEADER_SUCCESS;
459 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
461 /* Decode things from a file HEADER block into STAT_INFO, also setting
462 *FORMAT_POINTER depending on the header block format. If
463 DO_USER_GROUP, decode the user/group information (this is useful
464 for extraction, but waste time when merely listing).
466 read_header() has already decoded the checksum and length, so we don't.
468 This routine should *not* be called twice for the same block, since
469 the two calls might use different DO_USER_GROUP values and thus
470 might end up with different uid/gid for the two calls. If anybody
471 wants the uid/gid they should decode it first, and other callers
472 should decode it without uid/gid before calling a routine,
473 e.g. print_header, that assumes decoded data. */
475 decode_header (union block *header, struct tar_stat_info *stat_info,
476 enum archive_format *format_pointer, int do_user_group)
478 enum archive_format format;
480 if (strcmp (header->header.magic, TMAGIC) == 0)
482 if (header->star_header.prefix[130] == 0
483 && ISOCTAL (header->star_header.atime[0])
484 && header->star_header.atime[11] == ' '
485 && ISOCTAL (header->star_header.ctime[0])
486 && header->star_header.ctime[11] == ' ')
487 format = STAR_FORMAT;
489 format = POSIX_FORMAT;
491 else if (strcmp (header->header.magic, OLDGNU_MAGIC) == 0)
492 format = OLDGNU_FORMAT;
495 *format_pointer = format;
497 stat_info->stat.st_mode = MODE_FROM_HEADER (header->header.mode);
498 stat_info->stat.st_mtime = TIME_FROM_HEADER (header->header.mtime);
499 assign_string (&stat_info->uname, header->header.uname);
500 assign_string (&stat_info->gname, header->header.gname);
501 stat_info->devmajor = MAJOR_FROM_HEADER (header->header.devmajor);
502 stat_info->devminor = MINOR_FROM_HEADER (header->header.devminor);
504 if (format == OLDGNU_FORMAT && incremental_option)
506 stat_info->stat.st_atime = TIME_FROM_HEADER (header->oldgnu_header.atime);
507 stat_info->stat.st_ctime = TIME_FROM_HEADER (header->oldgnu_header.ctime);
510 if (format == V7_FORMAT)
512 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
513 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
514 stat_info->stat.st_rdev = 0;
519 if (format == STAR_FORMAT)
521 stat_info->stat.st_atime = TIME_FROM_HEADER (header->star_header.atime);
522 stat_info->stat.st_ctime = TIME_FROM_HEADER (header->star_header.ctime);
527 /* FIXME: Decide if this should somewhat depend on -p. */
529 if (numeric_owner_option
530 || !*header->header.uname
531 || !uname_to_uid (header->header.uname, &stat_info->stat.st_uid))
532 stat_info->stat.st_uid = UID_FROM_HEADER (header->header.uid);
534 if (numeric_owner_option
535 || !*header->header.gname
536 || !gname_to_gid (header->header.gname, &stat_info->stat.st_gid))
537 stat_info->stat.st_gid = GID_FROM_HEADER (header->header.gid);
540 switch (header->header.typeflag)
544 stat_info->stat.st_rdev = makedev (stat_info->devmajor, stat_info->devminor);
548 stat_info->stat.st_rdev = 0;
552 if (extended_header.size)
553 xheader_decode (stat_info);
556 /* Convert buffer at WHERE0 of size DIGS from external format to
557 uintmax_t. The data is of type TYPE. The buffer must represent a
558 value in the range -MINUS_MINVAL through MAXVAL. DIGS must be
559 positive. Return -1 on error, diagnosing the error if TYPE is
562 from_header (char const *where0, size_t digs, char const *type,
563 uintmax_t minus_minval, uintmax_t maxval)
566 char const *where = where0;
567 char const *lim = where + digs;
570 /* Accommodate buggy tar of unknown vintage, which outputs leading
571 NUL if the previous field overflows. */
574 /* Accommodate older tars, which output leading spaces. */
581 _("Blanks in header where numeric %s value expected"),
585 if (!ISSPACE ((unsigned char) *where))
591 if (ISODIGIT (*where))
593 char const *where1 = where;
594 uintmax_t overflow = 0;
598 value += *where++ - '0';
599 if (where == lim || ! ISODIGIT (*where))
601 overflow |= value ^ (value << LG_8 >> LG_8);
605 /* Parse the output of older, unportable tars, which generate
606 negative values in two's complement octal. If the leading
607 nonzero digit is 1, we can't recover the original value
608 reliably; so do this only if the digit is 2 or more. This
609 catches the common case of 32-bit negative time stamps. */
610 if ((overflow || maxval < value) && '2' <= *where1 && type)
612 /* Compute the negative of the input value, assuming two's
614 int digit = (*where1 - '0') | 4;
622 if (where == lim || ! ISODIGIT (*where))
624 digit = *where - '0';
625 overflow |= value ^ (value << LG_8 >> LG_8);
631 if (!overflow && value <= minus_minval)
634 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
635 (int) (where - where1), where1, type));
644 _("Archive octal value %.*s is out of %s range"),
645 (int) (where - where1), where1, type));
649 else if (*where == '-' || *where == '+')
651 /* Parse base-64 output produced only by tar test versions
652 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
653 Support for this will be withdrawn in future releases. */
655 static int warned_once;
660 _("Archive contains obsolescent base-64 headers")));
662 negative = *where++ == '-';
664 && (dig = base64_map[(unsigned char) *where]) < 64)
666 if (value << LG_64 >> LG_64 != value)
668 char *string = alloca (digs + 1);
669 memcpy (string, where0, digs);
673 _("Archive signed base-64 string %s is out of %s range"),
674 quote (string), type));
677 value = (value << LG_64) | dig;
681 else if (*where == '\200' /* positive base-256 */
682 || *where == '\377' /* negative base-256 */)
684 /* Parse base-256 output. A nonnegative number N is
685 represented as (256**DIGS)/2 + N; a negative number -N is
686 represented as (256**DIGS) - N, i.e. as two's complement.
687 The representation guarantees that the leading bit is
688 always on, so that we don't confuse this format with the
689 others (assuming ASCII bytes of 8 bits or more). */
690 int signbit = *where & (1 << (LG_256 - 2));
691 uintmax_t topbits = (((uintmax_t) - signbit)
692 << (CHAR_BIT * sizeof (uintmax_t)
693 - LG_256 - (LG_256 - 2)));
694 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
697 value = (value << LG_256) + (unsigned char) *where++;
700 if (((value << LG_256 >> LG_256) | topbits) != value)
704 _("Archive base-256 value is out of %s range"),
714 if (where != lim && *where && !ISSPACE ((unsigned char) *where))
718 char buf[1000]; /* Big enough to represent any header. */
719 static struct quoting_options *o;
723 o = clone_quoting_options (0);
724 set_quoting_style (o, locale_quoting_style);
727 while (where0 != lim && ! lim[-1])
729 quotearg_buffer (buf, sizeof buf, where0, lim - where, o);
731 _("Archive contains %.*s where numeric %s value expected"),
732 (int) sizeof buf, buf, type));
738 if (value <= (negative ? minus_minval : maxval))
739 return negative ? -value : value;
743 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
744 char maxval_buf[UINTMAX_STRSIZE_BOUND];
745 char value_buf[UINTMAX_STRSIZE_BOUND + 1];
746 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
747 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
749 *--value_string = '-';
751 *--minval_string = '-';
752 ERROR ((0, 0, _("Archive value %s is out of %s range %s.%s"),
754 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
761 gid_from_header (const char *p, size_t s)
763 return from_header (p, s, "gid_t",
764 - (uintmax_t) TYPE_MINIMUM (gid_t),
765 (uintmax_t) TYPE_MAXIMUM (gid_t));
769 major_from_header (const char *p, size_t s)
771 return from_header (p, s, "major_t",
772 - (uintmax_t) TYPE_MINIMUM (major_t),
773 (uintmax_t) TYPE_MAXIMUM (major_t));
777 minor_from_header (const char *p, size_t s)
779 return from_header (p, s, "minor_t",
780 - (uintmax_t) TYPE_MINIMUM (minor_t),
781 (uintmax_t) TYPE_MAXIMUM (minor_t));
785 mode_from_header (const char *p, size_t s)
787 /* Do not complain about unrecognized mode bits. */
788 unsigned u = from_header (p, s, "mode_t",
789 - (uintmax_t) TYPE_MINIMUM (mode_t),
790 TYPE_MAXIMUM (uintmax_t));
791 return ((u & TSUID ? S_ISUID : 0)
792 | (u & TSGID ? S_ISGID : 0)
793 | (u & TSVTX ? S_ISVTX : 0)
794 | (u & TUREAD ? S_IRUSR : 0)
795 | (u & TUWRITE ? S_IWUSR : 0)
796 | (u & TUEXEC ? S_IXUSR : 0)
797 | (u & TGREAD ? S_IRGRP : 0)
798 | (u & TGWRITE ? S_IWGRP : 0)
799 | (u & TGEXEC ? S_IXGRP : 0)
800 | (u & TOREAD ? S_IROTH : 0)
801 | (u & TOWRITE ? S_IWOTH : 0)
802 | (u & TOEXEC ? S_IXOTH : 0));
806 off_from_header (const char *p, size_t s)
808 /* Negative offsets are not allowed in tar files, so invoke
809 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
810 return from_header (p, s, "off_t", (uintmax_t) 0,
811 (uintmax_t) TYPE_MAXIMUM (off_t));
815 size_from_header (const char *p, size_t s)
817 return from_header (p, s, "size_t", (uintmax_t) 0,
818 (uintmax_t) TYPE_MAXIMUM (size_t));
822 time_from_header (const char *p, size_t s)
824 return from_header (p, s, "time_t",
825 - (uintmax_t) TYPE_MINIMUM (time_t),
826 (uintmax_t) TYPE_MAXIMUM (time_t));
830 uid_from_header (const char *p, size_t s)
832 return from_header (p, s, "uid_t",
833 - (uintmax_t) TYPE_MINIMUM (uid_t),
834 (uintmax_t) TYPE_MAXIMUM (uid_t));
838 uintmax_from_header (const char *p, size_t s)
840 return from_header (p, s, "uintmax_t", (uintmax_t) 0,
841 TYPE_MAXIMUM (uintmax_t));
845 /* Format O as a null-terminated decimal string into BUF _backwards_;
846 return pointer to start of result. */
848 stringify_uintmax_t_backwards (uintmax_t o, char *buf)
852 *--buf = '0' + (int) (o % 10);
853 while ((o /= 10) != 0);
857 /* Return a printable representation of T. The result points to
858 static storage that can be reused in the next call to this
859 function, to ctime, or to asctime. */
863 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
864 INT_STRLEN_BOUND (int) + 16)];
871 char const *time_stamp = p + 4;
872 for (p += 16; p[3] != '\n'; p++)
878 /* Use ISO 8610 format. See:
879 http://www.cl.cam.ac.uk/~mgk25/iso-time.html */
880 struct tm *tm = localtime (&t);
883 sprintf (buffer, "%04ld-%02d-%02d %02d:%02d:%02d",
884 tm->tm_year + 1900L, tm->tm_mon + 1, tm->tm_mday,
885 tm->tm_hour, tm->tm_min, tm->tm_sec);
890 /* The time stamp cannot be broken down, most likely because it
891 is out of range. Convert it as an integer,
892 right-adjusted in a field with the same width as the usual
893 19-byte 4-year ISO time format. */
894 p = stringify_uintmax_t_backwards (t < 0 ? - (uintmax_t) t : (uintmax_t) t,
895 buffer + sizeof buffer);
898 while (buffer + sizeof buffer - 19 - 1 < p)
903 /* Actually print it.
905 Plain and fancy file header block logging. Non-verbose just prints
906 the name, e.g. for "tar t" or "tar x". This should just contain
907 file names, so it can be fed back into tar with xargs or the "-T"
908 option. The verbose option can give a bunch of info, one line per
909 file. I doubt anybody tries to parse its format, or if they do,
910 they shouldn't. Unix tar is pretty random here anyway. */
913 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
914 HEAD_STANDARD, which must be set up in advance. Not very clean.. */
916 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
917 columns never shift during the listing. */
919 static int ugswidth = UGSWIDTH; /* maximum width encountered so far */
921 /* DATEWIDTH is the number of columns taken by the date and time fields. */
923 # define DATEWIDTH 19
925 # define DATEWIDTH 18
929 print_header (off_t block_ordinal)
932 char const *time_stamp;
933 char *temp_name = current_stat_info.orig_file_name ? current_stat_info.orig_file_name : current_stat_info.file_name;
935 /* These hold formatted ints. */
936 char uform[UINTMAX_STRSIZE_BOUND], gform[UINTMAX_STRSIZE_BOUND];
938 char size[2 * UINTMAX_STRSIZE_BOUND];
939 /* holds formatted size or major,minor */
940 char uintbuf[UINTMAX_STRSIZE_BOUND];
943 if (block_number_option)
945 char buf[UINTMAX_STRSIZE_BOUND];
946 if (block_ordinal < 0)
947 block_ordinal = current_block_ordinal ();
948 block_ordinal -= recent_long_name_blocks;
949 block_ordinal -= recent_long_link_blocks;
950 fprintf (stdlis, _("block %s: "),
951 STRINGIFY_BIGINT (block_ordinal, buf));
954 if (verbose_option <= 1)
956 /* Just the fax, mam. */
957 fprintf (stdlis, "%s\n", quotearg (temp_name));
961 /* File type and modes. */
964 switch (current_header->header.typeflag)
970 case GNUTYPE_MULTIVOL:
978 case GNUTYPE_LONGNAME:
979 case GNUTYPE_LONGLINK:
981 ERROR ((0, 0, _("Visible longname error")));
988 if (temp_name[strlen (temp_name) - 1] == '/')
994 case GNUTYPE_DUMPDIR:
1017 decode_mode (current_stat_info.stat.st_mode, modes + 1);
1021 time_stamp = tartime (current_stat_info.stat.st_mtime);
1023 /* User and group names. */
1025 if (current_stat_info.uname && current_format != V7_FORMAT
1026 && !numeric_owner_option)
1027 user = current_stat_info.uname;
1030 /* Try parsing it as an unsigned integer first, and as a
1031 uid_t if that fails. This method can list positive user
1032 ids that are too large to fit in a uid_t. */
1033 uintmax_t u = from_header (current_header->header.uid,
1034 sizeof current_header->header.uid, 0,
1036 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1038 user = STRINGIFY_BIGINT (u, uform);
1041 sprintf (uform, "%ld",
1042 (long) UID_FROM_HEADER (current_header->header.uid));
1047 if (current_stat_info.gname && current_format != V7_FORMAT
1048 && !numeric_owner_option)
1049 group = current_stat_info.gname;
1052 /* Try parsing it as an unsigned integer first, and as a
1053 gid_t if that fails. This method can list positive group
1054 ids that are too large to fit in a gid_t. */
1055 uintmax_t g = from_header (current_header->header.gid,
1056 sizeof current_header->header.gid, 0,
1058 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1060 group = STRINGIFY_BIGINT (g, gform);
1063 sprintf (gform, "%ld",
1064 (long) GID_FROM_HEADER (current_header->header.gid));
1069 /* Format the file size or major/minor device numbers. */
1071 switch (current_header->header.typeflag)
1076 STRINGIFY_BIGINT (major (current_stat_info.stat.st_rdev), uintbuf));
1079 STRINGIFY_BIGINT (minor (current_stat_info.stat.st_rdev), uintbuf));
1081 case GNUTYPE_SPARSE:
1084 (UINTMAX_FROM_HEADER (current_header
1085 ->oldgnu_header.realsize),
1089 strcpy (size, STRINGIFY_BIGINT (current_stat_info.stat.st_size, uintbuf));
1093 /* Figure out padding and print the whole line. */
1095 pad = strlen (user) + strlen (group) + strlen (size) + 1;
1099 fprintf (stdlis, "%s %s/%s %*s%s %s",
1100 modes, user, group, ugswidth - pad, "", size, time_stamp);
1102 fprintf (stdlis, " %s", quotearg (temp_name));
1104 switch (current_header->header.typeflag)
1107 fprintf (stdlis, " -> %s\n", quotearg (current_stat_info.link_name));
1111 fprintf (stdlis, _(" link to %s\n"), quotearg (current_stat_info.link_name));
1116 char type_string[2];
1117 type_string[0] = current_header->header.typeflag;
1118 type_string[1] = '\0';
1119 fprintf (stdlis, _(" unknown file type %s\n"),
1120 quote (type_string));
1126 case GNUTYPE_SPARSE:
1132 case GNUTYPE_DUMPDIR:
1133 putc ('\n', stdlis);
1136 case GNUTYPE_LONGLINK:
1137 fprintf (stdlis, _("--Long Link--\n"));
1140 case GNUTYPE_LONGNAME:
1141 fprintf (stdlis, _("--Long Name--\n"));
1144 case GNUTYPE_VOLHDR:
1145 fprintf (stdlis, _("--Volume Header--\n"));
1148 case GNUTYPE_MULTIVOL:
1151 (UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset),
1153 fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1157 fprintf (stdlis, _("--Mangled file names--\n"));
1164 /* Print a similar line when we make a directory automatically. */
1166 print_for_mkdir (char *pathname, int length, mode_t mode)
1170 if (verbose_option > 1)
1172 /* File type and modes. */
1175 decode_mode (mode, modes + 1);
1177 if (block_number_option)
1179 char buf[UINTMAX_STRSIZE_BOUND];
1180 fprintf (stdlis, _("block %s: "),
1181 STRINGIFY_BIGINT (current_block_ordinal (), buf));
1184 fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + DATEWIDTH,
1185 _("Creating directory:"), length, quotearg (pathname));
1189 /* Skip over SIZE bytes of data in blocks in the archive. */
1191 skip_file (off_t size)
1195 if (multi_volume_option)
1197 save_totsize = size;
1198 save_sizeleft = size;
1203 x = find_next_block ();
1205 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1207 set_next_block_after (x);
1209 if (multi_volume_option)
1210 save_sizeleft -= BLOCKSIZE;
1214 /* Skip the current member in the archive. */
1218 char save_typeflag = current_header->header.typeflag;
1219 set_next_block_after (current_header);
1221 if (current_format == OLDGNU_FORMAT
1222 && current_header->oldgnu_header.isextended)
1227 exhdr = find_next_block ();
1229 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1230 set_next_block_after (exhdr);
1232 while (exhdr->sparse_header.isextended);
1235 if (save_typeflag != DIRTYPE)
1236 skip_file (current_stat_info.stat.st_size);