]> Dogcows Code - chaz/tar/blobdiff - src/create.c
Fix copyright notice.
[chaz/tar] / src / create.c
index 0d7e67e0a719cde68a42949e6ba28b4c958e9637..7c2b49f13f588e82f445f72ec5d846953a33e3c7 100644 (file)
@@ -14,7 +14,7 @@
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
-   59 Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #include "system.h"
 
@@ -65,9 +65,11 @@ struct link *linklist = NULL;        /* points to first link in list */
 | fact is used by start_header and finish_header, so don't change it!     |
 `------------------------------------------------------------------------*/
 
-/* This should be equivalent to: sprintf (WHERE, "%*lo", SIZE, VALUE);
-   except that we don't assume VALUE fits in an unsigned long, and
-   except that sprintf fills in the trailing NUL and we don't.  */
+/* Output VALUE in octal, using SUBSTITUTE if value won't fit.
+   Output to buffer WHERE with size SIZE.
+   TYPE is the kind of value being output (useful for diagnostics).
+   Prefer SIZE - 1 octal digits (with leading '0's), followed by '\0';
+   but if SIZE octal digits would fit, omit the '\0'.  */
 
 static void
 to_oct (uintmax_t value, uintmax_t substitute, char *where, size_t size, const char *type)
@@ -75,6 +77,15 @@ to_oct (uintmax_t value, uintmax_t substitute, char *where, size_t size, const c
   uintmax_t v = value;
   size_t i = size;
 
+# define MAX_OCTAL_VAL_WITH_DIGITS(digits) \
+    ((digits) * 3 < sizeof (uintmax_t) * CHAR_BIT \
+     ? ((uintmax_t) 1 << ((digits) * 3)) - 1 \
+     : (uintmax_t) -1)
+
+  /* Output a trailing NUL unless the value is too large.  */
+  if (value <= MAX_OCTAL_VAL_WITH_DIGITS (size - 1))
+    where[--i] = '\0';
+
   /* Produce the digits -- at least one.  */
 
   do
@@ -84,16 +95,13 @@ to_oct (uintmax_t value, uintmax_t substitute, char *where, size_t size, const c
     }
   while (i != 0 && v != 0);
 
-  /* Leading spaces, if necessary.  */
+  /* Leading zeros, if necessary.  */
   while (i != 0)
-    where[--i] = ' ';
+    where[--i] = '0';
 
   if (v != 0)
     {
-      int bits = size * 3;
-      uintmax_t maxval = (bits < sizeof (uintmax_t) * CHAR_BIT
-                         ? ((uintmax_t) 1 << bits) - 1
-                         : (uintmax_t) -1);
+      uintmax_t maxval = MAX_OCTAL_VAL_WITH_DIGITS (size);
       char buf1[UINTMAX_STRSIZE_BOUND];
       char buf2[UINTMAX_STRSIZE_BOUND];
       char buf3[UINTMAX_STRSIZE_BOUND];
@@ -395,7 +403,7 @@ finish_header (union block *header)
 
   /* Fill in the checksum field.  It's formatted differently from the
      other fields: it has [6] digits, a null, then a space -- rather than
-     digits, then a null.  We use to_oct then write the null.
+     digits, then a null.  We use to_oct.
      The final space is already there, from checksumming,
      and to_oct doesn't modify it.
 
@@ -403,8 +411,7 @@ finish_header (union block *header)
 
      sprintf(header->header.chksum, "%6o", sum);  */
 
-  uintmax_to_oct ((uintmax_t) sum, header->header.chksum, 6);
-  header->header.chksum[6] = '\0';
+  uintmax_to_oct ((uintmax_t) sum, header->header.chksum, 7);
 
   set_next_block_after (header);
 
@@ -511,7 +518,7 @@ deal_with_sparse (char *name, union block *header)
   init_sparsearray ();
   clear_buffer (buffer);
 
-  while (count = read (file, buffer, sizeof buffer), count != 0)
+  while (count = full_read (file, buffer, sizeof buffer), count != 0)
     {
       /* Realloc the scratch area as necessary.  FIXME: should reallocate
         only at beginning of a new instance of non-zero data.  */
@@ -630,8 +637,8 @@ finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name)
 #if 0
          if (amount_read)
            {
-             count = read (file, start->buffer + amount_read,
-                           BLOCKSIZE - amount_read);
+             count = full_read (file, start->buffer + amount_read,
+                                BLOCKSIZE - amount_read);
              bufsize -= BLOCKSIZE - amount_read;
              amount_read = 0;
              set_next_block_after (start);
@@ -641,7 +648,7 @@ finish_sparse_file (int file, off_t *sizeleft, off_t fullsize, char *name)
 #endif
          /* Store the data.  */
 
-         count = read (file, start->buffer, BLOCKSIZE);
+         count = full_read (file, start->buffer, BLOCKSIZE);
          if (count < 0)
            {
              char buf[UINTMAX_STRSIZE_BOUND];
@@ -662,7 +669,7 @@ Read error at byte %s, reading %lu bytes, in file %s"),
        char buffer[BLOCKSIZE];
 
        clear_buffer (buffer);
-       count = read (file, buffer, bufsize);
+       count = full_read (file, buffer, bufsize);
        memcpy (start->buffer, buffer, BLOCKSIZE);
       }
 
@@ -1166,7 +1173,7 @@ Removing leading `/' from absolute links")));
            if (f < 0)
              count = bufsize;
            else
-             count = read (f, start->buffer, bufsize);
+             count = full_read (f, start->buffer, bufsize);
            if (count < 0)
              {
                char buf[UINTMAX_STRSIZE_BOUND];
This page took 0.024109 seconds and 4 git commands to generate.