Implement window size passing between real tty and virtual console.
[dragonfly/port-amd64.git] / contrib / tar / src / list.c
blob5c38adb344ed5bd597e7439942ab8553f0063d6a
1 /* List a tar archive, with support routines for reading a tar archive.
3 Copyright 1988, 1992, 1993, 1994, 1996, 1997, 1998, 1999, 2000,
4 2001 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
11 version.
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 /* $FreeBSD: src/contrib/tar/src/list.c,v 1.2.2.1 2002/07/14 13:19:44 sobomax Exp $ */
23 /* $DragonFly: src/contrib/tar/src/Attic/list.c,v 1.2 2003/06/17 04:24:06 dillon Exp $ */
25 /* Define to non-zero for forcing old ctime format instead of ISO format. */
26 #undef USE_OLD_CTIME
28 #include "system.h"
29 #include <quotearg.h>
30 #ifdef HAVE_LANGINFO_CODESET
31 #include <langinfo.h>
32 #endif
34 #include "common.h"
36 #define max(a, b) ((a) < (b) ? (b) : (a))
38 union block *current_header; /* points to current archive header */
39 struct stat current_stat; /* stat struct corresponding */
40 enum archive_format current_format; /* recognized format */
41 union block *recent_long_name; /* recent long name header and contents */
42 union block *recent_long_link; /* likewise, for long link */
43 size_t recent_long_name_blocks; /* number of blocks in recent_long_name */
44 size_t recent_long_link_blocks; /* likewise, for long link */
46 static uintmax_t from_header PARAMS ((const char *, size_t, const char *,
47 uintmax_t, uintmax_t));
49 /* Base 64 digits; see Internet RFC 2045 Table 1. */
50 static char const base_64_digits[64] =
52 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
53 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
54 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
55 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
56 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
59 /* Table of base-64 digit values indexed by unsigned chars.
60 The value is 64 for unsigned chars that are not base-64 digits. */
61 static char base64_map[UCHAR_MAX + 1];
63 static void
64 base64_init (void)
66 int i;
67 memset (base64_map, 64, sizeof base64_map);
68 for (i = 0; i < 64; i++)
69 base64_map[(int) base_64_digits[i]] = i;
72 /* Main loop for reading an archive. */
73 void
74 read_and (void (*do_something) ())
76 enum read_header status = HEADER_STILL_UNREAD;
77 enum read_header prev_status;
79 base64_init ();
80 name_gather ();
81 open_archive (ACCESS_READ);
83 while (1)
85 prev_status = status;
86 status = read_header (0);
87 /* check if the namelist got emptied during the course of reading */
88 /* the tape, if so stop by setting status to EOF */
89 if (namelist_freed)
90 status = HEADER_END_OF_FILE;
91 switch (status)
93 case HEADER_STILL_UNREAD:
94 abort ();
96 case HEADER_SUCCESS:
98 /* Valid header. We should decode next field (mode) first.
99 Ensure incoming names are null terminated. */
101 if (! name_match (current_file_name)
102 || (newer_mtime_option != TYPE_MINIMUM (time_t)
103 /* FIXME: We get mtime now, and again later; this causes
104 duplicate diagnostics if header.mtime is bogus. */
105 && ((current_stat.st_mtime
106 = TIME_FROM_HEADER (current_header->header.mtime))
107 < newer_mtime_option))
108 || excluded_name (current_file_name))
110 switch (current_header->header.typeflag)
112 case GNUTYPE_VOLHDR:
113 case GNUTYPE_MULTIVOL:
114 case GNUTYPE_NAMES:
115 break;
117 case DIRTYPE:
118 if (show_omitted_dirs_option)
119 WARN ((0, 0, _("%s: Omitting"),
120 quotearg_colon (current_file_name)));
121 /* Fall through. */
122 default:
123 skip_member ();
124 continue;
128 (*do_something) ();
129 continue;
131 case HEADER_ZERO_BLOCK:
132 if (block_number_option)
134 char buf[UINTMAX_STRSIZE_BOUND];
135 fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
136 STRINGIFY_BIGINT (current_block_ordinal (), buf));
139 set_next_block_after (current_header);
140 status = prev_status;
141 if (ignore_zeros_option)
142 continue;
143 break;
145 case HEADER_END_OF_FILE:
146 if (block_number_option)
148 char buf[UINTMAX_STRSIZE_BOUND];
149 fprintf (stdlis, _("block %s: ** End of File **\n"),
150 STRINGIFY_BIGINT (current_block_ordinal (), buf));
152 break;
154 case HEADER_FAILURE:
155 /* If the previous header was good, tell them that we are
156 skipping bad ones. */
157 set_next_block_after (current_header);
158 switch (prev_status)
160 case HEADER_STILL_UNREAD:
161 ERROR ((0, 0, _("This does not look like a tar archive")));
162 /* Fall through. */
164 case HEADER_ZERO_BLOCK:
165 case HEADER_SUCCESS:
166 ERROR ((0, 0, _("Skipping to next header")));
167 break;
169 case HEADER_END_OF_FILE:
170 case HEADER_FAILURE:
171 /* We are in the middle of a cascade of errors. */
172 break;
174 continue;
176 break;
179 close_archive ();
180 names_notfound (); /* print names not found */
183 /* Print a header block, based on tar options. */
184 void
185 list_archive (void)
187 /* Print the header block. */
189 if (verbose_option)
191 if (verbose_option > 1)
192 decode_header (current_header, &current_stat, &current_format, 0);
193 print_header ();
196 if (incremental_option && current_header->header.typeflag == GNUTYPE_DUMPDIR)
198 off_t size;
199 size_t written, check;
200 union block *data_block;
202 set_next_block_after (current_header);
203 if (multi_volume_option)
205 assign_string (&save_name, current_file_name);
206 save_totsize = current_stat.st_size;
208 for (size = current_stat.st_size; size > 0; size -= written)
210 if (multi_volume_option)
211 save_sizeleft = size;
212 data_block = find_next_block ();
213 if (!data_block)
215 ERROR ((0, 0, _("Unexpected EOF in archive")));
216 break; /* FIXME: What happens, then? */
218 written = available_space_after (data_block);
219 if (written > size)
220 written = size;
221 errno = 0;
222 check = fwrite (data_block->buffer, sizeof (char), written, stdlis);
223 set_next_block_after ((union block *)
224 (data_block->buffer + written - 1));
225 if (check != written)
227 write_error_details (current_file_name, check, written);
228 skip_file (size - written);
229 break;
232 if (multi_volume_option)
233 assign_string (&save_name, 0);
234 fputc ('\n', stdlis);
235 fflush (stdlis);
236 return;
240 if (multi_volume_option)
241 assign_string (&save_name, current_file_name);
243 skip_member ();
245 if (multi_volume_option)
246 assign_string (&save_name, 0);
249 /* Read a block that's supposed to be a header block. Return its
250 address in "current_header", and if it is good, the file's size in
251 current_stat.st_size.
253 Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a
254 block full of zeros (EOF marker).
256 If RAW_EXTENDED_HEADERS is nonzero, do not automagically fold the
257 GNU long name and link headers into later headers.
259 You must always set_next_block_after(current_header) to skip past
260 the header which this routine reads. */
262 /* The standard BSD tar sources create the checksum by adding up the
263 bytes in the header as type char. I think the type char was unsigned
264 on the PDP-11, but it's signed on the Next and Sun. It looks like the
265 sources to BSD tar were never changed to compute the checksum
266 correctly, so both the Sun and Next add the bytes of the header as
267 signed chars. This doesn't cause a problem until you get a file with
268 a name containing characters with the high bit set. So read_header
269 computes two checksums -- signed and unsigned. */
271 enum read_header
272 read_header (bool raw_extended_headers)
274 size_t i;
275 int unsigned_sum; /* the POSIX one :-) */
276 int signed_sum; /* the Sun one :-( */
277 int recorded_sum;
278 uintmax_t parsed_sum;
279 char *p;
280 union block *header;
281 union block *header_copy;
282 char *bp;
283 union block *data_block;
284 size_t size, written;
285 union block *next_long_name = 0;
286 union block *next_long_link = 0;
287 size_t next_long_name_blocks;
288 size_t next_long_link_blocks;
290 while (1)
292 header = find_next_block ();
293 current_header = header;
294 if (!header)
295 return HEADER_END_OF_FILE;
297 unsigned_sum = 0;
298 signed_sum = 0;
299 p = header->buffer;
300 for (i = sizeof *header; i-- != 0;)
302 unsigned_sum += (unsigned char) *p;
303 signed_sum += (signed char) (*p++);
306 if (unsigned_sum == 0)
307 return HEADER_ZERO_BLOCK;
309 /* Adjust checksum to count the "chksum" field as blanks. */
311 for (i = sizeof header->header.chksum; i-- != 0;)
313 unsigned_sum -= (unsigned char) header->header.chksum[i];
314 signed_sum -= (signed char) (header->header.chksum[i]);
316 unsigned_sum += ' ' * sizeof header->header.chksum;
317 signed_sum += ' ' * sizeof header->header.chksum;
319 parsed_sum = from_header (header->header.chksum,
320 sizeof header->header.chksum, 0,
321 (uintmax_t) 0,
322 (uintmax_t) TYPE_MAXIMUM (int));
323 if (parsed_sum == (uintmax_t) -1)
324 return HEADER_FAILURE;
326 recorded_sum = parsed_sum;
328 if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
329 return HEADER_FAILURE;
331 /* Good block. Decode file size and return. */
333 if (header->header.typeflag == LNKTYPE)
334 current_stat.st_size = 0; /* links 0 size on tape */
335 else
336 current_stat.st_size = OFF_FROM_HEADER (header->header.size);
338 if (header->header.typeflag == GNUTYPE_LONGNAME
339 || header->header.typeflag == GNUTYPE_LONGLINK)
341 if (raw_extended_headers)
342 return HEADER_SUCCESS_EXTENDED;
343 else
345 size_t name_size = current_stat.st_size;
346 size = name_size - name_size % BLOCKSIZE + 2 * BLOCKSIZE;
347 if (name_size != current_stat.st_size || size < name_size)
348 xalloc_die ();
351 header_copy = xmalloc (size + 1);
353 if (header->header.typeflag == GNUTYPE_LONGNAME)
355 if (next_long_name)
356 free (next_long_name);
357 next_long_name = header_copy;
358 next_long_name_blocks = size / BLOCKSIZE;
360 else
362 if (next_long_link)
363 free (next_long_link);
364 next_long_link = header_copy;
365 next_long_link_blocks = size / BLOCKSIZE;
368 set_next_block_after (header);
369 *header_copy = *header;
370 bp = header_copy->buffer + BLOCKSIZE;
372 for (size -= BLOCKSIZE; size > 0; size -= written)
374 data_block = find_next_block ();
375 if (! data_block)
377 ERROR ((0, 0, _("Unexpected EOF in archive")));
378 break;
380 written = available_space_after (data_block);
381 if (written > size)
382 written = size;
384 memcpy (bp, data_block->buffer, written);
385 bp += written;
386 set_next_block_after ((union block *)
387 (data_block->buffer + written - 1));
390 *bp = '\0';
392 /* Loop! */
395 else
397 char const *name;
398 struct posix_header const *h = &current_header->header;
399 char namebuf[sizeof h->prefix + 1 + NAME_FIELD_SIZE + 1];
401 if (recent_long_name)
402 free (recent_long_name);
404 if (next_long_name)
406 name = next_long_name->buffer + BLOCKSIZE;
407 recent_long_name = next_long_name;
408 recent_long_name_blocks = next_long_name_blocks;
410 else
412 /* Accept file names as specified by POSIX.1-1996
413 section 10.1.1. */
414 char *np = namebuf;
416 if (h->prefix[0] && strcmp (h->magic, TMAGIC) == 0)
418 memcpy (np, h->prefix, sizeof h->prefix);
419 np[sizeof h->prefix] = '\0';
420 np += strlen (np);
421 *np++ = '/';
423 /* Prevent later references to current_header from
424 mistakenly treating this as an old GNU header.
425 This assignment invalidates h->prefix. */
426 current_header->oldgnu_header.isextended = 0;
428 memcpy (np, h->name, sizeof h->name);
429 np[sizeof h->name] = '\0';
430 name = namebuf;
431 recent_long_name = 0;
432 recent_long_name_blocks = 0;
434 assign_string (&current_file_name, name);
436 if (recent_long_link)
437 free (recent_long_link);
439 if (next_long_link)
441 name = next_long_link->buffer + BLOCKSIZE;
442 recent_long_link = next_long_link;
443 recent_long_link_blocks = next_long_link_blocks;
445 else
447 memcpy (namebuf, h->linkname, sizeof h->linkname);
448 namebuf[sizeof h->linkname] = '\0';
449 name = namebuf;
450 recent_long_link = 0;
451 recent_long_link_blocks = 0;
453 assign_string (&current_link_name, name);
455 return HEADER_SUCCESS;
460 /* Decode things from a file HEADER block into STAT_INFO, also setting
461 *FORMAT_POINTER depending on the header block format. If
462 DO_USER_GROUP, decode the user/group information (this is useful
463 for extraction, but waste time when merely listing).
465 read_header() has already decoded the checksum and length, so we don't.
467 This routine should *not* be called twice for the same block, since
468 the two calls might use different DO_USER_GROUP values and thus
469 might end up with different uid/gid for the two calls. If anybody
470 wants the uid/gid they should decode it first, and other callers
471 should decode it without uid/gid before calling a routine,
472 e.g. print_header, that assumes decoded data. */
473 void
474 decode_header (union block *header, struct stat *stat_info,
475 enum archive_format *format_pointer, int do_user_group)
477 enum archive_format format;
479 if (strcmp (header->header.magic, TMAGIC) == 0)
480 format = POSIX_FORMAT;
481 else if (strcmp (header->header.magic, OLDGNU_MAGIC) == 0)
482 format = OLDGNU_FORMAT;
483 else
484 format = V7_FORMAT;
485 *format_pointer = format;
487 stat_info->st_mode = MODE_FROM_HEADER (header->header.mode);
488 stat_info->st_mtime = TIME_FROM_HEADER (header->header.mtime);
490 if (format == OLDGNU_FORMAT && incremental_option)
492 stat_info->st_atime = TIME_FROM_HEADER (header->oldgnu_header.atime);
493 stat_info->st_ctime = TIME_FROM_HEADER (header->oldgnu_header.ctime);
496 if (format == V7_FORMAT)
498 stat_info->st_uid = UID_FROM_HEADER (header->header.uid);
499 stat_info->st_gid = GID_FROM_HEADER (header->header.gid);
500 stat_info->st_rdev = 0;
502 else
504 if (do_user_group)
506 /* FIXME: Decide if this should somewhat depend on -p. */
508 if (numeric_owner_option
509 || !*header->header.uname
510 || !uname_to_uid (header->header.uname, &stat_info->st_uid))
511 stat_info->st_uid = UID_FROM_HEADER (header->header.uid);
513 if (numeric_owner_option
514 || !*header->header.gname
515 || !gname_to_gid (header->header.gname, &stat_info->st_gid))
516 stat_info->st_gid = GID_FROM_HEADER (header->header.gid);
518 switch (header->header.typeflag)
520 case BLKTYPE:
521 stat_info->st_rdev
522 = makedev (MAJOR_FROM_HEADER (header->header.devmajor),
523 MINOR_FROM_HEADER (header->header.devminor));
524 break;
526 case CHRTYPE:
527 stat_info->st_rdev
528 = makedev (MAJOR_FROM_HEADER (header->header.devmajor),
529 MINOR_FROM_HEADER (header->header.devminor));
530 break;
532 default:
533 stat_info->st_rdev = 0;
538 /* Convert buffer at WHERE0 of size DIGS from external format to
539 uintmax_t. The data is of type TYPE. The buffer must represent a
540 value in the range -MINUS_MINVAL through MAXVAL. DIGS must be
541 positive. Return -1 on error, diagnosing the error if TYPE is
542 nonzero. */
543 static uintmax_t
544 from_header (char const *where0, size_t digs, char const *type,
545 uintmax_t minus_minval, uintmax_t maxval)
547 uintmax_t value;
548 char const *where = where0;
549 char const *lim = where + digs;
550 int negative = 0;
552 /* Accommodate buggy tar of unknown vintage, which outputs leading
553 NUL if the previous field overflows. */
554 where += !*where;
556 /* Accommodate older tars, which output leading spaces. */
557 for (;;)
559 if (where == lim)
561 if (type)
562 ERROR ((0, 0,
563 _("Blanks in header where numeric %s value expected"),
564 type));
565 return -1;
567 if (!ISSPACE ((unsigned char) *where))
568 break;
569 where++;
572 value = 0;
573 if (ISODIGIT (*where))
575 char const *where1 = where;
576 uintmax_t overflow = 0;
578 for (;;)
580 value += *where++ - '0';
581 if (where == lim || ! ISODIGIT (*where))
582 break;
583 overflow |= value ^ (value << LG_8 >> LG_8);
584 value <<= LG_8;
587 /* Parse the output of older, unportable tars, which generate
588 negative values in two's complement octal. If the leading
589 nonzero digit is 1, we can't recover the original value
590 reliably; so do this only if the digit is 2 or more. This
591 catches the common case of 32-bit negative time stamps. */
592 if ((overflow || maxval < value) && '2' <= *where1 && type)
594 /* Compute the negative of the input value, assuming two's
595 complement. */
596 int digit = (*where1 - '0') | 4;
597 overflow = 0;
598 value = 0;
599 where = where1;
600 for (;;)
602 value += 7 - digit;
603 where++;
604 if (where == lim || ! ISODIGIT (*where))
605 break;
606 digit = *where - '0';
607 overflow |= value ^ (value << LG_8 >> LG_8);
608 value <<= LG_8;
610 value++;
611 overflow |= !value;
613 if (!overflow && value <= minus_minval)
615 WARN ((0, 0,
616 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
617 (int) (where - where1), where1, type));
618 negative = 1;
622 if (overflow)
624 if (type)
625 ERROR ((0, 0,
626 _("Archive octal value %.*s is out of %s range"),
627 (int) (where - where1), where1, type));
628 return -1;
631 else if (*where == '-' || *where == '+')
633 /* Parse base-64 output produced only by tar test versions
634 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
635 Support for this will be withdrawn in future releases. */
636 int dig;
637 static int warned_once;
638 if (! warned_once)
640 warned_once = 1;
641 WARN ((0, 0,
642 _("Archive contains obsolescent base-64 headers")));
644 negative = *where++ == '-';
645 while (where != lim
646 && (dig = base64_map[(unsigned char) *where]) < 64)
648 if (value << LG_64 >> LG_64 != value)
650 char *string = alloca (digs + 1);
651 memcpy (string, where0, digs);
652 string[digs] = '\0';
653 if (type)
654 ERROR ((0, 0,
655 _("Archive signed base-64 string %s is out of %s range"),
656 quote (string), type));
657 return -1;
659 value = (value << LG_64) | dig;
660 where++;
663 else if (*where == '\200' /* positive base-256 */
664 || *where == '\377' /* negative base-256 */)
666 /* Parse base-256 output. A nonnegative number N is
667 represented as (256**DIGS)/2 + N; a negative number -N is
668 represented as (256**DIGS) - N, i.e. as two's complement.
669 The representation guarantees that the leading bit is
670 always on, so that we don't confuse this format with the
671 others (assuming ASCII bytes of 8 bits or more). */
672 int signbit = *where & (1 << (LG_256 - 2));
673 uintmax_t topbits = (((uintmax_t) - signbit)
674 << (CHAR_BIT * sizeof (uintmax_t)
675 - LG_256 - (LG_256 - 2)));
676 value = (*where++ & ((1 << (LG_256 - 2)) - 1)) - signbit;
677 for (;;)
679 value = (value << LG_256) + (unsigned char) *where++;
680 if (where == lim)
681 break;
682 if (((value << LG_256 >> LG_256) | topbits) != value)
684 if (type)
685 ERROR ((0, 0,
686 _("Archive base-256 value is out of %s range"),
687 type));
688 return -1;
691 negative = signbit;
692 if (negative)
693 value = -value;
696 if (where != lim && *where && !ISSPACE ((unsigned char) *where))
698 if (type)
700 char buf[1000]; /* Big enough to represent any header. */
701 static struct quoting_options *o;
703 if (!o)
705 o = clone_quoting_options (0);
706 set_quoting_style (o, locale_quoting_style);
709 while (where0 != lim && ! lim[-1])
710 lim--;
711 quotearg_buffer (buf, sizeof buf, where0, lim - where, o);
712 ERROR ((0, 0,
713 _("Archive contains %.*s where numeric %s value expected"),
714 (int) sizeof buf, buf, type));
717 return -1;
720 if (value <= (negative ? minus_minval : maxval))
721 return negative ? -value : value;
723 if (type)
725 char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
726 char maxval_buf[UINTMAX_STRSIZE_BOUND];
727 char value_buf[UINTMAX_STRSIZE_BOUND + 1];
728 char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
729 char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
730 if (negative)
731 *--value_string = '-';
732 if (minus_minval)
733 *--minval_string = '-';
734 ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
735 value_string, type,
736 minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
739 return -1;
742 gid_t
743 gid_from_header (const char *p, size_t s)
745 return from_header (p, s, "gid_t",
746 - (uintmax_t) TYPE_MINIMUM (gid_t),
747 (uintmax_t) TYPE_MAXIMUM (gid_t));
750 major_t
751 major_from_header (const char *p, size_t s)
753 return from_header (p, s, "major_t",
754 - (uintmax_t) TYPE_MINIMUM (major_t),
755 (uintmax_t) TYPE_MAXIMUM (major_t));
758 minor_t
759 minor_from_header (const char *p, size_t s)
761 return from_header (p, s, "minor_t",
762 - (uintmax_t) TYPE_MINIMUM (minor_t),
763 (uintmax_t) TYPE_MAXIMUM (minor_t));
766 mode_t
767 mode_from_header (const char *p, size_t s)
769 /* Do not complain about unrecognized mode bits. */
770 unsigned u = from_header (p, s, "mode_t",
771 - (uintmax_t) TYPE_MINIMUM (mode_t),
772 TYPE_MAXIMUM (uintmax_t));
773 return ((u & TSUID ? S_ISUID : 0)
774 | (u & TSGID ? S_ISGID : 0)
775 | (u & TSVTX ? S_ISVTX : 0)
776 | (u & TUREAD ? S_IRUSR : 0)
777 | (u & TUWRITE ? S_IWUSR : 0)
778 | (u & TUEXEC ? S_IXUSR : 0)
779 | (u & TGREAD ? S_IRGRP : 0)
780 | (u & TGWRITE ? S_IWGRP : 0)
781 | (u & TGEXEC ? S_IXGRP : 0)
782 | (u & TOREAD ? S_IROTH : 0)
783 | (u & TOWRITE ? S_IWOTH : 0)
784 | (u & TOEXEC ? S_IXOTH : 0));
787 off_t
788 off_from_header (const char *p, size_t s)
790 /* Negative offsets are not allowed in tar files, so invoke
791 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
792 return from_header (p, s, "off_t", (uintmax_t) 0,
793 (uintmax_t) TYPE_MAXIMUM (off_t));
796 size_t
797 size_from_header (const char *p, size_t s)
799 return from_header (p, s, "size_t", (uintmax_t) 0,
800 (uintmax_t) TYPE_MAXIMUM (size_t));
803 time_t
804 time_from_header (const char *p, size_t s)
806 return from_header (p, s, "time_t",
807 - (uintmax_t) TYPE_MINIMUM (time_t),
808 (uintmax_t) TYPE_MAXIMUM (time_t));
811 uid_t
812 uid_from_header (const char *p, size_t s)
814 return from_header (p, s, "uid_t",
815 - (uintmax_t) TYPE_MINIMUM (uid_t),
816 (uintmax_t) TYPE_MAXIMUM (uid_t));
819 uintmax_t
820 uintmax_from_header (const char *p, size_t s)
822 return from_header (p, s, "uintmax_t", (uintmax_t) 0,
823 TYPE_MAXIMUM (uintmax_t));
827 /* Format O as a null-terminated decimal string into BUF _backwards_;
828 return pointer to start of result. */
829 char *
830 stringify_uintmax_t_backwards (uintmax_t o, char *buf)
832 *--buf = '\0';
834 *--buf = '0' + (int) (o % 10);
835 while ((o /= 10) != 0);
836 return buf;
839 /* Return a printable representation of T. The result points to
840 static storage that can be reused in the next call to this
841 function, to ctime, or to asctime. */
842 char const *
843 tartime (time_t t)
845 #if !defined(__FreeBSD__) || !defined(HAVE_LANGINFO_CODESET)
846 static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
847 INT_STRLEN_BOUND (int) + 16)];
848 char *p;
850 #if USE_OLD_CTIME
851 p = ctime (&t);
852 if (p)
854 char const *time_stamp = p + 4;
855 for (p += 16; p[3] != '\n'; p++)
856 p[0] = p[3];
857 p[0] = '\0';
858 return time_stamp;
860 #else
861 /* Use ISO 8610 format. See:
862 http://www.cl.cam.ac.uk/~mgk25/iso-time.html */
863 struct tm *tm = localtime (&t);
864 if (tm)
866 sprintf (buffer, "%04d-%02d-%02d %02d:%02d:%02d",
867 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
868 tm->tm_hour, tm->tm_min, tm->tm_sec);
869 return buffer;
871 #endif
873 /* The time stamp cannot be broken down, most likely because it
874 is out of range. Convert it as an integer,
875 right-adjusted in a field with the same width as the usual
876 19-byte 4-year ISO time format. */
877 p = stringify_uintmax_t_backwards (t < 0 ? - (uintmax_t) t : (uintmax_t) t,
878 buffer + sizeof buffer);
879 if (t < 0)
880 *--p = '-';
881 while (buffer + sizeof buffer - 19 - 1 < p)
882 *--p = ' ';
883 return p;
884 #else /* __FreeBSD__ */
885 static char buffer[80];
886 static int d_first = -1;
888 if (d_first < 0)
889 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
890 strftime(buffer, sizeof(buffer), d_first ? "%e %b %R %Y" : "%b %e %R %Y",
891 localtime(&t));
892 return buffer;
893 #endif /* __FreeBSD__ */
896 /* Actually print it.
898 Plain and fancy file header block logging. Non-verbose just prints
899 the name, e.g. for "tar t" or "tar x". This should just contain
900 file names, so it can be fed back into tar with xargs or the "-T"
901 option. The verbose option can give a bunch of info, one line per
902 file. I doubt anybody tries to parse its format, or if they do,
903 they shouldn't. Unix tar is pretty random here anyway. */
906 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
907 HEAD_STANDARD, which must be set up in advance. Not very clean... */
909 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
910 columns never shift during the listing. */
911 #define UGSWIDTH 18
912 static int ugswidth = UGSWIDTH; /* maximum width encountered so far */
914 /* DATEWIDTH is the number of columns taken by the date and time fields. */
915 #if USE_OLD_CDATE
916 # define DATEWIDTH 19
917 #else
918 # define DATEWIDTH 18
919 #endif
921 void
922 print_header (void)
924 char modes[11];
925 char const *time_stamp;
926 /* These hold formatted ints. */
927 char uform[UINTMAX_STRSIZE_BOUND], gform[UINTMAX_STRSIZE_BOUND];
928 char *user, *group;
929 char size[2 * UINTMAX_STRSIZE_BOUND];
930 /* holds formatted size or major,minor */
931 char uintbuf[UINTMAX_STRSIZE_BOUND];
932 int pad;
934 if (block_number_option)
936 char buf[UINTMAX_STRSIZE_BOUND];
937 fprintf (stdlis, _("block %s: "),
938 STRINGIFY_BIGINT (current_block_ordinal (), buf));
941 if (verbose_option <= 1)
943 /* Just the fax, mam. */
944 fprintf (stdlis, "%s\n", quotearg (current_file_name));
946 else
948 /* File type and modes. */
950 modes[0] = '?';
951 switch (current_header->header.typeflag)
953 case GNUTYPE_VOLHDR:
954 modes[0] = 'V';
955 break;
957 case GNUTYPE_MULTIVOL:
958 modes[0] = 'M';
959 break;
961 case GNUTYPE_NAMES:
962 modes[0] = 'N';
963 break;
965 case GNUTYPE_LONGNAME:
966 case GNUTYPE_LONGLINK:
967 ERROR ((0, 0, _("Visible longname error")));
968 break;
970 case GNUTYPE_SPARSE:
971 case REGTYPE:
972 case AREGTYPE:
973 case LNKTYPE:
974 modes[0] = '-';
975 if (current_file_name[strlen (current_file_name) - 1] == '/')
976 modes[0] = 'd';
977 break;
978 case GNUTYPE_DUMPDIR:
979 modes[0] = 'd';
980 break;
981 case DIRTYPE:
982 modes[0] = 'd';
983 break;
984 case SYMTYPE:
985 modes[0] = 'l';
986 break;
987 case BLKTYPE:
988 modes[0] = 'b';
989 break;
990 case CHRTYPE:
991 modes[0] = 'c';
992 break;
993 case FIFOTYPE:
994 modes[0] = 'p';
995 break;
996 case CONTTYPE:
997 modes[0] = 'C';
998 break;
1001 decode_mode (current_stat.st_mode, modes + 1);
1003 /* Time stamp. */
1005 time_stamp = tartime (current_stat.st_mtime);
1007 /* User and group names. */
1009 if (*current_header->header.uname && current_format != V7_FORMAT
1010 && !numeric_owner_option)
1011 user = current_header->header.uname;
1012 else
1014 /* Try parsing it as an unsigned integer first, and as a
1015 uid_t if that fails. This method can list positive user
1016 ids that are too large to fit in a uid_t. */
1017 uintmax_t u = from_header (current_header->header.uid,
1018 sizeof current_header->header.uid, 0,
1019 (uintmax_t) 0,
1020 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1021 if (u != -1)
1022 user = STRINGIFY_BIGINT (u, uform);
1023 else
1025 sprintf (uform, "%ld",
1026 (long) UID_FROM_HEADER (current_header->header.uid));
1027 user = uform;
1031 if (*current_header->header.gname && current_format != V7_FORMAT
1032 && !numeric_owner_option)
1033 group = current_header->header.gname;
1034 else
1036 /* Try parsing it as an unsigned integer first, and as a
1037 gid_t if that fails. This method can list positive group
1038 ids that are too large to fit in a gid_t. */
1039 uintmax_t g = from_header (current_header->header.gid,
1040 sizeof current_header->header.gid, 0,
1041 (uintmax_t) 0,
1042 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1043 if (g != -1)
1044 group = STRINGIFY_BIGINT (g, gform);
1045 else
1047 sprintf (gform, "%ld",
1048 (long) GID_FROM_HEADER (current_header->header.gid));
1049 group = gform;
1053 /* Format the file size or major/minor device numbers. */
1055 switch (current_header->header.typeflag)
1057 case CHRTYPE:
1058 case BLKTYPE:
1059 strcpy (size,
1060 STRINGIFY_BIGINT (major (current_stat.st_rdev), uintbuf));
1061 strcat (size, ",");
1062 strcat (size,
1063 STRINGIFY_BIGINT (minor (current_stat.st_rdev), uintbuf));
1064 break;
1065 case GNUTYPE_SPARSE:
1066 strcpy (size,
1067 STRINGIFY_BIGINT
1068 (UINTMAX_FROM_HEADER (current_header
1069 ->oldgnu_header.realsize),
1070 uintbuf));
1071 break;
1072 default:
1073 strcpy (size, STRINGIFY_BIGINT (current_stat.st_size, uintbuf));
1074 break;
1077 /* Figure out padding and print the whole line. */
1079 pad = strlen (user) + strlen (group) + strlen (size) + 1;
1080 if (pad > ugswidth)
1081 ugswidth = pad;
1083 fprintf (stdlis, "%s %s/%s %*s%s %s",
1084 modes, user, group, ugswidth - pad, "", size, time_stamp);
1086 fprintf (stdlis, " %s", quotearg (current_file_name));
1088 switch (current_header->header.typeflag)
1090 case SYMTYPE:
1091 fprintf (stdlis, " -> %s\n", quotearg (current_link_name));
1092 break;
1094 case LNKTYPE:
1095 fprintf (stdlis, _(" link to %s\n"), quotearg (current_link_name));
1096 break;
1098 default:
1100 char type_string[2];
1101 type_string[0] = current_header->header.typeflag;
1102 type_string[1] = '\0';
1103 fprintf (stdlis, _(" unknown file type %s\n"),
1104 quote (type_string));
1106 break;
1108 case AREGTYPE:
1109 case REGTYPE:
1110 case GNUTYPE_SPARSE:
1111 case CHRTYPE:
1112 case BLKTYPE:
1113 case DIRTYPE:
1114 case FIFOTYPE:
1115 case CONTTYPE:
1116 case GNUTYPE_DUMPDIR:
1117 putc ('\n', stdlis);
1118 break;
1120 case GNUTYPE_VOLHDR:
1121 fprintf (stdlis, _("--Volume Header--\n"));
1122 break;
1124 case GNUTYPE_MULTIVOL:
1125 strcpy (size,
1126 STRINGIFY_BIGINT
1127 (UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset),
1128 uintbuf));
1129 fprintf (stdlis, _("--Continued at byte %s--\n"), size);
1130 break;
1132 case GNUTYPE_NAMES:
1133 fprintf (stdlis, _("--Mangled file names--\n"));
1134 break;
1137 fflush (stdlis);
1140 /* Print a similar line when we make a directory automatically. */
1141 void
1142 print_for_mkdir (char *pathname, int length, mode_t mode)
1144 char modes[11];
1146 if (verbose_option > 1)
1148 /* File type and modes. */
1150 modes[0] = 'd';
1151 decode_mode (mode, modes + 1);
1153 if (block_number_option)
1155 char buf[UINTMAX_STRSIZE_BOUND];
1156 fprintf (stdlis, _("block %s: "),
1157 STRINGIFY_BIGINT (current_block_ordinal (), buf));
1160 fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + DATEWIDTH,
1161 _("Creating directory:"), length, quotearg (pathname));
1165 /* Skip over SIZE bytes of data in blocks in the archive. */
1166 void
1167 skip_file (off_t size)
1169 union block *x;
1171 if (multi_volume_option)
1173 save_totsize = size;
1174 save_sizeleft = size;
1177 while (size > 0)
1179 x = find_next_block ();
1180 if (! x)
1181 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1183 set_next_block_after (x);
1184 size -= BLOCKSIZE;
1185 if (multi_volume_option)
1186 save_sizeleft -= BLOCKSIZE;
1190 /* Skip the current member in the archive. */
1191 void
1192 skip_member (void)
1194 char save_typeflag = current_header->header.typeflag;
1195 set_next_block_after (current_header);
1197 if (current_header->oldgnu_header.isextended)
1199 union block *exhdr;
1202 exhdr = find_next_block ();
1203 if (!exhdr)
1204 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1205 set_next_block_after (exhdr);
1207 while (exhdr->sparse_header.isextended);
1210 if (save_typeflag != DIRTYPE)
1211 skip_file (current_stat.st_size);