]> sjero.net Git - wget/blobdiff - src/progress.c
Deal with progress bars in environs lacking mbtowc or wcwidth.
[wget] / src / progress.c
index a06f73d39c0b66ab8c3d218e6299307597683e9d..80dab04a1c808102ea58fb5b9734707e7137176b 100644 (file)
@@ -38,6 +38,9 @@ as that of the covered work.  */
 # include <unistd.h>
 #endif
 #include <signal.h>
+#ifdef HAVE_WCHAR_H
+# include <wchar.h>
+#endif
 
 #include "progress.h"
 #include "utils.h"
@@ -764,6 +767,7 @@ update_speed_ring (struct bar_progress *bp, wgint howmuch, double dltime)
 #endif
 }
 
+#if HAVE_MBTOWC && HAVE_WCWIDTH
 int
 count_cols (const char *mbs)
 {
@@ -771,16 +775,29 @@ count_cols (const char *mbs)
   int     bytes;
   int     remaining = strlen(mbs);
   int     cols = 0;
+  int     wccols;
 
   while (*mbs != '\0')
     {
       bytes = mbtowc (&wc, mbs, remaining);
+      assert (bytes != 0);  /* Only happens when *mbs == '\0' */
+      if (bytes == -1)
+        {
+          /* Invalid sequence. We'll just have to fudge it. */
+          return cols + remaining;
+        }
       mbs += bytes;
       remaining -= bytes;
-      cols += wcwidth(wc);
+      wccols = wcwidth(wc);
+      cols += (wccols == -1? 1 : wccols);
     }
   return cols;
 }
+#else
+# define count_cols(mbs) ((int)(strlen(mbs)))
+# undef  wcwidth
+# define wcwidth(wc) (1)
+#endif
 
 /* Translation note: "ETA" is English-centric, but this must
    be short, ideally 3 chars.  Abbreviate if necessary.  */
@@ -796,7 +813,11 @@ get_eta (void)
       int nbytes;
       int ncols;
 
+#if HAVE_WCWIDTH && HAVE_MBTOWC
       eta_trans = _(eta_str);
+#else
+      eta_trans = eta_str;
+#endif
 
       /* Determine the number of bytes used in the translated string,
        * versus the number of columns used. This is to figure out how