1 --- /dev/null 2007-03-01 09:16:39.219409909 +0000
2 +++ coreutils-6.8+/tests/sort/sort-mb-tests 2007-03-01 15:08:24.000000000 +0000
6 + 0) xx='../../src/sort';;
9 +test "$VERBOSE" && echo=echo || echo=:
10 +$echo testing program: $xx
12 +test "$srcdir" || srcdir=.
13 +test "$VERBOSE" && $xx --version 2> /dev/null
15 +export LC_ALL=en_US.UTF-8
16 +locale -k LC_CTYPE 2>&1 | grep -q charmap.*UTF-8 || exit 77
19 +$xx -t ? -k2 -n mb1.I > mb1.O
21 +if test $code != 0; then
22 + $echo "Test mb1 failed: $xx return code $code differs from expected value 0" 1>&2
23 + errors=`expr $errors + 1`
25 + cmp mb1.O $srcdir/mb1.X > /dev/null 2>&1
27 + 0) if test "$VERBOSE"; then $echo "passed mb1"; fi;;
28 + 1) $echo "Test mb1 failed: files mb1.O and $srcdir/mb1.X differ" 1>&2
29 + (diff -c mb1.O $srcdir/mb1.X) 2> /dev/null
30 + errors=`expr $errors + 1`;;
31 + 2) $echo "Test mb1 may have failed." 1>&2
32 + $echo The command "cmp mb1.O $srcdir/mb1.X" failed. 1>&2
33 + errors=`expr $errors + 1`;;
37 +$xx -t ? -k4 -n mb2.I > mb2.O
39 +if test $code != 0; then
40 + $echo "Test mb2 failed: $xx return code $code differs from expected value 0" 1>&2
41 + errors=`expr $errors + 1`
43 + cmp mb2.O $srcdir/mb2.X > /dev/null 2>&1
45 + 0) if test "$VERBOSE"; then $echo "passed mb2"; fi;;
46 + 1) $echo "Test mb2 failed: files mb2.O and $srcdir/mb2.X differ" 1>&2
47 + (diff -c mb2.O $srcdir/mb2.X) 2> /dev/null
48 + errors=`expr $errors + 1`;;
49 + 2) $echo "Test mb2 may have failed." 1>&2
50 + $echo The command "cmp mb2.O $srcdir/mb2.X" failed. 1>&2
51 + errors=`expr $errors + 1`;;
55 +if test $errors = 0; then
56 + $echo Passed all 113 tests. 1>&2
58 + $echo Failed $errors tests. 1>&2
60 +test $errors = 0 || errors=1
62 --- /dev/null 2007-03-01 09:16:39.219409909 +0000
63 +++ coreutils-6.8+/tests/sort/mb2.I 2007-03-01 15:08:24.000000000 +0000
69 --- /dev/null 2007-03-01 09:16:39.219409909 +0000
70 +++ coreutils-6.8+/tests/sort/mb2.X 2007-03-01 15:08:24.000000000 +0000
76 --- /dev/null 2007-03-01 09:16:39.219409909 +0000
77 +++ coreutils-6.8+/tests/sort/mb1.I 2007-03-01 15:08:24.000000000 +0000
83 --- /dev/null 2007-03-01 09:16:39.219409909 +0000
84 +++ coreutils-6.8+/tests/sort/mb1.X 2007-03-01 15:08:24.000000000 +0000
90 --- coreutils-6.8+/tests/sort/Makefile.am.i18n 2007-01-24 07:47:37.000000000 +0000
91 +++ coreutils-6.8+/tests/sort/Makefile.am 2007-03-01 15:09:59.000000000 +0000
96 -EXTRA_DIST = Test.pm $x-tests $(explicit) $(maint_gen)
97 -noinst_SCRIPTS = $x-tests
98 +run_gen += mb1.0 mb2.0
100 +EXTRA_DIST = Test.pm $x-tests $(explicit) $(maint_gen) mb1.I mb1.X mb2.I mb2.X
101 +noinst_SCRIPTS = $x-tests # $x-mb-tests
102 TESTS_ENVIRONMENT = \
103 CU_TEST_NAME=`basename $(abs_srcdir)`,$$tst \
104 PATH="$(VG_PATH_PREFIX)`pwd`/../../src$(PATH_SEPARATOR)$$PATH"
106 editpl = sed -e 's,@''PERL''@,$(PERL),g' -e 's,@''srcdir''@,$(srcdir),g'
109 +TESTS = $x-tests $x-mb-tests
111 mk_script = $(srcdir)/../mk-script
112 $(srcdir)/$x-tests: $(mk_script) Test.pm Makefile.am
113 --- coreutils-6.8+/lib/linebuffer.h.i18n 2005-05-14 07:44:24.000000000 +0100
114 +++ coreutils-6.8+/lib/linebuffer.h 2007-03-01 15:08:24.000000000 +0000
119 +/* Get mbstate_t. */
124 /* A `struct linebuffer' holds a line of text. */
128 size_t size; /* Allocated. */
129 size_t length; /* Used. */
136 /* Initialize linebuffer LINEBUFFER for use. */
137 --- coreutils-6.8+/src/expand.c.i18n 2007-01-14 15:41:28.000000000 +0000
138 +++ coreutils-6.8+/src/expand.c 2007-03-01 15:08:24.000000000 +0000
142 #include <sys/types.h>
144 +/* Get mbstate_t, mbrtowc(), wcwidth(). */
152 #include "xstrndup.h"
154 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
155 + installation; work around this configuration error. */
156 +#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
157 +# define MB_LEN_MAX 16
160 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
161 +#if HAVE_MBRTOWC && defined mbstate_t
162 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
165 /* The official name of this program (e.g., no `g' prefix). */
166 #define PROGRAM_NAME "expand"
169 stops = num_start + len - 1;
175 error (0, 0, _("tab size contains invalid character(s): %s"),
176 @@ -365,6 +383,142 @@
182 +expand_multibyte (void)
184 + FILE *fp; /* Input strem. */
185 + mbstate_t i_state; /* Current shift state of the input stream. */
186 + mbstate_t i_state_bak; /* Back up the I_STATE. */
187 + mbstate_t o_state; /* Current shift state of the output stream. */
188 + char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
189 + char *bufpos; /* Next read position of BUF. */
190 + size_t buflen = 0; /* The length of the byte sequence in buf. */
191 + wchar_t wc; /* A gotten wide character. */
192 + size_t mblength; /* The byte size of a multibyte character
193 + which shows as same character as WC. */
194 + int tab_index = 0; /* Index in `tab_list' of next tabstop. */
195 + int column = 0; /* Column on screen of the next char. */
196 + int next_tab_column; /* Column the next tab stop is on. */
197 + int convert = 1; /* If nonzero, perform translations. */
199 + fp = next_file ((FILE *) NULL);
203 + memset (&o_state, '\0', sizeof(mbstate_t));
204 + memset (&i_state, '\0', sizeof(mbstate_t));
208 + /* Refill the buffer BUF. */
209 + if (buflen < MB_LEN_MAX && !feof(fp) && !ferror(fp))
211 + memmove (buf, bufpos, buflen);
212 + buflen += fread (buf + buflen, sizeof(char), BUFSIZ, fp);
216 + /* No character is left in BUF. */
219 + fp = next_file (fp);
222 + break; /* No more files. */
225 + memset (&i_state, '\0', sizeof(mbstate_t));
230 + /* Get a wide character. */
231 + i_state_bak = i_state;
232 + mblength = mbrtowc (&wc, bufpos, buflen, &i_state);
236 + case (size_t)-1: /* illegal byte sequence. */
239 + i_state = i_state_bak;
243 + if (convert_entire_line == 0)
249 + case 0: /* null. */
251 + if (convert && convert_entire_line == 0)
257 + if (wc == L'\n') /* LF. */
264 + else if (wc == L'\t' && convert) /* Tab. */
268 + /* Do not let tab_index == first_free_tab;
269 + stop when it is 1 less. */
270 + while (tab_index < first_free_tab - 1
271 + && column >= tab_list[tab_index])
273 + next_tab_column = tab_list[tab_index];
274 + if (tab_index < first_free_tab - 1)
276 + if (column >= next_tab_column)
277 + next_tab_column = column + 1;
280 + next_tab_column = column + tab_size - column % tab_size;
282 + while (column < next_tab_column)
299 + int width; /* The width of WC. */
301 + width = wcwidth (wc);
302 + column += (width > 0) ? width : 0;
303 + if (convert_entire_line == 0)
307 + fwrite (bufpos, sizeof(char), mblength, stdout);
310 + buflen -= mblength;
311 + bufpos += mblength;
317 main (int argc, char **argv)
321 file_list = (optind < argc ? &argv[optind] : stdin_argv);
325 + if (MB_CUR_MAX > 1)
326 + expand_multibyte ();
331 if (have_read_stdin && fclose (stdin) != 0)
332 error (EXIT_FAILURE, errno, "-");
333 --- coreutils-6.8+/src/join.c.i18n 2007-01-14 15:41:28.000000000 +0000
334 +++ coreutils-6.8+/src/join.c 2007-03-01 15:08:24.000000000 +0000
336 #include <sys/types.h>
339 +/* Get mbstate_t, mbrtowc(), mbrtowc(), wcwidth(). */
344 +/* Get iswblank(), towupper. */
346 +# include <wctype.h>
351 #include "hard-locale.h"
352 #include "linebuffer.h"
353 -#include "memcasecmp.h"
356 #include "xmemcoll.h"
359 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
360 +#if HAVE_MBRTOWC && defined mbstate_t
361 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
364 /* The official name of this program (e.g., no `g' prefix). */
365 #define PROGRAM_NAME "join"
367 @@ -104,10 +118,12 @@
368 /* Last element in `outlist', where a new element can be added. */
369 static struct outlist *outlist_end = &outlist_head;
371 -/* Tab character separating fields. If negative, fields are separated
372 - by any nonempty string of blanks, otherwise by exactly one
373 - tab character whose value (when cast to unsigned char) equals TAB. */
374 -static int tab = -1;
375 +/* Tab character separating fields. If NULL, fields are separated
376 + by any nonempty string of blanks. */
377 +static char *tab = NULL;
379 +/* The number of bytes used for tab. */
380 +static size_t tablen = 0;
382 static struct option const longopts[] =
386 /* Fill in the `fields' structure in LINE. */
388 +/* Fill in the `fields' structure in LINE. */
391 xfields (struct line *line)
393 @@ -199,10 +217,11 @@
400 + unsigned char t = tab[0];
402 - for (; (sep = memchr (ptr, tab, lim - ptr)) != NULL; ptr = sep + 1)
403 + for (; (sep = memchr (ptr, t, lim - ptr)) != NULL; ptr = sep + 1)
404 extract_field (line, ptr, sep - ptr);
407 @@ -229,6 +248,148 @@
408 extract_field (line, ptr, lim - ptr);
413 +xfields_multibyte (struct line *line)
415 + char *ptr = line->buf.buffer;
416 + char const *lim = ptr + line->buf.length - 1;
418 + size_t mblength = 1;
419 + mbstate_t state, state_bak;
421 + memset (&state, 0, sizeof (mbstate_t));
428 + unsigned char t = tab[0];
430 + for (; ptr < lim; ptr = sep + mblength)
436 + mblength = mbrtowc (&wc, sep, lim - sep + 1, &state);
438 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
443 + mblength = (mblength < 1) ? 1 : mblength;
445 + if (mblength == tablen && !memcmp (sep, tab, mblength))
457 + extract_field (line, ptr, sep - ptr);
462 + /* Skip leading blanks before the first field. */
466 + mblength = mbrtowc (&wc, ptr, lim - ptr + 1, &state);
468 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
474 + mblength = (mblength < 1) ? 1 : mblength;
485 + mblength = mbrtowc (&wc, ptr, lim - ptr + 1, &state);
486 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
492 + mblength = (mblength < 1) ? 1 : mblength;
494 + sep = ptr + mblength;
498 + mblength = mbrtowc (&wc, sep, lim - sep + 1, &state);
499 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
505 + mblength = (mblength < 1) ? 1 : mblength;
513 + extract_field (line, ptr, sep - ptr);
518 + mblength = mbrtowc (&wc, sep, lim - sep + 1, &state);
519 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
525 + mblength = (mblength < 1) ? 1 : mblength;
527 + ptr = sep + mblength;
531 + mblength = mbrtowc (&wc, ptr, lim - ptr + 1, &state);
532 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
538 + mblength = (mblength < 1) ? 1 : mblength;
540 + if (!iswblank (wc))
546 + while (ptr != lim);
549 + extract_field (line, ptr, lim - ptr);
553 /* Read a line from FP into LINE and split it into fields.
554 Return true if successful. */
557 line->nfields_allocated = 0;
561 + if (MB_CUR_MAX > 1)
562 + xfields_multibyte (line);
568 @@ -303,56 +469,114 @@
569 keycmp (struct line const *line1, struct line const *line2)
571 /* Start of field to compare in each file. */
576 - size_t len2; /* Length of fields to compare. */
579 + size_t len[2]; /* Length of fields to compare. */
583 if (join_field_1 < line1->nfields)
585 - beg1 = line1->fields[join_field_1].beg;
586 - len1 = line1->fields[join_field_1].len;
587 + beg[0] = line1->fields[join_field_1].beg;
588 + len[0] = line1->fields[join_field_1].len;
598 if (join_field_2 < line2->nfields)
600 - beg2 = line2->fields[join_field_2].beg;
601 - len2 = line2->fields[join_field_2].len;
602 + beg[1] = line2->fields[join_field_2].beg;
603 + len[1] = line2->fields[join_field_2].len;
614 - return len2 == 0 ? 0 : -1;
617 + return len[1] == 0 ? 0 : -1;
623 - /* FIXME: ignore_case does not work with NLS (in particular,
624 - with multibyte chars). */
625 - diff = memcasecmp (beg1, beg2, MIN (len1, len2));
627 + if (MB_CUR_MAX > 1)
631 + mbstate_t state, state_bak;
633 + memset (&state, '\0', sizeof (mbstate_t));
635 + for (i = 0; i < 2; i++)
637 + copy[i] = alloca (len[i] + 1);
639 + for (j = 0; j < MIN (len[0], len[1]);)
642 + mblength = mbrtowc (&wc, beg[i] + j, len[i] - j, &state);
655 + uwc = towupper (wc);
659 + mbstate_t state_wc;
661 + memset (&state_wc, '\0', sizeof (mbstate_t));
662 + wcrtomb (copy[i] + j, uwc, &state_wc);
665 + memcpy (copy[i] + j, beg[i] + j, mblength);
675 + for (i = 0; i < 2; i++)
677 + copy[i] = alloca (len[i] + 1);
679 + for (j = 0; j < MIN (len[0], len[1]); j++)
680 + copy[i][j] = toupper (beg[i][j]);
688 - if (hard_LC_COLLATE)
689 - return xmemcoll (beg1, len1, beg2, len2);
690 - diff = memcmp (beg1, beg2, MIN (len1, len2));
691 + copy[0] = (unsigned char *) beg[0];
692 + copy[1] = (unsigned char *) beg[1];
695 + if (hard_LC_COLLATE)
696 + return xmemcoll ((char *) copy[0], len[0], (char *) copy[1], len[1]);
697 + diff = memcmp (copy[0], copy[1], MIN (len[0], len[1]));
701 - return len1 < len2 ? -1 : len1 != len2;
702 + return len[0] - len[1];
705 /* Print field N of LINE if it exists and is nonempty, otherwise
706 @@ -377,11 +601,18 @@
708 /* Print the join of LINE1 and LINE2. */
710 +#define PUT_TAB_CHAR \
714 + fwrite(tab, sizeof(char), tablen, stdout) : putchar (' '); \
719 prjoin (struct line const *line1, struct line const *line2)
721 const struct outlist *outlist;
722 - char output_separator = tab < 0 ? ' ' : tab;
724 outlist = outlist_head.next;
726 @@ -397,12 +628,12 @@
729 if (line1 == &uni_blank)
733 field = join_field_2;
739 field = join_field_1;
745 - putchar (output_separator);
750 @@ -434,23 +665,23 @@
751 prfield (join_field_1, line1);
752 for (i = 0; i < join_field_1 && i < line1->nfields; ++i)
754 - putchar (output_separator);
758 for (i = join_field_1 + 1; i < line1->nfields; ++i)
760 - putchar (output_separator);
765 for (i = 0; i < join_field_2 && i < line2->nfields; ++i)
767 - putchar (output_separator);
771 for (i = join_field_2 + 1; i < line2->nfields; ++i)
773 - putchar (output_separator);
778 @@ -859,20 +1090,41 @@
782 - unsigned char newtab = optarg[0];
787 error (EXIT_FAILURE, 0, _("empty tab"));
789 + newtab = xstrdup (optarg);
791 + if (MB_CUR_MAX > 1)
795 + memset (&state, 0, sizeof (mbstate_t));
796 + newtablen = mbrtowc (NULL, newtab,
797 + strnlen (newtab, MB_LEN_MAX),
799 + if (newtablen == (size_t) 0
800 + || newtablen == (size_t) -1
801 + || newtablen == (size_t) -2)
808 + if (newtablen == 1 && newtab[1])
810 + if (STREQ (newtab, "\\0"))
813 + if (tab != NULL && strcmp (tab, newtab))
815 - if (STREQ (optarg, "\\0"))
818 - error (EXIT_FAILURE, 0, _("multi-character tab %s"),
821 + error (EXIT_FAILURE, 0, _("incompatible tabs"));
823 - if (0 <= tab && tab != newtab)
824 - error (EXIT_FAILURE, 0, _("incompatible tabs"));
826 + tablen = newtablen;
830 --- coreutils-6.8+/src/uniq.c.i18n 2007-01-14 15:41:28.000000000 +0000
831 +++ coreutils-6.8+/src/uniq.c 2007-03-01 15:08:24.000000000 +0000
834 #include <sys/types.h>
836 +/* Get mbstate_t, mbrtowc(). */
841 +/* Get isw* functions. */
843 +# include <wctype.h>
847 #include "argmatch.h"
848 #include "linebuffer.h"
851 #include "xmemcoll.h"
853 -#include "memcasecmp.h"
854 +#include "xmemcoll.h"
856 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
857 + installation; work around this configuration error. */
858 +#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
859 +# define MB_LEN_MAX 16
862 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
863 +#if HAVE_MBRTOWC && defined mbstate_t
864 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
868 /* The official name of this program (e.g., no `g' prefix). */
869 #define PROGRAM_NAME "uniq"
871 /* Select whether/how to delimit groups of duplicate lines. */
872 static enum delimit_method delimit_groups;
874 +/* Function pointers. */
876 +(*find_field) (struct linebuffer *line);
878 static struct option const longopts[] =
880 {"count", no_argument, NULL, 'c'},
882 return a pointer to the beginning of the line's field to be compared. */
885 -find_field (const struct linebuffer *line)
886 +find_field_uni (struct linebuffer *line)
889 char *lp = line->buffer;
896 +# define MBCHAR_TO_WCHAR(WC, MBLENGTH, LP, POS, SIZE, STATEP, CONVFAIL) \
899 + mbstate_t state_bak; \
902 + state_bak = *STATEP; \
904 + MBLENGTH = mbrtowc (&WC, LP + POS, SIZE - POS, STATEP); \
906 + switch (MBLENGTH) \
910 + *STATEP = state_bak; \
912 + /* Fall through */ \
920 +find_field_multi (struct linebuffer *line)
923 + char *lp = line->buffer;
924 + size_t size = line->length - 1;
932 + statep = &(line->state);
935 + for (count = 0; count < skip_fields && pos < size; count++)
939 + MBCHAR_TO_WCHAR (wc, mblength, lp, pos, size, statep, convfail);
941 + if (convfail || !iswblank (wc))
951 + MBCHAR_TO_WCHAR (wc, mblength, lp, pos, size, statep, convfail);
953 + if (!convfail && iswblank (wc))
961 + for (count = 0; count < skip_chars && pos < size; count++)
963 + MBCHAR_TO_WCHAR (wc, mblength, lp, pos, size, statep, convfail);
971 /* Return false if two strings OLD and NEW match, true if not.
972 OLD and NEW point not to the beginnings of the lines
973 but rather to the beginnings of the fields to compare.
976 different (char *old, char *new, size_t oldlen, size_t newlen)
978 + char *copy_old, *copy_new;
980 if (check_chars < oldlen)
981 oldlen = check_chars;
982 if (check_chars < newlen)
983 @@ -234,14 +339,92 @@
987 - /* FIXME: This should invoke strcoll somehow. */
988 - return oldlen != newlen || memcasecmp (old, new, oldlen);
991 + copy_old = alloca (oldlen + 1);
992 + copy_new = alloca (oldlen + 1);
994 + for (i = 0; i < oldlen; i++)
996 + copy_old[i] = toupper (old[i]);
997 + copy_new[i] = toupper (new[i]);
1000 - else if (hard_LC_COLLATE)
1001 - return xmemcoll (old, oldlen, new, newlen) != 0;
1003 - return oldlen != newlen || memcmp (old, new, oldlen);
1005 + copy_old = (char *)old;
1006 + copy_new = (char *)new;
1009 + return xmemcoll (copy_old, oldlen, copy_new, newlen);
1014 +different_multi (const char *old, const char *new, size_t oldlen, size_t newlen, mbstate_t oldstate, mbstate_t newstate)
1016 + size_t i, j, chars;
1017 + const char *str[2];
1020 + mbstate_t state[2];
1023 + mbstate_t state_bak;
1029 + state[0] = oldstate;
1030 + state[1] = newstate;
1032 + for (i = 0; i < 2; i++)
1034 + copy[i] = alloca (len[i] + 1);
1036 + for (j = 0, chars = 0; j < len[i] && chars < check_chars; chars++)
1038 + state_bak = state[i];
1039 + mblength = mbrtowc (&wc, str[i] + j, len[i] - j, &(state[i]));
1045 + state[i] = state_bak;
1046 + /* Fall through */
1054 + uwc = towupper (wc);
1058 + mbstate_t state_wc;
1060 + memset (&state_wc, '\0', sizeof(mbstate_t));
1061 + wcrtomb (copy[i] + j, uwc, &state_wc);
1064 + memcpy (copy[i] + j, str[i] + j, mblength);
1067 + memcpy (copy[i] + j, str[i] + j, mblength);
1071 + copy[i][j] = '\0';
1075 + return xmemcoll (copy[0], len[0], copy[1], len[1]);
1079 /* Output the line in linebuffer LINE to standard output
1080 provided that the switches say it should be output.
1081 @@ -295,15 +478,43 @@
1083 char *prevfield IF_LINT (= NULL);
1084 size_t prevlen IF_LINT (= 0);
1086 + mbstate_t prevstate;
1088 + memset (&prevstate, '\0', sizeof (mbstate_t));
1091 while (!feof (stdin))
1096 + mbstate_t thisstate;
1099 if (readlinebuffer (thisline, stdin) == 0)
1101 thisfield = find_field (thisline);
1102 thislen = thisline->length - 1 - (thisfield - thisline->buffer);
1104 + if (MB_CUR_MAX > 1)
1106 + thisstate = thisline->state;
1108 + if (prevline->length == 0 || different_multi
1109 + (thisfield, prevfield, thislen, prevlen, thisstate, prevstate))
1111 + fwrite (thisline->buffer, sizeof (char),
1112 + thisline->length, stdout);
1114 + SWAP_LINES (prevline, thisline);
1115 + prevfield = thisfield;
1116 + prevlen = thislen;
1117 + prevstate = thisstate;
1122 if (prevline->length == 0
1123 || different (thisfield, prevfield, thislen, prevlen))
1125 @@ -322,17 +533,26 @@
1127 uintmax_t match_count = 0;
1128 bool first_delimiter = true;
1130 + mbstate_t prevstate;
1133 if (readlinebuffer (prevline, stdin) == 0)
1135 prevfield = find_field (prevline);
1136 prevlen = prevline->length - 1 - (prevfield - prevline->buffer);
1138 + prevstate = prevline->state;
1141 while (!feof (stdin))
1147 + mbstate_t thisstate;
1149 if (readlinebuffer (thisline, stdin) == 0)
1152 @@ -341,6 +561,15 @@
1154 thisfield = find_field (thisline);
1155 thislen = thisline->length - 1 - (thisfield - thisline->buffer);
1157 + if (MB_CUR_MAX > 1)
1159 + thisstate = thisline->state;
1160 + match = !different_multi (thisfield, prevfield,
1161 + thislen, prevlen, thisstate, prevstate);
1165 match = !different (thisfield, prevfield, thislen, prevlen);
1166 match_count += match;
1169 SWAP_LINES (prevline, thisline);
1170 prevfield = thisfield;
1173 + prevstate = thisstate;
1178 @@ -417,6 +649,19 @@
1180 atexit (close_stdout);
1183 + if (MB_CUR_MAX > 1)
1185 + find_field = find_field_multi;
1190 + find_field = find_field_uni;
1197 check_chars = SIZE_MAX;
1198 --- coreutils-6.8+/src/fold.c.i18n 2007-02-23 12:01:47.000000000 +0000
1199 +++ coreutils-6.8+/src/fold.c 2007-03-01 15:08:24.000000000 +0000
1202 #include <sys/types.h>
1204 +/* Get mbstate_t, mbrtowc(), wcwidth(). */
1206 +# include <wchar.h>
1209 +/* Get iswprint(), iswblank(), wcwidth(). */
1211 +# include <wctype.h>
1217 #include "xstrtol.h"
1219 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
1220 + installation; work around this configuration error. */
1221 +#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
1223 +# define MB_LEN_MAX 16
1226 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
1227 +#if HAVE_MBRTOWC && defined mbstate_t
1228 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
1233 /* The official name of this program (e.g., no `g' prefix). */
1236 #define AUTHORS "David MacKenzie"
1238 +#define FATAL_ERROR(Message) \
1241 + error (0, 0, (Message)); \
1246 +enum operating_mode
1248 + /* Fold texts by columns that are at the given positions. */
1251 + /* Fold texts by bytes that are at the given positions. */
1254 + /* Fold texts by characters that are at the given positions. */
1258 /* The name this program was run with. */
1261 +/* The argument shows current mode. (Default: column_mode) */
1262 +static enum operating_mode operating_mode;
1264 /* If nonzero, try to break on whitespace. */
1265 static bool break_spaces;
1267 -/* If nonzero, count bytes, not column positions. */
1268 -static bool count_bytes;
1270 /* If nonzero, at least one of the files we read was standard input. */
1271 static bool have_read_stdin;
1273 -static char const shortopts[] = "bsw:0::1::2::3::4::5::6::7::8::9::";
1274 +static char const shortopts[] = "bcsw:0::1::2::3::4::5::6::7::8::9::";
1276 static struct option const longopts[] =
1278 {"bytes", no_argument, NULL, 'b'},
1279 + {"characters", no_argument, NULL, 'c'},
1280 {"spaces", no_argument, NULL, 's'},
1281 {"width", required_argument, NULL, 'w'},
1282 {GETOPT_HELP_OPTION_DECL},
1286 -b, --bytes count bytes rather than columns\n\
1287 + -c, --characters count characters rather than columns\n\
1288 -s, --spaces break at spaces\n\
1289 -w, --width=WIDTH use WIDTH columns instead of 80\n\
1293 adjust_column (size_t column, char c)
1296 + if (operating_mode != byte_mode)
1300 @@ -121,30 +165,14 @@
1301 to stdout, with maximum line length WIDTH.
1302 Return true if successful. */
1305 -fold_file (char const *filename, size_t width)
1307 +fold_text (FILE *istream, size_t width, int *saved_errno)
1311 size_t column = 0; /* Screen column where next char will go. */
1312 size_t offset_out = 0; /* Index in `line_out' for next char. */
1313 static char *line_out = NULL;
1314 static size_t allocated_out = 0;
1317 - if (STREQ (filename, "-"))
1320 - have_read_stdin = true;
1323 - istream = fopen (filename, "r");
1325 - if (istream == NULL)
1327 - error (0, errno, "%s", filename);
1331 while ((c = getc (istream)) != EOF)
1333 @@ -172,6 +200,15 @@
1334 bool found_blank = false;
1335 size_t logical_end = offset_out;
1337 + /* If LINE_OUT has no wide character,
1338 + put a new wide character in LINE_OUT
1339 + if column is bigger than width. */
1340 + if (offset_out == 0)
1342 + line_out[offset_out++] = c;
1346 /* Look for the last blank. */
1349 @@ -218,11 +255,225 @@
1350 line_out[offset_out++] = c;
1353 - saved_errno = errno;
1354 + *saved_errno = errno;
1357 + fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
1364 +fold_multibyte_text (FILE *istream, size_t width, int *saved_errno)
1366 + char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
1367 + size_t buflen = 0; /* The length of the byte sequence in buf. */
1368 + char *bufpos; /* Next read position of BUF. */
1369 + wint_t wc; /* A gotten wide character. */
1370 + size_t mblength; /* The byte size of a multibyte character which shows
1371 + as same character as WC. */
1372 + mbstate_t state, state_bak; /* State of the stream. */
1373 + int convfail; /* 1, when conversion is failed. Otherwise 0. */
1375 + char *line_out = NULL;
1376 + size_t offset_out = 0; /* Index in `line_out' for next char. */
1377 + size_t allocated_out = 0;
1380 + size_t column = 0;
1382 + size_t last_blank_pos;
1383 + size_t last_blank_column;
1384 + int is_blank_seen;
1385 + int last_blank_increment;
1386 + int is_bs_following_last_blank;
1387 + size_t bs_following_last_blank_num;
1388 + int is_cr_after_last_blank;
1390 +#define CLEAR_FLAGS \
1393 + last_blank_pos = 0; \
1394 + last_blank_column = 0; \
1395 + is_blank_seen = 0; \
1396 + is_bs_following_last_blank = 0; \
1397 + bs_following_last_blank_num = 0; \
1398 + is_cr_after_last_blank = 0; \
1402 +#define START_NEW_LINE \
1413 + memset (&state, '\0', sizeof(mbstate_t));
1415 + for (;; bufpos += mblength, buflen -= mblength)
1417 + if (buflen < MB_LEN_MAX && !feof (istream) && !ferror (istream))
1419 + memmove (buf, bufpos, buflen);
1420 + buflen += fread (buf + buflen, sizeof(char), BUFSIZ, istream);
1427 + /* Get a wide character. */
1429 + state_bak = state;
1430 + mblength = mbrtowc ((wchar_t *)&wc, bufpos, buflen, &state);
1437 + state = state_bak;
1438 + /* Fall through. */
1446 + if (operating_mode == byte_mode) /* byte mode */
1447 + increment = mblength;
1448 + else if (operating_mode == character_mode) /* character mode */
1450 + else /* column mode */
1459 + fwrite (line_out, sizeof(char), offset_out, stdout);
1464 + increment = (column > 0) ? -1 : 0;
1468 + increment = -1 * column;
1472 + increment = 8 - column % 8;
1476 + increment = wcwidth (wc);
1477 + increment = (increment < 0) ? 0 : increment;
1482 + if (column + increment > width && break_spaces && last_blank_pos)
1484 + fwrite (line_out, sizeof(char), last_blank_pos, stdout);
1487 + offset_out = offset_out - last_blank_pos;
1488 + column = column - last_blank_column + ((is_cr_after_last_blank)
1489 + ? last_blank_increment : bs_following_last_blank_num);
1490 + memmove (line_out, line_out + last_blank_pos, offset_out);
1495 + if (column + increment > width && column != 0)
1497 + fwrite (line_out, sizeof(char), offset_out, stdout);
1502 + if (allocated_out < offset_out + mblength)
1504 + allocated_out += 1024;
1505 + line_out = xrealloc (line_out, allocated_out);
1508 + memcpy (line_out + offset_out, bufpos, mblength);
1509 + offset_out += mblength;
1510 + column += increment;
1512 + if (is_blank_seen && !convfail && wc == L'\r')
1513 + is_cr_after_last_blank = 1;
1515 + if (is_bs_following_last_blank && !convfail && wc == L'\b')
1516 + ++bs_following_last_blank_num;
1518 + is_bs_following_last_blank = 0;
1520 + if (break_spaces && !convfail && iswblank (wc))
1522 + last_blank_pos = offset_out;
1523 + last_blank_column = column;
1524 + is_blank_seen = 1;
1525 + last_blank_increment = increment;
1526 + is_bs_following_last_blank = 1;
1527 + bs_following_last_blank_num = 0;
1528 + is_cr_after_last_blank = 0;
1532 + *saved_errno = errno;
1535 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
1541 +/* Fold file FILENAME, or standard input if FILENAME is "-",
1542 + to stdout, with maximum line length WIDTH.
1543 + Return 0 if successful, 1 if an error occurs. */
1546 +fold_file (char *filename, size_t width)
1551 + if (STREQ (filename, "-"))
1554 + have_read_stdin = 1;
1557 + istream = fopen (filename, "r");
1559 + if (istream == NULL)
1561 + error (0, errno, "%s", filename);
1565 + /* Define how ISTREAM is being folded. */
1567 + if (MB_CUR_MAX > 1)
1568 + fold_multibyte_text (istream, width, &saved_errno);
1571 + fold_text (istream, width, &saved_errno);
1573 if (ferror (istream))
1575 error (0, saved_errno, "%s", filename);
1578 atexit (close_stdout);
1580 - break_spaces = count_bytes = have_read_stdin = false;
1581 + operating_mode = column_mode;
1582 + break_spaces = have_read_stdin = false;
1584 while ((optc = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1)
1586 @@ -264,7 +516,15 @@
1589 case 'b': /* Count bytes rather than columns. */
1590 - count_bytes = true;
1591 + if (operating_mode != column_mode)
1592 + FATAL_ERROR (_("only one way of folding may be specified"));
1593 + operating_mode = byte_mode;
1597 + if (operating_mode != column_mode)
1598 + FATAL_ERROR (_("only one way of folding may be specified"));
1599 + operating_mode = character_mode;
1602 case 's': /* Break at word boundaries. */
1603 --- coreutils-6.8+/src/sort.c.i18n 2007-02-24 11:23:23.000000000 +0000
1604 +++ coreutils-6.8+/src/sort.c 2007-03-01 15:10:57.000000000 +0000
1609 +#include <assert.h>
1611 #include <sys/types.h>
1612 #include <sys/wait.h>
1615 +# include <wchar.h>
1617 +/* Get isw* functions. */
1619 +# include <wctype.h>
1623 #include "argmatch.h"
1625 @@ -116,14 +125,38 @@
1626 /* Thousands separator; if -1, then there isn't one. */
1627 static int thousands_sep;
1629 +static int force_general_numcompare = 0;
1631 /* Nonzero if the corresponding locales are hard. */
1632 static bool hard_LC_COLLATE;
1633 -#if HAVE_NL_LANGINFO
1634 +#if HAVE_LANGINFO_CODESET
1635 static bool hard_LC_TIME;
1638 #define NONZERO(x) ((x) != 0)
1640 +/* get a multibyte character's byte length. */
1641 +#define GET_BYTELEN_OF_CHAR(LIM, PTR, MBLENGTH, STATE) \
1645 + mbstate_t state_bak; \
1647 + state_bak = STATE; \
1648 + mblength = mbrtowc (&wc, PTR, LIM - PTR, &STATE); \
1650 + switch (MBLENGTH) \
1652 + case (size_t)-1: \
1653 + case (size_t)-2: \
1654 + STATE = state_bak; \
1655 + /* Fall through. */ \
1662 /* The kind of blanks for '-b' to skip in various options. */
1663 enum blanktype { bl_start, bl_end, bl_both };
1665 @@ -261,13 +294,11 @@
1666 they were read if all keys compare equal. */
1669 -/* If TAB has this value, blanks separate fields. */
1670 -enum { TAB_DEFAULT = CHAR_MAX + 1 };
1672 -/* Tab character separating fields. If TAB_DEFAULT, then fields are
1673 +/* Tab character separating fields. If tab_length is 0, then fields are
1674 separated by the empty string between a non-blank character and a blank
1676 -static int tab = TAB_DEFAULT;
1677 +static char tab[MB_LEN_MAX + 1];
1678 +static size_t tab_length = 0;
1680 /* Flag to remove consecutive duplicate lines from the output.
1681 Only the last of a sequence of equal lines will be output. */
1682 @@ -639,6 +670,44 @@
1686 +/* Function pointers. */
1688 +(*inittables) (void);
1690 +(*begfield) (const struct line*, const struct keyfield *);
1692 +(*limfield) (const struct line*, const struct keyfield *);
1694 +(*getmonth) (char const *, size_t);
1696 +(*keycompare) (const struct line *, const struct line *);
1698 +(*numcompare) (const char *, const char *);
1700 +/* Test for white space multibyte character.
1701 + Set LENGTH the byte length of investigated multibyte character. */
1704 +ismbblank (const char *str, size_t len, size_t *length)
1710 + memset (&state, '\0', sizeof(mbstate_t));
1711 + mblength = mbrtowc (&wc, str, len, &state);
1713 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
1719 + *length = (mblength < 1) ? 1 : mblength;
1720 + return iswblank (wc);
1724 /* Clean up any remaining temporary files. */
1727 @@ -978,7 +1047,7 @@
1731 -#if HAVE_NL_LANGINFO
1732 +#if HAVE_LANGINFO_CODESET
1735 struct_month_cmp (const void *m1, const void *m2)
1736 @@ -993,7 +1062,7 @@
1737 /* Initialize the character class tables. */
1741 +inittables_uni (void)
1745 @@ -1005,7 +1074,7 @@
1746 fold_toupper[i] = toupper (i);
1749 -#if HAVE_NL_LANGINFO
1750 +#if HAVE_LANGINFO_CODESET
1751 /* If we're not in the "C" locale, read different names for months. */
1754 @@ -1031,6 +1100,64 @@
1760 +inittables_mb (void)
1764 + size_t s_len, mblength;
1765 + char mbc[MB_LEN_MAX];
1767 + mbstate_t state_mb, state_wc;
1769 + for (i = 0; i < MONTHS_PER_YEAR; i++)
1771 + s = (char *) nl_langinfo (ABMON_1 + i);
1772 + s_len = strlen (s);
1773 + monthtab[i].name = name = (char *) xmalloc (s_len + 1);
1774 + monthtab[i].val = i + 1;
1776 + memset (&state_mb, '\0', sizeof (mbstate_t));
1777 + memset (&state_wc, '\0', sizeof (mbstate_t));
1779 + for (j = 0; j < s_len;)
1781 + if (!ismbblank (s + j, s_len - j, &mblength))
1786 + for (k = 0; j < s_len;)
1788 + mblength = mbrtowc (&wc, (s + j), (s_len - j), &state_mb);
1789 + assert (mblength != (size_t)-1 && mblength != (size_t)-2);
1790 + if (mblength == 0)
1793 + pwc = towupper (wc);
1796 + memcpy (mbc, s + j, mblength);
1802 + mblength = wcrtomb (mbc, pwc, &state_wc);
1803 + assert (mblength != (size_t)0 && mblength != (size_t)-1);
1806 + for (l = 0; l < mblength; l++)
1807 + name[k++] = mbc[l];
1811 + qsort ((void *) monthtab, MONTHS_PER_YEAR,
1812 + sizeof (struct month), struct_month_cmp);
1816 /* Specify the amount of main memory to use when sorting. */
1818 specify_sort_size (char const *s)
1819 @@ -1241,7 +1368,7 @@
1823 -begfield (const struct line *line, const struct keyfield *key)
1824 +begfield_uni (const struct line *line, const struct keyfield *key)
1826 char *ptr = line->text, *lim = ptr + line->length - 1;
1827 size_t sword = key->sword;
1828 @@ -1251,10 +1378,10 @@
1829 /* The leading field separator itself is included in a field when -t
1832 - if (tab != TAB_DEFAULT)
1834 while (ptr < lim && sword--)
1836 - while (ptr < lim && *ptr != tab)
1837 + while (ptr < lim && *ptr != tab[0])
1841 @@ -1282,11 +1409,70 @@
1847 +begfield_mb (const struct line *line, const struct keyfield *key)
1850 + char *ptr = line->text, *lim = ptr + line->length - 1;
1851 + size_t sword = key->sword;
1852 + size_t schar = key->schar;
1856 + memset (&state, '\0', sizeof(mbstate_t));
1859 + while (ptr < lim && sword--)
1861 + while (ptr < lim && memcmp (ptr, tab, tab_length) != 0)
1863 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
1868 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
1873 + while (ptr < lim && sword--)
1875 + while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
1879 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
1882 + while (ptr < lim && !ismbblank (ptr, lim - ptr, &mblength))
1886 + if (key->skipsblanks)
1887 + while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
1890 + for (i = 0; i < schar; i++)
1892 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
1894 + if (ptr + mblength > lim)
1904 /* Return the limit of (a pointer to the first character after) the field
1905 in LINE specified by KEY. */
1908 -limfield (const struct line *line, const struct keyfield *key)
1909 +limfield_uni (const struct line *line, const struct keyfield *key)
1911 char *ptr = line->text, *lim = ptr + line->length - 1;
1912 size_t eword = key->eword, echar = key->echar;
1913 @@ -1299,10 +1485,10 @@
1914 `beginning' is the first character following the delimiting TAB.
1915 Otherwise, leave PTR pointing at the first `blank' character after
1916 the preceding field. */
1917 - if (tab != TAB_DEFAULT)
1919 while (ptr < lim && eword--)
1921 - while (ptr < lim && *ptr != tab)
1922 + while (ptr < lim && *ptr != tab[0])
1924 if (ptr < lim && (eword | echar))
1926 @@ -1348,10 +1534,10 @@
1929 /* Make LIM point to the end of (one byte past) the current field. */
1930 - if (tab != TAB_DEFAULT)
1934 - newlim = memchr (ptr, tab, lim - ptr);
1935 + newlim = memchr (ptr, tab[0], lim - ptr);
1939 @@ -1384,6 +1570,107 @@
1945 +limfield_mb (const struct line *line, const struct keyfield *key)
1947 + char *ptr = line->text, *lim = ptr + line->length - 1;
1948 + size_t eword = key->eword, echar = key->echar;
1953 + memset (&state, '\0', sizeof(mbstate_t));
1956 + while (ptr < lim && eword--)
1958 + while (ptr < lim && memcmp (ptr, tab, tab_length) != 0)
1960 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
1963 + if (ptr < lim && (eword | echar))
1965 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
1970 + while (ptr < lim && eword--)
1972 + while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
1976 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
1979 + while (ptr < lim && !ismbblank (ptr, lim - ptr, &mblength))
1984 +# ifdef POSIX_UNSPECIFIED
1985 + /* Make LIM point to the end of (one byte past) the current field. */
1991 + for (p = ptr; p < lim;)
1993 + if (memcmp (p, tab, tab_length) == 0)
1999 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2008 + while (newlim < lim && ismbblank (newlim, lim - newlim, &mblength))
2009 + newlim += mblength;
2012 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2015 + while (newlim < lim && !ismbblank (newlim, lim - newlim, &mblength))
2016 + newlim += mblength;
2021 + /* If we're skipping leading blanks, don't start counting characters
2022 + * until after skipping past any leading blanks. */
2023 + if (key->skipsblanks)
2024 + while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
2027 + memset (&state, '\0', sizeof(mbstate_t));
2029 + /* Advance PTR by ECHAR (if possible), but no further than LIM. */
2030 + for (i = 0; i < echar; i++)
2032 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2034 + if (ptr + mblength > lim)
2044 /* Fill BUF reading from FP, moving buf->left bytes from the end
2045 of buf->buf to the beginning first. If EOF is reached and the
2046 file wasn't terminated by a newline, supply one. Set up BUF's line
2047 @@ -1466,8 +1753,24 @@
2050 if (key->skipsblanks)
2051 - while (blanks[to_uchar (*line_start)])
2055 + if (MB_CUR_MAX > 1)
2059 + memset (&state, '\0', sizeof(mbstate_t));
2060 + while (line_start < line->keylim &&
2061 + ismbblank (line_start,
2062 + line->keylim - line_start,
2064 + line_start += mblength;
2068 + while (blanks[to_uchar (*line_start)])
2071 line->keybeg = line_start;
2074 @@ -1500,7 +1803,7 @@
2078 -numcompare (const char *a, const char *b)
2079 +numcompare_uni (const char *a, const char *b)
2081 while (blanks[to_uchar (*a)])
2083 @@ -1510,6 +1813,25 @@
2084 return strnumcmp (a, b, decimal_point, thousands_sep);
2089 +numcompare_mb (const char *a, const char *b)
2091 + size_t mblength, len;
2092 + len = strlen (a); /* okay for UTF-8 */
2093 + while (*a && ismbblank (a, len > MB_CUR_MAX ? MB_CUR_MAX : len, &mblength))
2098 + len = strlen (b); /* okay for UTF-8 */
2099 + while (*b && ismbblank (b, len > MB_CUR_MAX ? MB_CUR_MAX : len, &mblength))
2102 + return strnumcmp (a, b, decimal_point, thousands_sep);
2104 +#endif /* HAV_EMBRTOWC */
2107 general_numcompare (const char *sa, const char *sb)
2109 @@ -1543,7 +1865,7 @@
2110 Return 0 if the name in S is not recognized. */
2113 -getmonth (char const *month, size_t len)
2114 +getmonth_uni (char const *month, size_t len)
2117 size_t hi = MONTHS_PER_YEAR;
2118 @@ -1698,11 +2020,79 @@
2124 +getmonth_mb (const char *s, size_t len)
2127 + register size_t i;
2128 + register int lo = 0, hi = MONTHS_PER_YEAR, result;
2130 + size_t wclength, mblength;
2132 + const wchar_t **wpp;
2133 + wchar_t *month_wcs;
2136 + while (len > 0 && ismbblank (s, len, &mblength))
2145 + month = (char *) alloca (len + 1);
2147 + tmp = (char *) alloca (len + 1);
2148 + memcpy (tmp, s, len);
2150 + pp = (const char **)&tmp;
2151 + month_wcs = (wchar_t *) alloca ((len + 1) * sizeof (wchar_t));
2152 + memset (&state, '\0', sizeof(mbstate_t));
2154 + wclength = mbsrtowcs (month_wcs, pp, len + 1, &state);
2155 + assert (wclength != (size_t)-1 && *pp == NULL);
2157 + for (i = 0; i < wclength; i++)
2159 + month_wcs[i] = towupper(month_wcs[i]);
2160 + if (iswblank (month_wcs[i]))
2162 + month_wcs[i] = L'\0';
2167 + wpp = (const wchar_t **)&month_wcs;
2169 + mblength = wcsrtombs (month, wpp, len + 1, &state);
2170 + assert (mblength != (-1) && *wpp == NULL);
2174 + int ix = (lo + hi) / 2;
2176 + if (strncmp (month, monthtab[ix].name, strlen (monthtab[ix].name)) < 0)
2181 + while (hi - lo > 1);
2183 + result = (!strncmp (month, monthtab[lo].name, strlen (monthtab[lo].name))
2184 + ? monthtab[lo].val : 0);
2190 /* Compare two lines A and B trying every key in sequence until there
2191 are no more keys or a difference is found. */
2194 -keycompare (const struct line *a, const struct line *b)
2195 +keycompare_uni (const struct line *a, const struct line *b)
2197 struct keyfield const *key = keylist;
2199 @@ -1875,6 +2265,177 @@
2200 return key->reverse ? -diff : diff;
2205 +keycompare_mb (const struct line *a, const struct line *b)
2207 + struct keyfield *key = keylist;
2209 + /* For the first iteration only, the key positions have been
2210 + precomputed for us. */
2211 + char *texta = a->keybeg;
2212 + char *textb = b->keybeg;
2213 + char *lima = a->keylim;
2214 + char *limb = b->keylim;
2216 + size_t mblength_a, mblength_b;
2217 + wchar_t wc_a, wc_b;
2218 + mbstate_t state_a, state_b;
2222 + memset (&state_a, '\0', sizeof(mbstate_t));
2223 + memset (&state_b, '\0', sizeof(mbstate_t));
2227 + unsigned char *translate = (unsigned char *) key->translate;
2228 + bool const *ignore = key->ignore;
2230 + /* Find the lengths. */
2231 + size_t lena = lima <= texta ? 0 : lima - texta;
2232 + size_t lenb = limb <= textb ? 0 : limb - textb;
2234 + /* Actually compare the fields. */
2235 + if (key->numeric | key->general_numeric)
2237 + char savea = *lima, saveb = *limb;
2239 + *lima = *limb = '\0';
2240 + if (force_general_numcompare)
2241 + diff = general_numcompare (texta, textb);
2243 + diff = ((key->numeric ? numcompare : general_numcompare)
2245 + *lima = savea, *limb = saveb;
2247 + else if (key->month)
2248 + diff = getmonth (texta, lena) - getmonth (textb, lenb);
2251 + if (ignore || translate)
2253 + char *copy_a = (char *) alloca (lena + 1 + lenb + 1);
2254 + char *copy_b = copy_a + lena + 1;
2255 + size_t new_len_a, new_len_b;
2258 + /* Ignore and/or translate chars before comparing. */
2259 +# define IGNORE_CHARS(NEW_LEN, LEN, TEXT, COPY, WC, MBLENGTH, STATE) \
2263 + char mbc[MB_LEN_MAX]; \
2264 + mbstate_t state_wc; \
2266 + for (NEW_LEN = i = 0; i < LEN;) \
2268 + mbstate_t state_bak; \
2270 + state_bak = STATE; \
2271 + MBLENGTH = mbrtowc (&WC, TEXT + i, LEN - i, &STATE); \
2273 + if (MBLENGTH == (size_t)-2 || MBLENGTH == (size_t)-1 \
2274 + || MBLENGTH == 0) \
2276 + if (MBLENGTH == (size_t)-2 || MBLENGTH == (size_t)-1) \
2277 + STATE = state_bak; \
2279 + COPY[NEW_LEN++] = TEXT[i++]; \
2285 + if ((ignore == nonprinting && !iswprint (WC)) \
2286 + || (ignore == nondictionary \
2287 + && !iswalnum (WC) && !iswblank (WC))) \
2297 + uwc = towupper(WC); \
2300 + memcpy (mbc, TEXT + i, MBLENGTH); \
2307 + memset (&state_wc, '\0', sizeof (mbstate_t)); \
2309 + MBLENGTH = wcrtomb (mbc, WC, &state_wc); \
2310 + assert (MBLENGTH != (size_t)-1 && MBLENGTH != 0); \
2313 + for (j = 0; j < MBLENGTH; j++) \
2314 + COPY[NEW_LEN++] = mbc[j]; \
2317 + for (j = 0; j < MBLENGTH; j++) \
2318 + COPY[NEW_LEN++] = TEXT[i++]; \
2320 + COPY[NEW_LEN] = '\0'; \
2323 + IGNORE_CHARS (new_len_a, lena, texta, copy_a,
2324 + wc_a, mblength_a, state_a);
2325 + IGNORE_CHARS (new_len_b, lenb, textb, copy_b,
2326 + wc_b, mblength_b, state_b);
2327 + diff = xmemcoll (copy_a, new_len_a, copy_b, new_len_b);
2329 + else if (lena == 0)
2330 + diff = - NONZERO (lenb);
2331 + else if (lenb == 0)
2334 + diff = xmemcoll (texta, lena, textb, lenb);
2344 + /* Find the beginning and limit of the next field. */
2345 + if (key->eword != -1)
2346 + lima = limfield (a, key), limb = limfield (b, key);
2348 + lima = a->text + a->length - 1, limb = b->text + b->length - 1;
2350 + if (key->sword != -1)
2351 + texta = begfield (a, key), textb = begfield (b, key);
2354 + texta = a->text, textb = b->text;
2355 + if (key->skipsblanks)
2357 + while (texta < lima && ismbblank (texta, lima - texta, &mblength_a))
2358 + texta += mblength_a;
2359 + while (textb < limb && ismbblank (textb, limb - textb, &mblength_b))
2360 + textb += mblength_b;
2370 + return key->reverse ? -diff : diff;
2374 /* Compare two lines A and B, returning negative, zero, or positive
2375 depending on whether A compares less than, equal to, or greater than B. */
2377 @@ -2744,7 +3305,7 @@
2378 initialize_exit_failure (SORT_FAILURE);
2380 hard_LC_COLLATE = hard_locale (LC_COLLATE);
2381 -#if HAVE_NL_LANGINFO
2382 +#if HAVE_LANGINFO_CODESET
2383 hard_LC_TIME = hard_locale (LC_TIME);
2386 @@ -2765,6 +3326,27 @@
2391 + if (MB_CUR_MAX > 1)
2393 + inittables = inittables_mb;
2394 + begfield = begfield_mb;
2395 + limfield = limfield_mb;
2396 + getmonth = getmonth_mb;
2397 + keycompare = keycompare_mb;
2398 + numcompare = numcompare_mb;
2403 + inittables = inittables_uni;
2404 + begfield = begfield_uni;
2405 + limfield = limfield_uni;
2406 + getmonth = getmonth_uni;
2407 + keycompare = keycompare_uni;
2408 + numcompare = numcompare_uni;
2411 have_read_stdin = false;
2414 @@ -3015,13 +3597,35 @@
2418 - char newtab = optarg[0];
2420 + char newtab[MB_LEN_MAX + 1];
2421 + size_t newtab_length = 1;
2422 + strncpy (newtab, optarg, MB_LEN_MAX);
2424 error (SORT_FAILURE, 0, _("empty tab"));
2427 + if (MB_CUR_MAX > 1)
2433 + memset (&state, '\0', sizeof (mbstate_t));
2434 + newtab_length = mbrtowc (&wc, newtab, strnlen (newtab,
2437 + switch (newtab_length)
2442 + newtab_length = 1;
2446 + if (newtab_length == 1 && optarg[1])
2448 if (STREQ (optarg, "\\0"))
2453 /* Provoke with `sort -txx'. Complain about
2454 @@ -3032,9 +3636,12 @@
2458 - if (tab != TAB_DEFAULT && tab != newtab)
2460 + && (tab_length != newtab_length
2461 + || memcmp (tab, newtab, tab_length) != 0))
2462 error (SORT_FAILURE, 0, _("incompatible tabs"));
2464 + memcpy (tab, newtab, newtab_length);
2465 + tab_length = newtab_length;
2469 --- coreutils-6.8+/src/unexpand.c.i18n 2007-01-14 15:41:28.000000000 +0000
2470 +++ coreutils-6.8+/src/unexpand.c 2007-03-01 15:08:24.000000000 +0000
2474 #include <sys/types.h>
2476 +/* Get mbstate_t, mbrtowc(), wcwidth(). */
2478 +# include <wchar.h>
2484 #include "xstrndup.h"
2486 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
2487 + installation; work around this configuration error. */
2488 +#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
2489 +# define MB_LEN_MAX 16
2492 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
2493 +#if HAVE_MBRTOWC && defined mbstate_t
2494 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
2497 /* The official name of this program (e.g., no `g' prefix). */
2498 #define PROGRAM_NAME "unexpand"
2500 @@ -110,6 +127,208 @@
2504 +static FILE *next_file (FILE *fp);
2508 +unexpand_multibyte (void)
2510 + FILE *fp; /* Input stream. */
2511 + mbstate_t i_state; /* Current shift state of the input stream. */
2512 + mbstate_t i_state_bak; /* Back up the I_STATE. */
2513 + mbstate_t o_state; /* Current shift state of the output stream. */
2514 + char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
2515 + char *bufpos; /* Next read position of BUF. */
2516 + size_t buflen = 0; /* The length of the byte sequence in buf. */
2517 + wint_t wc; /* A gotten wide character. */
2518 + size_t mblength; /* The byte size of a multibyte character
2519 + which shows as same character as WC. */
2521 + /* Index in `tab_list' of next tabstop: */
2522 + int tab_index = 0; /* For calculating width of pending tabs. */
2523 + int print_tab_index = 0; /* For printing as many tabs as possible. */
2524 + unsigned int column = 0; /* Column on screen of next char. */
2525 + int next_tab_column; /* Column the next tab stop is on. */
2526 + int convert = 1; /* If nonzero, perform translations. */
2527 + unsigned int pending = 0; /* Pending columns of blanks. */
2529 + fp = next_file ((FILE *) NULL);
2533 + memset (&o_state, '\0', sizeof(mbstate_t));
2534 + memset (&i_state, '\0', sizeof(mbstate_t));
2538 + if (buflen < MB_LEN_MAX && !feof(fp) && !ferror(fp))
2540 + memmove (buf, bufpos, buflen);
2541 + buflen += fread (buf + buflen, sizeof(char), BUFSIZ, fp);
2545 + /* Get a wide character. */
2553 + i_state_bak = i_state;
2554 + mblength = mbrtowc ((wchar_t *)&wc, bufpos, buflen, &i_state);
2557 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
2559 + i_state = i_state_bak;
2563 + if (wc == L' ' && convert && column < INT_MAX)
2568 + else if (wc == L'\t' && convert)
2570 + if (tab_size == 0)
2572 + /* Do not let tab_index == first_free_tab;
2573 + stop when it is 1 less. */
2574 + while (tab_index < first_free_tab - 1
2575 + && column >= tab_list[tab_index])
2577 + next_tab_column = tab_list[tab_index];
2578 + if (tab_index < first_free_tab - 1)
2580 + if (column >= next_tab_column)
2582 + convert = 0; /* Ran out of tab stops. */
2583 + goto flush_pend_mb;
2588 + next_tab_column = column + tab_size - column % tab_size;
2590 + pending += next_tab_column - column;
2591 + column = next_tab_column;
2596 + /* Flush pending spaces. Print as many tabs as possible,
2597 + then print the rest as spaces. */
2603 + column -= pending;
2604 + while (pending > 0)
2606 + if (tab_size == 0)
2608 + /* Do not let print_tab_index == first_free_tab;
2609 + stop when it is 1 less. */
2610 + while (print_tab_index < first_free_tab - 1
2611 + && column >= tab_list[print_tab_index])
2612 + print_tab_index++;
2613 + next_tab_column = tab_list[print_tab_index];
2614 + if (print_tab_index < first_free_tab - 1)
2615 + print_tab_index++;
2620 + column + tab_size - column % tab_size;
2622 + if (next_tab_column - column <= pending)
2625 + pending -= next_tab_column - column;
2626 + column = next_tab_column;
2630 + --print_tab_index;
2631 + column += pending;
2632 + while (pending != 0)
2642 + fp = next_file (fp);
2644 + break; /* No more files. */
2647 + memset (&i_state, '\0', sizeof(mbstate_t));
2652 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
2657 + if (convert_entire_line == 0)
2663 + else if (mblength == 0)
2665 + if (convert && convert_entire_line == 0)
2681 + int width; /* The width of WC. */
2683 + width = wcwidth (wc);
2684 + column += (width > 0) ? width : 0;
2685 + if (convert_entire_line == 0)
2692 + tab_index = print_tab_index = 0;
2693 + column = pending = 0;
2696 + fwrite (bufpos, sizeof(char), mblength, stdout);
2699 + buflen -= mblength;
2700 + bufpos += mblength;
2709 @@ -531,7 +750,12 @@
2711 file_list = (optind < argc ? &argv[optind] : stdin_argv);
2715 + if (MB_CUR_MAX > 1)
2716 + unexpand_multibyte ();
2721 if (have_read_stdin && fclose (stdin) != 0)
2722 error (EXIT_FAILURE, errno, "-");
2723 --- coreutils-6.8+/src/pr.c.i18n 2007-01-14 15:41:28.000000000 +0000
2724 +++ coreutils-6.8+/src/pr.c 2007-03-01 15:08:24.000000000 +0000
2725 @@ -313,6 +313,32 @@
2728 #include <sys/types.h>
2730 +/* Get MB_LEN_MAX. */
2731 +#include <limits.h>
2732 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
2733 + installation; work around this configuration error. */
2734 +#if !defined MB_LEN_MAX || MB_LEN_MAX == 1
2735 +# define MB_LEN_MAX 16
2738 +/* Get MB_CUR_MAX. */
2739 +#include <stdlib.h>
2741 +/* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
2742 +/* Get mbstate_t, mbrtowc(), wcwidth(). */
2744 +# include <wchar.h>
2747 +/* Get iswprint(). -- for wcwidth(). */
2749 +# include <wctype.h>
2751 +#if !defined iswprint && !HAVE_ISWPRINT
2752 +# define iswprint(wc) 1
2757 #include "hard-locale.h"
2758 @@ -324,6 +350,18 @@
2759 #include "strftime.h"
2760 #include "xstrtol.h"
2762 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
2763 +#if HAVE_MBRTOWC && defined mbstate_t
2764 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
2767 +#ifndef HAVE_DECL_WCWIDTH
2768 +"this configure-time declaration test was not run"
2770 +#if !HAVE_DECL_WCWIDTH
2771 +extern int wcwidth ();
2774 /* The official name of this program (e.g., no `g' prefix). */
2775 #define PROGRAM_NAME "pr"
2777 @@ -416,7 +454,20 @@
2779 #define NULLCOL (COLUMN *)0
2781 -static int char_to_clump (char c);
2782 +/* Funtion pointers to switch functions for single byte locale or for
2783 + multibyte locale. If multibyte functions do not exist in your sysytem,
2784 + these pointers always point the function for single byte locale. */
2785 +static void (*print_char) (char c);
2786 +static int (*char_to_clump) (char c);
2788 +/* Functions for single byte locale. */
2789 +static void print_char_single (char c);
2790 +static int char_to_clump_single (char c);
2792 +/* Functions for multibyte locale. */
2793 +static void print_char_multi (char c);
2794 +static int char_to_clump_multi (char c);
2796 static bool read_line (COLUMN *p);
2797 static bool print_page (void);
2798 static bool print_stored (COLUMN *p);
2800 static void pad_across_to (int position);
2801 static void add_line_number (COLUMN *p);
2802 static void getoptarg (char *arg, char switch_char, char *character,
2803 + int *character_length, int *character_width,
2805 void usage (int status);
2806 static void print_files (int number_of_files, char **av);
2808 static void pad_down (int lines);
2809 static void read_rest_of_line (COLUMN *p);
2810 static void skip_read (COLUMN *p, int column_number);
2811 -static void print_char (char c);
2812 static void cleanup (void);
2813 static void print_sep_string (void);
2814 static void separator_string (const char *optarg_S);
2816 we store the leftmost columns contiguously in buff.
2817 To print a line from buff, get the index of the first character
2818 from line_vector[i], and print up to line_vector[i + 1]. */
2820 +static unsigned char *buff;
2822 /* Index of the position in buff where the next character
2825 static bool untabify_input = false;
2827 /* (-e) The input tab character. */
2828 -static char input_tab_char = '\t';
2829 +static char input_tab_char[MB_LEN_MAX] = "\t";
2831 /* (-e) Tabstops are at chars_per_tab, 2*chars_per_tab, 3*chars_per_tab, ...
2832 where the leftmost column is 1. */
2833 @@ -569,7 +620,10 @@
2834 static bool tabify_output = false;
2836 /* (-i) The output tab character. */
2837 -static char output_tab_char = '\t';
2838 +static char output_tab_char[MB_LEN_MAX] = "\t";
2840 +/* (-i) The byte length of output tab character. */
2841 +static int output_tab_char_length = 1;
2843 /* (-i) The width of the output tab. */
2844 static int chars_per_output_tab = 8;
2845 @@ -643,7 +697,13 @@
2846 static bool numbered_lines = false;
2848 /* (-n) Character which follows each line number. */
2849 -static char number_separator = '\t';
2850 +static char number_separator[MB_LEN_MAX] = "\t";
2852 +/* (-n) The byte length of the character which follows each line number. */
2853 +static int number_separator_length = 1;
2855 +/* (-n) The character width of the character which follows each line number. */
2856 +static int number_separator_width = 0;
2858 /* (-n) line counting starts with 1st line of input file (not with 1st
2859 line of 1st page printed). */
2861 -a|COLUMN|-m is a `space' and with the -J option a `tab'. */
2862 static char *col_sep_string = "";
2863 static int col_sep_length = 0;
2864 +static int col_sep_width = 0;
2865 static char *column_separator = " ";
2866 static char *line_separator = "\t";
2868 @@ -852,6 +913,13 @@
2869 col_sep_length = (int) strlen (optarg_S);
2870 col_sep_string = xmalloc (col_sep_length + 1);
2871 strcpy (col_sep_string, optarg_S);
2874 + if (MB_CUR_MAX > 1)
2875 + col_sep_width = mbswidth (col_sep_string, 0);
2878 + col_sep_width = col_sep_length;
2882 @@ -877,6 +945,21 @@
2884 atexit (close_stdout);
2886 +/* Define which functions are used, the ones for single byte locale or the ones
2887 + for multibyte locale. */
2889 + if (MB_CUR_MAX > 1)
2891 + print_char = print_char_multi;
2892 + char_to_clump = char_to_clump_multi;
2897 + print_char = print_char_single;
2898 + char_to_clump = char_to_clump_single;
2902 file_names = (argc > 1
2903 ? xmalloc ((argc - 1) * sizeof (char *))
2904 @@ -949,8 +1032,12 @@
2908 - getoptarg (optarg, 'e', &input_tab_char,
2909 - &chars_per_input_tab);
2911 + int dummy_length, dummy_width;
2913 + getoptarg (optarg, 'e', input_tab_char, &dummy_length,
2914 + &dummy_width, &chars_per_input_tab);
2916 /* Could check tab width > 0. */
2917 untabify_input = true;
2919 @@ -963,8 +1050,12 @@
2923 - getoptarg (optarg, 'i', &output_tab_char,
2924 - &chars_per_output_tab);
2928 + getoptarg (optarg, 'i', output_tab_char, &output_tab_char_length,
2929 + &dummy_width, &chars_per_output_tab);
2931 /* Could check tab width > 0. */
2932 tabify_output = true;
2934 @@ -991,8 +1082,8 @@
2936 numbered_lines = true;
2938 - getoptarg (optarg, 'n', &number_separator,
2939 - &chars_per_number);
2940 + getoptarg (optarg, 'n', number_separator, &number_separator_length,
2941 + &number_separator_width, &chars_per_number);
2945 @@ -1031,7 +1122,7 @@
2947 /* Reset an additional input of -s, -S dominates -s */
2948 col_sep_string = "";
2949 - col_sep_length = 0;
2950 + col_sep_length = col_sep_width = 0;
2951 use_col_separator = true;
2953 separator_string (optarg);
2954 @@ -1188,10 +1279,45 @@
2958 -getoptarg (char *arg, char switch_char, char *character, int *number)
2959 +getoptarg (char *arg, char switch_char, char *character, int *character_length,
2960 + int *character_width, int *number)
2962 if (!ISDIGIT (*arg))
2963 - *character = *arg++;
2965 +#ifdef HAVE_MBRTOWC
2966 + if (MB_CUR_MAX > 1) /* for multibyte locale. */
2971 + mbstate_t state = {'\0'};
2973 + mblength = mbrtowc (&wc, arg, strnlen(arg, MB_LEN_MAX), &state);
2975 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
2977 + *character_length = 1;
2978 + *character_width = 1;
2982 + *character_length = (mblength < 1) ? 1 : mblength;
2983 + width = wcwidth (wc);
2984 + *character_width = (width < 0) ? 0 : width;
2987 + strncpy (character, arg, *character_length);
2988 + arg += *character_length;
2990 + else /* for single byte locale. */
2993 + *character = *arg++;
2994 + *character_length = 1;
2995 + *character_width = 1;
3002 @@ -1256,7 +1382,7 @@
3004 col_sep_string = column_separator;
3006 - col_sep_length = 1;
3007 + col_sep_length = col_sep_width = 1;
3008 use_col_separator = true;
3010 /* It's rather pointless to define a TAB separator with column
3011 @@ -1288,11 +1414,11 @@
3012 TAB_WIDTH (chars_per_input_tab, chars_per_number); */
3014 /* Estimate chars_per_text without any margin and keep it constant. */
3015 - if (number_separator == '\t')
3016 + if (number_separator[0] == '\t')
3017 number_width = chars_per_number +
3018 TAB_WIDTH (chars_per_default_tab, chars_per_number);
3020 - number_width = chars_per_number + 1;
3021 + number_width = chars_per_number + number_separator_width;
3023 /* The number is part of the column width unless we are
3024 printing files in parallel. */
3025 @@ -1307,7 +1433,7 @@
3028 chars_per_column = (chars_per_line - chars_used_by_number -
3029 - (columns - 1) * col_sep_length) / columns;
3030 + (columns - 1) * col_sep_width) / columns;
3032 if (chars_per_column < 1)
3033 error (EXIT_FAILURE, 0, _("page width too narrow"));
3034 @@ -1432,7 +1558,7 @@
3036 /* Enlarge p->start_position of first column to use the same form of
3037 padding_not_printed with all columns. */
3038 - h = h + col_sep_length;
3039 + h = h + col_sep_width;
3041 /* This loop takes care of all but the rightmost column. */
3043 @@ -1466,7 +1592,7 @@
3047 - h = h_next + col_sep_length;
3048 + h = h_next + col_sep_width;
3049 h_next = h + chars_per_column;
3052 @@ -1756,9 +1882,9 @@
3053 align_column (COLUMN *p)
3055 padding_not_printed = p->start_position;
3056 - if (padding_not_printed - col_sep_length > 0)
3057 + if (padding_not_printed - col_sep_width > 0)
3059 - pad_across_to (padding_not_printed - col_sep_length);
3060 + pad_across_to (padding_not_printed - col_sep_width);
3061 padding_not_printed = ANYWHERE;
3064 @@ -2029,13 +2155,13 @@
3065 /* May be too generous. */
3066 buff = X2REALLOC (buff, &buff_allocated);
3068 - buff[buff_current++] = c;
3069 + buff[buff_current++] = (unsigned char) c;
3073 add_line_number (COLUMN *p)
3080 @@ -2058,22 +2184,24 @@
3081 /* Tabification is assumed for multiple columns, also for n-separators,
3082 but `default n-separator = TAB' hasn't been given priority over
3083 equal column_width also specified by POSIX. */
3084 - if (number_separator == '\t')
3085 + if (number_separator[0] == '\t')
3087 i = number_width - chars_per_number;
3089 (p->char_func) (' ');
3092 - (p->char_func) (number_separator);
3093 + for (j = 0; j < number_separator_length; j++)
3094 + (p->char_func) (number_separator[j]);
3097 /* To comply with POSIX, we avoid any expansion of default TAB
3098 separator with a single column output. No column_width requirement
3099 has to be considered. */
3101 - (p->char_func) (number_separator);
3102 - if (number_separator == '\t')
3103 + for (j = 0; j < number_separator_length; j++)
3104 + (p->char_func) (number_separator[j]);
3105 + if (number_separator[0] == '\t')
3106 output_position = POS_AFTER_TAB (chars_per_output_tab,
3109 @@ -2234,7 +2362,7 @@
3110 while (goal - h_old > 1
3111 && (h_new = POS_AFTER_TAB (chars_per_output_tab, h_old)) <= goal)
3113 - putchar (output_tab_char);
3114 + fwrite (output_tab_char, sizeof(char), output_tab_char_length, stdout);
3117 while (++h_old <= goal)
3118 @@ -2254,6 +2382,7 @@
3121 int l = col_sep_length;
3122 + int not_space_flag;
3126 @@ -2267,6 +2396,7 @@
3128 for (; separators_not_printed > 0; --separators_not_printed)
3130 + not_space_flag = 0;
3133 /* 3 types of sep_strings: spaces only, spaces and chars,
3134 @@ -2280,12 +2410,15 @@
3138 + not_space_flag = 1;
3139 if (spaces_not_printed > 0)
3140 print_white_space ();
3142 - ++output_position;
3145 + if (not_space_flag)
3146 + output_position += col_sep_width;
3148 /* sep_string ends with some spaces */
3149 if (spaces_not_printed > 0)
3150 print_white_space ();
3151 @@ -2313,7 +2446,7 @@
3152 required number of tabs and spaces. */
3155 -print_char (char c)
3156 +print_char_single (char c)
3160 @@ -2337,6 +2470,74 @@
3164 +#ifdef HAVE_MBRTOWC
3166 +print_char_multi (char c)
3168 + static size_t mbc_pos = 0;
3169 + static char mbc[MB_LEN_MAX] = {'\0'};
3170 + static mbstate_t state = {'\0'};
3171 + mbstate_t state_bak;
3176 + if (tabify_output)
3178 + state_bak = state;
3179 + mbc[mbc_pos++] = c;
3180 + mblength = mbrtowc (&wc, mbc, mbc_pos, &state);
3182 + while (mbc_pos > 0)
3187 + state = state_bak;
3191 + state = state_bak;
3192 + ++output_position;
3194 + memmove (mbc, mbc + 1, MB_CUR_MAX - 1);
3204 + memmove (mbc, mbc + mblength, MB_CUR_MAX - mblength);
3206 + ++spaces_not_printed;
3209 + else if (spaces_not_printed > 0)
3210 + print_white_space ();
3212 + /* Nonprintables are assumed to have width 0, except L'\b'. */
3213 + if ((width = wcwidth (wc)) < 1)
3216 + --output_position;
3219 + output_position += width;
3221 + fwrite (mbc, sizeof(char), mblength, stdout);
3222 + memmove (mbc, mbc + mblength, MB_CUR_MAX - mblength);
3223 + mbc_pos -= mblength;
3232 /* Skip to page PAGE before printing.
3233 PAGE may be larger than total number of pages. */
3235 @@ -2517,9 +2718,9 @@
3236 align_empty_cols = false;
3239 - if (padding_not_printed - col_sep_length > 0)
3240 + if (padding_not_printed - col_sep_width > 0)
3242 - pad_across_to (padding_not_printed - col_sep_length);
3243 + pad_across_to (padding_not_printed - col_sep_width);
3244 padding_not_printed = ANYWHERE;
3247 @@ -2620,9 +2821,9 @@
3251 - if (padding_not_printed - col_sep_length > 0)
3252 + if (padding_not_printed - col_sep_width > 0)
3254 - pad_across_to (padding_not_printed - col_sep_length);
3255 + pad_across_to (padding_not_printed - col_sep_width);
3256 padding_not_printed = ANYWHERE;
3259 @@ -2635,8 +2836,8 @@
3260 if (spaces_not_printed == 0)
3262 output_position = p->start_position + end_vector[line];
3263 - if (p->start_position - col_sep_length == chars_per_margin)
3264 - output_position -= col_sep_length;
3265 + if (p->start_position - col_sep_width == chars_per_margin)
3266 + output_position -= col_sep_width;
3270 @@ -2655,7 +2856,7 @@
3271 number of characters is 1.) */
3274 -char_to_clump (char c)
3275 +char_to_clump_single (char c)
3277 unsigned char uc = c;
3278 char *s = clump_buff;
3279 @@ -2665,10 +2866,10 @@
3281 int chars_per_c = 8;
3283 - if (c == input_tab_char)
3284 + if (c == input_tab_char[0])
3285 chars_per_c = chars_per_input_tab;
3287 - if (c == input_tab_char || c == '\t')
3288 + if (c == input_tab_char[0] || c == '\t')
3290 width = TAB_WIDTH (chars_per_c, input_position);
3292 @@ -2739,6 +2940,154 @@
3296 +#ifdef HAVE_MBRTOWC
3298 +char_to_clump_multi (char c)
3300 + static size_t mbc_pos = 0;
3301 + static char mbc[MB_LEN_MAX] = {'\0'};
3302 + static mbstate_t state = {'\0'};
3303 + mbstate_t state_bak;
3307 + register char *s = clump_buff;
3308 + register int i, j;
3312 + int chars_per_c = 8;
3314 + state_bak = state;
3315 + mbc[mbc_pos++] = c;
3316 + mblength = mbrtowc (&wc, mbc, mbc_pos, &state);
3320 + while (mbc_pos > 0)
3325 + state = state_bak;
3329 + state = state_bak;
3332 + if (use_esc_sequence || use_cntrl_prefix)
3337 + sprintf (esc_buff, "%03o", mbc[0]);
3338 + for (i = 0; i <= 2; ++i)
3339 + *s++ = (int) esc_buff[i];
3351 + /* Fall through */
3354 + if (memcmp (mbc, input_tab_char, mblength) == 0)
3355 + chars_per_c = chars_per_input_tab;
3357 + if (memcmp (mbc, input_tab_char, mblength) == 0 || c == '\t')
3361 + width_inc = TAB_WIDTH (chars_per_c, input_position);
3362 + width += width_inc;
3364 + if (untabify_input)
3366 + for (i = width_inc; i; --i)
3368 + chars += width_inc;
3372 + for (i = 0; i < mblength; i++)
3374 + chars += mblength;
3377 + else if ((wc_width = wcwidth (wc)) < 1)
3379 + if (use_esc_sequence)
3381 + for (i = 0; i < mblength; i++)
3386 + sprintf (esc_buff, "%03o", c);
3387 + for (j = 0; j <= 2; ++j)
3388 + *s++ = (int) esc_buff[j];
3391 + else if (use_cntrl_prefix)
3402 + for (i = 0; i < mblength; i++)
3407 + sprintf (esc_buff, "%03o", c);
3408 + for (j = 0; j <= 2; ++j)
3409 + *s++ = (int) esc_buff[j];
3413 + else if (wc == L'\b')
3422 + chars += mblength;
3423 + for (i = 0; i < mblength; i++)
3429 + width += wc_width;
3430 + chars += mblength;
3431 + for (i = 0; i < mblength; i++)
3435 + memmove (mbc, mbc + mblength, MB_CUR_MAX - mblength);
3436 + mbc_pos -= mblength;
3439 + input_position += width;
3444 /* We've just printed some files and need to clean up things before
3445 looking for more options and printing the next batch of files.
3447 --- coreutils-6.8+/src/cut.c.i18n 2007-01-14 15:41:28.000000000 +0000
3448 +++ coreutils-6.8+/src/cut.c 2007-03-01 15:08:24.000000000 +0000
3452 #include <sys/types.h>
3454 +/* Get mbstate_t, mbrtowc(). */
3456 +# include <wchar.h>
3463 #include "xstrndup.h"
3465 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
3466 + installation; work around this configuration error. */
3467 +#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
3469 +# define MB_LEN_MAX 16
3472 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
3473 +#if HAVE_MBRTOWC && defined mbstate_t
3474 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
3477 /* The official name of this program (e.g., no `g' prefix). */
3478 #define PROGRAM_NAME "cut"
3484 +/* Refill the buffer BUF to get a multibyte character. */
3485 +#define REFILL_BUFFER(BUF, BUFPOS, BUFLEN, STREAM) \
3488 + if (BUFLEN < MB_LEN_MAX && !feof (STREAM) && !ferror (STREAM)) \
3490 + memmove (BUF, BUFPOS, BUFLEN); \
3491 + BUFLEN += fread (BUF + BUFLEN, sizeof(char), BUFSIZ, STREAM); \
3497 +/* Get wide character on BUFPOS. BUFPOS is not included after that.
3498 + If byte sequence is not valid as a character, CONVFAIL is 1. Otherwise 0. */
3499 +#define GET_NEXT_WC_FROM_BUFFER(WC, BUFPOS, BUFLEN, MBLENGTH, STATE, CONVFAIL) \
3502 + mbstate_t state_bak; \
3510 + /* Get a wide character. */ \
3512 + state_bak = STATE; \
3513 + MBLENGTH = mbrtowc ((wchar_t *)&WC, BUFPOS, BUFLEN, &STATE); \
3515 + switch (MBLENGTH) \
3517 + case (size_t)-1: \
3518 + case (size_t)-2: \
3520 + STATE = state_bak; \
3521 + /* Fall througn. */ \
3534 /* The number of bytes allocated for FIELD_1_BUFFER. */
3535 static size_t field_1_bufsize;
3537 -/* The largest field or byte index used as an endpoint of a closed
3538 +/* The largest byte, character or field index used as an endpoint of a closed
3539 or degenerate range specification; this doesn't include the starting
3540 index of right-open-ended ranges. For example, with either range spec
3541 `2-5,9-', `2-3,5,9-' this variable would be set to 5. */
3542 @@ -97,10 +160,11 @@
3544 /* This is a bit vector.
3545 In byte mode, which bytes to output.
3546 + In character mode, which characters to output.
3547 In field mode, which DELIM-separated fields to output.
3548 - Both bytes and fields are numbered starting with 1,
3549 + Bytes, characters and fields are numbered starting with 1,
3550 so the zeroth bit of this array is unused.
3551 - A field or byte K has been selected if
3552 + A byte, character or field K has been selected if
3553 (K <= MAX_RANGE_ENDPOINT and is_printable_field(K))
3554 || (EOL_RANGE_START > 0 && K >= EOL_RANGE_START). */
3555 static unsigned char *printable_field;
3556 @@ -109,9 +173,12 @@
3560 - /* Output characters that are in the given bytes. */
3561 + /* Output bytes that are at the given positions. */
3564 + /* Output characters that are at the given positions. */
3567 /* Output the given delimeter-separated fields. */
3570 @@ -121,6 +188,13 @@
3572 static enum operating_mode operating_mode;
3574 +/* If nonzero, when in byte mode, don't split multibyte characters. */
3575 +static int byte_mode_character_aware;
3577 +/* If nonzero, the function for single byte locale is work
3578 + if this program runs on multibyte locale. */
3579 +static int force_singlebyte_mode;
3581 /* If true do not output lines containing no delimeter characters.
3582 Otherwise, all such lines are printed. This option is valid only
3586 /* The delimeter character for field mode. */
3587 static unsigned char delim;
3589 +static wchar_t wcdelim;
3592 /* True if the --output-delimiter=STRING option was specified. */
3593 static bool output_delimiter_specified;
3595 -f, --fields=LIST select only these fields; also print any line\n\
3596 that contains no delimiter character, unless\n\
3597 the -s option is specified\n\
3599 + -n with -b: don't split multibyte characters\n\
3602 --complement complement the set of selected bytes, characters\n\
3605 /* Starting a range. */
3607 - FATAL_ERROR (_("invalid byte or field list"));
3608 + FATAL_ERROR (_("invalid byte, character or field list"));
3612 @@ -387,14 +464,16 @@
3615 /* `n-'. From `initial' to end of line. */
3616 - eol_range_start = initial;
3617 + if (eol_range_start == 0 ||
3618 + (eol_range_start != 0 && eol_range_start > initial))
3619 + eol_range_start = initial;
3624 /* `m-n' or `-n' (1-n). */
3625 if (value < initial)
3626 - FATAL_ERROR (_("invalid byte or field list"));
3627 + FATAL_ERROR (_("invalid byte, character or field list"));
3629 /* Is there already a range going to end of line? */
3630 if (eol_range_start != 0)
3632 if (operating_mode == byte_mode)
3634 _("byte offset %s is too large"), quote (bad_num));
3635 + else if (operating_mode == character_mode)
3637 + _("character offset %s is too large"), quote (bad_num));
3640 _("field number %s is too large"), quote (bad_num));
3645 - FATAL_ERROR (_("invalid byte or field list"));
3646 + FATAL_ERROR (_("invalid byte, character or field list"));
3649 max_range_endpoint = 0;
3650 @@ -570,6 +652,63 @@
3655 +/* This function is in use for the following case.
3657 + 1. Read from the stream STREAM, printing to standard output any selected
3660 + 2. Read from stream STREAM, printing to standard output any selected bytes,
3661 + without splitting multibyte characters. */
3664 +cut_characters_or_cut_bytes_no_split (FILE *stream)
3666 + int idx; /* number of bytes or characters in the line so far. */
3667 + char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
3668 + char *bufpos; /* Next read position of BUF. */
3669 + size_t buflen; /* The length of the byte sequence in buf. */
3670 + wint_t wc; /* A gotten wide character. */
3671 + size_t mblength; /* The byte size of a multibyte character which shows
3672 + as same character as WC. */
3673 + mbstate_t state; /* State of the stream. */
3674 + int convfail; /* 1, when conversion is failed. Otherwise 0. */
3679 + memset (&state, '\0', sizeof(mbstate_t));
3683 + REFILL_BUFFER (buf, bufpos, buflen, stream);
3685 + GET_NEXT_WC_FROM_BUFFER (wc, bufpos, buflen, mblength, state, convfail);
3693 + else if (wc == L'\n')
3700 + idx += (operating_mode == byte_mode) ? mblength : 1;
3701 + if (print_kth (idx, NULL))
3702 + fwrite (bufpos, mblength, sizeof(char), stdout);
3705 + buflen -= mblength;
3706 + bufpos += mblength;
3711 /* Read from stream STREAM, printing to standard output any selected fields. */
3714 @@ -692,13 +831,192 @@
3720 +cut_fields_mb (FILE *stream)
3723 + unsigned int field_idx;
3724 + int found_any_selected_field;
3725 + int buffer_first_field;
3727 + char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
3728 + char *bufpos; /* Next read position of BUF. */
3729 + size_t buflen; /* The length of the byte sequence in buf. */
3730 + wint_t wc = 0; /* A gotten wide character. */
3731 + size_t mblength; /* The byte size of a multibyte character which shows
3732 + as same character as WC. */
3733 + mbstate_t state; /* State of the stream. */
3734 + int convfail; /* 1, when conversion is failed. Otherwise 0. */
3736 + found_any_selected_field = 0;
3740 + memset (&state, '\0', sizeof(mbstate_t));
3742 + c = getc (stream);
3743 + empty_input = (c == EOF);
3745 + ungetc (c, stream);
3749 + /* To support the semantics of the -s flag, we may have to buffer
3750 + all of the first field to determine whether it is `delimited.'
3751 + But that is unnecessary if all non-delimited lines must be printed
3752 + and the first field has been selected, or if non-delimited lines
3753 + must be suppressed and the first field has *not* been selected.
3754 + That is because a non-delimited line has exactly one field. */
3755 + buffer_first_field = (suppress_non_delimited ^ !print_kth (1, NULL));
3759 + if (field_idx == 1 && buffer_first_field)
3765 + REFILL_BUFFER (buf, bufpos, buflen, stream);
3767 + GET_NEXT_WC_FROM_BUFFER
3768 + (wc, bufpos, buflen, mblength, state, convfail);
3773 + field_1_buffer = xrealloc (field_1_buffer, len + mblength);
3774 + memcpy (field_1_buffer + len, bufpos, mblength);
3776 + buflen -= mblength;
3777 + bufpos += mblength;
3779 + if (!convfail && (wc == L'\n' || wc == wcdelim))
3786 + /* If the first field extends to the end of line (it is not
3787 + delimited) and we are printing all non-delimited lines,
3788 + print this one. */
3789 + if (convfail || (!convfail && wc != wcdelim))
3791 + if (suppress_non_delimited)
3797 + fwrite (field_1_buffer, sizeof (char), len, stdout);
3798 + /* Make sure the output line is newline terminated. */
3799 + if (convfail || (!convfail && wc != L'\n'))
3805 + if (print_kth (1, NULL))
3807 + /* Print the field, but not the trailing delimiter. */
3808 + fwrite (field_1_buffer, sizeof (char), len - 1, stdout);
3809 + found_any_selected_field = 1;
3816 + if (print_kth (field_idx, NULL))
3818 + if (found_any_selected_field)
3820 + fwrite (output_delimiter_string, sizeof (char),
3821 + output_delimiter_length, stdout);
3823 + found_any_selected_field = 1;
3828 + REFILL_BUFFER (buf, bufpos, buflen, stream);
3830 + GET_NEXT_WC_FROM_BUFFER
3831 + (wc, bufpos, buflen, mblength, state, convfail);
3835 + else if (!convfail && (wc == wcdelim || wc == L'\n'))
3837 + buflen -= mblength;
3838 + bufpos += mblength;
3842 + if (print_kth (field_idx, NULL))
3843 + fwrite (bufpos, mblength, sizeof(char), stdout);
3845 + buflen -= mblength;
3846 + bufpos += mblength;
3850 + if ((!convfail || wc == L'\n') && buflen < 1)
3853 + if (!convfail && wc == wcdelim)
3855 + else if (wc == WEOF || (!convfail && wc == L'\n'))
3857 + if (found_any_selected_field
3858 + || (!empty_input && !(suppress_non_delimited && field_idx == 1)))
3863 + found_any_selected_field = 0;
3870 cut_stream (FILE *stream)
3872 - if (operating_mode == byte_mode)
3873 - cut_bytes (stream);
3875 + if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
3877 + switch (operating_mode)
3880 + if (byte_mode_character_aware)
3881 + cut_characters_or_cut_bytes_no_split (stream);
3883 + cut_bytes (stream);
3886 + case character_mode:
3887 + cut_characters_or_cut_bytes_no_split (stream);
3891 + cut_fields_mb (stream);
3899 - cut_fields (stream);
3902 + if (operating_mode == field_mode)
3903 + cut_fields (stream);
3905 + cut_bytes (stream);
3909 /* Process file FILE to standard output.
3910 @@ -748,6 +1066,8 @@
3912 bool delim_specified = false;
3913 char *spec_list_string IF_LINT(= NULL);
3914 + char mbdelim[MB_LEN_MAX + 1];
3915 + size_t delimlen = 0;
3917 initialize_main (&argc, &argv);
3918 program_name = argv[0];
3919 @@ -770,7 +1090,6 @@
3924 /* Build the byte list. */
3925 if (operating_mode != undefined_mode)
3926 FATAL_ERROR (_("only one type of list may be specified"));
3927 @@ -778,6 +1097,14 @@
3928 spec_list_string = optarg;
3932 + /* Build the character list. */
3933 + if (operating_mode != undefined_mode)
3934 + FATAL_ERROR (_("only one type of list may be specified"));
3935 + operating_mode = character_mode;
3936 + spec_list_string = optarg;
3940 /* Build the field list. */
3941 if (operating_mode != undefined_mode)
3942 @@ -789,10 +1116,35 @@
3944 /* New delimiter. */
3945 /* Interpret -d '' to mean `use the NUL byte as the delimiter.' */
3946 - if (optarg[0] != '\0' && optarg[1] != '\0')
3947 - FATAL_ERROR (_("the delimiter must be a single character"));
3948 - delim = optarg[0];
3949 - delim_specified = true;
3952 + if(MB_CUR_MAX > 1)
3956 + memset (&state, '\0', sizeof(mbstate_t));
3957 + delimlen = mbrtowc (&wcdelim, optarg, strnlen(optarg, MB_LEN_MAX), &state);
3959 + if (delimlen == (size_t)-1 || delimlen == (size_t)-2)
3960 + ++force_singlebyte_mode;
3963 + delimlen = (delimlen < 1) ? 1 : delimlen;
3964 + if (wcdelim != L'\0' && *(optarg + delimlen) != '\0')
3965 + FATAL_ERROR (_("the delimiter must be a single character"));
3966 + memcpy (mbdelim, optarg, delimlen);
3970 + if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
3973 + if (optarg[0] != '\0' && optarg[1] != '\0')
3974 + FATAL_ERROR (_("the delimiter must be a single character"));
3975 + delim = (unsigned char) optarg[0];
3977 + delim_specified = true;
3981 case OUTPUT_DELIMITER_OPTION:
3982 @@ -805,6 +1157,7 @@
3986 + byte_mode_character_aware = 1;
3990 @@ -827,7 +1180,7 @@
3991 if (operating_mode == undefined_mode)
3992 FATAL_ERROR (_("you must specify a list of bytes, characters, or fields"));
3994 - if (delim != '\0' && operating_mode != field_mode)
3995 + if (delim_specified && operating_mode != field_mode)
3996 FATAL_ERROR (_("an input delimiter may be specified only\
3997 when operating on fields"));
3999 @@ -854,15 +1207,34 @@
4002 if (!delim_specified)
4006 +#ifdef HAVE_MBRTOWC
4008 + mbdelim[0] = '\t';
4009 + mbdelim[1] = '\0';
4014 if (output_delimiter_string == NULL)
4016 - static char dummy[2];
4019 - output_delimiter_string = dummy;
4020 - output_delimiter_length = 1;
4021 +#ifdef HAVE_MBRTOWC
4022 + if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
4024 + output_delimiter_string = xstrdup(mbdelim);
4025 + output_delimiter_length = delimlen;
4028 + if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
4031 + static char dummy[2];
4034 + output_delimiter_string = dummy;
4035 + output_delimiter_length = 1;