<<< Date Index >>>     <<< Thread Index >>>

Compatibility problem when calling mutt_pretty_size



Hi,

The third parameter to function mutt_pretty_size, defined as long, gets
a size argument.  From browser.c, function folder_format_str,
mutt_pretty_size is called with the st_size member of struct stat.  Per
POSIX, the st_size member is defined as off_t, and using long for file
sizes is not portable.  On some systems, off_t is defined as a 64 bit
type, while long is 32 bit.  This is the case for instance on Cygwin.
Or, on 32 bit Linux with -D_FILE_OFFSET_BITS=64.

Wouldn't it be better to change the third parameter to mutt_pretty_size
as an intmax_t and use the 'j' format specifier in calls to printf?  Ok,
I admit that mailbox sizes of more than 2 Gigs are rather seldom...

If you don't want to rely on intmax_t and the 'j' specifier being
available, there would also be the alternative to define the third
parameter as long long and use the lld format specifier.

The below patch, implementing the intmax_t approach, is against the
latest snapshot (20071008).

ChangeLog:

        * muttlib.c: Change third parameter of mutt_pretty_size to intmax_t.
        * protos.h: Change prototype accordingly.

--- muttlib.c.ORIG      2007-10-08 12:50:41.000000000 +0200
+++ muttlib.c   2007-10-08 12:51:28.000000000 +0200
@@ -812,7 +812,7 @@ void mutt_pretty_mailbox (char *s)
   }
 }
 
-void mutt_pretty_size (char *s, size_t len, long n)
+void mutt_pretty_size (char *s, size_t len, intmax_t n)
 {
   if (n == 0)
     strfcpy (s, "0K", len);
@@ -821,14 +821,14 @@ void mutt_pretty_size (char *s, size_t l
   else if (n < 1023949) /* 10K - 999K */
   {
     /* 51 is magic which causes 10189/10240 to be rounded up to 10 */
-    snprintf (s, len, "%ldK", (n + 51) / 1024);
+    snprintf (s, len, "%ljK", (n + 51) / 1024);
   }
   else if (n < 10433332) /* 1.0M - 9.9M */
     snprintf (s, len, "%3.1fM", n / 1048576.0);
   else /* 10M+ */
   {
     /* (10433332 + 52428) / 1048576 = 10 */
-    snprintf (s, len, "%ldM", (n + 52428) / 1048576);
+    snprintf (s, len, "%ljM", (n + 52428) / 1048576);
   }
 }
 
--- protos.h.ORIG       2007-10-08 12:50:46.000000000 +0200
+++ protos.h    2007-10-08 12:51:38.000000000 +0200
@@ -229,7 +229,7 @@ void mutt_perror (const char *);
 void mutt_prepare_envelope (ENVELOPE *, int);
 void mutt_unprepare_envelope (ENVELOPE *);
 void mutt_pretty_mailbox (char *);
-void mutt_pretty_size (char *, size_t, long);
+void mutt_pretty_size (char *, size_t, intmax_t);
 void mutt_pipe_message (HEADER *);
 void mutt_print_message (HEADER *);
 void mutt_print_patchlist (void);


Corinna

-- 
Corinna Vinschen
Cygwin Project Co-Leader
Red Hat