]> Dogcows Code - chaz/tar/blobdiff - src/list.c
spelling fix
[chaz/tar] / src / list.c
index 1c245319cd57ff0c4147137eb38676d3d6a6a824..5067a86c816341573c78f519c4bfe1c9abcf02ff 100644 (file)
@@ -26,6 +26,8 @@
 
 #include "common.h"
 
+#define max(a, b) ((a) < (b) ? (b) : (a))
+
 union block *current_header;   /* points to current archive header */
 struct stat current_stat;      /* stat struct corresponding */
 enum archive_format current_format; /* recognized format */
@@ -55,7 +57,6 @@ read_and (void (*do_something) ())
 {
   enum read_header status = HEADER_STILL_UNREAD;
   enum read_header prev_status;
-  char save_typeflag;
 
   base64_init ();
   name_gather ();
@@ -83,7 +84,7 @@ read_and (void (*do_something) ())
              || current_stat.st_mtime < newer_mtime_option
              || excluded_name (current_file_name))
            {
-             int isextended = 0;
+             char save_typeflag;
 
              if (current_header->header.typeflag == GNUTYPE_VOLHDR
                  || current_header->header.typeflag == GNUTYPE_MULTIVOL
@@ -98,28 +99,10 @@ read_and (void (*do_something) ())
 
              /* Skip past it in the archive.  */
 
-             if (current_header->oldgnu_header.isextended)
-               isextended = 1;
              save_typeflag = current_header->header.typeflag;
              set_next_block_after (current_header);
-             if (isextended)
-               {
-#if 0
-                 union block *exhdr;
-
-                 while (1)
-                   {
-                     exhdr = find_next_block ();
-                     if (!exhdr->sparse_header.isextended)
-                       {
-                         set_next_block_after (exhdr);
-                         break;
-                       }
-                   }
-                 set_next_block_after (exhdr);
-#endif
-                 skip_extended_headers ();
-               }
+             if (current_header->oldgnu_header.isextended)
+               skip_extended_headers ();
 
              /* Skip to the next header on the archive.  */
 
@@ -191,8 +174,6 @@ read_and (void (*do_something) ())
 void
 list_archive (void)
 {
-  int isextended = 0;          /* to remember if current_header is extended */
-
   /* Print the header block.  */
 
   if (verbose_option)
@@ -241,44 +222,18 @@ list_archive (void)
            }
        }
       if (multi_volume_option)
-       assign_string (&save_name, NULL);
+       assign_string (&save_name, 0);
       fputc ('\n', stdlis);
       fflush (stdlis);
       return;
 
     }
 
-  /* Check to see if we have an extended header to skip over also.  */
-
-  if (current_header->oldgnu_header.isextended)
-    isextended = 1;
-
-  /* Skip past the header in the archive.  */
+  /* Skip past the header in the archive, and past any extended headers.  */
 
   set_next_block_after (current_header);
-
-  /* If we needed to skip any extended headers, do so now, by reading
-     extended headers and skipping past them in the archive.  */
-
-  if (isextended)
-    {
-#if 0
-      union block *exhdr;
-
-      while (1)
-       {
-         exhdr = find_next_block ();
-
-         if (!exhdr->sparse_header.isextended)
-           {
-             set_next_block_after (exhdr);
-             break;
-           }
-         set_next_block_after (exhdr);
-       }
-#endif
-      skip_extended_headers ();
-    }
+  if (current_header->oldgnu_header.isextended)
+    skip_extended_headers ();
 
   if (multi_volume_option)
     assign_string (&save_name, current_file_name);
@@ -288,7 +243,7 @@ list_archive (void)
   skip_file (current_stat.st_size);
 
   if (multi_volume_option)
-    assign_string (&save_name, NULL);
+    assign_string (&save_name, 0);
 }
 
 /*-----------------------------------------------------------------------.
@@ -307,21 +262,18 @@ list_archive (void)
    bytes in the header as type char.  I think the type char was unsigned
    on the PDP-11, but it's signed on the Next and Sun.  It looks like the
    sources to BSD tar were never changed to compute the checksum
-   currectly, so both the Sun and Next add the bytes of the header as
+   correctly, so both the Sun and Next add the bytes of the header as
    signed chars.  This doesn't cause a problem until you get a file with
    a name containing characters with the high bit set.  So read_header
    computes two checksums -- signed and unsigned.  */
 
-/* FIXME: The signed checksum computation is broken on machines where char's
-   are unsigned.  It's uneasy to handle all cases correctly...  */
-
 enum read_header
 read_header (void)
 {
   size_t i;
-  long unsigned_sum;           /* the POSIX one :-) */
-  long signed_sum;             /* the Sun one :-( */
-  long recorded_sum;
+  int unsigned_sum;            /* the POSIX one :-) */
+  int signed_sum;              /* the Sun one :-( */
+  int recorded_sum;
   uintmax_t parsed_sum;
   char *p;
   union block *header;
@@ -338,43 +290,35 @@ read_header (void)
       if (!header)
        return HEADER_END_OF_FILE;
 
-      parsed_sum = from_chars (header->header.chksum,
-                              sizeof header->header.chksum,
-                              (char *) 0, (uintmax_t) 0,
-                              (uintmax_t) TYPE_MAXIMUM (long));
-      if (parsed_sum == (uintmax_t) -1)
-       return HEADER_FAILURE;
-
-      recorded_sum = parsed_sum;
       unsigned_sum = 0;
       signed_sum = 0;
       p = header->buffer;
-      for (i = sizeof (*header); i-- != 0;)
+      for (i = sizeof *header; i-- != 0;)
        {
-         /* We can't use unsigned char here because of old compilers,
-            e.g. V7.  */
-
-         unsigned_sum += 0xFF & *p;
-         signed_sum += *p++;
+         unsigned_sum += (unsigned char) *p;
+         signed_sum += signed_char (*p++);
        }
 
+      if (unsigned_sum == 0)
+       return HEADER_ZERO_BLOCK;
+
       /* Adjust checksum to count the "chksum" field as blanks.  */
 
-      for (i = sizeof (header->header.chksum); i-- != 0;)
+      for (i = sizeof header->header.chksum; i-- != 0;)
        {
-         unsigned_sum -= 0xFF & header->header.chksum[i];
-         signed_sum -= header->header.chksum[i];
+         unsigned_sum -= (unsigned char) header->header.chksum[i];
+         signed_sum -= signed_char (header->header.chksum[i]);
        }
       unsigned_sum += ' ' * sizeof header->header.chksum;
       signed_sum += ' ' * sizeof header->header.chksum;
 
-      if (unsigned_sum == sizeof header->header.chksum * ' ')
-       {
-         /* This is a zeroed block...whole block is 0's except for the
-            blanks we faked for the checksum field.  */
+      parsed_sum = from_chars (header->header.chksum,
+                              sizeof header->header.chksum, 0,
+                              (uintmax_t) 0, (uintmax_t) TYPE_MAXIMUM (int));
+      if (parsed_sum == (uintmax_t) -1)
+       return HEADER_FAILURE;
 
-         return HEADER_ZERO_BLOCK;
-       }
+      recorded_sum = parsed_sum;
 
       if (unsigned_sum != recorded_sum && signed_sum != recorded_sum)
        return HEADER_FAILURE;
@@ -399,12 +343,12 @@ read_header (void)
          size = current_stat.st_size;
          if (size != current_stat.st_size)
            FATAL_ERROR ((0, 0, _("Memory exhausted")));
-         bp = *longp = (char *) xmalloc (size);
+         bp = *longp = xmalloc (size);
 
          for (; size > 0; size -= written)
            {
              data_block = find_next_block ();
-             if (data_block == NULL)
+             if (! data_block)
                {
                  ERROR ((0, 0, _("Unexpected EOF on archive file")));
                  break;
@@ -448,6 +392,11 @@ read_header (void)
              name = namebuf;
            }
          assign_string (&current_file_name, name);
+         if (next_long_name)
+           {
+             free (next_long_name);
+             next_long_name = 0;
+           }
          
          name = next_long_link;
          if (! name)
@@ -457,8 +406,12 @@ read_header (void)
              name = namebuf;
            }
          assign_string (&current_link_name, name);
+         if (next_long_link)
+           {
+             free (next_long_link);
+             next_long_link = 0;
+           }
 
-         next_long_link = next_long_name = 0;
          return HEADER_SUCCESS;
        }
     }
@@ -560,6 +513,11 @@ from_chars (char const *where0, size_t digs, char const *type,
   char const *lim = where + digs;
   int negative = 0;
 
+  /* Accommodate buggy tar of unknown vintage, which outputs leading
+     NUL if the previous field overflows.  */
+  where += !*where;
+
+  /* Accommodate older tars, which output leading spaces.  */
   for (;;)
     {
       if (where == lim)
@@ -581,7 +539,12 @@ from_chars (char const *where0, size_t digs, char const *type,
       do
        {
          if (value << LG_8 >> LG_8 != value)
-           goto out_of_range;
+           {
+             ERROR ((0, 0,
+                     _("Archive octal string `%.*s' is out of %s range"),
+                     (int) digs, where0, type));
+             return -1;
+           }
          value = (value << LG_8) | (*where++ - '0');
        }
       while (where != lim && ISODIGIT (*where));
@@ -604,7 +567,12 @@ from_chars (char const *where0, size_t digs, char const *type,
             && (dig = base64_map[(unsigned char) *where]) < 64)
        {
          if (value << LG_64 >> LG_64 != value)
-           goto out_of_range;
+           {
+             ERROR ((0, 0,
+                     _("Archive signed base 64 string `%.*s' is out of %s range"),
+                     (int) digs, where0, type));
+             return -1;
+           }
          value = (value << LG_64) | dig;
          where++;
        }
@@ -619,7 +587,7 @@ from_chars (char const *where0, size_t digs, char const *type,
 
          if (!o)
            {
-             o = clone_quoting_options ((struct quoting_options *) 0);
+             o = clone_quoting_options (0);
              set_quoting_style (o, c_quoting_style);
            }
 
@@ -627,7 +595,7 @@ from_chars (char const *where0, size_t digs, char const *type,
            lim--;
          quotearg_buffer (buf, sizeof buf, where0, lim - where, o);
          ERROR ((0, 0,
-                 _("Header contains `%.*s' where numeric %s value expected"),
+                 _("Archive contains `%.*s' where numeric %s value expected"),
                  (int) sizeof buf, buf, type));
        }
 
@@ -637,10 +605,22 @@ from_chars (char const *where0, size_t digs, char const *type,
   if (value <= (negative ? minus_minval : maxval))
     return negative ? -value : value;
 
- out_of_range:
   if (type)
-    ERROR ((0, 0, _("Numeric value `%.*s' is out of range for %s"),
-           (int) digs, where0, type));
+    {
+      char minval_buf[UINTMAX_STRSIZE_BOUND + 1];
+      char maxval_buf[UINTMAX_STRSIZE_BOUND];
+      char value_buf[UINTMAX_STRSIZE_BOUND + 1];
+      char *minval_string = STRINGIFY_BIGINT (minus_minval, minval_buf + 1);
+      char *value_string = STRINGIFY_BIGINT (value, value_buf + 1);
+      if (negative)
+       *--value_string = '-';
+      if (minus_minval)
+       *--minval_string = '-';
+      ERROR ((0, 0, _("Archive value %s is out of %s range %s..%s"),
+             value_string, type,
+             minval_string, STRINGIFY_BIGINT (maxval, maxval_buf)));
+    }
+
   return -1;
 }
 
@@ -692,8 +672,9 @@ mode_from_chars (const char *p, size_t s)
 off_t
 off_from_chars (const char *p, size_t s)
 {
-  return from_chars (p, s, "off_t",
-                    - (uintmax_t) TYPE_MINIMUM (off_t),
+  /* Negative offsets are not allowed in tar files, so invoke
+     from_chars with minimum value 0, not TYPE_MINIMUM (off_t).  */
+  return from_chars (p, s, "off_t", (uintmax_t) 0,
                     (uintmax_t) TYPE_MAXIMUM (off_t));
 }
 
@@ -715,7 +696,8 @@ time_from_chars (const char *p, size_t s)
 uid_t
 uid_from_chars (const char *p, size_t s)
 {
-  return from_chars (p, s, "uid_t", (uintmax_t) 0,
+  return from_chars (p, s, "uid_t",
+                    - (uintmax_t) TYPE_MINIMUM (uid_t),
                     (uintmax_t) TYPE_MAXIMUM (uid_t));
 }
 
@@ -749,20 +731,34 @@ stringify_uintmax_t_backwards (uintmax_t o, char *buf)
 
 /* Also, see http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html.  */
 
-static char *
-isotime (const time_t *time)
+static char const *
+isotime (time_t time)
 {
-  static char buffer[INT_STRLEN_BOUND (int) + 16];
-  struct tm *tm = localtime (time);
+  static char buffer[max (UINTMAX_STRSIZE_BOUND + 1,
+                         INT_STRLEN_BOUND (int) + 16)];
+  struct tm *tm = localtime (&time);
   if (tm)
-    sprintf (buffer, "%04d-%02d-%02d %02d:%02d:%02d",
-            tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
-            tm->tm_hour, tm->tm_min, tm->tm_sec);
+    {
+      sprintf (buffer, "%04d-%02d-%02d %02d:%02d:%02d",
+              tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+              tm->tm_hour, tm->tm_min, tm->tm_sec);
+      return buffer;
+    }
   else
-    /* Interpose %s between ?? and - to avoid ANSI C trigraph brain damage.  */
-    sprintf (buffer, "????%s-??%s-?? ??:??:??", "", "");
-
-  return buffer;
+    {
+      /* The time stamp cannot be broken down, most likely because it
+        is out of range.  Convert it as an integer,
+        right-adjusted in a field with the same width as the usual
+        19-byte 4-year ISO time format.  */
+      uintmax_t abstime = time < 0 ? - (uintmax_t) time : time;
+      char *p = stringify_uintmax_t_backwards (abstime,
+                                              buffer + sizeof buffer);
+      if (time < 0)
+       *--p = '-';
+      while (buffer + sizeof buffer - 19 - 1 < p)
+       *--p = ' ';
+      return p;
+    }
 }
 
 #endif /* not USE_OLD_CTIME */
@@ -823,7 +819,7 @@ void
 print_header (void)
 {
   char modes[11];
-  char const *timestamp;
+  char const *time_stamp;
   /* These hold formatted ints.  */
   char uform[UINTMAX_STRSIZE_BOUND], gform[UINTMAX_STRSIZE_BOUND];
   char *user, *group;
@@ -912,7 +908,7 @@ print_header (void)
 
       decode_mode (current_stat.st_mode, modes + 1);
 
-      /* Timestamp.  */
+      /* Time stamp.  */
 
       longie = current_stat.st_mtime;
 #if USE_OLD_CTIME
@@ -920,16 +916,16 @@ print_header (void)
        char *ct = ctime (&longie);
        if (ct)
          {
-           timestamp = ct + 4;
+           time_stamp = ct + 4;
            for (ct += 16; ct[4] != '\n'; ct++)
              ct[0] = ct[4];
            ct[0] = '\0';
          }
        else
-         timestamp = "??? ?? ??:?? ????";
+         time_stamp = "??? ?? ??:?? ????";
       }
 #else
-      timestamp = isotime (&longie);
+      time_stamp = isotime (longie);
 #endif
 
       /* User and group names.  */
@@ -956,9 +952,11 @@ print_header (void)
        {
        case CHRTYPE:
        case BLKTYPE:
-         sprintf (size, "%lu,%lu",
-                  (unsigned long) major (current_stat.st_rdev),
-                  (unsigned long) minor (current_stat.st_rdev));
+         strcpy (size,
+                 STRINGIFY_BIGINT (major (current_stat.st_rdev), uintbuf));
+         strcat (size, ",");
+         strcat (size,
+                 STRINGIFY_BIGINT (minor (current_stat.st_rdev), uintbuf));
          break;
        case GNUTYPE_SPARSE:
          strcpy (size,
@@ -978,7 +976,7 @@ print_header (void)
        ugswidth = pad;
 
       fprintf (stdlis, "%s %s/%s %*s%s %s",
-              modes, user, group, ugswidth - pad, "", size, timestamp);
+              modes, user, group, ugswidth - pad, "", size, time_stamp);
 
       name = quote_copy_string (current_file_name);
       if (name)
@@ -1104,7 +1102,7 @@ skip_file (off_t size)
   while (size > 0)
     {
       x = find_next_block ();
-      if (x == NULL)
+      if (! x)
        FATAL_ERROR ((0, 0, _("Unexpected EOF on archive file")));
 
       set_next_block_after (x);
@@ -1123,14 +1121,12 @@ skip_extended_headers (void)
 {
   union block *exhdr;
 
-  while (1)
+  do
     {
       exhdr = find_next_block ();
-      if (!exhdr->sparse_header.isextended)
-       {
-         set_next_block_after (exhdr);
-         break;
-       }
+      if (!exhdr)
+       FATAL_ERROR ((0, 0, _("Unexpected EOF on archive file")));
       set_next_block_after (exhdr);
     }
+  while (exhdr->sparse_header.isextended);
 }
This page took 0.034249 seconds and 4 git commands to generate.