]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Trying to figure out what printf modifiers to use on what platforms to
authorTim Kientzle <kientzle@gmail.com>
Sun, 1 Nov 2009 02:14:14 +0000 (22:14 -0400)
committerTim Kientzle <kientzle@gmail.com>
Sun, 1 Nov 2009 02:14:14 +0000 (22:14 -0400)
display 64-bit values is getting to be  real PITA.  Just roll our
own conversion and use that.

SVN-Revision: 1562

tar/bsdtar.h
tar/bsdtar_platform.h
tar/read.c
tar/util.c
tar/write.c

index ff1aff5661004d3f9d83289fa394b230754b8cee..5e8dc7f89ec57b57f1a3911533d9c9434ee8259e 100644 (file)
@@ -142,6 +142,7 @@ int need_report(void);
 int    pathcmp(const char *a, const char *b);
 void   safe_fprintf(FILE *, const char *fmt, ...);
 void   set_chdir(struct bsdtar *, const char *newdir);
+const char *tar_i64toa(int64_t);
 void   tar_mode_c(struct bsdtar *bsdtar);
 void   tar_mode_r(struct bsdtar *bsdtar);
 void   tar_mode_t(struct bsdtar *bsdtar);
index 6276839243aead343a6089fb1c716a5543afe17d..d863913372d88cd92c884b7450623deed249406a 100644 (file)
 # endif
 #endif
 
-
-/*
- * We need to be able to display a filesize using printf().  The type
- * and format string here must be compatible with one another and
- * large enough for any file.
- */
-#if HAVE_UINTMAX_T
-#define        BSDTAR_FILESIZE_TYPE    uintmax_t
-#define        BSDTAR_FILESIZE_PRINTF  "%ju"
-#else
-#if HAVE_UNSIGNED_LONG_LONG
-#define        BSDTAR_FILESIZE_TYPE    unsigned long long
-#define        BSDTAR_FILESIZE_PRINTF  "%llu"
-#else
-#define        BSDTAR_FILESIZE_TYPE    unsigned long
-#define        BSDTAR_FILESIZE_PRINTF  "%lu"
-#endif
-#endif
-
 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
 #define        ARCHIVE_STAT_CTIME_NANOS(st)    (st)->st_ctimespec.tv_nsec
 #define        ARCHIVE_STAT_MTIME_NANOS(st)    (st)->st_mtimespec.tv_nsec
index c5a8718c276c32f82cd6e2dd67eaa6e60357512a..9816ed3590faae6323efbde664fc0851ca722c56 100644 (file)
@@ -99,7 +99,7 @@ progress_func(void *cookie)
        struct bsdtar *bsdtar = progress_data->bsdtar;
        struct archive *a = progress_data->archive;
        struct archive_entry *entry = progress_data->entry;
-       uintmax_t comp, uncomp;
+       uint64_t comp, uncomp;
 
        if (!need_report())
                return;
@@ -110,16 +110,16 @@ progress_func(void *cookie)
                comp = archive_position_compressed(a);
                uncomp = archive_position_uncompressed(a);
                fprintf(stderr,
-                   "In: %ju bytes, compression %d%%;",
-                   comp, (int)((uncomp - comp) * 100 / uncomp));
-               fprintf(stderr, "  Out: %d files, %ju bytes\n",
-                   archive_file_count(a), uncomp);
+                   "In: %s bytes, compression %d%%;",
+                   tar_i64toa(comp), (int)((uncomp - comp) * 100 / uncomp));
+               fprintf(stderr, "  Out: %d files, %s bytes\n",
+                   archive_file_count(a), tar_i64toa(uncomp));
        }
        if (entry != NULL) {
-               safe_fprintf(stderr, "Current: %s (%ju bytes)",
-                   archive_entry_pathname(entry),
-                   (uintmax_t)archive_entry_size(entry));
-               fprintf(stderr, "\n");
+               safe_fprintf(stderr, "Current: %s",
+                   archive_entry_pathname(entry));
+               fprintf(stderr, " (%s bytes)\n",
+                   tar_i64toa(archive_entry_size(entry)));
        }
 }
 
@@ -403,13 +403,7 @@ list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
                    (unsigned long)archive_entry_rdevmajor(entry),
                    (unsigned long)archive_entry_rdevminor(entry));
        } else {
-               /*
-                * Note the use of platform-dependent macros to format
-                * the filesize here.  We need the format string and the
-                * corresponding type for the cast.
-                */
-               sprintf(tmp, BSDTAR_FILESIZE_PRINTF,
-                   (BSDTAR_FILESIZE_TYPE)archive_entry_size(entry));
+               strcpy(tmp, tar_i64toa(archive_entry_size(entry)));
        }
        if (w + strlen(tmp) >= bsdtar->gs_width)
                bsdtar->gs_width = w+strlen(tmp)+1;
index a55692384bdbd11e31f7b09242347fa19a9d2efc..2b0751aea0f8822f44c9f1a7d5f655c799b1ddc9 100644 (file)
@@ -487,6 +487,28 @@ edit_pathname(struct bsdtar *bsdtar, struct archive_entry *entry)
        return (0);
 }
 
+/*
+ * It would be nice to just use printf() for formatting large numbers,
+ * but the compatibility problems are quite a headache.  Hence the
+ * following simple utility function.
+ */
+const char *
+tar_i64toa(int64_t n0)
+{
+       static char buff[24];
+       int64_t n = n0 < 0 ? -n0 : n0;
+       char *p = buff + sizeof(buff);
+
+       *--p = '\0';
+       do {
+               *--p = '0' + (n % 10);
+               n /= 10;
+       } while (n > 0);
+       if (n0 < 0)
+               *--p = '-';
+       return p;
+}
+
 /*
  * Like strcmp(), but try to be a little more aware of the fact that
  * we're comparing two paths.  Right now, it just handles leading
index 1d41bb36fdee309253a315307df79d4afe0b67eb..d4f20bca020ff0a6a647547bb7f58d2f7a21c40f 100644 (file)
@@ -498,8 +498,8 @@ cleanup:
        bsdtar->diskreader = NULL;
 
        if (bsdtar->option_totals) {
-               fprintf(stderr, "Total bytes written: " BSDTAR_FILESIZE_PRINTF "\n",
-                   (BSDTAR_FILESIZE_TYPE)archive_position_compressed(a));
+               fprintf(stderr, "Total bytes written: %s\n",
+                   tar_i64toa(archive_position_compressed(a)));
        }
 
        archive_write_finish(a);
@@ -956,21 +956,22 @@ static void
 report_write(struct bsdtar *bsdtar, struct archive *a,
     struct archive_entry *entry, int64_t progress)
 {
-       uintmax_t comp, uncomp;
+       uint64_t comp, uncomp;
        if (bsdtar->verbose)
                fprintf(stderr, "\n");
        comp = archive_position_compressed(a);
        uncomp = archive_position_uncompressed(a);
-       fprintf(stderr, "In: %d files, %ju bytes;",
-           archive_file_count(a), uncomp);
+       fprintf(stderr, "In: %d files, %s bytes;",
+           archive_file_count(a), tar_i64toa(uncomp));
        fprintf(stderr,
-           " Out: %ju bytes, compression %d%%\n",
-           comp, (int)((uncomp - comp) * 100 / uncomp));
-       safe_fprintf(stderr, "Current: %s (%ju/%ju bytes)",
+           " Out: %s bytes, compression %d%%\n",
+           tar_i64toa(comp), (int)((uncomp - comp) * 100 / uncomp));
+       /* Can't have two calls to tar_i64toa() pending, so split the output. */
+       safe_fprintf(stderr, "Current: %s (%s",
            archive_entry_pathname(entry),
-           (uintmax_t)progress,
-           (uintmax_t)archive_entry_size(entry));
-       fprintf(stderr, "\n");
+           tar_i64toa(progress));
+       fprintf(stderr, "/%s bytes)\n",
+           tar_i64toa(archive_entry_size(entry)));
 }