Include <quotearg.h>.
[debian/tar] / src / list.c
1 /* List a tar archive, with support routines for reading a tar archive.
2    Copyright 1988,92,93,94,96,97,98,1999 Free Software Foundation, Inc.
3    Written by John Gilmore, on 1985-08-26.
4
5    This program is free software; you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by the
7    Free Software Foundation; either version 2, or (at your option) any later
8    version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
13    Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation, Inc.,
17    59 Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 /* Define to non-zero for forcing old ctime() instead of isotime().  */
20 #undef USE_OLD_CTIME
21
22 #include "system.h"
23 #include <quotearg.h>
24
25 #include <ctype.h>
26 #include <time.h>
27
28 #define ISODIGIT(Char) \
29   ((unsigned char) (Char) >= '0' && (unsigned char) (Char) <= '7')
30 #define ISSPACE(Char) (ISASCII (Char) && isspace (Char))
31
32 #ifndef FNM_LEADING_DIR
33 # include <fnmatch.h>
34 #endif
35
36 #include "common.h"
37
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
42 static uintmax_t from_oct PARAMS ((const char *, size_t, const char *, uintmax_t));
43
44
45 /*-----------------------------------.
46 | Main loop for reading an archive.  |
47 `-----------------------------------*/
48
49 void
50 read_and (void (*do_something) ())
51 {
52   enum read_header status = HEADER_STILL_UNREAD;
53   enum read_header prev_status;
54   char save_typeflag;
55
56   name_gather ();
57   open_archive (ACCESS_READ);
58
59   while (1)
60     {
61       prev_status = status;
62       status = read_header ();
63       switch (status)
64         {
65         case HEADER_STILL_UNREAD:
66           abort ();
67
68         case HEADER_SUCCESS:
69
70           /* Valid header.  We should decode next field (mode) first.
71              Ensure incoming names are null terminated.  */
72
73           if (ending_file_option &&
74               fnmatch (ending_file_option, current_file_name,
75                        FNM_LEADING_DIR) == 0) {
76             goto all_done;
77           }
78
79           /* FIXME: This is a quick kludge before 1.12 goes out.  */
80           current_stat.st_mtime = TIME_FROM_OCT (current_header->header.mtime);
81
82           if (!name_match (current_file_name)
83               || current_stat.st_mtime < newer_mtime_option
84               || (exclude_option && check_exclude (current_file_name)))
85             {
86               int isextended = 0;
87
88               if (current_header->header.typeflag == GNUTYPE_VOLHDR
89                   || current_header->header.typeflag == GNUTYPE_MULTIVOL
90                   || current_header->header.typeflag == GNUTYPE_NAMES)
91                 {
92                   (*do_something) ();
93                   continue;
94                 }
95               if (show_omitted_dirs_option
96                   && current_header->header.typeflag == DIRTYPE)
97                 WARN ((0, 0, _("Omitting %s"), current_file_name));
98
99               /* Skip past it in the archive.  */
100
101               if (current_header->oldgnu_header.isextended)
102                 isextended = 1;
103               save_typeflag = current_header->header.typeflag;
104               set_next_block_after (current_header);
105               if (isextended)
106                 {
107 #if 0
108                   union block *exhdr;
109
110                   while (1)
111                     {
112                       exhdr = find_next_block ();
113                       if (!exhdr->sparse_header.isextended)
114                         {
115                           set_next_block_after (exhdr);
116                           break;
117                         }
118                     }
119                   set_next_block_after (exhdr);
120 #endif
121                   skip_extended_headers ();
122                 }
123
124               /* Skip to the next header on the archive.  */
125
126               if (save_typeflag != DIRTYPE)
127                 skip_file (current_stat.st_size);
128               continue;
129             }
130
131           (*do_something) ();
132           continue;
133
134         case HEADER_ZERO_BLOCK:
135           if (block_number_option)
136             {
137               char buf[UINTMAX_STRSIZE_BOUND];
138               fprintf (stdlis, _("block %s: ** Block of NULs **\n"),
139                        STRINGIFY_BIGINT (current_block_ordinal (), buf));
140             }
141
142           set_next_block_after (current_header);
143           status = prev_status;
144           if (ignore_zeros_option)
145             continue;
146           break;
147
148         case HEADER_END_OF_FILE:
149           if (block_number_option)
150             {
151               char buf[UINTMAX_STRSIZE_BOUND];
152               fprintf (stdlis, _("block %s: ** End of File **\n"),
153                        STRINGIFY_BIGINT (current_block_ordinal (), buf));
154             }
155           break;
156
157         case HEADER_FAILURE:
158           /* If the previous header was good, tell them that we are
159              skipping bad ones.  */
160           set_next_block_after (current_header);
161           switch (prev_status)
162             {
163             case HEADER_STILL_UNREAD:
164               WARN ((0, 0, _("Hmm, this doesn't look like a tar archive")));
165               /* Fall through.  */
166
167             case HEADER_ZERO_BLOCK:
168             case HEADER_SUCCESS:
169               WARN ((0, 0, _("Skipping to next file header")));
170               break;
171
172             case HEADER_END_OF_FILE:
173             case HEADER_FAILURE:
174               /* We are in the middle of a cascade of errors.  */
175               break;
176             }
177           continue;
178         }
179       break;
180     }
181  all_done: ;
182
183   apply_delayed_set_stat ();
184   close_archive ();
185   names_notfound ();            /* print names not found */
186 }
187
188 /*---------------------------------------------.
189 | Print a header block, based on tar options.  |
190 `---------------------------------------------*/
191
192 void
193 list_archive (void)
194 {
195   int isextended = 0;           /* to remember if current_header is extended */
196
197   /* Print the header block.  */
198
199   if (verbose_option)
200     {
201       if (verbose_option > 1)
202         decode_header (current_header, &current_stat, &current_format, 0);
203       print_header ();
204     }
205
206   if (incremental_option && current_header->header.typeflag == GNUTYPE_DUMPDIR)
207     {
208       off_t size;
209       size_t written, check;
210       union block *data_block;
211
212       set_next_block_after (current_header);
213       if (multi_volume_option)
214         {
215           assign_string (&save_name, current_file_name);
216           save_totsize = current_stat.st_size;
217         }
218       for (size = current_stat.st_size; size > 0; size -= written)
219         {
220           if (multi_volume_option)
221             save_sizeleft = size;
222           data_block = find_next_block ();
223           if (!data_block)
224             {
225               ERROR ((0, 0, _("EOF in archive file")));
226               break;            /* FIXME: What happens, then?  */
227             }
228           written = available_space_after (data_block);
229           if (written > size)
230             written = size;
231           errno = 0;            /* FIXME: errno should be read-only */
232           check = fwrite (data_block->buffer, sizeof (char), written, stdlis);
233           set_next_block_after ((union block *)
234                                 (data_block->buffer + written - 1));
235           if (check != written)
236             {
237               ERROR ((0, errno, _("Only wrote %lu of %lu bytes to file %s"),
238                       (unsigned long) check,
239                       (unsigned long) written, current_file_name));
240               skip_file (size - written);
241               break;
242             }
243         }
244       if (multi_volume_option)
245         assign_string (&save_name, NULL);
246       fputc ('\n', stdlis);
247       fflush (stdlis);
248       return;
249
250     }
251
252   /* Check to see if we have an extended header to skip over also.  */
253
254   if (current_header->oldgnu_header.isextended)
255     isextended = 1;
256
257   /* Skip past the header in the archive.  */
258
259   set_next_block_after (current_header);
260
261   /* If we needed to skip any extended headers, do so now, by reading
262      extended headers and skipping past them in the archive.  */
263
264   if (isextended)
265     {
266 #if 0
267       union block *exhdr;
268
269       while (1)
270         {
271           exhdr = find_next_block ();
272
273           if (!exhdr->sparse_header.isextended)
274             {
275               set_next_block_after (exhdr);
276               break;
277             }
278           set_next_block_after (exhdr);
279         }
280 #endif
281       skip_extended_headers ();
282     }
283
284   if (multi_volume_option)
285     assign_string (&save_name, current_file_name);
286
287   /* Skip to the next header on the archive.  */
288
289   skip_file (current_stat.st_size);
290
291   if (multi_volume_option)
292     assign_string (&save_name, NULL);
293 }
294
295 /*-----------------------------------------------------------------------.
296 | Read a block that's supposed to be a header block.  Return its address |
297 | in "current_header", and if it is good, the file's size in             |
298 | current_stat.st_size.                                                  |
299 |                                                                        |
300 | Return 1 for success, 0 if the checksum is bad, EOF on eof, 2 for a    |
301 | block full of zeros (EOF marker).                                      |
302 |                                                                        |
303 | You must always set_next_block_after(current_header) to skip past the  |
304 | header which this routine reads.                                       |
305 `-----------------------------------------------------------------------*/
306
307 /* The standard BSD tar sources create the checksum by adding up the
308    bytes in the header as type char.  I think the type char was unsigned
309    on the PDP-11, but it's signed on the Next and Sun.  It looks like the
310    sources to BSD tar were never changed to compute the checksum
311    currectly, so both the Sun and Next add the bytes of the header as
312    signed chars.  This doesn't cause a problem until you get a file with
313    a name containing characters with the high bit set.  So read_header
314    computes two checksums -- signed and unsigned.  */
315
316 /* FIXME: The signed checksum computation is broken on machines where char's
317    are unsigned.  It's uneasy to handle all cases correctly...  */
318
319 enum read_header
320 read_header (void)
321 {
322   size_t i;
323   long unsigned_sum;            /* the POSIX one :-) */
324   long signed_sum;              /* the Sun one :-( */
325   long recorded_sum;
326   uintmax_t parsed_sum;
327   char *p;
328   union block *header;
329   char **longp;
330   char *bp;
331   union block *data_block;
332   size_t size, written;
333   static char *next_long_name, *next_long_link;
334
335   while (1)
336     {
337       header = find_next_block ();
338       current_header = header;
339       if (!header)
340         return HEADER_END_OF_FILE;
341
342       parsed_sum = from_oct (header->header.chksum,
343                              sizeof header->header.chksum,
344                              (char *) 0, TYPE_MAXIMUM (long));
345       if (parsed_sum == (uintmax_t) -1)
346         return HEADER_FAILURE;
347
348       recorded_sum = parsed_sum;
349       unsigned_sum = 0;
350       signed_sum = 0;
351       p = header->buffer;
352       for (i = sizeof (*header); i-- != 0;)
353         {
354           /* We can't use unsigned char here because of old compilers,
355              e.g. V7.  */
356
357           unsigned_sum += 0xFF & *p;
358           signed_sum += *p++;
359         }
360
361       /* Adjust checksum to count the "chksum" field as blanks.  */
362
363       for (i = sizeof (header->header.chksum); i-- != 0;)
364         {
365           unsigned_sum -= 0xFF & header->header.chksum[i];
366           signed_sum -= header->header.chksum[i];
367         }
368       unsigned_sum += ' ' * sizeof header->header.chksum;
369       signed_sum += ' ' * sizeof header->header.chksum;
370
371       if (unsigned_sum == sizeof header->header.chksum * ' ')
372         {
373           /* This is a zeroed block...whole block is 0's except for the
374              blanks we faked for the checksum field.  */
375
376           return HEADER_ZERO_BLOCK;
377         }
378
379       if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
380         return HEADER_FAILURE;
381
382       /* Good block.  Decode file size and return.  */
383
384       if (header->header.typeflag == LNKTYPE)
385         current_stat.st_size = 0;       /* links 0 size on tape */
386       else
387         current_stat.st_size = OFF_FROM_OCT (header->header.size);
388
389       header->header.name[NAME_FIELD_SIZE - 1] = '\0';
390       if (header->header.typeflag == GNUTYPE_LONGNAME
391           || header->header.typeflag == GNUTYPE_LONGLINK)
392         {
393           longp = ((header->header.typeflag == GNUTYPE_LONGNAME)
394                    ? &next_long_name
395                    : &next_long_link);
396
397           set_next_block_after (header);
398           if (*longp)
399             free (*longp);
400           size = current_stat.st_size;
401           if (size != current_stat.st_size)
402             FATAL_ERROR ((0, 0, _("Memory exhausted")));
403           bp = *longp = (char *) xmalloc (size);
404
405           for (; size > 0; size -= written)
406             {
407               data_block = find_next_block ();
408               if (data_block == NULL)
409                 {
410                   ERROR ((0, 0, _("Unexpected EOF on archive file")));
411                   break;
412                 }
413               written = available_space_after (data_block);
414               if (written > size)
415                 written = size;
416
417               memcpy (bp, data_block->buffer, written);
418               bp += written;
419               set_next_block_after ((union block *)
420                                     (data_block->buffer + written - 1));
421             }
422
423           /* Loop!  */
424
425         }
426       else
427         {
428           char *name = next_long_name;
429           struct posix_header *h = &current_header->header;
430           char namebuf[sizeof h->prefix + 1 + sizeof h->name + 1];
431
432           if (! name)
433             {
434               /* Accept file names as specified by POSIX.1-1996
435                  section 10.1.1.  */
436               char *np = namebuf;
437               if (h->prefix[0])
438                 {
439                   memcpy (np, h->prefix, sizeof h->prefix);
440                   np[sizeof h->prefix] = '\0';
441                   np += strlen (np);
442                   *np++ = '/';
443                 }
444               memcpy (np, h->name, sizeof h->name);
445               np[sizeof h->name] = '\0';
446               name = namebuf;
447             }
448
449           assign_string (&current_file_name, name);
450           assign_string (&current_link_name,
451                          (next_long_link ? next_long_link
452                           : current_header->header.linkname));
453           next_long_link = next_long_name = 0;
454           return HEADER_SUCCESS;
455         }
456     }
457 }
458
459 /*-------------------------------------------------------------------------.
460 | Decode things from a file HEADER block into STAT_INFO, also setting      |
461 | *FORMAT_POINTER depending on the header block format.  If DO_USER_GROUP, |
462 | decode the user/group information (this is useful for extraction, but    |
463 | waste time when merely listing).                                         |
464 |                                                                          |
465 | read_header() has already decoded the checksum and length, so we don't.  |
466 |                                                                          |
467 | This routine should *not* be called twice for the same block, since the  |
468 | two calls might use different DO_USER_GROUP values and thus might end up |
469 | with different uid/gid for the two calls.  If anybody wants the uid/gid  |
470 | they should decode it first, and other callers should decode it without  |
471 | uid/gid before calling a routine, e.g. print_header, that assumes        |
472 | decoded data.                                                            |
473 `-------------------------------------------------------------------------*/
474
475 void
476 decode_header (union block *header, struct stat *stat_info,
477                enum archive_format *format_pointer, int do_user_group)
478 {
479   enum archive_format format;
480
481   if (strcmp (header->header.magic, TMAGIC) == 0)
482     format = POSIX_FORMAT;
483   else if (strcmp (header->header.magic, OLDGNU_MAGIC) == 0)
484     format = OLDGNU_FORMAT;
485   else
486     format = V7_FORMAT;
487   *format_pointer = format;
488
489   stat_info->st_mode = MODE_FROM_OCT (header->header.mode);
490   stat_info->st_mode &= 07777;
491   stat_info->st_mtime = TIME_FROM_OCT (header->header.mtime);
492
493   if (format == OLDGNU_FORMAT && incremental_option)
494     {
495       stat_info->st_atime = TIME_FROM_OCT (header->oldgnu_header.atime);
496       stat_info->st_ctime = TIME_FROM_OCT (header->oldgnu_header.ctime);
497     }
498
499   if (format == V7_FORMAT)
500     {
501       stat_info->st_uid = UID_FROM_OCT (header->header.uid);
502       stat_info->st_gid = GID_FROM_OCT (header->header.gid);
503       stat_info->st_rdev = 0;
504     }
505   else
506     {
507       if (do_user_group)
508         {
509           /* FIXME: Decide if this should somewhat depend on -p.  */
510
511           if (numeric_owner_option
512               || !*header->header.uname
513               || !uname_to_uid (header->header.uname, &stat_info->st_uid))
514             stat_info->st_uid = UID_FROM_OCT (header->header.uid);
515
516           if (numeric_owner_option
517               || !*header->header.gname
518               || !gname_to_gid (header->header.gname, &stat_info->st_gid))
519             stat_info->st_gid = GID_FROM_OCT (header->header.gid);
520         }
521       switch (header->header.typeflag)
522         {
523 #ifdef S_IFBLK
524         case BLKTYPE:
525           stat_info->st_rdev
526             = makedev (MAJOR_FROM_OCT (header->header.devmajor),
527                        MINOR_FROM_OCT (header->header.devminor));
528           break;
529 #endif
530
531 #ifdef S_IFCHR
532         case CHRTYPE:
533           stat_info->st_rdev
534             = makedev (MAJOR_FROM_OCT (header->header.devmajor),
535                        MINOR_FROM_OCT (header->header.devminor));
536           break;
537 #endif
538
539         default:
540           stat_info->st_rdev = 0;
541         }
542     }
543 }
544
545 /*------------------------------------------------------------------------.
546 | Quick and dirty octal conversion.  Result is -1 if the field is invalid |
547 | (all blank, or nonoctal).                                               |
548 `------------------------------------------------------------------------*/
549
550 static uintmax_t
551 from_oct (const char *where0, size_t digs0, const char *type, uintmax_t maxval)
552 {
553   uintmax_t value;
554   const char *where = where0;
555   size_t digs = digs0;
556
557   for (;;)
558     {
559       if (digs == 0)
560         {
561           if (type)
562             ERROR ((0, 0, _("Blanks in header where octal %s value expected"),
563                     type));
564           return -1;
565         }
566       if (!ISSPACE (*where))
567         break;
568       where++;
569       digs--;
570     }
571
572   value = 0;
573   while (digs != 0 && ISODIGIT (*where))
574     {
575       /* Scan til nonoctal.  */
576
577       if (value << 3 >> 3 != value)
578         goto out_of_range;
579       value = (value << 3) | (*where++ - '0');
580       --digs;
581     }
582
583   if (digs != 0 && *where && !ISSPACE (*where))
584     {
585       if (type)
586         {
587           char buf[1000]; /* Big enough to represent any header.  */
588           static struct quoting_options *o;
589
590           if (!o)
591             {
592               o = clone_quoting_options ((struct quoting_options *) 0);
593               set_quoting_style (o, escape_quoting_style);
594             }
595
596           quotearg_buffer (buf, sizeof buf, where0, digs0, o);
597           ERROR ((0, 0,
598                   _("Header contains \"%.*s\" where octal %s value expected"),
599                   (int) sizeof buf, buf, type));
600         }
601
602       return -1;
603     }
604
605   if (value <= maxval)
606     return value;
607
608  out_of_range:
609   if (type)
610     ERROR ((0, 0, _("Octal value `%.*s' is out of range for %s"),
611             (int) digs0, where0, type));
612   return -1;
613 }
614 gid_t
615 gid_from_oct (const char *p, size_t s)
616 {
617   return from_oct (p, s, "gid_t", (uintmax_t) TYPE_MAXIMUM (gid_t));
618 }
619 major_t
620 major_from_oct (const char *p, size_t s)
621 {
622   return from_oct (p, s, "major_t", (uintmax_t) TYPE_MAXIMUM (major_t));
623 }
624 minor_t
625 minor_from_oct (const char *p, size_t s)
626 {
627   return from_oct (p, s, "minor_t", (uintmax_t) TYPE_MAXIMUM (minor_t));
628 }
629 mode_t
630 mode_from_oct (const char *p, size_t s)
631 {
632   return from_oct (p, s, "mode_t", (uintmax_t) TYPE_MAXIMUM (mode_t));
633 }
634 off_t
635 off_from_oct (const char *p, size_t s)
636 {
637   return from_oct (p, s, "off_t", (uintmax_t) TYPE_MAXIMUM (off_t));
638 }
639 size_t
640 size_from_oct (const char *p, size_t s)
641 {
642   return from_oct (p, s, "size_t", (uintmax_t) TYPE_MAXIMUM (size_t));
643 }
644 time_t
645 time_from_oct (const char *p, size_t s)
646 {
647   return from_oct (p, s, "time_t", (uintmax_t) TYPE_MAXIMUM (time_t));
648 }
649 uid_t
650 uid_from_oct (const char *p, size_t s)
651 {
652   return from_oct (p, s, "uid_t", (uintmax_t) TYPE_MAXIMUM (uid_t));
653 }
654 uintmax_t
655 uintmax_from_oct (const char *p, size_t s)
656 {
657   return from_oct (p, s, "uintmax_t", TYPE_MAXIMUM (uintmax_t));
658 }
659
660
661
662 /*----------------------------------------------------------------------.
663 | Format O as a null-terminated decimal string into BUF _backwards_;    |
664 | return pointer to start of result.                                    |
665 `----------------------------------------------------------------------*/
666 char *
667 stringify_uintmax_t_backwards (uintmax_t o, char *buf)
668 {
669   *--buf = '\0';
670   do
671     *--buf = '0' + (int) (o % 10);
672   while ((o /= 10) != 0);
673   return buf;
674 }
675
676 #if !USE_OLD_CTIME
677
678 /*-------------------------------------------.
679 | Return the time formatted along ISO 8601.  |
680 `-------------------------------------------*/
681
682 /* Also, see http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html.  */
683
684 static char *
685 isotime (const time_t *time)
686 {
687   static char buffer[21];
688   struct tm *tm;
689
690   tm = localtime (time);
691   sprintf (buffer, "%4d-%02d-%02d %02d:%02d:%02d\n",
692            tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
693            tm->tm_hour, tm->tm_min, tm->tm_sec);
694   return buffer;
695 }
696
697 #endif /* not USE_OLD_CTIME */
698
699 /*-------------------------------------------------------------------------.
700 | Decode MODE from its binary form in a stat structure, and encode it into |
701 | a 9 characters string STRING, terminated with a NUL.                     |
702 `-------------------------------------------------------------------------*/
703
704 static void
705 decode_mode (mode_t mode, char *string)
706 {
707   mode_t mask;
708   const char *rwx = "rwxrwxrwx";
709
710   for (mask = 0400; mask != 0; mask >>= 1)
711     if (mode & mask)
712       *string++ = *rwx++;
713     else
714       {
715         *string++ = '-';
716         rwx++;
717       }
718
719   if (mode & S_ISUID)
720     string[-7] = string[-7] == 'x' ? 's' : 'S';
721   if (mode & S_ISGID)
722     string[-4] = string[-4] == 'x' ? 's' : 'S';
723   if (mode & S_ISVTX)
724     string[-1] = string[-1] == 'x' ? 't' : 'T';
725
726   *string = '\0';
727 }
728
729 /*-------------------------------------------------------------------------.
730 | Actually print it.                                                       |
731 |                                                                          |
732 | Plain and fancy file header block logging.  Non-verbose just prints the  |
733 | name, e.g. for "tar t" or "tar x".  This should just contain file names, |
734 | so it can be fed back into tar with xargs or the "-T" option.  The       |
735 | verbose option can give a bunch of info, one line per file.  I doubt     |
736 | anybody tries to parse its format, or if they do, they shouldn't.  Unix  |
737 | tar is pretty random here anyway.                                        |
738 `-------------------------------------------------------------------------*/
739
740 /* FIXME: Note that print_header uses the globals HEAD, HSTAT, and
741    HEAD_STANDARD, which must be set up in advance.  Not very clean...  */
742
743 /* UGSWIDTH starts with 18, so with user and group names <= 8 chars, the
744    columns never shift during the listing.  */
745 #define UGSWIDTH 18
746 static int ugswidth = UGSWIDTH; /* maximum width encountered so far */
747
748 /* DATEWIDTH is the number of columns taken by the date and time fields.  */
749 #if USE_OLD_CDATE
750 # define DATEWIDTH 19
751 #else
752 # define DATEWIDTH 18
753 #endif
754
755 void
756 print_header (void)
757 {
758   char modes[11];
759   char *timestamp;
760   char uform[11], gform[11];    /* these hold formatted ints */
761   char *user, *group;
762   char size[2 * UINTMAX_STRSIZE_BOUND];
763                                 /* holds formatted size or major,minor */
764   char uintbuf[UINTMAX_STRSIZE_BOUND];
765   time_t longie;                /* to make ctime() call portable */
766   int pad;
767   char *name;
768
769   if (block_number_option)
770     {
771       char buf[UINTMAX_STRSIZE_BOUND];
772       fprintf (stdlis, _("block %s: "),
773                STRINGIFY_BIGINT (current_block_ordinal (), buf));
774     }
775
776   if (verbose_option <= 1)
777     {
778       /* Just the fax, mam.  */
779
780       char *quoted_name = quote_copy_string (current_file_name);
781
782       if (quoted_name)
783         {
784           fprintf (stdlis, "%s\n", quoted_name);
785           free (quoted_name);
786         }
787       else
788         fprintf (stdlis, "%s\n", current_file_name);
789     }
790   else
791     {
792       /* File type and modes.  */
793
794       modes[0] = '?';
795       switch (current_header->header.typeflag)
796         {
797         case GNUTYPE_VOLHDR:
798           modes[0] = 'V';
799           break;
800
801         case GNUTYPE_MULTIVOL:
802           modes[0] = 'M';
803           break;
804
805         case GNUTYPE_NAMES:
806           modes[0] = 'N';
807           break;
808
809         case GNUTYPE_LONGNAME:
810         case GNUTYPE_LONGLINK:
811           ERROR ((0, 0, _("Visible longname error")));
812           break;
813
814         case GNUTYPE_SPARSE:
815         case REGTYPE:
816         case AREGTYPE:
817         case LNKTYPE:
818           modes[0] = '-';
819           if (current_file_name[strlen (current_file_name) - 1] == '/')
820             modes[0] = 'd';
821           break;
822         case GNUTYPE_DUMPDIR:
823           modes[0] = 'd';
824           break;
825         case DIRTYPE:
826           modes[0] = 'd';
827           break;
828         case SYMTYPE:
829           modes[0] = 'l';
830           break;
831         case BLKTYPE:
832           modes[0] = 'b';
833           break;
834         case CHRTYPE:
835           modes[0] = 'c';
836           break;
837         case FIFOTYPE:
838           modes[0] = 'p';
839           break;
840         case CONTTYPE:
841           modes[0] = 'C';
842           break;
843         }
844
845       decode_mode (current_stat.st_mode, modes + 1);
846
847       /* Timestamp.  */
848
849       longie = current_stat.st_mtime;
850 #if USE_OLD_CTIME
851       timestamp = ctime (&longie);
852       timestamp[16] = '\0';
853       timestamp[24] = '\0';
854 #else
855       timestamp = isotime (&longie);
856       timestamp[16] = '\0';
857 #endif
858
859       /* User and group names.  */
860
861       if (*current_header->header.uname && current_format != V7_FORMAT)
862         user = current_header->header.uname;
863       else
864         user = STRINGIFY_BIGINT (UINTMAX_FROM_OCT (current_header->header.uid),
865                                  uform);
866
867       if (*current_header->header.gname && current_format != V7_FORMAT)
868         group = current_header->header.gname;
869       else
870         group = STRINGIFY_BIGINT (UINTMAX_FROM_OCT
871                                   (current_header->header.gid),
872                                   gform);
873
874       /* Format the file size or major/minor device numbers.  */
875
876       switch (current_header->header.typeflag)
877         {
878 #if defined(S_IFBLK) || defined(S_IFCHR)
879         case CHRTYPE:
880         case BLKTYPE:
881           sprintf (size, "%lu,%lu",
882                    (unsigned long) major (current_stat.st_rdev),
883                    (unsigned long) minor (current_stat.st_rdev));
884           break;
885 #endif
886         case GNUTYPE_SPARSE:
887           strcpy (size,
888                   STRINGIFY_BIGINT
889                   (UINTMAX_FROM_OCT (current_header->oldgnu_header.realsize),
890                    uintbuf));
891           break;
892         default:
893           strcpy (size, STRINGIFY_BIGINT (current_stat.st_size, uintbuf));
894           break;
895         }
896
897       /* Figure out padding and print the whole line.  */
898
899       pad = strlen (user) + strlen (group) + strlen (size) + 1;
900       if (pad > ugswidth)
901         ugswidth = pad;
902
903 #if USE_OLD_CTIME
904       fprintf (stdlis, "%s %s/%s %*s%s %s %s",
905                modes, user, group, ugswidth - pad, "",
906                size, timestamp + 4, timestamp + 20);
907 #else
908       fprintf (stdlis, "%s %s/%s %*s%s %s",
909                modes, user, group, ugswidth - pad, "", size, timestamp);
910 #endif
911
912       name = quote_copy_string (current_file_name);
913       if (name)
914         {
915           fprintf (stdlis, " %s", name);
916           free (name);
917         }
918       else
919         fprintf (stdlis, " %s", current_file_name);
920
921       switch (current_header->header.typeflag)
922         {
923         case SYMTYPE:
924           name = quote_copy_string (current_link_name);
925           if (name)
926             {
927               fprintf (stdlis, " -> %s\n", name);
928               free (name);
929             }
930           else
931             fprintf (stdlis, " -> %s\n", current_link_name);
932           break;
933
934         case LNKTYPE:
935           name = quote_copy_string (current_link_name);
936           if (name)
937             {
938               fprintf (stdlis, _(" link to %s\n"), name);
939               free (name);
940             }
941           else
942             fprintf (stdlis, _(" link to %s\n"), current_link_name);
943           break;
944
945         default:
946           fprintf (stdlis, _(" unknown file type `%c'\n"),
947                    current_header->header.typeflag);
948           break;
949
950         case AREGTYPE:
951         case REGTYPE:
952         case GNUTYPE_SPARSE:
953         case CHRTYPE:
954         case BLKTYPE:
955         case DIRTYPE:
956         case FIFOTYPE:
957         case CONTTYPE:
958         case GNUTYPE_DUMPDIR:
959           putc ('\n', stdlis);
960           break;
961
962         case GNUTYPE_VOLHDR:
963           fprintf (stdlis, _("--Volume Header--\n"));
964           break;
965
966         case GNUTYPE_MULTIVOL:
967           strcpy (size,
968                   STRINGIFY_BIGINT
969                   (UINTMAX_FROM_OCT (current_header->oldgnu_header.offset),
970                    uintbuf));
971           fprintf (stdlis, _("--Continued at byte %s--\n"), size);
972           break;
973
974         case GNUTYPE_NAMES:
975           fprintf (stdlis, _("--Mangled file names--\n"));
976           break;
977         }
978     }
979   fflush (stdlis);
980 }
981
982 /*--------------------------------------------------------------.
983 | Print a similar line when we make a directory automatically.  |
984 `--------------------------------------------------------------*/
985
986 void
987 print_for_mkdir (char *pathname, int length, mode_t mode)
988 {
989   char modes[11];
990   char *name;
991
992   if (verbose_option > 1)
993     {
994       /* File type and modes.  */
995
996       modes[0] = 'd';
997       decode_mode (mode, modes + 1);
998
999       if (block_number_option)
1000         {
1001           char buf[UINTMAX_STRSIZE_BOUND];
1002           fprintf (stdlis, _("block %s: "),
1003                    STRINGIFY_BIGINT (current_block_ordinal (), buf));
1004         }
1005       name = quote_copy_string (pathname);
1006       if (name)
1007         {
1008           fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + DATEWIDTH,
1009                    _("Creating directory:"), length, name);
1010           free (name);
1011         }
1012       else
1013         fprintf (stdlis, "%s %*s %.*s\n", modes, ugswidth + DATEWIDTH,
1014                  _("Creating directory:"), length, pathname);
1015     }
1016 }
1017
1018 /*--------------------------------------------------------.
1019 | Skip over SIZE bytes of data in blocks in the archive.  |
1020 `--------------------------------------------------------*/
1021
1022 void
1023 skip_file (off_t size)
1024 {
1025   union block *x;
1026
1027   if (multi_volume_option)
1028     {
1029       save_totsize = size;
1030       save_sizeleft = size;
1031     }
1032
1033   while (size > 0)
1034     {
1035       x = find_next_block ();
1036       if (x == NULL)
1037         FATAL_ERROR ((0, 0, _("Unexpected EOF on archive file")));
1038
1039       set_next_block_after (x);
1040       size -= BLOCKSIZE;
1041       if (multi_volume_option)
1042         save_sizeleft -= BLOCKSIZE;
1043     }
1044 }
1045
1046 /*---.
1047 | ?  |
1048 `---*/
1049
1050 void
1051 skip_extended_headers (void)
1052 {
1053   union block *exhdr;
1054
1055   while (1)
1056     {
1057       exhdr = find_next_block ();
1058       if (!exhdr->sparse_header.isextended)
1059         {
1060           set_next_block_after (exhdr);
1061           break;
1062         }
1063       set_next_block_after (exhdr);
1064     }
1065 }