1 Submitted by: Alexander E. Patrakov
3 Initial Package Version: 5.93
4 Upstream Status: Not accepted, but planned for 6.0
5 Origin: RedHat CVS, see below how to regenerate
6 http://cvs.fedora.redhat.com/viewcvs/*checkout*/devel/coreutils/coreutils-i18n.patch?rev=1.14
7 Description: This patch fixes various problems with multibyte character support.
8 LSB >= 2.0 tests for features added by this patch, but only Coreutils-5.2.1 plus
9 http://www.linuxfromscratch.org/~alexander/patches/coreutils-5.2.1-i18n_fixes-1.patch
10 actually pass the Li18nux2000-level1 testsuite.
13 Get http://cvs.fedora.redhat.com/viewcvs/*checkout*/devel/coreutils/coreutils-i18n.patch?rev=1.14
14 Replace the coreutils-5.93/tests/sort/Makefile.in hunk with that from rev 1.13
17 --- coreutils-5.93/lib/linebuffer.h.i18n 2005-05-14 08:58:06.000000000 +0100
18 +++ coreutils-5.93/lib/linebuffer.h 2005-12-23 08:53:01.000000000 +0000
28 /* A `struct linebuffer' holds a line of text. */
32 size_t size; /* Allocated. */
33 size_t length; /* Used. */
40 /* Initialize linebuffer LINEBUFFER for use. */
41 --- coreutils-5.93/src/cut.c.i18n 2005-08-12 08:16:25.000000000 +0100
42 +++ coreutils-5.93/src/cut.c 2005-12-23 08:53:01.000000000 +0000
46 #include <sys/types.h>
48 +/* Get mbstate_t, mbrtowc(). */
59 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
60 + installation; work around this configuration error. */
61 +#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
63 +# define MB_LEN_MAX 16
66 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
67 +#if HAVE_MBRTOWC && defined mbstate_t
68 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
71 /* The official name of this program (e.g., no `g' prefix). */
72 #define PROGRAM_NAME "cut"
78 +/* Refill the buffer BUF to get a multibyte character. */
79 +#define REFILL_BUFFER(BUF, BUFPOS, BUFLEN, STREAM) \
82 + if (BUFLEN < MB_LEN_MAX && !feof (STREAM) && !ferror (STREAM)) \
84 + memmove (BUF, BUFPOS, BUFLEN); \
85 + BUFLEN += fread (BUF + BUFLEN, sizeof(char), BUFSIZ, STREAM); \
91 +/* Get wide character on BUFPOS. BUFPOS is not included after that.
92 + If byte sequence is not valid as a character, CONVFAIL is 1. Otherwise 0. */
93 +#define GET_NEXT_WC_FROM_BUFFER(WC, BUFPOS, BUFLEN, MBLENGTH, STATE, CONVFAIL) \
96 + mbstate_t state_bak; \
104 + /* Get a wide character. */ \
106 + state_bak = STATE; \
107 + MBLENGTH = mbrtowc ((wchar_t *)&WC, BUFPOS, BUFLEN, &STATE); \
109 + switch (MBLENGTH) \
114 + STATE = state_bak; \
115 + /* Fall througn. */ \
128 /* The number of bytes allocated for FIELD_1_BUFFER. */
129 static size_t field_1_bufsize;
131 -/* The largest field or byte index used as an endpoint of a closed
132 +/* The largest byte, character or field index used as an endpoint of a closed
133 or degenerate range specification; this doesn't include the starting
134 index of right-open-ended ranges. For example, with either range spec
135 `2-5,9-', `2-3,5,9-' this variable would be set to 5. */
138 /* This is a bit vector.
139 In byte mode, which bytes to output.
140 + In character mode, which characters to output.
141 In field mode, which DELIM-separated fields to output.
142 - Both bytes and fields are numbered starting with 1,
143 + Bytes, characters and fields are numbered starting with 1,
144 so the zeroth bit of this array is unused.
145 - A field or byte K has been selected if
146 + A byte, character or field K has been selected if
147 (K <= MAX_RANGE_ENDPOINT and is_printable_field(K))
148 || (EOL_RANGE_START > 0 && K >= EOL_RANGE_START). */
149 static unsigned char *printable_field;
154 - /* Output characters that are in the given bytes. */
155 + /* Output bytes that are at the given positions. */
158 + /* Output characters that are at the given positions. */
161 /* Output the given delimeter-separated fields. */
166 static enum operating_mode operating_mode;
168 +/* If nonzero, when in byte mode, don't split multibyte characters. */
169 +static int byte_mode_character_aware;
171 +/* If nonzero, the function for single byte locale is work
172 + if this program runs on multibyte locale. */
173 +static int force_singlebyte_mode;
175 /* If true do not output lines containing no delimeter characters.
176 Otherwise, all such lines are printed. This option is valid only
180 /* The delimeter character for field mode. */
181 static unsigned char delim;
183 +static wchar_t wcdelim;
186 /* True if the --output-delimiter=STRING option was specified. */
187 static bool output_delimiter_specified;
189 -f, --fields=LIST select only these fields; also print any line\n\
190 that contains no delimiter character, unless\n\
191 the -s option is specified\n\
193 + -n with -b: don't split multibyte characters\n\
196 --complement complement the set of selected bytes, characters\n\
199 /* Starting a range. */
201 - FATAL_ERROR (_("invalid byte or field list"));
202 + FATAL_ERROR (_("invalid byte, character or field list"));
206 @@ -385,14 +462,16 @@
209 /* `n-'. From `initial' to end of line. */
210 - eol_range_start = initial;
211 + if (eol_range_start == 0 ||
212 + (eol_range_start != 0 && eol_range_start > initial))
213 + eol_range_start = initial;
218 /* `m-n' or `-n' (1-n). */
220 - FATAL_ERROR (_("invalid byte or field list"));
221 + FATAL_ERROR (_("invalid byte, character or field list"));
223 /* Is there already a range going to end of line? */
224 if (eol_range_start != 0)
226 if (operating_mode == byte_mode)
228 _("byte offset %s is too large"), quote (bad_num));
229 + else if (operating_mode == character_mode)
231 + _("character offset %s is too large"), quote (bad_num));
234 _("field number %s is too large"), quote (bad_num));
239 - FATAL_ERROR (_("invalid byte or field list"));
240 + FATAL_ERROR (_("invalid byte, character or field list"));
243 max_range_endpoint = 0;
249 +/* This function is in use for the following case.
251 + 1. Read from the stream STREAM, printing to standard output any selected
254 + 2. Read from stream STREAM, printing to standard output any selected bytes,
255 + without splitting multibyte characters. */
258 +cut_characters_or_cut_bytes_no_split (FILE *stream)
260 + int idx; /* number of bytes or characters in the line so far. */
261 + char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
262 + char *bufpos; /* Next read position of BUF. */
263 + size_t buflen; /* The length of the byte sequence in buf. */
264 + wint_t wc; /* A gotten wide character. */
265 + size_t mblength; /* The byte size of a multibyte character which shows
266 + as same character as WC. */
267 + mbstate_t state; /* State of the stream. */
268 + int convfail; /* 1, when conversion is failed. Otherwise 0. */
273 + memset (&state, '\0', sizeof(mbstate_t));
277 + REFILL_BUFFER (buf, bufpos, buflen, stream);
279 + GET_NEXT_WC_FROM_BUFFER (wc, bufpos, buflen, mblength, state, convfail);
287 + else if (wc == L'\n')
294 + idx += (operating_mode == byte_mode) ? mblength : 1;
295 + if (print_kth (idx, NULL))
296 + fwrite (bufpos, mblength, sizeof(char), stdout);
299 + buflen -= mblength;
300 + bufpos += mblength;
305 /* Read from stream STREAM, printing to standard output any selected fields. */
308 @@ -689,13 +828,192 @@
314 +cut_fields_mb (FILE *stream)
317 + unsigned int field_idx;
318 + int found_any_selected_field;
319 + int buffer_first_field;
321 + char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
322 + char *bufpos; /* Next read position of BUF. */
323 + size_t buflen; /* The length of the byte sequence in buf. */
324 + wint_t wc = 0; /* A gotten wide character. */
325 + size_t mblength; /* The byte size of a multibyte character which shows
326 + as same character as WC. */
327 + mbstate_t state; /* State of the stream. */
328 + int convfail; /* 1, when conversion is failed. Otherwise 0. */
330 + found_any_selected_field = 0;
334 + memset (&state, '\0', sizeof(mbstate_t));
337 + empty_input = (c == EOF);
339 + ungetc (c, stream);
343 + /* To support the semantics of the -s flag, we may have to buffer
344 + all of the first field to determine whether it is `delimited.'
345 + But that is unnecessary if all non-delimited lines must be printed
346 + and the first field has been selected, or if non-delimited lines
347 + must be suppressed and the first field has *not* been selected.
348 + That is because a non-delimited line has exactly one field. */
349 + buffer_first_field = (suppress_non_delimited ^ !print_kth (1, NULL));
353 + if (field_idx == 1 && buffer_first_field)
359 + REFILL_BUFFER (buf, bufpos, buflen, stream);
361 + GET_NEXT_WC_FROM_BUFFER
362 + (wc, bufpos, buflen, mblength, state, convfail);
367 + field_1_buffer = xrealloc (field_1_buffer, len + mblength);
368 + memcpy (field_1_buffer + len, bufpos, mblength);
370 + buflen -= mblength;
371 + bufpos += mblength;
373 + if (!convfail && (wc == L'\n' || wc == wcdelim))
380 + /* If the first field extends to the end of line (it is not
381 + delimited) and we are printing all non-delimited lines,
383 + if (convfail || (!convfail && wc != wcdelim))
385 + if (suppress_non_delimited)
391 + fwrite (field_1_buffer, sizeof (char), len, stdout);
392 + /* Make sure the output line is newline terminated. */
393 + if (convfail || (!convfail && wc != L'\n'))
399 + if (print_kth (1, NULL))
401 + /* Print the field, but not the trailing delimiter. */
402 + fwrite (field_1_buffer, sizeof (char), len - 1, stdout);
403 + found_any_selected_field = 1;
410 + if (print_kth (field_idx, NULL))
412 + if (found_any_selected_field)
414 + fwrite (output_delimiter_string, sizeof (char),
415 + output_delimiter_length, stdout);
417 + found_any_selected_field = 1;
422 + REFILL_BUFFER (buf, bufpos, buflen, stream);
424 + GET_NEXT_WC_FROM_BUFFER
425 + (wc, bufpos, buflen, mblength, state, convfail);
429 + else if (!convfail && (wc == wcdelim || wc == L'\n'))
431 + buflen -= mblength;
432 + bufpos += mblength;
436 + if (print_kth (field_idx, NULL))
437 + fwrite (bufpos, mblength, sizeof(char), stdout);
439 + buflen -= mblength;
440 + bufpos += mblength;
444 + if ((!convfail || wc == L'\n') && buflen < 1)
447 + if (!convfail && wc == wcdelim)
449 + else if (wc == WEOF || (!convfail && wc == L'\n'))
451 + if (found_any_selected_field
452 + || (!empty_input && !(suppress_non_delimited && field_idx == 1)))
457 + found_any_selected_field = 0;
464 cut_stream (FILE *stream)
466 - if (operating_mode == byte_mode)
467 - cut_bytes (stream);
469 + if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
471 + switch (operating_mode)
474 + if (byte_mode_character_aware)
475 + cut_characters_or_cut_bytes_no_split (stream);
477 + cut_bytes (stream);
480 + case character_mode:
481 + cut_characters_or_cut_bytes_no_split (stream);
485 + cut_fields_mb (stream);
493 - cut_fields (stream);
496 + if (operating_mode == field_mode)
497 + cut_fields (stream);
499 + cut_bytes (stream);
503 /* Process file FILE to standard output.
506 bool delim_specified = false;
507 char *spec_list_string IF_LINT(= NULL);
508 + char mbdelim[MB_LEN_MAX + 1];
509 + size_t delimlen = 0;
511 initialize_main (&argc, &argv);
512 program_name = argv[0];
518 /* Build the byte list. */
519 if (operating_mode != undefined_mode)
520 FATAL_ERROR (_("only one type of list may be specified"));
521 @@ -775,6 +1094,14 @@
522 spec_list_string = optarg;
526 + /* Build the character list. */
527 + if (operating_mode != undefined_mode)
528 + FATAL_ERROR (_("only one type of list may be specified"));
529 + operating_mode = character_mode;
530 + spec_list_string = optarg;
534 /* Build the field list. */
535 if (operating_mode != undefined_mode)
536 @@ -786,10 +1113,35 @@
539 /* Interpret -d '' to mean `use the NUL byte as the delimiter.' */
540 - if (optarg[0] != '\0' && optarg[1] != '\0')
541 - FATAL_ERROR (_("the delimiter must be a single character"));
543 - delim_specified = true;
550 + memset (&state, '\0', sizeof(mbstate_t));
551 + delimlen = mbrtowc (&wcdelim, optarg, strnlen(optarg, MB_LEN_MAX), &state);
553 + if (delimlen == (size_t)-1 || delimlen == (size_t)-2)
554 + ++force_singlebyte_mode;
557 + delimlen = (delimlen < 1) ? 1 : delimlen;
558 + if (wcdelim != L'\0' && *(optarg + delimlen) != '\0')
559 + FATAL_ERROR (_("the delimiter must be a single character"));
560 + memcpy (mbdelim, optarg, delimlen);
564 + if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
567 + if (optarg[0] != '\0' && optarg[1] != '\0')
568 + FATAL_ERROR (_("the delimiter must be a single character"));
569 + delim = (unsigned char) optarg[0];
571 + delim_specified = true;
575 case OUTPUT_DELIMITER_OPTION:
580 + byte_mode_character_aware = 1;
585 if (operating_mode == undefined_mode)
586 FATAL_ERROR (_("you must specify a list of bytes, characters, or fields"));
588 - if (delim != '\0' && operating_mode != field_mode)
589 + if (delim_specified && operating_mode != field_mode)
590 FATAL_ERROR (_("an input delimiter may be specified only\
591 when operating on fields"));
593 @@ -851,15 +1204,34 @@
596 if (!delim_specified)
608 if (output_delimiter_string == NULL)
610 - static char dummy[2];
613 - output_delimiter_string = dummy;
614 - output_delimiter_length = 1;
616 + if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
618 + output_delimiter_string = xstrdup(mbdelim);
619 + output_delimiter_length = delimlen;
622 + if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
625 + static char dummy[2];
628 + output_delimiter_string = dummy;
629 + output_delimiter_length = 1;
634 --- coreutils-5.93/src/pr.c.i18n 2005-09-16 08:50:33.000000000 +0100
635 +++ coreutils-5.93/src/pr.c 2005-12-23 08:53:01.000000000 +0000
639 #include <sys/types.h>
641 +/* Get MB_LEN_MAX. */
643 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
644 + installation; work around this configuration error. */
645 +#if !defined MB_LEN_MAX || MB_LEN_MAX == 1
646 +# define MB_LEN_MAX 16
649 +/* Get MB_CUR_MAX. */
652 +/* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
653 +/* Get mbstate_t, mbrtowc(), wcwidth(). */
658 +/* Get iswprint(). -- for wcwidth(). */
660 +# include <wctype.h>
662 +#if !defined iswprint && !HAVE_ISWPRINT
663 +# define iswprint(wc) 1
668 #include "hard-locale.h"
670 #include "strftime.h"
673 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
674 +#if HAVE_MBRTOWC && defined mbstate_t
675 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
678 +#ifndef HAVE_DECL_WCWIDTH
679 +"this configure-time declaration test was not run"
681 +#if !HAVE_DECL_WCWIDTH
682 +extern int wcwidth ();
685 /* The official name of this program (e.g., no `g' prefix). */
686 #define PROGRAM_NAME "pr"
690 #define NULLCOL (COLUMN *)0
692 -static int char_to_clump (char c);
693 +/* Funtion pointers to switch functions for single byte locale or for
694 + multibyte locale. If multibyte functions do not exist in your sysytem,
695 + these pointers always point the function for single byte locale. */
696 +static void (*print_char) (char c);
697 +static int (*char_to_clump) (char c);
699 +/* Functions for single byte locale. */
700 +static void print_char_single (char c);
701 +static int char_to_clump_single (char c);
703 +/* Functions for multibyte locale. */
704 +static void print_char_multi (char c);
705 +static int char_to_clump_multi (char c);
707 static bool read_line (COLUMN *p);
708 static bool print_page (void);
709 static bool print_stored (COLUMN *p);
711 static void pad_across_to (int position);
712 static void add_line_number (COLUMN *p);
713 static void getoptarg (char *arg, char switch_char, char *character,
714 + int *character_length, int *character_width,
716 void usage (int status);
717 static void print_files (int number_of_files, char **av);
719 static void pad_down (int lines);
720 static void read_rest_of_line (COLUMN *p);
721 static void skip_read (COLUMN *p, int column_number);
722 -static void print_char (char c);
723 static void cleanup (void);
724 static void print_sep_string (void);
725 static void separator_string (const char *optarg_S);
727 we store the leftmost columns contiguously in buff.
728 To print a line from buff, get the index of the first character
729 from line_vector[i], and print up to line_vector[i + 1]. */
731 +static unsigned char *buff;
733 /* Index of the position in buff where the next character
736 static bool untabify_input = false;
738 /* (-e) The input tab character. */
739 -static char input_tab_char = '\t';
740 +static char input_tab_char[MB_LEN_MAX] = "\t";
742 /* (-e) Tabstops are at chars_per_tab, 2*chars_per_tab, 3*chars_per_tab, ...
743 where the leftmost column is 1. */
745 static bool tabify_output = false;
747 /* (-i) The output tab character. */
748 -static char output_tab_char = '\t';
749 +static char output_tab_char[MB_LEN_MAX] = "\t";
751 +/* (-i) The byte length of output tab character. */
752 +static int output_tab_char_length = 1;
754 /* (-i) The width of the output tab. */
755 static int chars_per_output_tab = 8;
757 static bool numbered_lines = false;
759 /* (-n) Character which follows each line number. */
760 -static char number_separator = '\t';
761 +static char number_separator[MB_LEN_MAX] = "\t";
763 +/* (-n) The byte length of the character which follows each line number. */
764 +static int number_separator_length = 1;
766 +/* (-n) The character width of the character which follows each line number. */
767 +static int number_separator_width = 0;
769 /* (-n) line counting starts with 1st line of input file (not with 1st
770 line of 1st page printed). */
772 -a|COLUMN|-m is a `space' and with the -J option a `tab'. */
773 static char *col_sep_string = "";
774 static int col_sep_length = 0;
775 +static int col_sep_width = 0;
776 static char *column_separator = " ";
777 static char *line_separator = "\t";
780 col_sep_length = (int) strlen (optarg_S);
781 col_sep_string = xmalloc (col_sep_length + 1);
782 strcpy (col_sep_string, optarg_S);
785 + if (MB_CUR_MAX > 1)
786 + col_sep_width = mbswidth (col_sep_string, 0);
789 + col_sep_width = col_sep_length;
795 atexit (close_stdout);
797 +/* Define which functions are used, the ones for single byte locale or the ones
798 + for multibyte locale. */
800 + if (MB_CUR_MAX > 1)
802 + print_char = print_char_multi;
803 + char_to_clump = char_to_clump_multi;
808 + print_char = print_char_single;
809 + char_to_clump = char_to_clump_single;
813 file_names = (argc > 1
814 ? xmalloc ((argc - 1) * sizeof (char *))
815 @@ -949,8 +1032,12 @@
819 - getoptarg (optarg, 'e', &input_tab_char,
820 - &chars_per_input_tab);
822 + int dummy_length, dummy_width;
824 + getoptarg (optarg, 'e', input_tab_char, &dummy_length,
825 + &dummy_width, &chars_per_input_tab);
827 /* Could check tab width > 0. */
828 untabify_input = true;
830 @@ -963,8 +1050,12 @@
834 - getoptarg (optarg, 'i', &output_tab_char,
835 - &chars_per_output_tab);
839 + getoptarg (optarg, 'i', output_tab_char, &output_tab_char_length,
840 + &dummy_width, &chars_per_output_tab);
842 /* Could check tab width > 0. */
843 tabify_output = true;
847 numbered_lines = true;
849 - getoptarg (optarg, 'n', &number_separator,
850 - &chars_per_number);
851 + getoptarg (optarg, 'n', number_separator, &number_separator_length,
852 + &number_separator_width, &chars_per_number);
856 @@ -1031,7 +1122,7 @@
858 /* Reset an additional input of -s, -S dominates -s */
860 - col_sep_length = 0;
861 + col_sep_length = col_sep_width = 0;
862 use_col_separator = true;
864 separator_string (optarg);
865 @@ -1188,10 +1279,45 @@
869 -getoptarg (char *arg, char switch_char, char *character, int *number)
870 +getoptarg (char *arg, char switch_char, char *character, int *character_length,
871 + int *character_width, int *number)
874 - *character = *arg++;
877 + if (MB_CUR_MAX > 1) /* for multibyte locale. */
882 + mbstate_t state = {'\0'};
884 + mblength = mbrtowc (&wc, arg, strnlen(arg, MB_LEN_MAX), &state);
886 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
888 + *character_length = 1;
889 + *character_width = 1;
893 + *character_length = (mblength < 1) ? 1 : mblength;
894 + width = wcwidth (wc);
895 + *character_width = (width < 0) ? 0 : width;
898 + strncpy (character, arg, *character_length);
899 + arg += *character_length;
901 + else /* for single byte locale. */
904 + *character = *arg++;
905 + *character_length = 1;
906 + *character_width = 1;
913 @@ -1256,7 +1382,7 @@
915 col_sep_string = column_separator;
917 - col_sep_length = 1;
918 + col_sep_length = col_sep_width = 1;
919 use_col_separator = true;
921 /* It's rather pointless to define a TAB separator with column
922 @@ -1288,11 +1414,11 @@
923 TAB_WIDTH (chars_per_input_tab, chars_per_number); */
925 /* Estimate chars_per_text without any margin and keep it constant. */
926 - if (number_separator == '\t')
927 + if (number_separator[0] == '\t')
928 number_width = chars_per_number +
929 TAB_WIDTH (chars_per_default_tab, chars_per_number);
931 - number_width = chars_per_number + 1;
932 + number_width = chars_per_number + number_separator_width;
934 /* The number is part of the column width unless we are
935 printing files in parallel. */
936 @@ -1307,7 +1433,7 @@
939 chars_per_column = (chars_per_line - chars_used_by_number -
940 - (columns - 1) * col_sep_length) / columns;
941 + (columns - 1) * col_sep_width) / columns;
943 if (chars_per_column < 1)
944 error (EXIT_FAILURE, 0, _("page width too narrow"));
945 @@ -1432,7 +1558,7 @@
947 /* Enlarge p->start_position of first column to use the same form of
948 padding_not_printed with all columns. */
949 - h = h + col_sep_length;
950 + h = h + col_sep_width;
952 /* This loop takes care of all but the rightmost column. */
954 @@ -1466,7 +1592,7 @@
958 - h = h_next + col_sep_length;
959 + h = h_next + col_sep_width;
960 h_next = h + chars_per_column;
963 @@ -1756,9 +1882,9 @@
964 align_column (COLUMN *p)
966 padding_not_printed = p->start_position;
967 - if (padding_not_printed - col_sep_length > 0)
968 + if (padding_not_printed - col_sep_width > 0)
970 - pad_across_to (padding_not_printed - col_sep_length);
971 + pad_across_to (padding_not_printed - col_sep_width);
972 padding_not_printed = ANYWHERE;
975 @@ -2029,13 +2155,13 @@
976 /* May be too generous. */
977 buff = X2REALLOC (buff, &buff_allocated);
979 - buff[buff_current++] = c;
980 + buff[buff_current++] = (unsigned char) c;
984 add_line_number (COLUMN *p)
991 @@ -2058,22 +2184,24 @@
992 /* Tabification is assumed for multiple columns, also for n-separators,
993 but `default n-separator = TAB' hasn't been given priority over
994 equal column_width also specified by POSIX. */
995 - if (number_separator == '\t')
996 + if (number_separator[0] == '\t')
998 i = number_width - chars_per_number;
1000 (p->char_func) (' ');
1003 - (p->char_func) (number_separator);
1004 + for (j = 0; j < number_separator_length; j++)
1005 + (p->char_func) (number_separator[j]);
1008 /* To comply with POSIX, we avoid any expansion of default TAB
1009 separator with a single column output. No column_width requirement
1010 has to be considered. */
1012 - (p->char_func) (number_separator);
1013 - if (number_separator == '\t')
1014 + for (j = 0; j < number_separator_length; j++)
1015 + (p->char_func) (number_separator[j]);
1016 + if (number_separator[0] == '\t')
1017 output_position = POS_AFTER_TAB (chars_per_output_tab,
1020 @@ -2234,7 +2362,7 @@
1021 while (goal - h_old > 1
1022 && (h_new = POS_AFTER_TAB (chars_per_output_tab, h_old)) <= goal)
1024 - putchar (output_tab_char);
1025 + fwrite (output_tab_char, sizeof(char), output_tab_char_length, stdout);
1028 while (++h_old <= goal)
1029 @@ -2254,6 +2382,7 @@
1032 int l = col_sep_length;
1033 + int not_space_flag;
1037 @@ -2267,6 +2396,7 @@
1039 for (; separators_not_printed > 0; --separators_not_printed)
1041 + not_space_flag = 0;
1044 /* 3 types of sep_strings: spaces only, spaces and chars,
1045 @@ -2280,12 +2410,15 @@
1049 + not_space_flag = 1;
1050 if (spaces_not_printed > 0)
1051 print_white_space ();
1053 - ++output_position;
1056 + if (not_space_flag)
1057 + output_position += col_sep_width;
1059 /* sep_string ends with some spaces */
1060 if (spaces_not_printed > 0)
1061 print_white_space ();
1062 @@ -2313,7 +2446,7 @@
1063 required number of tabs and spaces. */
1066 -print_char (char c)
1067 +print_char_single (char c)
1071 @@ -2337,6 +2470,74 @@
1075 +#ifdef HAVE_MBRTOWC
1077 +print_char_multi (char c)
1079 + static size_t mbc_pos = 0;
1080 + static unsigned char mbc[MB_LEN_MAX] = {'\0'};
1081 + static mbstate_t state = {'\0'};
1082 + mbstate_t state_bak;
1087 + if (tabify_output)
1089 + state_bak = state;
1090 + mbc[mbc_pos++] = (unsigned char)c;
1091 + mblength = mbrtowc (&wc, mbc, mbc_pos, &state);
1093 + while (mbc_pos > 0)
1098 + state = state_bak;
1102 + state = state_bak;
1103 + ++output_position;
1105 + memmove (mbc, mbc + 1, MB_CUR_MAX - 1);
1115 + memmove (mbc, mbc + mblength, MB_CUR_MAX - mblength);
1117 + ++spaces_not_printed;
1120 + else if (spaces_not_printed > 0)
1121 + print_white_space ();
1123 + /* Nonprintables are assumed to have width 0, except L'\b'. */
1124 + if ((width = wcwidth (wc)) < 1)
1127 + --output_position;
1130 + output_position += width;
1132 + fwrite (mbc, sizeof(char), mblength, stdout);
1133 + memmove (mbc, mbc + mblength, MB_CUR_MAX - mblength);
1134 + mbc_pos -= mblength;
1143 /* Skip to page PAGE before printing.
1144 PAGE may be larger than total number of pages. */
1146 @@ -2517,9 +2718,9 @@
1147 align_empty_cols = false;
1150 - if (padding_not_printed - col_sep_length > 0)
1151 + if (padding_not_printed - col_sep_width > 0)
1153 - pad_across_to (padding_not_printed - col_sep_length);
1154 + pad_across_to (padding_not_printed - col_sep_width);
1155 padding_not_printed = ANYWHERE;
1158 @@ -2620,9 +2821,9 @@
1162 - if (padding_not_printed - col_sep_length > 0)
1163 + if (padding_not_printed - col_sep_width > 0)
1165 - pad_across_to (padding_not_printed - col_sep_length);
1166 + pad_across_to (padding_not_printed - col_sep_width);
1167 padding_not_printed = ANYWHERE;
1170 @@ -2635,8 +2836,8 @@
1171 if (spaces_not_printed == 0)
1173 output_position = p->start_position + end_vector[line];
1174 - if (p->start_position - col_sep_length == chars_per_margin)
1175 - output_position -= col_sep_length;
1176 + if (p->start_position - col_sep_width == chars_per_margin)
1177 + output_position -= col_sep_width;
1181 @@ -2655,7 +2856,7 @@
1182 number of characters is 1.) */
1185 -char_to_clump (char c)
1186 +char_to_clump_single (char c)
1188 unsigned char uc = c;
1189 char *s = clump_buff;
1190 @@ -2665,10 +2866,10 @@
1192 int chars_per_c = 8;
1194 - if (c == input_tab_char)
1195 + if (c == input_tab_char[0])
1196 chars_per_c = chars_per_input_tab;
1198 - if (c == input_tab_char || c == '\t')
1199 + if (c == input_tab_char[0] || c == '\t')
1201 width = TAB_WIDTH (chars_per_c, input_position);
1203 @@ -2739,6 +2940,154 @@
1207 +#ifdef HAVE_MBRTOWC
1209 +char_to_clump_multi (char c)
1211 + static size_t mbc_pos = 0;
1212 + static char mbc[MB_LEN_MAX] = {'\0'};
1213 + static mbstate_t state = {'\0'};
1214 + mbstate_t state_bak;
1218 + register int *s = clump_buff;
1219 + register int i, j;
1223 + int chars_per_c = 8;
1225 + state_bak = state;
1226 + mbc[mbc_pos++] = c;
1227 + mblength = mbrtowc (&wc, mbc, mbc_pos, &state);
1231 + while (mbc_pos > 0)
1236 + state = state_bak;
1240 + state = state_bak;
1243 + if (use_esc_sequence || use_cntrl_prefix)
1248 + sprintf (esc_buff, "%03o", mbc[0]);
1249 + for (i = 0; i <= 2; ++i)
1250 + *s++ = (int) esc_buff[i];
1262 + /* Fall through */
1265 + if (memcmp (mbc, input_tab_char, mblength) == 0)
1266 + chars_per_c = chars_per_input_tab;
1268 + if (memcmp (mbc, input_tab_char, mblength) == 0 || c == '\t')
1272 + width_inc = TAB_WIDTH (chars_per_c, input_position);
1273 + width += width_inc;
1275 + if (untabify_input)
1277 + for (i = width_inc; i; --i)
1279 + chars += width_inc;
1283 + for (i = 0; i < mblength; i++)
1285 + chars += mblength;
1288 + else if ((wc_width = wcwidth (wc)) < 1)
1290 + if (use_esc_sequence)
1292 + for (i = 0; i < mblength; i++)
1297 + sprintf (esc_buff, "%03o", c);
1298 + for (j = 0; j <= 2; ++j)
1299 + *s++ = (int) esc_buff[j];
1302 + else if (use_cntrl_prefix)
1313 + for (i = 0; i < mblength; i++)
1318 + sprintf (esc_buff, "%03o", c);
1319 + for (j = 0; j <= 2; ++j)
1320 + *s++ = (int) esc_buff[j];
1324 + else if (wc == L'\b')
1333 + chars += mblength;
1334 + for (i = 0; i < mblength; i++)
1340 + width += wc_width;
1341 + chars += mblength;
1342 + for (i = 0; i < mblength; i++)
1346 + memmove (mbc, mbc + mblength, MB_CUR_MAX - mblength);
1347 + mbc_pos -= mblength;
1350 + input_position += width;
1355 /* We've just printed some files and need to clean up things before
1356 looking for more options and printing the next batch of files.
1358 --- coreutils-5.93/src/uniq.c.i18n 2005-07-05 07:32:54.000000000 +0100
1359 +++ coreutils-5.93/src/uniq.c 2005-12-23 08:53:01.000000000 +0000
1362 #include <sys/types.h>
1364 +/* Get mbstate_t, mbrtowc(). */
1366 +# include <wchar.h>
1369 +/* Get isw* functions. */
1371 +# include <wctype.h>
1375 #include "argmatch.h"
1376 #include "linebuffer.h"
1379 #include "xmemcoll.h"
1380 #include "xstrtol.h"
1381 -#include "memcasecmp.h"
1382 +#include "xmemcoll.h"
1384 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
1385 + installation; work around this configuration error. */
1386 +#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
1387 +# define MB_LEN_MAX 16
1390 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
1391 +#if HAVE_MBRTOWC && defined mbstate_t
1392 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
1396 /* The official name of this program (e.g., no `g' prefix). */
1397 #define PROGRAM_NAME "uniq"
1398 @@ -109,6 +131,10 @@
1399 /* Select whether/how to delimit groups of duplicate lines. */
1400 static enum delimit_method delimit_groups;
1402 +/* Function pointers. */
1404 +(*find_field) (struct linebuffer *line);
1406 static struct option const longopts[] =
1408 {"count", no_argument, NULL, 'c'},
1410 return a pointer to the beginning of the line's field to be compared. */
1413 -find_field (const struct linebuffer *line)
1414 +find_field_uni (struct linebuffer *line)
1417 char *lp = line->buffer;
1418 @@ -210,6 +236,83 @@
1424 +# define MBCHAR_TO_WCHAR(WC, MBLENGTH, LP, POS, SIZE, STATEP, CONVFAIL) \
1427 + mbstate_t state_bak; \
1430 + state_bak = *STATEP; \
1432 + MBLENGTH = mbrtowc (&WC, LP + POS, SIZE - POS, STATEP); \
1434 + switch (MBLENGTH) \
1436 + case (size_t)-2: \
1437 + case (size_t)-1: \
1438 + *STATEP = state_bak; \
1440 + /* Fall through */ \
1448 +find_field_multi (struct linebuffer *line)
1451 + char *lp = line->buffer;
1452 + size_t size = line->length - 1;
1456 + mbstate_t *statep;
1460 + statep = &(line->state);
1462 + /* skip fields. */
1463 + for (count = 0; count < skip_fields && pos < size; count++)
1465 + while (pos < size)
1467 + MBCHAR_TO_WCHAR (wc, mblength, lp, pos, size, statep, convfail);
1469 + if (convfail || !iswblank (wc))
1477 + while (pos < size)
1479 + MBCHAR_TO_WCHAR (wc, mblength, lp, pos, size, statep, convfail);
1481 + if (!convfail && iswblank (wc))
1488 + /* skip fields. */
1489 + for (count = 0; count < skip_chars && pos < size; count++)
1491 + MBCHAR_TO_WCHAR (wc, mblength, lp, pos, size, statep, convfail);
1499 /* Return false if two strings OLD and NEW match, true if not.
1500 OLD and NEW point not to the beginnings of the lines
1501 but rather to the beginnings of the fields to compare.
1504 different (char *old, char *new, size_t oldlen, size_t newlen)
1506 + char *copy_old, *copy_new;
1508 if (check_chars < oldlen)
1509 oldlen = check_chars;
1510 if (check_chars < newlen)
1511 @@ -225,14 +330,92 @@
1515 - /* FIXME: This should invoke strcoll somehow. */
1516 - return oldlen != newlen || memcasecmp (old, new, oldlen);
1519 + copy_old = alloca (oldlen + 1);
1520 + copy_new = alloca (oldlen + 1);
1522 + for (i = 0; i < oldlen; i++)
1524 + copy_old[i] = toupper (old[i]);
1525 + copy_new[i] = toupper (new[i]);
1528 - else if (hard_LC_COLLATE)
1529 - return xmemcoll (old, oldlen, new, newlen) != 0;
1531 - return oldlen != newlen || memcmp (old, new, oldlen);
1533 + copy_old = (char *)old;
1534 + copy_new = (char *)new;
1537 + return xmemcoll (copy_old, oldlen, copy_new, newlen);
1542 +different_multi (const char *old, const char *new, size_t oldlen, size_t newlen, mbstate_t oldstate, mbstate_t newstate)
1544 + size_t i, j, chars;
1545 + const char *str[2];
1548 + mbstate_t state[2];
1551 + mbstate_t state_bak;
1557 + state[0] = oldstate;
1558 + state[1] = newstate;
1560 + for (i = 0; i < 2; i++)
1562 + copy[i] = alloca (len[i] + 1);
1564 + for (j = 0, chars = 0; j < len[i] && chars < check_chars; chars++)
1566 + state_bak = state[i];
1567 + mblength = mbrtowc (&wc, str[i] + j, len[i] - j, &(state[i]));
1573 + state[i] = state_bak;
1574 + /* Fall through */
1582 + uwc = towupper (wc);
1586 + mbstate_t state_wc;
1588 + memset (&state_wc, '\0', sizeof(mbstate_t));
1589 + wcrtomb (copy[i] + j, uwc, &state_wc);
1592 + memcpy (copy[i] + j, str[i] + j, mblength);
1595 + memcpy (copy[i] + j, str[i] + j, mblength);
1599 + copy[i][j] = '\0';
1603 + return xmemcoll (copy[0], len[0], copy[1], len[1]);
1607 /* Output the line in linebuffer LINE to standard output
1608 provided that the switches say it should be output.
1609 @@ -286,15 +469,43 @@
1611 char *prevfield IF_LINT (= NULL);
1612 size_t prevlen IF_LINT (= 0);
1614 + mbstate_t prevstate;
1616 + memset (&prevstate, '\0', sizeof (mbstate_t));
1619 while (!feof (stdin))
1624 + mbstate_t thisstate;
1627 if (readlinebuffer (thisline, stdin) == 0)
1629 thisfield = find_field (thisline);
1630 thislen = thisline->length - 1 - (thisfield - thisline->buffer);
1632 + if (MB_CUR_MAX > 1)
1634 + thisstate = thisline->state;
1636 + if (prevline->length == 0 || different_multi
1637 + (thisfield, prevfield, thislen, prevlen, thisstate, prevstate))
1639 + fwrite (thisline->buffer, sizeof (char),
1640 + thisline->length, stdout);
1642 + SWAP_LINES (prevline, thisline);
1643 + prevfield = thisfield;
1644 + prevlen = thislen;
1645 + prevstate = thisstate;
1650 if (prevline->length == 0
1651 || different (thisfield, prevfield, thislen, prevlen))
1653 @@ -313,17 +524,26 @@
1655 uintmax_t match_count = 0;
1656 bool first_delimiter = true;
1658 + mbstate_t prevstate;
1661 if (readlinebuffer (prevline, stdin) == 0)
1663 prevfield = find_field (prevline);
1664 prevlen = prevline->length - 1 - (prevfield - prevline->buffer);
1666 + prevstate = prevline->state;
1669 while (!feof (stdin))
1675 + mbstate_t thisstate;
1677 if (readlinebuffer (thisline, stdin) == 0)
1680 @@ -332,6 +552,15 @@
1682 thisfield = find_field (thisline);
1683 thislen = thisline->length - 1 - (thisfield - thisline->buffer);
1685 + if (MB_CUR_MAX > 1)
1687 + thisstate = thisline->state;
1688 + match = !different_multi (thisfield, prevfield,
1689 + thislen, prevlen, thisstate, prevstate);
1693 match = !different (thisfield, prevfield, thislen, prevlen);
1694 match_count += match;
1697 SWAP_LINES (prevline, thisline);
1698 prevfield = thisfield;
1701 + prevstate = thisstate;
1706 @@ -408,6 +640,19 @@
1708 atexit (close_stdout);
1711 + if (MB_CUR_MAX > 1)
1713 + find_field = find_field_multi;
1718 + find_field = find_field_uni;
1725 check_chars = SIZE_MAX;
1726 --- coreutils-5.93/src/expand.c.i18n 2005-08-12 08:16:25.000000000 +0100
1727 +++ coreutils-5.93/src/expand.c 2005-12-23 08:53:01.000000000 +0000
1731 #include <sys/types.h>
1733 +/* Get mbstate_t, mbrtowc(), wcwidth(). */
1735 +# include <wchar.h>
1741 #include "xstrndup.h"
1743 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
1744 + installation; work around this configuration error. */
1745 +#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
1746 +# define MB_LEN_MAX 16
1749 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
1750 +#if HAVE_MBRTOWC && defined mbstate_t
1751 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
1754 /* The official name of this program (e.g., no `g' prefix). */
1755 #define PROGRAM_NAME "expand"
1758 stops = num_start + len - 1;
1764 error (0, 0, _("tab size contains invalid character(s): %s"),
1765 @@ -364,6 +382,142 @@
1771 +expand_multibyte (void)
1773 + FILE *fp; /* Input strem. */
1774 + mbstate_t i_state; /* Current shift state of the input stream. */
1775 + mbstate_t i_state_bak; /* Back up the I_STATE. */
1776 + mbstate_t o_state; /* Current shift state of the output stream. */
1777 + char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
1778 + char *bufpos; /* Next read position of BUF. */
1779 + size_t buflen = 0; /* The length of the byte sequence in buf. */
1780 + wchar_t wc; /* A gotten wide character. */
1781 + size_t mblength; /* The byte size of a multibyte character
1782 + which shows as same character as WC. */
1783 + int tab_index = 0; /* Index in `tab_list' of next tabstop. */
1784 + int column = 0; /* Column on screen of the next char. */
1785 + int next_tab_column; /* Column the next tab stop is on. */
1786 + int convert = 1; /* If nonzero, perform translations. */
1788 + fp = next_file ((FILE *) NULL);
1792 + memset (&o_state, '\0', sizeof(mbstate_t));
1793 + memset (&i_state, '\0', sizeof(mbstate_t));
1797 + /* Refill the buffer BUF. */
1798 + if (buflen < MB_LEN_MAX && !feof(fp) && !ferror(fp))
1800 + memmove (buf, bufpos, buflen);
1801 + buflen += fread (buf + buflen, sizeof(char), BUFSIZ, fp);
1805 + /* No character is left in BUF. */
1808 + fp = next_file (fp);
1811 + break; /* No more files. */
1814 + memset (&i_state, '\0', sizeof(mbstate_t));
1819 + /* Get a wide character. */
1820 + i_state_bak = i_state;
1821 + mblength = mbrtowc (&wc, bufpos, buflen, &i_state);
1825 + case (size_t)-1: /* illegal byte sequence. */
1828 + i_state = i_state_bak;
1832 + if (convert_entire_line == 0)
1835 + putchar (*bufpos);
1838 + case 0: /* null. */
1840 + if (convert && convert_entire_line == 0)
1846 + if (wc == L'\n') /* LF. */
1853 + else if (wc == L'\t' && convert) /* Tab. */
1855 + if (tab_size == 0)
1857 + /* Do not let tab_index == first_free_tab;
1858 + stop when it is 1 less. */
1859 + while (tab_index < first_free_tab - 1
1860 + && column >= tab_list[tab_index])
1862 + next_tab_column = tab_list[tab_index];
1863 + if (tab_index < first_free_tab - 1)
1865 + if (column >= next_tab_column)
1866 + next_tab_column = column + 1;
1869 + next_tab_column = column + tab_size - column % tab_size;
1871 + while (column < next_tab_column)
1877 + else /* Others. */
1888 + int width; /* The width of WC. */
1890 + width = wcwidth (wc);
1891 + column += (width > 0) ? width : 0;
1892 + if (convert_entire_line == 0)
1896 + fwrite (bufpos, sizeof(char), mblength, stdout);
1899 + buflen -= mblength;
1900 + bufpos += mblength;
1906 main (int argc, char **argv)
1908 @@ -428,7 +582,12 @@
1910 file_list = (optind < argc ? &argv[optind] : stdin_argv);
1914 + if (MB_CUR_MAX > 1)
1915 + expand_multibyte ();
1920 if (have_read_stdin && fclose (stdin) != 0)
1921 error (EXIT_FAILURE, errno, "-");
1922 --- coreutils-5.93/src/fold.c.i18n 2005-08-12 08:29:38.000000000 +0100
1923 +++ coreutils-5.93/src/fold.c 2005-12-23 08:53:01.000000000 +0000
1926 #include <sys/types.h>
1928 +/* Get mbstate_t, mbrtowc(), wcwidth(). */
1930 +# include <wchar.h>
1933 +/* Get iswprint(), iswblank(), wcwidth(). */
1935 +# include <wctype.h>
1941 #include "xstrtol.h"
1943 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
1944 + installation; work around this configuration error. */
1945 +#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
1947 +# define MB_LEN_MAX 16
1950 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
1951 +#if HAVE_MBRTOWC && defined mbstate_t
1952 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
1957 /* The official name of this program (e.g., no `g' prefix). */
1960 #define AUTHORS "David MacKenzie"
1962 +#define FATAL_ERROR(Message) \
1965 + error (0, 0, (Message)); \
1970 +enum operating_mode
1972 + /* Fold texts by columns that are at the given positions. */
1975 + /* Fold texts by bytes that are at the given positions. */
1978 + /* Fold texts by characters that are at the given positions. */
1982 /* The name this program was run with. */
1985 +/* The argument shows current mode. (Default: column_mode) */
1986 +static enum operating_mode operating_mode;
1988 /* If nonzero, try to break on whitespace. */
1989 static bool break_spaces;
1991 -/* If nonzero, count bytes, not column positions. */
1992 -static bool count_bytes;
1994 /* If nonzero, at least one of the files we read was standard input. */
1995 static bool have_read_stdin;
1997 -static char const shortopts[] = "bsw:0::1::2::3::4::5::6::7::8::9::";
1998 +static char const shortopts[] = "bcsw:0::1::2::3::4::5::6::7::8::9::";
2000 static struct option const longopts[] =
2002 {"bytes", no_argument, NULL, 'b'},
2003 + {"characters", no_argument, NULL, 'c'},
2004 {"spaces", no_argument, NULL, 's'},
2005 {"width", required_argument, NULL, 'w'},
2006 {GETOPT_HELP_OPTION_DECL},
2010 -b, --bytes count bytes rather than columns\n\
2011 + -c, --characters count characters rather than columns\n\
2012 -s, --spaces break at spaces\n\
2013 -w, --width=WIDTH use WIDTH columns instead of 80\n\
2017 adjust_column (size_t column, char c)
2020 + if (operating_mode != byte_mode)
2024 @@ -117,35 +161,14 @@
2028 -/* Fold file FILENAME, or standard input if FILENAME is "-",
2029 - to stdout, with maximum line length WIDTH.
2030 - Return true if successful. */
2033 -fold_file (char *filename, size_t width)
2035 +fold_text (FILE *istream, size_t width, int *saved_errno)
2039 size_t column = 0; /* Screen column where next char will go. */
2040 size_t offset_out = 0; /* Index in `line_out' for next char. */
2041 static char *line_out = NULL;
2042 static size_t allocated_out = 0;
2045 - if (STREQ (filename, "-"))
2048 - have_read_stdin = true;
2051 - istream = fopen (filename, "r");
2053 - if (istream == NULL)
2055 - error (0, errno, "%s", filename);
2059 while ((c = getc (istream)) != EOF)
2061 if (offset_out + 1 >= allocated_out)
2062 @@ -172,6 +195,15 @@
2063 bool found_blank = false;
2064 size_t logical_end = offset_out;
2066 + /* If LINE_OUT has no wide character,
2067 + put a new wide character in LINE_OUT
2068 + if column is bigger than width. */
2069 + if (offset_out == 0)
2071 + line_out[offset_out++] = c;
2075 /* Look for the last blank. */
2078 @@ -218,11 +250,225 @@
2079 line_out[offset_out++] = c;
2082 - saved_errno = errno;
2083 + *saved_errno = errno;
2086 + fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
2093 +fold_multibyte_text (FILE *istream, int width, int *saved_errno)
2095 + char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
2096 + size_t buflen = 0; /* The length of the byte sequence in buf. */
2097 + char *bufpos; /* Next read position of BUF. */
2098 + wint_t wc; /* A gotten wide character. */
2099 + size_t mblength; /* The byte size of a multibyte character which shows
2100 + as same character as WC. */
2101 + mbstate_t state, state_bak; /* State of the stream. */
2102 + int convfail; /* 1, when conversion is failed. Otherwise 0. */
2104 + char *line_out = NULL;
2105 + size_t offset_out = 0; /* Index in `line_out' for next char. */
2106 + size_t allocated_out = 0;
2109 + size_t column = 0;
2111 + size_t last_blank_pos;
2112 + size_t last_blank_column;
2113 + int is_blank_seen;
2114 + int last_blank_increment;
2115 + int is_bs_following_last_blank;
2116 + size_t bs_following_last_blank_num;
2117 + int is_cr_after_last_blank;
2119 +#define CLEAR_FLAGS \
2122 + last_blank_pos = 0; \
2123 + last_blank_column = 0; \
2124 + is_blank_seen = 0; \
2125 + is_bs_following_last_blank = 0; \
2126 + bs_following_last_blank_num = 0; \
2127 + is_cr_after_last_blank = 0; \
2131 +#define START_NEW_LINE \
2142 + memset (&state, '\0', sizeof(mbstate_t));
2144 + for (;; bufpos += mblength, buflen -= mblength)
2146 + if (buflen < MB_LEN_MAX && !feof (istream) && !ferror (istream))
2148 + memmove (buf, bufpos, buflen);
2149 + buflen += fread (buf + buflen, sizeof(char), BUFSIZ, istream);
2156 + /* Get a wide character. */
2158 + state_bak = state;
2159 + mblength = mbrtowc ((wchar_t *)&wc, bufpos, buflen, &state);
2166 + state = state_bak;
2167 + /* Fall through. */
2175 + if (operating_mode == byte_mode) /* byte mode */
2176 + increment = mblength;
2177 + else if (operating_mode == character_mode) /* character mode */
2179 + else /* column mode */
2188 + fwrite (line_out, sizeof(char), offset_out, stdout);
2193 + increment = (column > 0) ? -1 : 0;
2197 + increment = -1 * column;
2201 + increment = 8 - column % 8;
2205 + increment = wcwidth (wc);
2206 + increment = (increment < 0) ? 0 : increment;
2211 + if (column + increment > width && break_spaces && last_blank_pos)
2213 + fwrite (line_out, sizeof(char), last_blank_pos, stdout);
2216 + offset_out = offset_out - last_blank_pos;
2217 + column = column - last_blank_column + ((is_cr_after_last_blank)
2218 + ? last_blank_increment : bs_following_last_blank_num);
2219 + memmove (line_out, line_out + last_blank_pos, offset_out);
2224 + if (column + increment > width && column != 0)
2226 + fwrite (line_out, sizeof(char), offset_out, stdout);
2231 + if (allocated_out < offset_out + mblength)
2233 + allocated_out += 1024;
2234 + line_out = xrealloc (line_out, allocated_out);
2237 + memcpy (line_out + offset_out, bufpos, mblength);
2238 + offset_out += mblength;
2239 + column += increment;
2241 + if (is_blank_seen && !convfail && wc == L'\r')
2242 + is_cr_after_last_blank = 1;
2244 + if (is_bs_following_last_blank && !convfail && wc == L'\b')
2245 + ++bs_following_last_blank_num;
2247 + is_bs_following_last_blank = 0;
2249 + if (break_spaces && !convfail && iswblank (wc))
2251 + last_blank_pos = offset_out;
2252 + last_blank_column = column;
2253 + is_blank_seen = 1;
2254 + last_blank_increment = increment;
2255 + is_bs_following_last_blank = 1;
2256 + bs_following_last_blank_num = 0;
2257 + is_cr_after_last_blank = 0;
2261 + *saved_errno = errno;
2264 fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
2270 +/* Fold file FILENAME, or standard input if FILENAME is "-",
2271 + to stdout, with maximum line length WIDTH.
2272 + Return 0 if successful, 1 if an error occurs. */
2275 +fold_file (char *filename, int width)
2280 + if (STREQ (filename, "-"))
2283 + have_read_stdin = 1;
2286 + istream = fopen (filename, "r");
2288 + if (istream == NULL)
2290 + error (0, errno, "%s", filename);
2294 + /* Define how ISTREAM is being folded. */
2296 + if (MB_CUR_MAX > 1)
2297 + fold_multibyte_text (istream, width, &saved_errno);
2300 + fold_text (istream, width, &saved_errno);
2302 if (ferror (istream))
2304 error (0, saved_errno, "%s", filename);
2307 atexit (close_stdout);
2309 - break_spaces = count_bytes = have_read_stdin = false;
2310 + operating_mode = column_mode;
2311 + break_spaces = have_read_stdin = false;
2313 while ((optc = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1)
2315 @@ -264,7 +511,15 @@
2318 case 'b': /* Count bytes rather than columns. */
2319 - count_bytes = true;
2320 + if (operating_mode != column_mode)
2321 + FATAL_ERROR (_("only one way of folding may be specified"));
2322 + operating_mode = byte_mode;
2326 + if (operating_mode != column_mode)
2327 + FATAL_ERROR (_("only one way of folding may be specified"));
2328 + operating_mode = character_mode;
2331 case 's': /* Break at word boundaries. */
2332 --- coreutils-5.93/src/join.c.i18n 2005-08-12 08:16:25.000000000 +0100
2333 +++ coreutils-5.93/src/join.c 2005-12-23 08:53:01.000000000 +0000
2335 #include <sys/types.h>
2338 +/* Get mbstate_t, mbrtowc(), mbrtowc(), wcwidth(). */
2340 +# include <wchar.h>
2343 +/* Get iswblank(), towupper. */
2345 +# include <wctype.h>
2350 #include "hard-locale.h"
2351 #include "linebuffer.h"
2352 -#include "memcasecmp.h"
2354 #include "stdio--.h"
2355 #include "xmemcoll.h"
2356 #include "xstrtol.h"
2358 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
2359 +#if HAVE_MBRTOWC && defined mbstate_t
2360 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
2363 /* The official name of this program (e.g., no `g' prefix). */
2364 #define PROGRAM_NAME "join"
2366 @@ -104,10 +118,12 @@
2367 /* Last element in `outlist', where a new element can be added. */
2368 static struct outlist *outlist_end = &outlist_head;
2370 -/* Tab character separating fields. If negative, fields are separated
2371 - by any nonempty string of blanks, otherwise by exactly one
2372 - tab character whose value (when cast to unsigned char) equals TAB. */
2373 -static int tab = -1;
2374 +/* Tab character separating fields. If NULL, fields are separated
2375 + by any nonempty string of blanks. */
2376 +static char *tab = NULL;
2378 +/* The number of bytes used for tab. */
2379 +static size_t tablen = 0;
2381 static struct option const longopts[] =
2385 /* Fill in the `fields' structure in LINE. */
2387 +/* Fill in the `fields' structure in LINE. */
2390 xfields (struct line *line)
2392 @@ -206,10 +224,11 @@
2399 + unsigned char t = tab[0];
2401 - for (; (sep = memchr (ptr, tab, lim - ptr)) != NULL; ptr = sep + 1)
2402 + for (; (sep = memchr (ptr, t, lim - ptr)) != NULL; ptr = sep + 1)
2403 extract_field (line, ptr, sep - ptr);
2406 @@ -236,6 +255,148 @@
2407 extract_field (line, ptr, lim - ptr);
2412 +xfields_multibyte (struct line *line)
2414 + char *ptr = line->buf.buffer;
2415 + char const *lim = ptr + line->buf.length - 1;
2417 + size_t mblength = 1;
2418 + mbstate_t state, state_bak;
2420 + memset (&state, 0, sizeof (mbstate_t));
2427 + unsigned char t = tab[0];
2429 + for (; ptr < lim; ptr = sep + mblength)
2434 + state_bak = state;
2435 + mblength = mbrtowc (&wc, sep, lim - sep + 1, &state);
2437 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
2440 + state = state_bak;
2442 + mblength = (mblength < 1) ? 1 : mblength;
2444 + if (mblength == tablen && !memcmp (sep, tab, mblength))
2456 + extract_field (line, ptr, sep - ptr);
2461 + /* Skip leading blanks before the first field. */
2464 + state_bak = state;
2465 + mblength = mbrtowc (&wc, ptr, lim - ptr + 1, &state);
2467 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
2470 + state = state_bak;
2473 + mblength = (mblength < 1) ? 1 : mblength;
2475 + if (!iswblank(wc))
2483 + state_bak = state;
2484 + mblength = mbrtowc (&wc, ptr, lim - ptr + 1, &state);
2485 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
2488 + state = state_bak;
2491 + mblength = (mblength < 1) ? 1 : mblength;
2493 + sep = ptr + mblength;
2494 + while (sep != lim)
2496 + state_bak = state;
2497 + mblength = mbrtowc (&wc, sep, lim - sep + 1, &state);
2498 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
2501 + state = state_bak;
2504 + mblength = (mblength < 1) ? 1 : mblength;
2506 + if (iswblank (wc))
2512 + extract_field (line, ptr, sep - ptr);
2516 + state_bak = state;
2517 + mblength = mbrtowc (&wc, sep, lim - sep + 1, &state);
2518 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
2521 + state = state_bak;
2524 + mblength = (mblength < 1) ? 1 : mblength;
2526 + ptr = sep + mblength;
2527 + while (ptr != lim)
2529 + state_bak = state;
2530 + mblength = mbrtowc (&wc, ptr, lim - ptr + 1, &state);
2531 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
2534 + state = state_bak;
2537 + mblength = (mblength < 1) ? 1 : mblength;
2539 + if (!iswblank (wc))
2545 + while (ptr != lim);
2548 + extract_field (line, ptr, lim - ptr);
2552 /* Read a line from FP into LINE and split it into fields.
2553 Return true if successful. */
2555 @@ -256,6 +417,11 @@
2556 line->nfields_allocated = 0;
2558 line->fields = NULL;
2560 + if (MB_CUR_MAX > 1)
2561 + xfields_multibyte (line);
2567 @@ -310,56 +476,114 @@
2568 keycmp (struct line const *line1, struct line const *line2)
2570 /* Start of field to compare in each file. */
2575 - size_t len2; /* Length of fields to compare. */
2578 + size_t len[2]; /* Length of fields to compare. */
2582 if (join_field_1 < line1->nfields)
2584 - beg1 = line1->fields[join_field_1].beg;
2585 - len1 = line1->fields[join_field_1].len;
2586 + beg[0] = line1->fields[join_field_1].beg;
2587 + len[0] = line1->fields[join_field_1].len;
2597 if (join_field_2 < line2->nfields)
2599 - beg2 = line2->fields[join_field_2].beg;
2600 - len2 = line2->fields[join_field_2].len;
2601 + beg[1] = line2->fields[join_field_2].beg;
2602 + len[1] = line2->fields[join_field_2].len;
2613 - return len2 == 0 ? 0 : -1;
2616 + return len[1] == 0 ? 0 : -1;
2622 - /* FIXME: ignore_case does not work with NLS (in particular,
2623 - with multibyte chars). */
2624 - diff = memcasecmp (beg1, beg2, MIN (len1, len2));
2625 +#ifdef HAVE_MBRTOWC
2626 + if (MB_CUR_MAX > 1)
2630 + mbstate_t state, state_bak;
2632 + memset (&state, '\0', sizeof (mbstate_t));
2634 + for (i = 0; i < 2; i++)
2636 + copy[i] = alloca (len[i] + 1);
2638 + for (j = 0; j < MIN (len[0], len[1]);)
2640 + state_bak = state;
2641 + mblength = mbrtowc (&wc, beg[i] + j, len[i] - j, &state);
2647 + state = state_bak;
2648 + /* Fall through */
2654 + uwc = towupper (wc);
2658 + mbstate_t state_wc;
2660 + memset (&state_wc, '\0', sizeof (mbstate_t));
2661 + wcrtomb (copy[i] + j, uwc, &state_wc);
2664 + memcpy (copy[i] + j, beg[i] + j, mblength);
2668 + copy[i][j] = '\0';
2674 + for (i = 0; i < 2; i++)
2676 + copy[i] = alloca (len[i] + 1);
2678 + for (j = 0; j < MIN (len[0], len[1]); j++)
2679 + copy[i][j] = toupper (beg[i][j]);
2681 + copy[i][j] = '\0';
2687 - if (hard_LC_COLLATE)
2688 - return xmemcoll (beg1, len1, beg2, len2);
2689 - diff = memcmp (beg1, beg2, MIN (len1, len2));
2690 + copy[0] = (unsigned char *) beg[0];
2691 + copy[1] = (unsigned char *) beg[1];
2694 + if (HAVE_SETLOCALE && hard_LC_COLLATE)
2695 + return xmemcoll ((char *) copy[0], len[0], (char *) copy[1], len[1]);
2696 + diff = memcmp (copy[0], copy[1], MIN (len[0], len[1]));
2700 - return len1 < len2 ? -1 : len1 != len2;
2701 + return len[0] - len[1];
2704 /* Print field N of LINE if it exists and is nonempty, otherwise
2705 @@ -384,11 +608,18 @@
2707 /* Print the join of LINE1 and LINE2. */
2709 +#define PUT_TAB_CHAR \
2713 + fwrite(tab, sizeof(char), tablen, stdout) : putchar (' '); \
2718 prjoin (struct line const *line1, struct line const *line2)
2720 const struct outlist *outlist;
2721 - char output_separator = tab < 0 ? ' ' : tab;
2723 outlist = outlist_head.next;
2725 @@ -404,12 +635,12 @@
2728 if (line1 == &uni_blank)
2732 field = join_field_2;
2738 field = join_field_1;
2744 - putchar (output_separator);
2749 @@ -441,23 +672,23 @@
2750 prfield (join_field_1, line1);
2751 for (i = 0; i < join_field_1 && i < line1->nfields; ++i)
2753 - putchar (output_separator);
2757 for (i = join_field_1 + 1; i < line1->nfields; ++i)
2759 - putchar (output_separator);
2764 for (i = 0; i < join_field_2 && i < line2->nfields; ++i)
2766 - putchar (output_separator);
2770 for (i = join_field_2 + 1; i < line2->nfields; ++i)
2772 - putchar (output_separator);
2777 @@ -869,20 +1100,41 @@
2781 - unsigned char newtab = optarg[0];
2786 error (EXIT_FAILURE, 0, _("empty tab"));
2788 + newtab = xstrdup (optarg);
2790 + if (MB_CUR_MAX > 1)
2794 + memset (&state, 0, sizeof (mbstate_t));
2795 + newtablen = mbrtowc (NULL, newtab,
2796 + strnlen (newtab, MB_LEN_MAX),
2798 + if (newtablen == (size_t) 0
2799 + || newtablen == (size_t) -1
2800 + || newtablen == (size_t) -2)
2807 + if (newtablen == 1 && newtab[1])
2809 + if (STREQ (newtab, "\\0"))
2812 + if (tab != NULL && strcmp (tab, newtab))
2814 - if (STREQ (optarg, "\\0"))
2817 - error (EXIT_FAILURE, 0, _("multi-character tab %s"),
2820 + error (EXIT_FAILURE, 0, _("incompatible tabs"));
2822 - if (0 <= tab && tab != newtab)
2823 - error (EXIT_FAILURE, 0, _("incompatible tabs"));
2825 + tablen = newtablen;
2829 --- coreutils-5.93/src/unexpand.c.i18n 2005-08-12 08:16:25.000000000 +0100
2830 +++ coreutils-5.93/src/unexpand.c 2005-12-23 08:53:01.000000000 +0000
2834 #include <sys/types.h>
2836 +/* Get mbstate_t, mbrtowc(), wcwidth(). */
2838 +# include <wchar.h>
2844 #include "xstrndup.h"
2846 +/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
2847 + installation; work around this configuration error. */
2848 +#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
2849 +# define MB_LEN_MAX 16
2852 +/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t. */
2853 +#if HAVE_MBRTOWC && defined mbstate_t
2854 +# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
2857 /* The official name of this program (e.g., no `g' prefix). */
2858 #define PROGRAM_NAME "unexpand"
2860 @@ -110,6 +127,208 @@
2864 +static FILE *next_file (FILE *fp);
2868 +unexpand_multibyte (void)
2870 + FILE *fp; /* Input stream. */
2871 + mbstate_t i_state; /* Current shift state of the input stream. */
2872 + mbstate_t i_state_bak; /* Back up the I_STATE. */
2873 + mbstate_t o_state; /* Current shift state of the output stream. */
2874 + char buf[MB_LEN_MAX + BUFSIZ]; /* For spooling a read byte sequence. */
2875 + char *bufpos; /* Next read position of BUF. */
2876 + size_t buflen = 0; /* The length of the byte sequence in buf. */
2877 + wint_t wc; /* A gotten wide character. */
2878 + size_t mblength; /* The byte size of a multibyte character
2879 + which shows as same character as WC. */
2881 + /* Index in `tab_list' of next tabstop: */
2882 + int tab_index = 0; /* For calculating width of pending tabs. */
2883 + int print_tab_index = 0; /* For printing as many tabs as possible. */
2884 + unsigned int column = 0; /* Column on screen of next char. */
2885 + int next_tab_column; /* Column the next tab stop is on. */
2886 + int convert = 1; /* If nonzero, perform translations. */
2887 + unsigned int pending = 0; /* Pending columns of blanks. */
2889 + fp = next_file ((FILE *) NULL);
2893 + memset (&o_state, '\0', sizeof(mbstate_t));
2894 + memset (&i_state, '\0', sizeof(mbstate_t));
2898 + if (buflen < MB_LEN_MAX && !feof(fp) && !ferror(fp))
2900 + memmove (buf, bufpos, buflen);
2901 + buflen += fread (buf + buflen, sizeof(char), BUFSIZ, fp);
2905 + /* Get a wide character. */
2913 + i_state_bak = i_state;
2914 + mblength = mbrtowc ((wchar_t *)&wc, bufpos, buflen, &i_state);
2917 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
2919 + i_state = i_state_bak;
2923 + if (wc == L' ' && convert && column < INT_MAX)
2928 + else if (wc == L'\t' && convert)
2930 + if (tab_size == 0)
2932 + /* Do not let tab_index == first_free_tab;
2933 + stop when it is 1 less. */
2934 + while (tab_index < first_free_tab - 1
2935 + && column >= tab_list[tab_index])
2937 + next_tab_column = tab_list[tab_index];
2938 + if (tab_index < first_free_tab - 1)
2940 + if (column >= next_tab_column)
2942 + convert = 0; /* Ran out of tab stops. */
2943 + goto flush_pend_mb;
2948 + next_tab_column = column + tab_size - column % tab_size;
2950 + pending += next_tab_column - column;
2951 + column = next_tab_column;
2956 + /* Flush pending spaces. Print as many tabs as possible,
2957 + then print the rest as spaces. */
2963 + column -= pending;
2964 + while (pending > 0)
2966 + if (tab_size == 0)
2968 + /* Do not let print_tab_index == first_free_tab;
2969 + stop when it is 1 less. */
2970 + while (print_tab_index < first_free_tab - 1
2971 + && column >= tab_list[print_tab_index])
2972 + print_tab_index++;
2973 + next_tab_column = tab_list[print_tab_index];
2974 + if (print_tab_index < first_free_tab - 1)
2975 + print_tab_index++;
2980 + column + tab_size - column % tab_size;
2982 + if (next_tab_column - column <= pending)
2985 + pending -= next_tab_column - column;
2986 + column = next_tab_column;
2990 + --print_tab_index;
2991 + column += pending;
2992 + while (pending != 0)
3002 + fp = next_file (fp);
3004 + break; /* No more files. */
3007 + memset (&i_state, '\0', sizeof(mbstate_t));
3012 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
3017 + if (convert_entire_line == 0)
3023 + else if (mblength == 0)
3025 + if (convert && convert_entire_line == 0)
3041 + int width; /* The width of WC. */
3043 + width = wcwidth (wc);
3044 + column += (width > 0) ? width : 0;
3045 + if (convert_entire_line == 0)
3052 + tab_index = print_tab_index = 0;
3053 + column = pending = 0;
3056 + fwrite (bufpos, sizeof(char), mblength, stdout);
3059 + buflen -= mblength;
3060 + bufpos += mblength;
3069 @@ -532,7 +751,12 @@
3071 file_list = (optind < argc ? &argv[optind] : stdin_argv);
3075 + if (MB_CUR_MAX > 1)
3076 + unexpand_multibyte ();
3081 if (have_read_stdin && fclose (stdin) != 0)
3082 error (EXIT_FAILURE, errno, "-");
3083 --- coreutils-5.93/src/sort.c.i18n 2005-10-07 19:48:28.000000000 +0100
3084 +++ coreutils-5.93/src/sort.c 2005-12-23 10:38:44.000000000 +0000
3089 +#include <assert.h>
3091 #include <sys/types.h>
3094 +# include <wchar.h>
3096 +/* Get isw* functions. */
3098 +# include <wctype.h>
3103 #include "hard-locale.h"
3104 @@ -95,14 +104,38 @@
3105 /* Thousands separator; if -1, then there isn't one. */
3106 static int thousands_sep;
3108 +static int force_general_numcompare = 0;
3110 /* Nonzero if the corresponding locales are hard. */
3111 static bool hard_LC_COLLATE;
3112 -#if HAVE_NL_LANGINFO
3113 +#if HAVE_LANGINFO_CODESET
3114 static bool hard_LC_TIME;
3117 #define NONZERO(x) ((x) != 0)
3119 +/* get a multibyte character's byte length. */
3120 +#define GET_BYTELEN_OF_CHAR(LIM, PTR, MBLENGTH, STATE) \
3124 + mbstate_t state_bak; \
3126 + state_bak = STATE; \
3127 + mblength = mbrtowc (&wc, PTR, LIM - PTR, &STATE); \
3129 + switch (MBLENGTH) \
3131 + case (size_t)-1: \
3132 + case (size_t)-2: \
3133 + STATE = state_bak; \
3134 + /* Fall through. */ \
3141 /* The kind of blanks for '-b' to skip in various options. */
3142 enum blanktype { bl_start, bl_end, bl_both };
3144 @@ -239,13 +272,11 @@
3145 they were read if all keys compare equal. */
3148 -/* If TAB has this value, blanks separate fields. */
3149 -enum { TAB_DEFAULT = CHAR_MAX + 1 };
3151 -/* Tab character separating fields. If TAB_DEFAULT, then fields are
3152 +/* Tab character separating fields. If tab_length is 0, then fields are
3153 separated by the empty string between a non-blank character and a blank
3155 -static int tab = TAB_DEFAULT;
3156 +static char tab[MB_LEN_MAX + 1];
3157 +static size_t tab_length = 0;
3159 /* Flag to remove consecutive duplicate lines from the output.
3160 Only the last of a sequence of equal lines will be output. */
3161 @@ -392,6 +423,44 @@
3162 static struct tempnode *volatile temphead;
3163 static struct tempnode *volatile *temptail = &temphead;
3165 +/* Function pointers. */
3167 +(*inittables) (void);
3169 +(*begfield) (const struct line*, const struct keyfield *);
3171 +(*limfield) (const struct line*, const struct keyfield *);
3173 +(*getmonth) (char const *, size_t);
3175 +(*keycompare) (const struct line *, const struct line *);
3177 +(*numcompare) (const char *, const char *);
3179 +/* Test for white space multibyte character.
3180 + Set LENGTH the byte length of investigated multibyte character. */
3183 +ismbblank (const char *str, size_t len, size_t *length)
3189 + memset (&state, '\0', sizeof(mbstate_t));
3190 + mblength = mbrtowc (&wc, str, len, &state);
3192 + if (mblength == (size_t)-1 || mblength == (size_t)-2)
3198 + *length = (mblength < 1) ? 1 : mblength;
3199 + return iswblank (wc);
3203 /* Clean up any remaining temporary files. */
3210 -#if HAVE_NL_LANGINFO
3211 +#if HAVE_LANGINFO_CODESET
3214 struct_month_cmp (const void *m1, const void *m2)
3216 /* Initialize the character class tables. */
3220 +inittables_uni (void)
3225 fold_toupper[i] = (ISLOWER (i) ? toupper (i) : i);
3228 -#if HAVE_NL_LANGINFO
3229 +#if HAVE_LANGINFO_CODESET
3230 /* If we're not in the "C" locale, read different names for months. */
3233 @@ -598,6 +667,64 @@
3239 +inittables_mb (void)
3243 + size_t s_len, mblength;
3244 + char mbc[MB_LEN_MAX];
3246 + mbstate_t state_mb, state_wc;
3248 + for (i = 0; i < MONTHS_PER_YEAR; i++)
3250 + s = (char *) nl_langinfo (ABMON_1 + i);
3251 + s_len = strlen (s);
3252 + monthtab[i].name = name = (char *) xmalloc (s_len + 1);
3253 + monthtab[i].val = i + 1;
3255 + memset (&state_mb, '\0', sizeof (mbstate_t));
3256 + memset (&state_wc, '\0', sizeof (mbstate_t));
3258 + for (j = 0; j < s_len;)
3260 + if (!ismbblank (s + j, s_len - j, &mblength))
3265 + for (k = 0; j < s_len;)
3267 + mblength = mbrtowc (&wc, (s + j), (s_len - j), &state_mb);
3268 + assert (mblength != (size_t)-1 && mblength != (size_t)-2);
3269 + if (mblength == 0)
3272 + pwc = towupper (wc);
3275 + memcpy (mbc, s + j, mblength);
3281 + mblength = wcrtomb (mbc, pwc, &state_wc);
3282 + assert (mblength != (size_t)0 && mblength != (size_t)-1);
3285 + for (l = 0; l < mblength; l++)
3286 + name[k++] = mbc[l];
3290 + qsort ((void *) monthtab, MONTHS_PER_YEAR,
3291 + sizeof (struct month), struct_month_cmp);
3295 /* Specify the amount of main memory to use when sorting. */
3297 specify_sort_size (char const *s)
3302 -begfield (const struct line *line, const struct keyfield *key)
3303 +begfield_uni (const struct line *line, const struct keyfield *key)
3305 char *ptr = line->text, *lim = ptr + line->length - 1;
3306 size_t sword = key->sword;
3307 @@ -818,10 +945,10 @@
3308 /* The leading field separator itself is included in a field when -t
3311 - if (tab != TAB_DEFAULT)
3313 while (ptr < lim && sword--)
3315 - while (ptr < lim && *ptr != tab)
3316 + while (ptr < lim && *ptr != tab[0])
3320 @@ -849,11 +976,70 @@
3326 +begfield_mb (const struct line *line, const struct keyfield *key)
3329 + char *ptr = line->text, *lim = ptr + line->length - 1;
3330 + size_t sword = key->sword;
3331 + size_t schar = key->schar;
3335 + memset (&state, '\0', sizeof(mbstate_t));
3338 + while (ptr < lim && sword--)
3340 + while (ptr < lim && memcmp (ptr, tab, tab_length) != 0)
3342 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
3347 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
3352 + while (ptr < lim && sword--)
3354 + while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
3358 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
3361 + while (ptr < lim && !ismbblank (ptr, lim - ptr, &mblength))
3365 + if (key->skipsblanks)
3366 + while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
3369 + for (i = 0; i < schar; i++)
3371 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
3373 + if (ptr + mblength > lim)
3383 /* Return the limit of (a pointer to the first character after) the field
3384 in LINE specified by KEY. */
3387 -limfield (const struct line *line, const struct keyfield *key)
3388 +limfield_uni (const struct line *line, const struct keyfield *key)
3390 char *ptr = line->text, *lim = ptr + line->length - 1;
3391 size_t eword = key->eword, echar = key->echar;
3392 @@ -866,10 +1052,10 @@
3393 `beginning' is the first character following the delimiting TAB.
3394 Otherwise, leave PTR pointing at the first `blank' character after
3395 the preceding field. */
3396 - if (tab != TAB_DEFAULT)
3398 while (ptr < lim && eword--)
3400 - while (ptr < lim && *ptr != tab)
3401 + while (ptr < lim && *ptr != tab[0])
3403 if (ptr < lim && (eword | echar))
3405 @@ -915,10 +1101,10 @@
3408 /* Make LIM point to the end of (one byte past) the current field. */
3409 - if (tab != TAB_DEFAULT)
3413 - newlim = memchr (ptr, tab, lim - ptr);
3414 + newlim = memchr (ptr, tab[0], lim - ptr);
3418 @@ -951,6 +1137,107 @@
3424 +limfield_mb (const struct line *line, const struct keyfield *key)
3426 + char *ptr = line->text, *lim = ptr + line->length - 1;
3427 + size_t eword = key->eword, echar = key->echar;
3432 + memset (&state, '\0', sizeof(mbstate_t));
3435 + while (ptr < lim && eword--)
3437 + while (ptr < lim && memcmp (ptr, tab, tab_length) != 0)
3439 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
3442 + if (ptr < lim && (eword | echar))
3444 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
3449 + while (ptr < lim && eword--)
3451 + while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
3455 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
3458 + while (ptr < lim && !ismbblank (ptr, lim - ptr, &mblength))
3463 +# ifdef POSIX_UNSPECIFIED
3464 + /* Make LIM point to the end of (one byte past) the current field. */
3470 + for (p = ptr; p < lim;)
3472 + if (memcmp (p, tab, tab_length) == 0)
3478 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
3487 + while (newlim < lim && ismbblank (newlim, lim - newlim, &mblength))
3488 + newlim += mblength;
3491 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
3494 + while (newlim < lim && !ismbblank (newlim, lim - newlim, &mblength))
3495 + newlim += mblength;
3500 + /* If we're skipping leading blanks, don't start counting characters
3501 + * until after skipping past any leading blanks. */
3502 + if (key->skipsblanks)
3503 + while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
3506 + memset (&state, '\0', sizeof(mbstate_t));
3508 + /* Advance PTR by ECHAR (if possible), but no further than LIM. */
3509 + for (i = 0; i < echar; i++)
3511 + GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
3513 + if (ptr + mblength > lim)
3523 /* Fill BUF reading from FP, moving buf->left bytes from the end
3524 of buf->buf to the beginning first. If EOF is reached and the
3525 file wasn't terminated by a newline, supply one. Set up BUF's line
3526 @@ -1067,7 +1354,7 @@
3530 -numcompare (const char *a, const char *b)
3531 +numcompare_uni (const char *a, const char *b)
3533 while (blanks[to_uchar (*a)])
3535 @@ -1077,6 +1364,25 @@
3536 return strnumcmp (a, b, decimal_point, thousands_sep);
3541 +numcompare_mb (const char *a, const char *b)
3543 + size_t mblength, len;
3544 + len = strlen (a); /* okay for UTF-8 */
3545 + while (*a && ismbblank (a, len > MB_CUR_MAX ? MB_CUR_MAX : len, &mblength))
3550 + len = strlen (b); /* okay for UTF-8 */
3551 + while (*b && ismbblank (b, len > MB_CUR_MAX ? MB_CUR_MAX : len, &mblength))
3554 + return strnumcmp (a, b, decimal_point, thousands_sep);
3556 +#endif /* HAV_EMBRTOWC */
3559 general_numcompare (const char *sa, const char *sb)
3561 @@ -1110,7 +1416,7 @@
3562 Return 0 if the name in S is not recognized. */
3565 -getmonth (char const *month, size_t len)
3566 +getmonth_uni (char const *month, size_t len)
3569 size_t hi = MONTHS_PER_YEAR;
3570 @@ -1152,11 +1458,79 @@
3576 +getmonth_mb (const char *s, size_t len)
3579 + register size_t i;
3580 + register int lo = 0, hi = MONTHS_PER_YEAR, result;
3582 + size_t wclength, mblength;
3584 + const wchar_t **wpp;
3585 + wchar_t *month_wcs;
3588 + while (len > 0 && ismbblank (s, len, &mblength))
3597 + month = (char *) alloca (len + 1);
3599 + tmp = (char *) alloca (len + 1);
3600 + memcpy (tmp, s, len);
3602 + pp = (const char **)&tmp;
3603 + month_wcs = (wchar_t *) alloca ((len + 1) * sizeof (wchar_t));
3604 + memset (&state, '\0', sizeof(mbstate_t));
3606 + wclength = mbsrtowcs (month_wcs, pp, len + 1, &state);
3607 + assert (wclength != (size_t)-1 && *pp == NULL);
3609 + for (i = 0; i < wclength; i++)
3611 + month_wcs[i] = towupper(month_wcs[i]);
3612 + if (iswblank (month_wcs[i]))
3614 + month_wcs[i] = L'\0';
3619 + wpp = (const wchar_t **)&month_wcs;
3621 + mblength = wcsrtombs (month, wpp, len + 1, &state);
3622 + assert (mblength != (-1) && *wpp == NULL);
3626 + int ix = (lo + hi) / 2;
3628 + if (strncmp (month, monthtab[ix].name, strlen (monthtab[ix].name)) < 0)
3633 + while (hi - lo > 1);
3635 + result = (!strncmp (month, monthtab[lo].name, strlen (monthtab[lo].name))
3636 + ? monthtab[lo].val : 0);
3642 /* Compare two lines A and B trying every key in sequence until there
3643 are no more keys or a difference is found. */
3646 -keycompare (const struct line *a, const struct line *b)
3647 +keycompare_uni (const struct line *a, const struct line *b)
3649 struct keyfield const *key = keylist;
3651 @@ -1326,6 +1700,177 @@
3652 return key->reverse ? -diff : diff;
3657 +keycompare_mb (const struct line *a, const struct line *b)
3659 + struct keyfield *key = keylist;
3661 + /* For the first iteration only, the key positions have been
3662 + precomputed for us. */
3663 + char *texta = a->keybeg;
3664 + char *textb = b->keybeg;
3665 + char *lima = a->keylim;
3666 + char *limb = b->keylim;
3668 + size_t mblength_a, mblength_b;
3669 + wchar_t wc_a, wc_b;
3670 + mbstate_t state_a, state_b;
3674 + memset (&state_a, '\0', sizeof(mbstate_t));
3675 + memset (&state_b, '\0', sizeof(mbstate_t));
3679 + unsigned char *translate = (unsigned char *) key->translate;
3680 + bool const *ignore = key->ignore;
3682 + /* Find the lengths. */
3683 + size_t lena = lima <= texta ? 0 : lima - texta;
3684 + size_t lenb = limb <= textb ? 0 : limb - textb;
3686 + /* Actually compare the fields. */
3687 + if (key->numeric | key->general_numeric)
3689 + char savea = *lima, saveb = *limb;
3691 + *lima = *limb = '\0';
3692 + if (force_general_numcompare)
3693 + diff = general_numcompare (texta, textb);
3695 + diff = ((key->numeric ? numcompare : general_numcompare)
3697 + *lima = savea, *limb = saveb;
3699 + else if (key->month)
3700 + diff = getmonth (texta, lena) - getmonth (textb, lenb);
3703 + if (ignore || translate)
3705 + char *copy_a = (char *) alloca (lena + 1 + lenb + 1);
3706 + char *copy_b = copy_a + lena + 1;
3707 + size_t new_len_a, new_len_b;
3710 + /* Ignore and/or translate chars before comparing. */
3711 +# define IGNORE_CHARS(NEW_LEN, LEN, TEXT, COPY, WC, MBLENGTH, STATE) \
3715 + char mbc[MB_LEN_MAX]; \
3716 + mbstate_t state_wc; \
3718 + for (NEW_LEN = i = 0; i < LEN;) \
3720 + mbstate_t state_bak; \
3722 + state_bak = STATE; \
3723 + MBLENGTH = mbrtowc (&WC, TEXT + i, LEN - i, &STATE); \
3725 + if (MBLENGTH == (size_t)-2 || MBLENGTH == (size_t)-1 \
3726 + || MBLENGTH == 0) \
3728 + if (MBLENGTH == (size_t)-2 || MBLENGTH == (size_t)-1) \
3729 + STATE = state_bak; \
3731 + COPY[NEW_LEN++] = TEXT[i++]; \
3737 + if ((ignore == nonprinting && !iswprint (WC)) \
3738 + || (ignore == nondictionary \
3739 + && !iswalnum (WC) && !iswblank (WC))) \
3749 + uwc = towupper(WC); \
3752 + memcpy (mbc, TEXT + i, MBLENGTH); \
3759 + memset (&state_wc, '\0', sizeof (mbstate_t)); \
3761 + MBLENGTH = wcrtomb (mbc, WC, &state_wc); \
3762 + assert (MBLENGTH != (size_t)-1 && MBLENGTH != 0); \
3765 + for (j = 0; j < MBLENGTH; j++) \
3766 + COPY[NEW_LEN++] = mbc[j]; \
3769 + for (j = 0; j < MBLENGTH; j++) \
3770 + COPY[NEW_LEN++] = TEXT[i++]; \
3772 + COPY[NEW_LEN] = '\0'; \
3775 + IGNORE_CHARS (new_len_a, lena, texta, copy_a,
3776 + wc_a, mblength_a, state_a);
3777 + IGNORE_CHARS (new_len_b, lenb, textb, copy_b,
3778 + wc_b, mblength_b, state_b);
3779 + diff = xmemcoll (copy_a, new_len_a, copy_b, new_len_b);
3781 + else if (lena == 0)
3782 + diff = - NONZERO (lenb);
3783 + else if (lenb == 0)
3786 + diff = xmemcoll (texta, lena, textb, lenb);
3796 + /* Find the beginning and limit of the next field. */
3797 + if (key->eword != -1)
3798 + lima = limfield (a, key), limb = limfield (b, key);
3800 + lima = a->text + a->length - 1, limb = b->text + b->length - 1;
3802 + if (key->sword != -1)
3803 + texta = begfield (a, key), textb = begfield (b, key);
3806 + texta = a->text, textb = b->text;
3807 + if (key->skipsblanks)
3809 + while (texta < lima && ismbblank (texta, lima - texta, &mblength_a))
3810 + texta += mblength_a;
3811 + while (textb < limb && ismbblank (textb, limb - textb, &mblength_b))
3812 + textb += mblength_b;
3822 + return key->reverse ? -diff : diff;
3826 /* Compare two lines A and B, returning negative, zero, or positive
3827 depending on whether A compares less than, equal to, or greater than B. */
3829 @@ -2127,7 +2672,7 @@
3830 atexit (close_stdout);
3832 hard_LC_COLLATE = hard_locale (LC_COLLATE);
3833 -#if HAVE_NL_LANGINFO
3834 +#if HAVE_LANGINFO_CODESET
3835 hard_LC_TIME = hard_locale (LC_TIME);
3838 @@ -2148,6 +2693,27 @@
3843 + if (MB_CUR_MAX > 1)
3845 + inittables = inittables_mb;
3846 + begfield = begfield_mb;
3847 + limfield = limfield_mb;
3848 + getmonth = getmonth_mb;
3849 + keycompare = keycompare_mb;
3850 + numcompare = numcompare_mb;
3855 + inittables = inittables_uni;
3856 + begfield = begfield_uni;
3857 + limfield = limfield_uni;
3858 + getmonth = getmonth_uni;
3859 + keycompare = keycompare_uni;
3860 + numcompare = numcompare_uni;
3863 have_read_stdin = false;
3866 @@ -2349,13 +2915,35 @@
3870 - char newtab = optarg[0];
3872 + char newtab[MB_LEN_MAX + 1];
3873 + size_t newtab_length = 1;
3874 + strncpy (newtab, optarg, MB_LEN_MAX);
3876 error (SORT_FAILURE, 0, _("empty tab"));
3879 + if (MB_CUR_MAX > 1)
3885 + memset (&state, '\0', sizeof (mbstate_t));
3886 + newtab_length = mbrtowc (&wc, newtab, strnlen (newtab,
3889 + switch (newtab_length)
3894 + newtab_length = 1;
3898 + if (newtab_length == 1 && optarg[1])
3900 if (STREQ (optarg, "\\0"))
3905 /* Provoke with `sort -txx'. Complain about
3906 @@ -2366,9 +2954,12 @@
3910 - if (tab != TAB_DEFAULT && tab != newtab)
3912 + && (tab_length != newtab_length
3913 + || memcmp (tab, newtab, tab_length) != 0))
3914 error (SORT_FAILURE, 0, _("incompatible tabs"));
3916 + memcpy (tab, newtab, newtab_length);
3917 + tab_length = newtab_length;
3921 --- /dev/null 2005-10-10 09:36:06.437701000 +0100
3922 +++ coreutils-5.93/tests/sort/sort-mb-tests 2005-12-23 08:53:01.000000000 +0000
3926 + 0) xx='../../src/sort';;
3929 +test "$VERBOSE" && echo=echo || echo=:
3930 +$echo testing program: $xx
3932 +test "$srcdir" || srcdir=.
3933 +test "$VERBOSE" && $xx --version 2> /dev/null
3935 +export LC_ALL=en_US.UTF-8
3936 +locale -k LC_CTYPE 2>&1 | grep -q charmap.*UTF-8 || exit 77
3939 +$xx -t @ -k2 -n mb1.I > mb1.O
3941 +if test $code != 0; then
3942 + $echo "Test mb1 failed: $xx return code $code differs from expected value 0" 1>&2
3943 + errors=`expr $errors + 1`
3945 + cmp mb1.O $srcdir/mb1.X > /dev/null 2>&1
3947 + 0) if test "$VERBOSE"; then $echo "passed mb1"; fi;;
3948 + 1) $echo "Test mb1 failed: files mb1.O and $srcdir/mb1.X differ" 1>&2
3949 + (diff -c mb1.O $srcdir/mb1.X) 2> /dev/null
3950 + errors=`expr $errors + 1`;;
3951 + 2) $echo "Test mb1 may have failed." 1>&2
3952 + $echo The command "cmp mb1.O $srcdir/mb1.X" failed. 1>&2
3953 + errors=`expr $errors + 1`;;
3957 +$xx -t @ -k4 -n mb2.I > mb2.O
3959 +if test $code != 0; then
3960 + $echo "Test mb2 failed: $xx return code $code differs from expected value 0" 1>&2
3961 + errors=`expr $errors + 1`
3963 + cmp mb2.O $srcdir/mb2.X > /dev/null 2>&1
3965 + 0) if test "$VERBOSE"; then $echo "passed mb2"; fi;;
3966 + 1) $echo "Test mb2 failed: files mb2.O and $srcdir/mb2.X differ" 1>&2
3967 + (diff -c mb2.O $srcdir/mb2.X) 2> /dev/null
3968 + errors=`expr $errors + 1`;;
3969 + 2) $echo "Test mb2 may have failed." 1>&2
3970 + $echo The command "cmp mb2.O $srcdir/mb2.X" failed. 1>&2
3971 + errors=`expr $errors + 1`;;
3975 +if test $errors = 0; then
3976 + $echo Passed all 113 tests. 1>&2
3978 + $echo Failed $errors tests. 1>&2
3980 +test $errors = 0 || errors=1
3982 --- /dev/null 2005-10-10 09:36:06.437701000 +0100
3983 +++ coreutils-5.93/tests/sort/mb1.I 2005-12-23 08:53:01.000000000 +0000
3989 --- /dev/null 2005-10-10 09:36:06.437701000 +0100
3990 +++ coreutils-5.93/tests/sort/mb2.I 2005-12-23 08:53:01.000000000 +0000
3996 --- /dev/null 2005-10-10 09:36:06.437701000 +0100
3997 +++ coreutils-5.93/tests/sort/mb1.X 2005-12-23 08:53:01.000000000 +0000
4003 --- /dev/null 2005-10-10 09:36:06.437701000 +0100
4004 +++ coreutils-5.93/tests/sort/mb2.X 2005-12-23 08:53:01.000000000 +0000
4010 --- coreutils-5.93/tests/sort/Makefile.am.i18n 2005-10-24 22:02:25.000000000 +0100
4011 +++ coreutils-5.93/tests/sort/Makefile.am 2005-12-23 08:53:01.000000000 +0000
4013 nul-nls.E use-nl.O use-nl.E o2.O o2.E nul-tab.O nul-tab.E
4016 -EXTRA_DIST = Test.pm $x-tests $(explicit) $(maint_gen)
4017 -noinst_SCRIPTS = $x-tests
4018 +run_gen += mb1.O mb2.O
4020 +EXTRA_DIST = Test.pm $x-tests $(explicit) $(maint_gen) mb1.I mb1.X mb2.I mb2.X
4021 +noinst_SCRIPTS = $x-tests # $x-mb-tests
4022 TESTS_ENVIRONMENT = \
4023 PATH="`pwd`/../../src$(PATH_SEPARATOR)$$PATH"
4025 editpl = sed -e 's,@''PERL''@,$(PERL),g' -e 's,@''srcdir''@,$(srcdir),g'
4028 +TESTS = $x-tests $x-mb-tests
4030 mk_script = $(srcdir)/../mk-script
4031 $(srcdir)/$x-tests: $(mk_script) Test.pm Makefile.am
4032 --- coreutils-5.93/tests/sort/Makefile.in.i18n 2005-10-22 19:13:23.000000000 +0100
4033 +++ coreutils-5.93/tests/sort/Makefile.in 2005-10-26 22:17:04.000000000 +0100
4034 @@ -338,13 +338,15 @@
4035 o-no-file1.E create-empty.O create-empty.E neg-nls.O neg-nls.E nul-nls.O \
4036 nul-nls.E use-nl.O use-nl.E o2.O o2.E nul-tab.O nul-tab.E
4038 -EXTRA_DIST = Test.pm $x-tests $(explicit) $(maint_gen)
4039 -noinst_SCRIPTS = $x-tests
4040 +run_gen += mb1.O mb2.O
4042 +EXTRA_DIST = Test.pm $x-tests $(explicit) $(maint_gen) mb1.I mb1.X mb2.I mb2.X
4043 +noinst_SCRIPTS = $x-tests # $x-mb-tests
4044 TESTS_ENVIRONMENT = \
4045 PATH="`pwd`/../../src$(PATH_SEPARATOR)$$PATH"
4047 editpl = sed -e 's,@''PERL''@,$(PERL),g' -e 's,@''srcdir''@,$(srcdir),g'
4049 +TESTS = $x-tests $x-mb-tests
4050 mk_script = $(srcdir)/../mk-script
4051 MAINTAINERCLEANFILES = $x-tests $(maint_gen)
4052 CLEANFILES = $(run_gen)