summaryrefslogtreecommitdiffstats
path: root/lib/mbsalign.c
diff options
context:
space:
mode:
authorKarel Zak2010-05-07 14:12:26 +0200
committerKarel Zak2010-05-07 14:12:26 +0200
commit5f94ca33cf7772e0c36b5b3e5b1cf1ab01f60180 (patch)
treeca7fbaf1bc031a50d958993eb52e29407fc6fdd9 /lib/mbsalign.c
parenttests: update blkid/md-raid1-* tests (diff)
downloadkernel-qcow2-util-linux-5f94ca33cf7772e0c36b5b3e5b1cf1ab01f60180.tar.gz
kernel-qcow2-util-linux-5f94ca33cf7772e0c36b5b3e5b1cf1ab01f60180.tar.xz
kernel-qcow2-util-linux-5f94ca33cf7772e0c36b5b3e5b1cf1ab01f60180.zip
cfdisk: support non-ascii characters in input
On Sat, Apr 03, 2010 at 12:58:48PM +0000, Jorge wrote: > When you want to write changes to disk you're asked for a > confirmation, like this one: > > Are you sure you want to write the partition table to disk? (yes > or no) > > There is no problem on the English version, but when you launch the > program in Spanish you get this: > > ¿Está seguro de que desea escribir la tabla de particiones en el > disco? > (sí o no): > > You can't type the "í" character. Trying to do so will end in no > input at all. That is, typing in my keyboard "´" then "i" leads to > nothing. So you can't write changes to disk, and you must launch the > program in English for it to operate. Reported-by: Jorge <yo@jorgesuarezdelis.name> Addresses: https://bugs.launchpad.net/ubuntu/+source/util-linux/+bug/205327 Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/mbsalign.c')
-rw-r--r--lib/mbsalign.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/mbsalign.c b/lib/mbsalign.c
index c1a5472e8..82ffc09cd 100644
--- a/lib/mbsalign.c
+++ b/lib/mbsalign.c
@@ -99,6 +99,40 @@ rpl_wcswidth (const wchar_t *s, size_t n)
}
#endif
+/* Truncate multi-byte string to @width and returns number of
+ * bytes of the new string @str, and in @width returns number
+ * of cells.
+ */
+size_t
+mbs_truncate(char *str, size_t *width)
+{
+ size_t bytes = strlen(str);
+#ifdef HAVE_WIDECHAR
+ size_t sz = mbstowcs(NULL, str, 0);
+ wchar_t *wcs = NULL;
+
+ if (sz == (size_t) -1)
+ goto done;
+
+ wcs = malloc((sz + 1) * sizeof(wchar_t));
+ if (!wcs)
+ goto done;
+
+ if (!mbstowcs(wcs, str, sz))
+ goto done;
+ *width = wc_truncate(wcs, *width);
+ bytes = wcstombs(str, wcs, bytes);
+done:
+ free(wcs);
+#else
+ if (*width < bytes)
+ bytes = *width;
+#endif
+ if (bytes >= 0)
+ str[bytes] = '\0';
+ return bytes;
+}
+
/* Write N_SPACES space characters to DEST while ensuring
nothing is written beyond DEST_END. A terminating NUL
is always added to DEST.