[PATCH] fdisk: fix format list for localization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



l (list) command is localizable text using gettext.
When you localize it, list result isn't formated well.

If current locale is UTF-8, one character may use 2 or 3 bytes
even if output width is one.
---
 fdisk/fdisk.c |   30 ++++++++++++++++++++++++++++--
 1 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
index 09efd1c..7373a00 100644
--- a/fdisk/fdisk.c
+++ b/fdisk/fdisk.c
@@ -25,6 +25,7 @@
 #include "nls.h"
 #include "blkdev.h"
 #include "common.h"
+#include "widechar.h"
 #include "fdisk.h"
 #include "wholedisk.h"
 
@@ -559,6 +560,11 @@ void list_types(struct systypes *sys)
 {
 	unsigned int last[4], done = 0, next = 0, size;
 	int i;
+#ifdef HAVE_WIDECHAR
+	int n, width;
+	wchar_t wc_buf[15]; /* max length */
+	char mb_buf[45]; /* max length * UTF-8 max length */
+#endif
 
 	for (i = 0; sys[i].name; i++);
 	size = i;
@@ -568,8 +574,28 @@ void list_types(struct systypes *sys)
 	i = done = 0;
 
 	do {
-		printf("%c%2x  %-15.15s", i ? ' ' : '\n',
-		        sys[next].type, _(sys[next].name));
+		printf("%c%2x  ", i ? ' ' : '\n', sys[next].type);
+#ifdef HAVE_WIDECHAR
+		/* the width of format string with non-ascii character
+		   doesn't work correctly. We need calcurate it */
+		n = mbstowcs(wc_buf, _(sys[next].name), 15);
+		width = wcswidth(wc_buf, n);
+		while (width > 15)
+			width = wcswidth(wc_buf, --n);
+		if (n > 0 && width > 0) {
+			/* Don't mix using printf and wprintf */
+			wc_buf[n] = '\0';
+			wcstombs(mb_buf, wc_buf, 45);
+			printf("%s", mb_buf);
+			for (n = 0; n < 15 - width; n++)
+				printf(" ");
+		} else {
+			/* convert error */
+			printf("%-15.15s", _(sys[next].name));
+		}
+#else
+		printf("%-15.15s", _(sys[next].name));
+#endif
  		next = last[i++] + done;
 		if (i > 3 || next >= last[i]) {
 			i = 0;
-- 
1.7.0

--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux