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 (0);
82 case HEADER_STILL_UNREAD
:
87 /* Valid header. We should decode next field (mode) first.
88 Ensure incoming names are null terminated. */
90 if (! name_match (current_stat_info
.file_name
)
91 || (newer_mtime_option
!= TYPE_MINIMUM (time_t)
92 /* FIXME: We get mtime now, and again later; this causes
93 duplicate diagnostics if header.mtime is bogus. */
94 && ((current_stat_info
.stat
.st_mtime
95 = TIME_FROM_HEADER (current_header
->header
.mtime
))
96 < newer_mtime_option
))
97 || excluded_name (current_stat_info
.file_name
))
99 switch (current_header
->header
.typeflag
)
102 case GNUTYPE_MULTIVOL
:
107 if (show_omitted_dirs_option
)
108 WARN ((0, 0, _("%s: Omitting"),
109 quotearg_colon (current_stat_info
.file_name
)));
120 case HEADER_ZERO_BLOCK
:
121 if (block_number_option
)
123 char buf
[UINTMAX_STRSIZE_BOUND
];
124 fprintf (stdlis
, _("block %s: ** Block of NULs **\n"),
125 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
128 set_next_block_after (current_header
);
129 status
= prev_status
;
130 if (ignore_zeros_option
)
134 case HEADER_END_OF_FILE
:
135 if (block_number_option
)
137 char buf
[UINTMAX_STRSIZE_BOUND
];
138 fprintf (stdlis
, _("block %s: ** End of File **\n"),
139 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
144 /* If the previous header was good, tell them that we are
145 skipping bad ones. */
146 set_next_block_after (current_header
);
149 case HEADER_STILL_UNREAD
:
150 ERROR ((0, 0, _("This does not look like a tar archive")));
153 case HEADER_ZERO_BLOCK
:
155 ERROR ((0, 0, _("Skipping to next header")));
158 case HEADER_END_OF_FILE
:
160 /* We are in the middle of a cascade of errors. */
169 names_notfound (); /* print names not found */
172 /* Print a header block, based on tar options. */
176 /* Print the header block. */
180 if (verbose_option
> 1)
181 decode_header (current_header
, ¤t_stat_info
, ¤t_format
, 0);
185 if (incremental_option
&& current_header
->header
.typeflag
== GNUTYPE_DUMPDIR
)
188 size_t written
, check
;
189 union block
*data_block
;
191 set_next_block_after (current_header
);
192 if (multi_volume_option
)
194 assign_string (&save_name
, current_stat_info
.file_name
);
195 save_totsize
= current_stat_info
.stat
.st_size
;
197 for (size
= current_stat_info
.stat
.st_size
; size
> 0; size
-= written
)
199 if (multi_volume_option
)
200 save_sizeleft
= size
;
201 data_block
= find_next_block ();
204 ERROR ((0, 0, _("Unexpected EOF in archive")));
205 break; /* FIXME: What happens, then? */
207 written
= available_space_after (data_block
);
211 check
= fwrite (data_block
->buffer
, sizeof (char), written
, stdlis
);
212 set_next_block_after ((union block
*)
213 (data_block
->buffer
+ written
- 1));
214 if (check
!= written
)
216 write_error_details (current_stat_info
.file_name
, check
, written
);
217 skip_file (size
- written
);
221 if (multi_volume_option
)
222 assign_string (&save_name
, 0);
223 fputc ('\n', stdlis
);
229 if (multi_volume_option
)
230 assign_string (&save_name
, current_stat_info
.file_name
);
234 if (multi_volume_option
)
235 assign_string (&save_name
, 0);
238 /* Read a block that's supposed to be a header block. Return its
239 address in "current_header", and if it is good, the file's size in
240 current_stat_info.stat.st_size.
242 Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a
243 block full of zeros (EOF marker).
245 If RAW_EXTENDED_HEADERS is nonzero, do not automagically fold the
246 GNU long name and link headers into later headers.
248 You must always set_next_block_after(current_header) to skip past
249 the header which this routine reads. */
251 /* The standard BSD tar sources create the checksum by adding up the
252 bytes in the header as type char. I think the type char was unsigned
253 on the PDP-11, but it's signed on the Next and Sun. It looks like the
254 sources to BSD tar were never changed to compute the checksum
255 correctly, so both the Sun and Next add the bytes of the header as
256 signed chars. This doesn't cause a problem until you get a file with
257 a name containing characters with the high bit set. So read_header
258 computes two checksums -- signed and unsigned. */
261 read_header (bool raw_extended_headers
)
264 int unsigned_sum
; /* the POSIX one :-) */
265 int signed_sum
; /* the Sun one :-( */
267 uintmax_t parsed_sum
;
270 union block
*header_copy
;
272 union block
*data_block
;
273 size_t size
, written
;
274 union block
*next_long_name
= 0;
275 union block
*next_long_link
= 0;
276 size_t next_long_name_blocks
;
277 size_t next_long_link_blocks
;
281 header
= find_next_block ();
282 current_header
= header
;
284 return HEADER_END_OF_FILE
;
289 for (i
= sizeof *header
; i
-- != 0;)
291 unsigned_sum
+= (unsigned char) *p
;
292 signed_sum
+= (signed char) (*p
++);
295 if (unsigned_sum
== 0)
296 return HEADER_ZERO_BLOCK
;
298 /* Adjust checksum to count the "chksum" field as blanks. */
300 for (i
= sizeof header
->header
.chksum
; i
-- != 0;)
302 unsigned_sum
-= (unsigned char) header
->header
.chksum
[i
];
303 signed_sum
-= (signed char) (header
->header
.chksum
[i
]);
305 unsigned_sum
+= ' ' * sizeof header
->header
.chksum
;
306 signed_sum
+= ' ' * sizeof header
->header
.chksum
;
308 parsed_sum
= from_header (header
->header
.chksum
,
309 sizeof header
->header
.chksum
, 0,
311 (uintmax_t) TYPE_MAXIMUM (int));
312 if (parsed_sum
== (uintmax_t) -1)
313 return HEADER_FAILURE
;
315 recorded_sum
= parsed_sum
;
317 if (unsigned_sum
!= recorded_sum
&& signed_sum
!= recorded_sum
)
318 return HEADER_FAILURE
;
320 /* Good block. Decode file size and return. */
322 if (header
->header
.typeflag
== XHDTYPE
323 || header
->header
.typeflag
== XGLTYPE
)
325 xheader_read (header
, OFF_FROM_HEADER (header
->header
.size
));
329 if (header
->header
.typeflag
== LNKTYPE
)
330 current_stat_info
.stat
.st_size
= 0; /* links 0 size on tape */
332 current_stat_info
.stat
.st_size
= OFF_FROM_HEADER (header
->header
.size
);
334 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
335 || header
->header
.typeflag
== GNUTYPE_LONGLINK
)
337 if (raw_extended_headers
)
338 return HEADER_SUCCESS_EXTENDED
;
341 size_t name_size
= current_stat_info
.stat
.st_size
;
342 size
= name_size
- name_size
% BLOCKSIZE
+ 2 * BLOCKSIZE
;
343 if (name_size
!= current_stat_info
.stat
.st_size
|| size
< name_size
)
347 header_copy
= xmalloc (size
+ 1);
349 if (header
->header
.typeflag
== GNUTYPE_LONGNAME
)
352 free (next_long_name
);
353 next_long_name
= header_copy
;
354 next_long_name_blocks
= size
/ BLOCKSIZE
;
359 free (next_long_link
);
360 next_long_link
= header_copy
;
361 next_long_link_blocks
= size
/ BLOCKSIZE
;
364 set_next_block_after (header
);
365 *header_copy
= *header
;
366 bp
= header_copy
->buffer
+ BLOCKSIZE
;
368 for (size
-= BLOCKSIZE
; size
> 0; size
-= written
)
370 data_block
= find_next_block ();
373 ERROR ((0, 0, _("Unexpected EOF in archive")));
376 written
= available_space_after (data_block
);
380 memcpy (bp
, data_block
->buffer
, written
);
382 set_next_block_after ((union block
*)
383 (data_block
->buffer
+ written
- 1));
394 struct posix_header
const *h
= ¤t_header
->header
;
395 char namebuf
[sizeof h
->prefix
+ 1 + NAME_FIELD_SIZE
+ 1];
397 if (recent_long_name
)
398 free (recent_long_name
);
402 name
= next_long_name
->buffer
+ BLOCKSIZE
;
403 recent_long_name
= next_long_name
;
404 recent_long_name_blocks
= next_long_name_blocks
;
408 /* Accept file names as specified by POSIX.1-1996
412 if (h
->prefix
[0] && strcmp (h
->magic
, TMAGIC
) == 0)
414 memcpy (np
, h
->prefix
, sizeof h
->prefix
);
415 np
[sizeof h
->prefix
] = '\0';
419 /* Prevent later references to current_header from
420 mistakenly treating this as an old GNU header.
421 This assignment invalidates h->prefix. */
422 current_header
->oldgnu_header
.isextended
= 0;
424 memcpy (np
, h
->name
, sizeof h
->name
);
425 np
[sizeof h
->name
] = '\0';
427 recent_long_name
= 0;
428 recent_long_name_blocks
= 0;
430 assign_string (¤t_stat_info
.orig_file_name
, name
);
431 assign_string (¤t_stat_info
.file_name
, name
);
432 current_stat_info
.had_trailing_slash
= strip_trailing_slashes (current_stat_info
.file_name
);
434 if (recent_long_link
)
435 free (recent_long_link
);
439 name
= next_long_link
->buffer
+ BLOCKSIZE
;
440 recent_long_link
= next_long_link
;
441 recent_long_link_blocks
= next_long_link_blocks
;
445 memcpy (namebuf
, h
->linkname
, sizeof h
->linkname
);
446 namebuf
[sizeof h
->linkname
] = '\0';
448 recent_long_link
= 0;
449 recent_long_link_blocks
= 0;
451 assign_string (¤t_stat_info
.link_name
, name
);
453 return HEADER_SUCCESS
;
458 #define ISOCTAL(c) ((c)>='0'&&(c)<='7')
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. */
474 decode_header (union block
*header
, struct tar_stat_info
*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)
481 if (header
->star_header
.prefix
[130] == 0
482 && ISOCTAL (header
->star_header
.atime
[0])
483 && header
->star_header
.atime
[11] == ' '
484 && ISOCTAL (header
->star_header
.ctime
[0])
485 && header
->star_header
.ctime
[11] == ' ')
486 format
= STAR_FORMAT
;
488 format
= POSIX_FORMAT
;
490 else if (strcmp (header
->header
.magic
, OLDGNU_MAGIC
) == 0)
491 format
= OLDGNU_FORMAT
;
494 *format_pointer
= format
;
496 stat_info
->stat
.st_mode
= MODE_FROM_HEADER (header
->header
.mode
);
497 stat_info
->stat
.st_mtime
= TIME_FROM_HEADER (header
->header
.mtime
);
498 assign_string (&stat_info
->uname
, header
->header
.uname
);
499 assign_string (&stat_info
->gname
, header
->header
.gname
);
500 stat_info
->devmajor
= MAJOR_FROM_HEADER (header
->header
.devmajor
);
501 stat_info
->devminor
= MINOR_FROM_HEADER (header
->header
.devminor
);
503 if (format
== OLDGNU_FORMAT
&& incremental_option
)
505 stat_info
->stat
.st_atime
= TIME_FROM_HEADER (header
->oldgnu_header
.atime
);
506 stat_info
->stat
.st_ctime
= TIME_FROM_HEADER (header
->oldgnu_header
.ctime
);
509 if (format
== V7_FORMAT
)
511 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
512 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
513 stat_info
->stat
.st_rdev
= 0;
518 if (format
== STAR_FORMAT
)
520 stat_info
->stat
.st_atime
= TIME_FROM_HEADER (header
->star_header
.atime
);
521 stat_info
->stat
.st_ctime
= TIME_FROM_HEADER (header
->star_header
.ctime
);
526 /* FIXME: Decide if this should somewhat depend on -p. */
528 if (numeric_owner_option
529 || !*header
->header
.uname
530 || !uname_to_uid (header
->header
.uname
, &stat_info
->stat
.st_uid
))
531 stat_info
->stat
.st_uid
= UID_FROM_HEADER (header
->header
.uid
);
533 if (numeric_owner_option
534 || !*header
->header
.gname
535 || !gname_to_gid (header
->header
.gname
, &stat_info
->stat
.st_gid
))
536 stat_info
->stat
.st_gid
= GID_FROM_HEADER (header
->header
.gid
);
539 switch (header
->header
.typeflag
)
543 stat_info
->stat
.st_rdev
= makedev (stat_info
->devmajor
, stat_info
->devminor
);
547 stat_info
->stat
.st_rdev
= 0;
551 if (extended_header
.nblocks
)
552 xheader_decode (stat_info
);
555 /* Convert buffer at WHERE0 of size DIGS from external format to
556 uintmax_t. The data is of type TYPE. The buffer must represent a
557 value in the range -MINUS_MINVAL through MAXVAL. DIGS must be
558 positive. Return -1 on error, diagnosing the error if TYPE is
561 from_header (char const *where0
, size_t digs
, char const *type
,
562 uintmax_t minus_minval
, uintmax_t maxval
)
565 char const *where
= where0
;
566 char const *lim
= where
+ digs
;
569 /* Accommodate buggy tar of unknown vintage, which outputs leading
570 NUL if the previous field overflows. */
573 /* Accommodate older tars, which output leading spaces. */
580 _("Blanks in header where numeric %s value expected"),
584 if (!ISSPACE ((unsigned char) *where
))
590 if (ISODIGIT (*where
))
592 char const *where1
= where
;
593 uintmax_t overflow
= 0;
597 value
+= *where
++ - '0';
598 if (where
== lim
|| ! ISODIGIT (*where
))
600 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
604 /* Parse the output of older, unportable tars, which generate
605 negative values in two's complement octal. If the leading
606 nonzero digit is 1, we can't recover the original value
607 reliably; so do this only if the digit is 2 or more. This
608 catches the common case of 32-bit negative time stamps. */
609 if ((overflow
|| maxval
< value
) && '2' <= *where1
&& type
)
611 /* Compute the negative of the input value, assuming two's
613 int digit
= (*where1
- '0') | 4;
621 if (where
== lim
|| ! ISODIGIT (*where
))
623 digit
= *where
- '0';
624 overflow
|= value
^ (value
<< LG_8
>> LG_8
);
630 if (!overflow
&& value
<= minus_minval
)
633 _("Archive octal value %.*s is out of %s range; assuming two's complement"),
634 (int) (where
- where1
), where1
, type
));
643 _("Archive octal value %.*s is out of %s range"),
644 (int) (where
- where1
), where1
, type
));
648 else if (*where
== '-' || *where
== '+')
650 /* Parse base-64 output produced only by tar test versions
651 1.13.6 (1999-08-11) through 1.13.11 (1999-08-23).
652 Support for this will be withdrawn in future releases. */
654 static int warned_once
;
659 _("Archive contains obsolescent base-64 headers")));
661 negative
= *where
++ == '-';
663 && (dig
= base64_map
[(unsigned char) *where
]) < 64)
665 if (value
<< LG_64
>> LG_64
!= value
)
667 char *string
= alloca (digs
+ 1);
668 memcpy (string
, where0
, digs
);
672 _("Archive signed base-64 string %s is out of %s range"),
673 quote (string
), type
));
676 value
= (value
<< LG_64
) | dig
;
680 else if (*where
== '\200' /* positive base-256 */
681 || *where
== '\377' /* negative base-256 */)
683 /* Parse base-256 output. A nonnegative number N is
684 represented as (256**DIGS)/2 + N; a negative number -N is
685 represented as (256**DIGS) - N, i.e. as two's complement.
686 The representation guarantees that the leading bit is
687 always on, so that we don't confuse this format with the
688 others (assuming ASCII bytes of 8 bits or more). */
689 int signbit
= *where
& (1 << (LG_256
- 2));
690 uintmax_t topbits
= (((uintmax_t) - signbit
)
691 << (CHAR_BIT
* sizeof (uintmax_t)
692 - LG_256
- (LG_256
- 2)));
693 value
= (*where
++ & ((1 << (LG_256
- 2)) - 1)) - signbit
;
696 value
= (value
<< LG_256
) + (unsigned char) *where
++;
699 if (((value
<< LG_256
>> LG_256
) | topbits
) != value
)
703 _("Archive base-256 value is out of %s range"),
713 if (where
!= lim
&& *where
&& !ISSPACE ((unsigned char) *where
))
717 char buf
[1000]; /* Big enough to represent any header. */
718 static struct quoting_options
*o
;
722 o
= clone_quoting_options (0);
723 set_quoting_style (o
, locale_quoting_style
);
726 while (where0
!= lim
&& ! lim
[-1])
728 quotearg_buffer (buf
, sizeof buf
, where0
, lim
- where
, o
);
730 _("Archive contains %.*s where numeric %s value expected"),
731 (int) sizeof buf
, buf
, type
));
737 if (value
<= (negative
? minus_minval
: maxval
))
738 return negative
? -value
: value
;
742 char minval_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
743 char maxval_buf
[UINTMAX_STRSIZE_BOUND
];
744 char value_buf
[UINTMAX_STRSIZE_BOUND
+ 1];
745 char *minval_string
= STRINGIFY_BIGINT (minus_minval
, minval_buf
+ 1);
746 char *value_string
= STRINGIFY_BIGINT (value
, value_buf
+ 1);
748 *--value_string
= '-';
750 *--minval_string
= '-';
751 ERROR ((0, 0, _("Archive value %s is out of %s range %s.%s"),
753 minval_string
, STRINGIFY_BIGINT (maxval
, maxval_buf
)));
760 gid_from_header (const char *p
, size_t s
)
762 return from_header (p
, s
, "gid_t",
763 - (uintmax_t) TYPE_MINIMUM (gid_t
),
764 (uintmax_t) TYPE_MAXIMUM (gid_t
));
768 major_from_header (const char *p
, size_t s
)
770 return from_header (p
, s
, "major_t",
771 - (uintmax_t) TYPE_MINIMUM (major_t
),
772 (uintmax_t) TYPE_MAXIMUM (major_t
));
776 minor_from_header (const char *p
, size_t s
)
778 return from_header (p
, s
, "minor_t",
779 - (uintmax_t) TYPE_MINIMUM (minor_t
),
780 (uintmax_t) TYPE_MAXIMUM (minor_t
));
784 mode_from_header (const char *p
, size_t s
)
786 /* Do not complain about unrecognized mode bits. */
787 unsigned u
= from_header (p
, s
, "mode_t",
788 - (uintmax_t) TYPE_MINIMUM (mode_t
),
789 TYPE_MAXIMUM (uintmax_t));
790 return ((u
& TSUID
? S_ISUID
: 0)
791 | (u
& TSGID
? S_ISGID
: 0)
792 | (u
& TSVTX
? S_ISVTX
: 0)
793 | (u
& TUREAD
? S_IRUSR
: 0)
794 | (u
& TUWRITE
? S_IWUSR
: 0)
795 | (u
& TUEXEC
? S_IXUSR
: 0)
796 | (u
& TGREAD
? S_IRGRP
: 0)
797 | (u
& TGWRITE
? S_IWGRP
: 0)
798 | (u
& TGEXEC
? S_IXGRP
: 0)
799 | (u
& TOREAD
? S_IROTH
: 0)
800 | (u
& TOWRITE
? S_IWOTH
: 0)
801 | (u
& TOEXEC
? S_IXOTH
: 0));
805 off_from_header (const char *p
, size_t s
)
807 /* Negative offsets are not allowed in tar files, so invoke
808 from_header with minimum value 0, not TYPE_MINIMUM (off_t). */
809 return from_header (p
, s
, "off_t", (uintmax_t) 0,
810 (uintmax_t) TYPE_MAXIMUM (off_t
));
814 size_from_header (const char *p
, size_t s
)
816 return from_header (p
, s
, "size_t", (uintmax_t) 0,
817 (uintmax_t) TYPE_MAXIMUM (size_t));
821 time_from_header (const char *p
, size_t s
)
823 return from_header (p
, s
, "time_t",
824 - (uintmax_t) TYPE_MINIMUM (time_t),
825 (uintmax_t) TYPE_MAXIMUM (time_t));
829 uid_from_header (const char *p
, size_t s
)
831 return from_header (p
, s
, "uid_t",
832 - (uintmax_t) TYPE_MINIMUM (uid_t
),
833 (uintmax_t) TYPE_MAXIMUM (uid_t
));
837 uintmax_from_header (const char *p
, size_t s
)
839 return from_header (p
, s
, "uintmax_t", (uintmax_t) 0,
840 TYPE_MAXIMUM (uintmax_t));
844 /* Format O as a null-terminated decimal string into BUF _backwards_;
845 return pointer to start of result. */
847 stringify_uintmax_t_backwards (uintmax_t o
, char *buf
)
851 *--buf
= '0' + (int) (o
% 10);
852 while ((o
/= 10) != 0);
856 /* Return a printable representation of T. The result points to
857 static storage that can be reused in the next call to this
858 function, to ctime, or to asctime. */
862 static char buffer
[max (UINTMAX_STRSIZE_BOUND
+ 1,
863 INT_STRLEN_BOUND (int) + 16)];
870 char const *time_stamp
= p
+ 4;
871 for (p
+= 16; p
[3] != '\n'; p
++)
877 /* Use ISO 8610 format. See:
878 http://www.cl.cam.ac.uk/~mgk25/iso-time.html */
879 struct tm
*tm
= localtime (&t
);
882 sprintf (buffer
, "%04ld-%02d-%02d %02d:%02d:%02d",
883 tm
->tm_year
+ 1900L, tm
->tm_mon
+ 1, tm
->tm_mday
,
884 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
889 /* The time stamp cannot be broken down, most likely because it
890 is out of range. Convert it as an integer,
891 right-adjusted in a field with the same width as the usual
892 19-byte 4-year ISO time format. */
893 p
= stringify_uintmax_t_backwards (t
< 0 ? - (uintmax_t) t
: (uintmax_t) t
,
894 buffer
+ sizeof buffer
);
897 while (buffer
+ sizeof buffer
- 19 - 1 < p
)
902 /* Actually print it.
904 Plain and fancy file header block logging. Non-verbose just prints
905 the name, e.g. for "tar t" or "tar x". This should just contain
906 file names, so it can be fed back into tar with xargs or the "-T"
907 option. The verbose option can give a bunch of info, one line per
908 file. I doubt anybody tries to parse its format, or if they do,
909 they shouldn't. Unix tar is pretty random here anyway. */
912 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
913 HEAD_STANDARD, which must be set up in advance. Not very clean.. */
915 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
916 columns never shift during the listing. */
918 static int ugswidth
= UGSWIDTH
; /* maximum width encountered so far */
920 /* DATEWIDTH is the number of columns taken by the date and time fields. */
922 # define DATEWIDTH 19
924 # define DATEWIDTH 18
928 print_header (off_t block_ordinal
)
931 char const *time_stamp
;
932 char *temp_name
= current_stat_info
.orig_file_name
? current_stat_info
.orig_file_name
: current_stat_info
.file_name
;
934 /* These hold formatted ints. */
935 char uform
[UINTMAX_STRSIZE_BOUND
], gform
[UINTMAX_STRSIZE_BOUND
];
937 char size
[2 * UINTMAX_STRSIZE_BOUND
];
938 /* holds formatted size or major,minor */
939 char uintbuf
[UINTMAX_STRSIZE_BOUND
];
942 if (block_number_option
)
944 char buf
[UINTMAX_STRSIZE_BOUND
];
945 if (block_ordinal
< 0)
946 block_ordinal
= current_block_ordinal ();
947 block_ordinal
-= recent_long_name_blocks
;
948 block_ordinal
-= recent_long_link_blocks
;
949 fprintf (stdlis
, _("block %s: "),
950 STRINGIFY_BIGINT (block_ordinal
, buf
));
953 if (verbose_option
<= 1)
955 /* Just the fax, mam. */
956 fprintf (stdlis
, "%s\n", quotearg (temp_name
));
960 /* File type and modes. */
963 switch (current_header
->header
.typeflag
)
969 case GNUTYPE_MULTIVOL
:
977 case GNUTYPE_LONGNAME
:
978 case GNUTYPE_LONGLINK
:
980 ERROR ((0, 0, _("Visible longname error")));
987 if (temp_name
[strlen (temp_name
) - 1] == '/')
993 case GNUTYPE_DUMPDIR
:
1016 decode_mode (current_stat_info
.stat
.st_mode
, modes
+ 1);
1020 time_stamp
= tartime (current_stat_info
.stat
.st_mtime
);
1022 /* User and group names. */
1024 if (*current_header
->header
.uname
&& current_format
!= V7_FORMAT
1025 && !numeric_owner_option
)
1026 user
= current_header
->header
.uname
;
1029 /* Try parsing it as an unsigned integer first, and as a
1030 uid_t if that fails. This method can list positive user
1031 ids that are too large to fit in a uid_t. */
1032 uintmax_t u
= from_header (current_header
->header
.uid
,
1033 sizeof current_header
->header
.uid
, 0,
1035 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1037 user
= STRINGIFY_BIGINT (u
, uform
);
1040 sprintf (uform
, "%ld",
1041 (long) UID_FROM_HEADER (current_header
->header
.uid
));
1046 if (*current_header
->header
.gname
&& current_format
!= V7_FORMAT
1047 && !numeric_owner_option
)
1048 group
= current_header
->header
.gname
;
1051 /* Try parsing it as an unsigned integer first, and as a
1052 gid_t if that fails. This method can list positive group
1053 ids that are too large to fit in a gid_t. */
1054 uintmax_t g
= from_header (current_header
->header
.gid
,
1055 sizeof current_header
->header
.gid
, 0,
1057 (uintmax_t) TYPE_MAXIMUM (uintmax_t));
1059 group
= STRINGIFY_BIGINT (g
, gform
);
1062 sprintf (gform
, "%ld",
1063 (long) GID_FROM_HEADER (current_header
->header
.gid
));
1068 /* Format the file size or major/minor device numbers. */
1070 switch (current_header
->header
.typeflag
)
1075 STRINGIFY_BIGINT (major (current_stat_info
.stat
.st_rdev
), uintbuf
));
1078 STRINGIFY_BIGINT (minor (current_stat_info
.stat
.st_rdev
), uintbuf
));
1080 case GNUTYPE_SPARSE
:
1083 (UINTMAX_FROM_HEADER (current_header
1084 ->oldgnu_header
.realsize
),
1088 strcpy (size
, STRINGIFY_BIGINT (current_stat_info
.stat
.st_size
, uintbuf
));
1092 /* Figure out padding and print the whole line. */
1094 pad
= strlen (user
) + strlen (group
) + strlen (size
) + 1;
1098 fprintf (stdlis
, "%s %s/%s %*s%s %s",
1099 modes
, user
, group
, ugswidth
- pad
, "", size
, time_stamp
);
1101 fprintf (stdlis
, " %s", quotearg (temp_name
));
1103 switch (current_header
->header
.typeflag
)
1106 fprintf (stdlis
, " -> %s\n", quotearg (current_stat_info
.link_name
));
1110 fprintf (stdlis
, _(" link to %s\n"), quotearg (current_stat_info
.link_name
));
1115 char type_string
[2];
1116 type_string
[0] = current_header
->header
.typeflag
;
1117 type_string
[1] = '\0';
1118 fprintf (stdlis
, _(" unknown file type %s\n"),
1119 quote (type_string
));
1125 case GNUTYPE_SPARSE
:
1131 case GNUTYPE_DUMPDIR
:
1132 putc ('\n', stdlis
);
1135 case GNUTYPE_LONGLINK
:
1136 fprintf (stdlis
, _("--Long Link--\n"));
1139 case GNUTYPE_LONGNAME
:
1140 fprintf (stdlis
, _("--Long Name--\n"));
1143 case GNUTYPE_VOLHDR
:
1144 fprintf (stdlis
, _("--Volume Header--\n"));
1147 case GNUTYPE_MULTIVOL
:
1150 (UINTMAX_FROM_HEADER (current_header
->oldgnu_header
.offset
),
1152 fprintf (stdlis
, _("--Continued at byte %s--\n"), size
);
1156 fprintf (stdlis
, _("--Mangled file names--\n"));
1163 /* Print a similar line when we make a directory automatically. */
1165 print_for_mkdir (char *pathname
, int length
, mode_t mode
)
1169 if (verbose_option
> 1)
1171 /* File type and modes. */
1174 decode_mode (mode
, modes
+ 1);
1176 if (block_number_option
)
1178 char buf
[UINTMAX_STRSIZE_BOUND
];
1179 fprintf (stdlis
, _("block %s: "),
1180 STRINGIFY_BIGINT (current_block_ordinal (), buf
));
1183 fprintf (stdlis
, "%s %*s %.*s\n", modes
, ugswidth
+ DATEWIDTH
,
1184 _("Creating directory:"), length
, quotearg (pathname
));
1188 /* Skip over SIZE bytes of data in blocks in the archive. */
1190 skip_file (off_t size
)
1194 if (multi_volume_option
)
1196 save_totsize
= size
;
1197 save_sizeleft
= size
;
1202 x
= find_next_block ();
1204 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1206 set_next_block_after (x
);
1208 if (multi_volume_option
)
1209 save_sizeleft
-= BLOCKSIZE
;
1213 /* Skip the current member in the archive. */
1217 char save_typeflag
= current_header
->header
.typeflag
;
1218 set_next_block_after (current_header
);
1220 if (current_format
== OLDGNU_FORMAT
1221 && current_header
->oldgnu_header
.isextended
)
1226 exhdr
= find_next_block ();
1228 FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
1229 set_next_block_after (exhdr
);
1231 while (exhdr
->sparse_header
.isextended
);
1234 if (save_typeflag
!= DIRTYPE
)
1235 skip_file (current_stat_info
.stat
.st_size
);