Browse code

Upgrading coreutils to version 8.25 (PR 1660871)

Change-Id: Ic97463b29a942e164310c7b60753cda769bd37bc
Reviewed-on: http://photon-jenkins.eng.vmware.com/884
Tested-by: jenkins-photon <wangnan2015@hotmail.com>
Reviewed-by: suezzelur <anishs@vmware.com>

dthaluru authored on 2016/05/18 08:13:39
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,4790 @@
0
+Submitted by:            DJ Lucas (dj_AT_linuxfromscratch_DOT_org)
1
+Date:                    2016-02-09
2
+Initial Package Version: 8.25
3
+Upstream Status:         Rejected
4
+Origin:                  Based on Suse's i18n patches at https://build.opensuse.org/package/view_file/Base:System/coreutils/coreutils-i18n.patch
5
+Description:             Fixes several i18n issues with various Coreutils programs
6
+
7
+diff -Naurp coreutils-8.25-orig/lib/linebuffer.h coreutils-8.25/lib/linebuffer.h
8
+--- coreutils-8.25-orig/lib/linebuffer.h	2016-01-01 07:45:55.000000000 -0600
9
+@@ -21,6 +21,11 @@
10
+ 
11
+ # include <stdio.h>
12
+ 
13
++/* Get mbstate_t.  */
14
++# if HAVE_WCHAR_H
15
++#  include <wchar.h>
16
++# endif
17
++
18
+ /* A 'struct linebuffer' holds a line of text. */
19
+ 
20
+ struct linebuffer
21
+@@ -28,6 +33,9 @@ struct linebuffer
22
+   size_t size;                  /* Allocated. */
23
+   size_t length;                /* Used. */
24
+   char *buffer;
25
++# if HAVE_WCHAR_H
26
++  mbstate_t state;
27
++# endif
28
+ };
29
+ 
30
+ /* Initialize linebuffer LINEBUFFER for use. */
31
+diff -Naurp coreutils-8.25-orig/src/cut.c coreutils-8.25/src/cut.c
32
+--- coreutils-8.25-orig/src/cut.c	2016-01-13 05:08:59.000000000 -0600
33
+@@ -28,6 +28,11 @@
34
+ #include <assert.h>
35
+ #include <getopt.h>
36
+ #include <sys/types.h>
37
++
38
++/* Get mbstate_t, mbrtowc().  */
39
++#if HAVE_WCHAR_H
40
++# include <wchar.h>
41
++#endif
42
+ #include "system.h"
43
+ 
44
+ #include "error.h"
45
+@@ -38,6 +43,18 @@
46
+ 
47
+ #include "set-fields.h"
48
+ 
49
++/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
50
++   installation; work around this configuration error.        */
51
++#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
52
++# undef MB_LEN_MAX
53
++# define MB_LEN_MAX 16
54
++#endif
55
++
56
++/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
57
++#if HAVE_MBRTOWC && defined mbstate_t
58
++# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
59
++#endif
60
++
61
+ /* The official name of this program (e.g., no 'g' prefix).  */
62
+ #define PROGRAM_NAME "cut"
63
+ 
64
+@@ -54,6 +71,52 @@
65
+     }									\
66
+   while (0)
67
+ 
68
++/* Refill the buffer BUF to get a multibyte character. */
69
++#define REFILL_BUFFER(BUF, BUFPOS, BUFLEN, STREAM)                        \
70
++  do                                                                        \
71
++    {                                                                        \
72
++      if (BUFLEN < MB_LEN_MAX && !feof (STREAM) && !ferror (STREAM))        \
73
++        {                                                                \
74
++          memmove (BUF, BUFPOS, BUFLEN);                                \
75
++          BUFLEN += fread (BUF + BUFLEN, sizeof(char), BUFSIZ, STREAM); \
76
++          BUFPOS = BUF;                                                        \
77
++        }                                                                \
78
++    }                                                                        \
79
++  while (0)
80
++
81
++/* Get wide character on BUFPOS. BUFPOS is not included after that.
82
++   If byte sequence is not valid as a character, CONVFAIL is true. Otherwise false. */
83
++#define GET_NEXT_WC_FROM_BUFFER(WC, BUFPOS, BUFLEN, MBLENGTH, STATE, CONVFAIL) \
84
++  do                                                                        \
85
++    {                                                                        \
86
++      mbstate_t state_bak;                                                \
87
++                                                                        \
88
++      if (BUFLEN < 1)                                                        \
89
++        {                                                                \
90
++          WC = WEOF;                                                        \
91
++          break;                                                        \
92
++        }                                                                \
93
++                                                                        \
94
++      /* Get a wide character. */                                        \
95
++      CONVFAIL = false;                                                        \
96
++      state_bak = STATE;                                                \
97
++      MBLENGTH = mbrtowc ((wchar_t *)&WC, BUFPOS, BUFLEN, &STATE);        \
98
++                                                                        \
99
++      switch (MBLENGTH)                                                        \
100
++        {                                                                \
101
++        case (size_t)-1:                                                \
102
++        case (size_t)-2:                                                \
103
++          CONVFAIL = true;                                                        \
104
++          STATE = state_bak;                                                \
105
++          /* Fall througn. */                                                \
106
++                                                                        \
107
++        case 0:                                                                \
108
++          MBLENGTH = 1;                                                        \
109
++          break;                                                        \
110
++        }                                                                \
111
++    }                                                                        \
112
++  while (0)
113
++
114
+ 
115
+ /* Pointer inside RP.  When checking if a byte or field is selected
116
+    by a finite range, we check if it is between CURRENT_RP.LO
117
+@@ -61,6 +124,9 @@
118
+    CURRENT_RP.HI then we make CURRENT_RP to point to the next range pair. */
119
+ static struct field_range_pair *current_rp;
120
+ 
121
++/* Length of the delimiter given as argument to -d.  */
122
++size_t delimlen;
123
++
124
+ /* This buffer is used to support the semantics of the -s option
125
+    (or lack of same) when the specified field list includes (does
126
+    not include) the first field.  In both of those cases, the entire
127
+@@ -77,15 +143,25 @@ enum operating_mode
128
+   {
129
+     undefined_mode,
130
+ 
131
+-    /* Output characters that are in the given bytes. */
132
++    /* Output bytes that are at the given positions. */
133
+     byte_mode,
134
+ 
135
++    /* Output characters that are at the given positions. */
136
++    character_mode,
137
++
138
+     /* Output the given delimiter-separated fields. */
139
+     field_mode
140
+   };
141
+ 
142
+ static enum operating_mode operating_mode;
143
+ 
144
++/* If nonzero, when in byte mode, don't split multibyte characters.  */
145
++static int byte_mode_character_aware;
146
++
147
++/* If nonzero, the function for single byte locale is work
148
++   if this program runs on multibyte locale. */
149
++static int force_singlebyte_mode;
150
++
151
+ /* If true do not output lines containing no delimiter characters.
152
+    Otherwise, all such lines are printed.  This option is valid only
153
+    with field mode.  */
154
+@@ -97,6 +173,9 @@ static bool complement;
155
+ 
156
+ /* The delimiter character for field mode. */
157
+ static unsigned char delim;
158
++#if HAVE_WCHAR_H
159
++static wchar_t wcdelim;
160
++#endif
161
+ 
162
+ /* The delimiter for each line/record. */
163
+ static unsigned char line_delim = '\n';
164
+@@ -164,7 +243,7 @@ Print selected parts of lines from each
165
+   -f, --fields=LIST       select only these fields;  also print any line\n\
166
+                             that contains no delimiter character, unless\n\
167
+                             the -s option is specified\n\
168
+-  -n                      (ignored)\n\
169
++  -n                      with -b: don't split multibyte characters\n\
170
+ "), stdout);
171
+       fputs (_("\
172
+       --complement        complement the set of selected bytes, characters\n\
173
+@@ -280,6 +359,82 @@ cut_bytes (FILE *stream)
174
+     }
175
+ }
176
+ 
177
++#if HAVE_MBRTOWC
178
++/* This function is in use for the following case.
179
++
180
++   1. Read from the stream STREAM, printing to standard output any selected
181
++   characters.
182
++
183
++   2. Read from stream STREAM, printing to standard output any selected bytes,
184
++   without splitting multibyte characters.  */
185
++
186
++static void
187
++cut_characters_or_cut_bytes_no_split (FILE *stream)
188
++{
189
++  size_t idx;                /* number of bytes or characters in the line so far. */
190
++  char buf[MB_LEN_MAX + BUFSIZ];  /* For spooling a read byte sequence. */
191
++  char *bufpos;                /* Next read position of BUF. */
192
++  size_t buflen;        /* The length of the byte sequence in buf. */
193
++  wint_t wc;                /* A gotten wide character. */
194
++  size_t mblength;        /* The byte size of a multibyte character which shows
195
++                           as same character as WC. */
196
++  mbstate_t state;        /* State of the stream. */
197
++  bool convfail = false;  /* true, when conversion failed. Otherwise false. */
198
++  /* Whether to begin printing delimiters between ranges for the current line.
199
++     Set after we've begun printing data corresponding to the first range.  */
200
++  bool print_delimiter = false;
201
++
202
++  idx = 0;
203
++  buflen = 0;
204
++  bufpos = buf;
205
++  memset (&state, '\0', sizeof(mbstate_t));
206
++
207
++  current_rp = frp;
208
++
209
++  while (1)
210
++    {
211
++      REFILL_BUFFER (buf, bufpos, buflen, stream);
212
++
213
++      GET_NEXT_WC_FROM_BUFFER (wc, bufpos, buflen, mblength, state, convfail);
214
++      (void) convfail;  /* ignore unused */
215
++
216
++      if (wc == WEOF)
217
++        {
218
++          if (idx > 0)
219
++            putchar (line_delim);
220
++          break;
221
++        }
222
++      else if (wc == line_delim)
223
++        {
224
++          putchar (line_delim);
225
++          idx = 0;
226
++          print_delimiter = false;
227
++          current_rp = frp;
228
++        }
229
++      else
230
++        {
231
++          next_item (&idx);
232
++          if (print_kth (idx))
233
++            {
234
++              if (output_delimiter_specified)
235
++                {
236
++                  if (print_delimiter && is_range_start_index (idx))
237
++                    {
238
++                      fwrite (output_delimiter_string, sizeof (char),
239
++                              output_delimiter_length, stdout);
240
++                    }
241
++                  print_delimiter = true;
242
++                }
243
++              fwrite (bufpos, mblength, sizeof(char), stdout);
244
++            }
245
++        }
246
++
247
++      buflen -= mblength;
248
++      bufpos += mblength;
249
++    }
250
++}
251
++#endif
252
++
253
+ /* Read from stream STREAM, printing to standard output any selected fields.  */
254
+ 
255
+ static void
256
+@@ -425,13 +580,211 @@ cut_fields (FILE *stream)
257
+     }
258
+ }
259
+ 
260
++#if HAVE_MBRTOWC
261
++static void
262
++cut_fields_mb (FILE *stream)
263
++{
264
++  int c;
265
++  size_t field_idx;
266
++  int found_any_selected_field;
267
++  int buffer_first_field;
268
++  int empty_input;
269
++  char buf[MB_LEN_MAX + BUFSIZ];  /* For spooling a read byte sequence. */
270
++  char *bufpos;                /* Next read position of BUF. */
271
++  size_t buflen;        /* The length of the byte sequence in buf. */
272
++  wint_t wc = 0;        /* A gotten wide character. */
273
++  size_t mblength;        /* The byte size of a multibyte character which shows
274
++                           as same character as WC. */
275
++  mbstate_t state;        /* State of the stream. */
276
++  bool convfail = false;  /* true, when conversion failed. Otherwise false. */
277
++
278
++  current_rp = frp;
279
++
280
++  found_any_selected_field = 0;
281
++  field_idx = 1;
282
++  bufpos = buf;
283
++  buflen = 0;
284
++  memset (&state, '\0', sizeof(mbstate_t));
285
++
286
++  c = getc (stream);
287
++  empty_input = (c == EOF);
288
++  if (c != EOF)
289
++  {
290
++    ungetc (c, stream);
291
++    wc = 0;
292
++  }
293
++  else
294
++    wc = WEOF;
295
++
296
++  /* To support the semantics of the -s flag, we may have to buffer
297
++     all of the first field to determine whether it is `delimited.'
298
++     But that is unnecessary if all non-delimited lines must be printed
299
++     and the first field has been selected, or if non-delimited lines
300
++     must be suppressed and the first field has *not* been selected.
301
++     That is because a non-delimited line has exactly one field.  */
302
++  buffer_first_field = (suppress_non_delimited ^ !print_kth (1));
303
++
304
++  while (1)
305
++    {
306
++      if (field_idx == 1 && buffer_first_field)
307
++        {
308
++          int len = 0;
309
++
310
++          while (1)
311
++            {
312
++              REFILL_BUFFER (buf, bufpos, buflen, stream);
313
++
314
++              GET_NEXT_WC_FROM_BUFFER
315
++                (wc, bufpos, buflen, mblength, state, convfail);
316
++
317
++              if (wc == WEOF)
318
++                break;
319
++
320
++              field_1_buffer = xrealloc (field_1_buffer, len + mblength);
321
++              memcpy (field_1_buffer + len, bufpos, mblength);
322
++              len += mblength;
323
++              buflen -= mblength;
324
++              bufpos += mblength;
325
++
326
++              if (!convfail && (wc == line_delim || wc == wcdelim))
327
++                break;
328
++            }
329
++
330
++          if (len <= 0 && wc == WEOF)
331
++            break;
332
++
333
++          /* If the first field extends to the end of line (it is not
334
++             delimited) and we are printing all non-delimited lines,
335
++             print this one.  */
336
++          if (convfail || (!convfail && wc != wcdelim))
337
++            {
338
++              if (suppress_non_delimited)
339
++                {
340
++                  /* Empty.        */
341
++                }
342
++              else
343
++                {
344
++                  fwrite (field_1_buffer, sizeof (char), len, stdout);
345
++                  /* Make sure the output line is newline terminated.  */
346
++                  if (convfail || (!convfail && wc != line_delim))
347
++                    putchar (line_delim);
348
++                }
349
++              continue;
350
++            }
351
++
352
++          if (print_kth (1))
353
++            {
354
++              /* Print the field, but not the trailing delimiter.  */
355
++              fwrite (field_1_buffer, sizeof (char), len - 1, stdout);
356
++              found_any_selected_field = 1;
357
++            }
358
++          next_item (&field_idx);
359
++        }
360
++
361
++      if (wc != WEOF)
362
++        {
363
++          if (print_kth (field_idx))
364
++            {
365
++              if (found_any_selected_field)
366
++                {
367
++                  fwrite (output_delimiter_string, sizeof (char),
368
++                          output_delimiter_length, stdout);
369
++                }
370
++              found_any_selected_field = 1;
371
++            }
372
++
373
++          while (1)
374
++            {
375
++              REFILL_BUFFER (buf, bufpos, buflen, stream);
376
++
377
++              GET_NEXT_WC_FROM_BUFFER
378
++                (wc, bufpos, buflen, mblength, state, convfail);
379
++
380
++              if (wc == WEOF)
381
++                break;
382
++              else if (!convfail && (wc == wcdelim || wc == line_delim))
383
++                {
384
++                  buflen -= mblength;
385
++                  bufpos += mblength;
386
++                  break;
387
++                }
388
++
389
++              if (print_kth (field_idx))
390
++                fwrite (bufpos, mblength, sizeof(char), stdout);
391
++
392
++              buflen -= mblength;
393
++              bufpos += mblength;
394
++            }
395
++        }
396
++
397
++      if ((!convfail || wc == line_delim) && buflen < 1)
398
++        wc = WEOF;
399
++
400
++      if (!convfail && wc == wcdelim)
401
++        next_item (&field_idx);
402
++      else if (wc == WEOF || (!convfail && wc == line_delim))
403
++        {
404
++          if (found_any_selected_field
405
++              || (!empty_input && !(suppress_non_delimited && field_idx == 1)))
406
++            putchar (line_delim);
407
++          if (wc == WEOF)
408
++            break;
409
++          field_idx = 1;
410
++          current_rp = frp;
411
++          found_any_selected_field = 0;
412
++        }
413
++    }
414
++}
415
++#endif
416
++
417
+ static void
418
+ cut_stream (FILE *stream)
419
+ {
420
+-  if (operating_mode == byte_mode)
421
+-    cut_bytes (stream);
422
++#if HAVE_MBRTOWC
423
++  if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
424
++    {
425
++      switch (operating_mode)
426
++        {
427
++        case byte_mode:
428
++          if (byte_mode_character_aware)
429
++            cut_characters_or_cut_bytes_no_split (stream);
430
++          else
431
++            cut_bytes (stream);
432
++          break;
433
++
434
++        case character_mode:
435
++          cut_characters_or_cut_bytes_no_split (stream);
436
++          break;
437
++
438
++        case field_mode:
439
++          if (delimlen == 1)
440
++            {
441
++              /* Check if we have utf8 multibyte locale, so we can use this
442
++                 optimization because of uniqueness of characters, which is
443
++                 not true for e.g. SJIS */
444
++              char * loc = setlocale(LC_CTYPE, NULL);
445
++              if (loc && (strstr (loc, "UTF-8") || strstr (loc, "utf-8") ||
446
++                  strstr (loc, "UTF8") || strstr (loc, "utf8")))
447
++                {
448
++                  cut_fields (stream);
449
++                  break;
450
++                }
451
++            }
452
++          cut_fields_mb (stream);
453
++          break;
454
++
455
++        default:
456
++          abort ();
457
++        }
458
++    }
459
+   else
460
+-    cut_fields (stream);
461
++#endif
462
++    {
463
++      if (operating_mode == field_mode)
464
++        cut_fields (stream);
465
++      else
466
++        cut_bytes (stream);
467
++    }
468
+ }
469
+ 
470
+ /* Process file FILE to standard output.
471
+@@ -483,6 +836,7 @@ main (int argc, char **argv)
472
+   bool ok;
473
+   bool delim_specified = false;
474
+   char *spec_list_string IF_LINT ( = NULL);
475
++  char mbdelim[MB_LEN_MAX + 1];
476
+ 
477
+   initialize_main (&argc, &argv);
478
+   set_program_name (argv[0]);
479
+@@ -505,7 +859,6 @@ main (int argc, char **argv)
480
+       switch (optc)
481
+         {
482
+         case 'b':
483
+-        case 'c':
484
+           /* Build the byte list. */
485
+           if (operating_mode != undefined_mode)
486
+             FATAL_ERROR (_("only one type of list may be specified"));
487
+@@ -513,6 +866,14 @@ main (int argc, char **argv)
488
+           spec_list_string = optarg;
489
+           break;
490
+ 
491
++        case 'c':
492
++          /* Build the character list. */
493
++          if (operating_mode != undefined_mode)
494
++            FATAL_ERROR (_("only one type of list may be specified"));
495
++          operating_mode = character_mode;
496
++          spec_list_string = optarg;
497
++          break;
498
++
499
+         case 'f':
500
+           /* Build the field list. */
501
+           if (operating_mode != undefined_mode)
502
+@@ -524,10 +885,38 @@ main (int argc, char **argv)
503
+         case 'd':
504
+           /* New delimiter. */
505
+           /* Interpret -d '' to mean 'use the NUL byte as the delimiter.'  */
506
+-          if (optarg[0] != '\0' && optarg[1] != '\0')
507
+-            FATAL_ERROR (_("the delimiter must be a single character"));
508
+-          delim = optarg[0];
509
+-          delim_specified = true;
510
++            {
511
++#if HAVE_MBRTOWC
512
++              if(MB_CUR_MAX > 1)
513
++                {
514
++                  mbstate_t state;
515
++
516
++                  memset (&state, '\0', sizeof(mbstate_t));
517
++                  delimlen = mbrtowc (&wcdelim, optarg, strnlen(optarg, MB_LEN_MAX), &state);
518
++
519
++                  if (delimlen == (size_t)-1 || delimlen == (size_t)-2)
520
++                    ++force_singlebyte_mode;
521
++                  else
522
++                    {
523
++                      delimlen = (delimlen < 1) ? 1 : delimlen;
524
++                      if (wcdelim != L'\0' && *(optarg + delimlen) != '\0')
525
++                        FATAL_ERROR (_("the delimiter must be a single character"));
526
++                      memcpy (mbdelim, optarg, delimlen);
527
++                      mbdelim[delimlen] = '\0';
528
++                      if (delimlen == 1)
529
++                        delim = *optarg;
530
++                    }
531
++                }
532
++
533
++              if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
534
++#endif
535
++                {
536
++                  if (optarg[0] != '\0' && optarg[1] != '\0')
537
++                    FATAL_ERROR (_("the delimiter must be a single character"));
538
++                  delim = (unsigned char) optarg[0];
539
++                }
540
++            delim_specified = true;
541
++          }
542
+           break;
543
+ 
544
+         case OUTPUT_DELIMITER_OPTION:
545
+@@ -540,6 +929,7 @@ main (int argc, char **argv)
546
+           break;
547
+ 
548
+         case 'n':
549
++          byte_mode_character_aware = 1;
550
+           break;
551
+ 
552
+         case 's':
553
+@@ -579,15 +969,34 @@ main (int argc, char **argv)
554
+               | (complement ? SETFLD_COMPLEMENT : 0) );
555
+ 
556
+   if (!delim_specified)
557
+-    delim = '\t';
558
++    {
559
++      delim = '\t';
560
++#ifdef HAVE_MBRTOWC
561
++      wcdelim = L'\t';
562
++      mbdelim[0] = '\t';
563
++      mbdelim[1] = '\0';
564
++      delimlen = 1;
565
++#endif
566
++    }
567
+ 
568
+   if (output_delimiter_string == NULL)
569
+     {
570
+-      static char dummy[2];
571
+-      dummy[0] = delim;
572
+-      dummy[1] = '\0';
573
+-      output_delimiter_string = dummy;
574
+-      output_delimiter_length = 1;
575
++#ifdef HAVE_MBRTOWC
576
++      if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
577
++        {
578
++          output_delimiter_string = xstrdup(mbdelim);
579
++          output_delimiter_length = delimlen;
580
++        }
581
++
582
++      if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
583
++#endif
584
++        {
585
++          static char dummy[2];
586
++          dummy[0] = delim;
587
++          dummy[1] = '\0';
588
++          output_delimiter_string = dummy;
589
++          output_delimiter_length = 1;
590
++        }
591
+     }
592
+ 
593
+   if (optind == argc)
594
+diff -Naurp coreutils-8.25-orig/src/expand.c coreutils-8.25/src/expand.c
595
+--- coreutils-8.25-orig/src/expand.c	2016-01-01 07:48:50.000000000 -0600
596
+@@ -37,12 +37,34 @@
597
+ #include <stdio.h>
598
+ #include <getopt.h>
599
+ #include <sys/types.h>
600
++
601
++/* Get mbstate_t, mbrtowc(), wcwidth(). */
602
++#if HAVE_WCHAR_H
603
++# include <wchar.h>
604
++#endif
605
++
606
++/* Get iswblank(). */
607
++#if HAVE_WCTYPE_H
608
++# include <wctype.h>
609
++#endif
610
++
611
+ #include "system.h"
612
+ #include "error.h"
613
+ #include "fadvise.h"
614
+ #include "quote.h"
615
+ #include "xstrndup.h"
616
+ 
617
++/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
618
++   installation; work around this configuration error.  */
619
++#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
620
++# define MB_LEN_MAX 16
621
++#endif
622
++
623
++/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
624
++#if HAVE_MBRTOWC && defined mbstate_t
625
++# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
626
++#endif
627
++
628
+ /* The official name of this program (e.g., no 'g' prefix).  */
629
+ #define PROGRAM_NAME "expand"
630
+ 
631
+@@ -357,6 +379,142 @@ expand (void)
632
+     }
633
+ }
634
+ 
635
++#if HAVE_MBRTOWC
636
++static void
637
++expand_multibyte (void)
638
++{
639
++  FILE *fp;			/* Input strem. */
640
++  mbstate_t i_state;		/* Current shift state of the input stream. */
641
++  mbstate_t i_state_bak;	/* Back up the I_STATE. */
642
++  mbstate_t o_state;		/* Current shift state of the output stream. */
643
++  char buf[MB_LEN_MAX + BUFSIZ];  /* For spooling a read byte sequence. */
644
++  char *bufpos = buf;			/* Next read position of BUF. */
645
++  size_t buflen = 0;		/* The length of the byte sequence in buf. */
646
++  wchar_t wc;			/* A gotten wide character. */
647
++  size_t mblength;		/* The byte size of a multibyte character
648
++				   which shows as same character as WC. */
649
++  int tab_index = 0;		/* Index in `tab_list' of next tabstop. */
650
++  int column = 0;		/* Column on screen of the next char. */
651
++  int next_tab_column;		/* Column the next tab stop is on. */
652
++  int convert = 1;		/* If nonzero, perform translations. */
653
++
654
++  fp = next_file ((FILE *) NULL);
655
++  if (fp == NULL)
656
++    return;
657
++
658
++  memset (&o_state, '\0', sizeof(mbstate_t));
659
++  memset (&i_state, '\0', sizeof(mbstate_t));
660
++
661
++  for (;;)
662
++    {
663
++      /* Refill the buffer BUF. */
664
++      if (buflen < MB_LEN_MAX && !feof(fp) && !ferror(fp))
665
++	{
666
++	  memmove (buf, bufpos, buflen);
667
++	  buflen += fread (buf + buflen, sizeof(char), BUFSIZ, fp);
668
++	  bufpos = buf;
669
++	}
670
++
671
++      /* No character is left in BUF. */
672
++      if (buflen < 1)
673
++	{
674
++	  fp = next_file (fp);
675
++
676
++	  if (fp == NULL)
677
++	    break;		/* No more files. */
678
++	  else
679
++	    {
680
++	      memset (&i_state, '\0', sizeof(mbstate_t));
681
++	      continue;
682
++	    }
683
++	}
684
++
685
++      /* Get a wide character. */
686
++      i_state_bak = i_state;
687
++      mblength = mbrtowc (&wc, bufpos, buflen, &i_state);
688
++
689
++      switch (mblength)
690
++	{
691
++	case (size_t)-1:	/* illegal byte sequence. */
692
++	case (size_t)-2:
693
++	  mblength = 1;
694
++	  i_state = i_state_bak;
695
++	  if (convert)
696
++	    {
697
++	      ++column;
698
++	      if (convert_entire_line == 0 && !isblank(*bufpos))
699
++		convert = 0;
700
++	    }
701
++	  putchar (*bufpos);
702
++	  break;
703
++
704
++	case 0:		/* null. */
705
++	  mblength = 1;
706
++	  if (convert && convert_entire_line == 0)
707
++	    convert = 0;
708
++	  putchar ('\0');
709
++	  break;
710
++
711
++	default:
712
++	  if (wc == L'\n')   /* LF. */
713
++	    {
714
++	      tab_index = 0;
715
++	      column = 0;
716
++	      convert = 1;
717
++	      putchar ('\n');
718
++	    }
719
++	  else if (wc == L'\t' && convert)	/* Tab. */
720
++	    {
721
++	      if (tab_size == 0)
722
++		{
723
++		  /* Do not let tab_index == first_free_tab;
724
++		     stop when it is 1 less. */
725
++		  while (tab_index < first_free_tab - 1
726
++		      && column >= tab_list[tab_index])
727
++		    tab_index++;
728
++		  next_tab_column = tab_list[tab_index];
729
++		  if (tab_index < first_free_tab - 1)
730
++		    tab_index++;
731
++		  if (column >= next_tab_column)
732
++		    next_tab_column = column + 1;
733
++		}
734
++	      else
735
++		next_tab_column = column + tab_size - column % tab_size;
736
++
737
++	      while (column < next_tab_column)
738
++		{
739
++		  putchar (' ');
740
++		  ++column;
741
++		}
742
++	    }
743
++	  else  /* Others. */
744
++	    {
745
++	      if (convert)
746
++		{
747
++		  if (wc == L'\b')
748
++		    {
749
++		      if (column > 0)
750
++			--column;
751
++		    }
752
++		  else
753
++		    {
754
++		      int width;		/* The width of WC. */
755
++
756
++		      width = wcwidth (wc);
757
++		      column += (width > 0) ? width : 0;
758
++		      if (convert_entire_line == 0 && !iswblank(wc))
759
++			convert = 0;
760
++		    }
761
++		}
762
++	      fwrite (bufpos, sizeof(char), mblength, stdout);
763
++	    }
764
++	}
765
++      buflen -= mblength;
766
++      bufpos += mblength;
767
++    }
768
++}
769
++#endif
770
++
771
+ int
772
+ main (int argc, char **argv)
773
+ {
774
+@@ -421,7 +579,12 @@ main (int argc, char **argv)
775
+ 
776
+   file_list = (optind < argc ? &argv[optind] : stdin_argv);
777
+ 
778
+-  expand ();
779
++#if HAVE_MBRTOWC
780
++  if (MB_CUR_MAX > 1)
781
++    expand_multibyte ();
782
++  else
783
++#endif
784
++    expand ();
785
+ 
786
+   if (have_read_stdin && fclose (stdin) != 0)
787
+     error (EXIT_FAILURE, errno, "-");
788
+diff -Naurp coreutils-8.25-orig/src/fold.c coreutils-8.25/src/fold.c
789
+--- coreutils-8.25-orig/src/fold.c	2016-01-01 07:48:50.000000000 -0600
790
+@@ -22,11 +22,33 @@
791
+ #include <getopt.h>
792
+ #include <sys/types.h>
793
+ 
794
++/* Get mbstate_t, mbrtowc(), wcwidth().  */
795
++#if HAVE_WCHAR_H
796
++# include <wchar.h>
797
++#endif
798
++
799
++/* Get iswprint(), iswblank(), wcwidth().  */
800
++#if HAVE_WCTYPE_H
801
++# include <wctype.h>
802
++#endif
803
++
804
+ #include "system.h"
805
+ #include "error.h"
806
+ #include "fadvise.h"
807
+ #include "xdectoint.h"
808
+ 
809
++/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
810
++      installation; work around this configuration error.  */
811
++#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
812
++# undef MB_LEN_MAX
813
++# define MB_LEN_MAX 16
814
++#endif
815
++
816
++/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
817
++#if HAVE_MBRTOWC && defined mbstate_t
818
++# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
819
++#endif
820
++
821
+ #define TAB_WIDTH 8
822
+ 
823
+ /* The official name of this program (e.g., no 'g' prefix).  */
824
+@@ -34,20 +56,41 @@
825
+ 
826
+ #define AUTHORS proper_name ("David MacKenzie")
827
+ 
828
++#define FATAL_ERROR(Message)                                            \
829
++  do                                                                    \
830
++    {                                                                   \
831
++      error (0, 0, (Message));                                          \
832
++      usage (2);                                                        \
833
++    }                                                                   \
834
++  while (0)
835
++
836
++enum operating_mode
837
++{
838
++  /* Fold texts by columns that are at the given positions. */
839
++  column_mode,
840
++
841
++  /* Fold texts by bytes that are at the given positions. */
842
++  byte_mode,
843
++
844
++  /* Fold texts by characters that are at the given positions. */
845
++  character_mode,
846
++};
847
++
848
++/* The argument shows current mode. (Default: column_mode) */
849
++static enum operating_mode operating_mode;
850
++
851
+ /* If nonzero, try to break on whitespace. */
852
+ static bool break_spaces;
853
+ 
854
+-/* If nonzero, count bytes, not column positions. */
855
+-static bool count_bytes;
856
+-
857
+ /* If nonzero, at least one of the files we read was standard input. */
858
+ static bool have_read_stdin;
859
+ 
860
+-static char const shortopts[] = "bsw:0::1::2::3::4::5::6::7::8::9::";
861
++static char const shortopts[] = "bcsw:0::1::2::3::4::5::6::7::8::9::";
862
+ 
863
+ static struct option const longopts[] =
864
+ {
865
+   {"bytes", no_argument, NULL, 'b'},
866
++  {"characters", no_argument, NULL, 'c'},
867
+   {"spaces", no_argument, NULL, 's'},
868
+   {"width", required_argument, NULL, 'w'},
869
+   {GETOPT_HELP_OPTION_DECL},
870
+@@ -75,6 +118,7 @@ Wrap input lines in each FILE, writing t
871
+ 
872
+       fputs (_("\
873
+   -b, --bytes         count bytes rather than columns\n\
874
++  -c, --characters    count characters rather than columns\n\
875
+   -s, --spaces        break at spaces\n\
876
+   -w, --width=WIDTH   use WIDTH columns instead of 80\n\
877
+ "), stdout);
878
+@@ -92,7 +136,7 @@ Wrap input lines in each FILE, writing t
879
+ static size_t
880
+ adjust_column (size_t column, char c)
881
+ {
882
+-  if (!count_bytes)
883
++  if (operating_mode != byte_mode)
884
+     {
885
+       if (c == '\b')
886
+         {
887
+@@ -115,30 +159,14 @@ adjust_column (size_t column, char c)
888
+    to stdout, with maximum line length WIDTH.
889
+    Return true if successful.  */
890
+ 
891
+-static bool
892
+-fold_file (char const *filename, size_t width)
893
++static void
894
++fold_text (FILE *istream, size_t width, int *saved_errno)
895
+ {
896
+-  FILE *istream;
897
+   int c;
898
+   size_t column = 0;		/* Screen column where next char will go. */
899
+   size_t offset_out = 0;	/* Index in 'line_out' for next char. */
900
+   static char *line_out = NULL;
901
+   static size_t allocated_out = 0;
902
+-  int saved_errno;
903
+-
904
+-  if (STREQ (filename, "-"))
905
+-    {
906
+-      istream = stdin;
907
+-      have_read_stdin = true;
908
+-    }
909
+-  else
910
+-    istream = fopen (filename, "r");
911
+-
912
+-  if (istream == NULL)
913
+-    {
914
+-      error (0, errno, "%s", quotef (filename));
915
+-      return false;
916
+-    }
917
+ 
918
+   fadvise (istream, FADVISE_SEQUENTIAL);
919
+ 
920
+@@ -168,6 +196,15 @@ fold_file (char const *filename, size_t
921
+               bool found_blank = false;
922
+               size_t logical_end = offset_out;
923
+ 
924
++              /* If LINE_OUT has no wide character,
925
++                 put a new wide character in LINE_OUT
926
++                 if column is bigger than width. */
927
++              if (offset_out == 0)
928
++                {
929
++                  line_out[offset_out++] = c;
930
++                  continue;
931
++                }
932
++
933
+               /* Look for the last blank. */
934
+               while (logical_end)
935
+                 {
936
+@@ -214,11 +251,221 @@ fold_file (char const *filename, size_t
937
+       line_out[offset_out++] = c;
938
+     }
939
+ 
940
+-  saved_errno = errno;
941
++  *saved_errno = errno;
942
++
943
++  if (offset_out)
944
++    fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
945
++
946
++}
947
++
948
++#if HAVE_MBRTOWC
949
++static void
950
++fold_multibyte_text (FILE *istream, size_t width, int *saved_errno)
951
++{
952
++  char buf[MB_LEN_MAX + BUFSIZ];  /* For spooling a read byte sequence. */
953
++  size_t buflen = 0;        /* The length of the byte sequence in buf. */
954
++  char *bufpos = buf;         /* Next read position of BUF. */
955
++  wint_t wc;                /* A gotten wide character. */
956
++  size_t mblength;        /* The byte size of a multibyte character which shows
957
++                           as same character as WC. */
958
++  mbstate_t state, state_bak;        /* State of the stream. */
959
++  int convfail = 0;                /* 1, when conversion is failed. Otherwise 0. */
960
++
961
++  static char *line_out = NULL;
962
++  size_t offset_out = 0;        /* Index in `line_out' for next char. */
963
++  static size_t allocated_out = 0;
964
++
965
++  int increment;
966
++  size_t column = 0;
967
++
968
++  size_t last_blank_pos;
969
++  size_t last_blank_column;
970
++  int is_blank_seen;
971
++  int last_blank_increment = 0;
972
++  int is_bs_following_last_blank;
973
++  size_t bs_following_last_blank_num;
974
++  int is_cr_after_last_blank;
975
++
976
++#define CLEAR_FLAGS                                \
977
++   do                                                \
978
++     {                                                \
979
++        last_blank_pos = 0;                        \
980
++        last_blank_column = 0;                        \
981
++        is_blank_seen = 0;                        \
982
++        is_bs_following_last_blank = 0;                \
983
++        bs_following_last_blank_num = 0;        \
984
++        is_cr_after_last_blank = 0;                \
985
++     }                                                \
986
++   while (0)
987
++
988
++#define START_NEW_LINE                        \
989
++   do                                        \
990
++     {                                        \
991
++      putchar ('\n');                        \
992
++      column = 0;                        \
993
++      offset_out = 0;                        \
994
++      CLEAR_FLAGS;                        \
995
++    }                                        \
996
++   while (0)
997
++
998
++  CLEAR_FLAGS;
999
++  memset (&state, '\0', sizeof(mbstate_t));
1000
++
1001
++  for (;; bufpos += mblength, buflen -= mblength)
1002
++    {
1003
++      if (buflen < MB_LEN_MAX && !feof (istream) && !ferror (istream))
1004
++        {
1005
++          memmove (buf, bufpos, buflen);
1006
++          buflen += fread (buf + buflen, sizeof(char), BUFSIZ, istream);
1007
++          bufpos = buf;
1008
++        }
1009
++
1010
++      if (buflen < 1)
1011
++        break;
1012
++
1013
++      /* Get a wide character. */
1014
++      state_bak = state;
1015
++      mblength = mbrtowc ((wchar_t *)&wc, bufpos, buflen, &state);
1016
++
1017
++      switch (mblength)
1018
++        {
1019
++        case (size_t)-1:
1020
++        case (size_t)-2:
1021
++          convfail++;
1022
++          state = state_bak;
1023
++          /* Fall through. */
1024
++
1025
++        case 0:
1026
++          mblength = 1;
1027
++          break;
1028
++        }
1029
++
1030
++rescan:
1031
++      if (operating_mode == byte_mode)                        /* byte mode */
1032
++        increment = mblength;
1033
++      else if (operating_mode == character_mode)        /* character mode */
1034
++        increment = 1;
1035
++      else                                                /* column mode */
1036
++        {
1037
++          if (convfail)
1038
++            increment = 1;
1039
++          else
1040
++            {
1041
++              switch (wc)
1042
++                {
1043
++                case L'\n':
1044
++                  fwrite (line_out, sizeof(char), offset_out, stdout);
1045
++                  START_NEW_LINE;
1046
++                  continue;
1047
++
1048
++                case L'\b':
1049
++                  increment = (column > 0) ? -1 : 0;
1050
++                  break;
1051
++
1052
++                case L'\r':
1053
++                  increment = -1 * column;
1054
++                  break;
1055
++
1056
++                case L'\t':
1057
++                  increment = 8 - column % 8;
1058
++                  break;
1059
++
1060
++                default:
1061
++                  increment = wcwidth (wc);
1062
++                  increment = (increment < 0) ? 0 : increment;
1063
++                }
1064
++            }
1065
++        }
1066
++
1067
++      if (column + increment > width && break_spaces && last_blank_pos)
1068
++        {
1069
++          fwrite (line_out, sizeof(char), last_blank_pos, stdout);
1070
++          putchar ('\n');
1071
++
1072
++          offset_out = offset_out - last_blank_pos;
1073
++          column = column - last_blank_column + ((is_cr_after_last_blank)
1074
++              ? last_blank_increment : bs_following_last_blank_num);
1075
++          memmove (line_out, line_out + last_blank_pos, offset_out);
1076
++          CLEAR_FLAGS;
1077
++          goto rescan;
1078
++        }
1079
++
1080
++      if (column + increment > width && column != 0)
1081
++        {
1082
++          fwrite (line_out, sizeof(char), offset_out, stdout);
1083
++          START_NEW_LINE;
1084
++          goto rescan;
1085
++        }
1086
++
1087
++      if (allocated_out < offset_out + mblength)
1088
++        {
1089
++          line_out = X2REALLOC (line_out, &allocated_out);
1090
++        }
1091
++
1092
++      memcpy (line_out + offset_out, bufpos, mblength);
1093
++      offset_out += mblength;
1094
++      column += increment;
1095
++
1096
++      if (is_blank_seen && !convfail && wc == L'\r')
1097
++        is_cr_after_last_blank = 1;
1098
++
1099
++      if (is_bs_following_last_blank && !convfail && wc == L'\b')
1100
++        ++bs_following_last_blank_num;
1101
++      else
1102
++        is_bs_following_last_blank = 0;
1103
++
1104
++      if (break_spaces && !convfail && iswblank (wc))
1105
++        {
1106
++          last_blank_pos = offset_out;
1107
++          last_blank_column = column;
1108
++          is_blank_seen = 1;
1109
++          last_blank_increment = increment;
1110
++          is_bs_following_last_blank = 1;
1111
++          bs_following_last_blank_num = 0;
1112
++          is_cr_after_last_blank = 0;
1113
++        }
1114
++    }
1115
++
1116
++  *saved_errno = errno;
1117
+ 
1118
+   if (offset_out)
1119
+     fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
1120
+ 
1121
++}
1122
++#endif
1123
++
1124
++/* Fold file FILENAME, or standard input if FILENAME is "-",
1125
++   to stdout, with maximum line length WIDTH.
1126
++   Return 0 if successful, 1 if an error occurs. */
1127
++
1128
++static bool
1129
++fold_file (char const *filename, size_t width)
1130
++{
1131
++  FILE *istream;
1132
++  int saved_errno;
1133
++
1134
++  if (STREQ (filename, "-"))
1135
++    {
1136
++      istream = stdin;
1137
++      have_read_stdin = 1;
1138
++    }
1139
++  else
1140
++    istream = fopen (filename, "r");
1141
++
1142
++  if (istream == NULL)
1143
++    {
1144
++      error (0, errno, "%s", quotef (filename));
1145
++      return 1;
1146
++    }
1147
++
1148
++  /* Define how ISTREAM is being folded. */
1149
++#if HAVE_MBRTOWC
1150
++  if (MB_CUR_MAX > 1)
1151
++    fold_multibyte_text (istream, width, &saved_errno);
1152
++  else
1153
++#endif
1154
++    fold_text (istream, width, &saved_errno);
1155
++
1156
+   if (ferror (istream))
1157
+     {
1158
+       error (0, saved_errno, "%s", quotef (filename));
1159
+@@ -251,7 +498,8 @@ main (int argc, char **argv)
1160
+ 
1161
+   atexit (close_stdout);
1162
+ 
1163
+-  break_spaces = count_bytes = have_read_stdin = false;
1164
++  operating_mode = column_mode;
1165
++  break_spaces = have_read_stdin = false;
1166
+ 
1167
+   while ((optc = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1)
1168
+     {
1169
+@@ -260,7 +508,15 @@ main (int argc, char **argv)
1170
+       switch (optc)
1171
+         {
1172
+         case 'b':		/* Count bytes rather than columns. */
1173
+-          count_bytes = true;
1174
++          if (operating_mode != column_mode)
1175
++            FATAL_ERROR (_("only one way of folding may be specified"));
1176
++          operating_mode = byte_mode;
1177
++          break;
1178
++
1179
++        case 'c':
1180
++          if (operating_mode != column_mode)
1181
++            FATAL_ERROR (_("only one way of folding may be specified"));
1182
++          operating_mode = character_mode;
1183
+           break;
1184
+ 
1185
+         case 's':		/* Break at word boundaries. */
1186
+diff -Naurp coreutils-8.25-orig/src/join.c coreutils-8.25/src/join.c
1187
+--- coreutils-8.25-orig/src/join.c	2016-01-13 05:08:59.000000000 -0600
1188
+@@ -22,18 +22,32 @@
1189
+ #include <sys/types.h>
1190
+ #include <getopt.h>
1191
+ 
1192
++/* Get mbstate_t, mbrtowc(), mbrtowc(), wcwidth().  */
1193
++#if HAVE_WCHAR_H
1194
++# include <wchar.h>
1195
++#endif
1196
++
1197
++/* Get iswblank(), towupper.  */
1198
++#if HAVE_WCTYPE_H
1199
++# include <wctype.h>
1200
++#endif
1201
++
1202
+ #include "system.h"
1203
+ #include "error.h"
1204
+ #include "fadvise.h"
1205
+ #include "hard-locale.h"
1206
+ #include "linebuffer.h"
1207
+-#include "memcasecmp.h"
1208
+ #include "quote.h"
1209
+ #include "stdio--.h"
1210
+ #include "xmemcoll.h"
1211
+ #include "xstrtol.h"
1212
+ #include "argmatch.h"
1213
+ 
1214
++/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
1215
++#if HAVE_MBRTOWC && defined mbstate_t
1216
++# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
1217
++#endif
1218
++
1219
+ /* The official name of this program (e.g., no 'g' prefix).  */
1220
+ #define PROGRAM_NAME "join"
1221
+ 
1222
+@@ -135,10 +149,12 @@ static struct outlist outlist_head;
1223
+ /* Last element in 'outlist', where a new element can be added.  */
1224
+ static struct outlist *outlist_end = &outlist_head;
1225
+ 
1226
+-/* Tab character separating fields.  If negative, fields are separated
1227
+-   by any nonempty string of blanks, otherwise by exactly one
1228
+-   tab character whose value (when cast to unsigned char) equals TAB.  */
1229
+-static int tab = -1;
1230
++/* Tab character separating fields.  If NULL, fields are separated
1231
++   by any nonempty string of blanks.  */
1232
++static char *tab = NULL;
1233
++
1234
++/* The number of bytes used for tab. */
1235
++static size_t tablen = 0;
1236
+ 
1237
+ /* If nonzero, check that the input is correctly ordered. */
1238
+ static enum
1239
+@@ -275,13 +291,14 @@ xfields (struct line *line)
1240
+   if (ptr == lim)
1241
+     return;
1242
+ 
1243
+-  if (0 <= tab && tab != '\n')
1244
++  if (tab != NULL)
1245
+     {
1246
++      unsigned char t = tab[0];
1247
+       char *sep;
1248
+-      for (; (sep = memchr (ptr, tab, lim - ptr)) != NULL; ptr = sep + 1)
1249
++      for (; (sep = memchr (ptr, t, lim - ptr)) != NULL; ptr = sep + 1)
1250
+         extract_field (line, ptr, sep - ptr);
1251
+     }
1252
+-  else if (tab < 0)
1253
++   else
1254
+     {
1255
+       /* Skip leading blanks before the first field.  */
1256
+       while (field_sep (*ptr))
1257
+@@ -305,6 +322,147 @@ xfields (struct line *line)
1258
+   extract_field (line, ptr, lim - ptr);
1259
+ }
1260
+ 
1261
++#if HAVE_MBRTOWC
1262
++static void
1263
++xfields_multibyte (struct line *line)
1264
++{
1265
++  char *ptr = line->buf.buffer;
1266
++  char const *lim = ptr + line->buf.length - 1;
1267
++  wchar_t wc = 0;
1268
++  size_t mblength = 1;
1269
++  mbstate_t state, state_bak;
1270
++
1271
++  memset (&state, 0, sizeof (mbstate_t));
1272
++
1273
++  if (ptr >= lim)
1274
++    return;
1275
++
1276
++  if (tab != NULL)
1277
++    {
1278
++      char *sep = ptr;
1279
++      for (; ptr < lim; ptr = sep + mblength)
1280
++	{
1281
++	  sep = ptr;
1282
++	  while (sep < lim)
1283
++	    {
1284
++	      state_bak = state;
1285
++	      mblength = mbrtowc (&wc, sep, lim - sep + 1, &state);
1286
++
1287
++	      if (mblength == (size_t)-1 || mblength == (size_t)-2)
1288
++		{
1289
++		  mblength = 1;
1290
++		  state = state_bak;
1291
++		}
1292
++	      mblength = (mblength < 1) ? 1 : mblength;
1293
++
1294
++	      if (mblength == tablen && !memcmp (sep, tab, mblength))
1295
++		break;
1296
++	      else
1297
++		{
1298
++		  sep += mblength;
1299
++		  continue;
1300
++		}
1301
++	    }
1302
++
1303
++	  if (sep >= lim)
1304
++	    break;
1305
++
1306
++	  extract_field (line, ptr, sep - ptr);
1307
++	}
1308
++    }
1309
++  else
1310
++    {
1311
++      /* Skip leading blanks before the first field.  */
1312
++      while(ptr < lim)
1313
++      {
1314
++        state_bak = state;
1315
++        mblength = mbrtowc (&wc, ptr, lim - ptr + 1, &state);
1316
++
1317
++        if (mblength == (size_t)-1 || mblength == (size_t)-2)
1318
++          {
1319
++            mblength = 1;
1320
++            state = state_bak;
1321
++            break;
1322
++          }
1323
++        mblength = (mblength < 1) ? 1 : mblength;
1324
++
1325
++        if (!iswblank(wc) && wc != '\n')
1326
++          break;
1327
++        ptr += mblength;
1328
++      }
1329
++
1330
++      do
1331
++	{
1332
++	  char *sep;
1333
++	  state_bak = state;
1334
++	  mblength = mbrtowc (&wc, ptr, lim - ptr + 1, &state);
1335
++	  if (mblength == (size_t)-1 || mblength == (size_t)-2)
1336
++	    {
1337
++	      mblength = 1;
1338
++	      state = state_bak;
1339
++	      break;
1340
++	    }
1341
++	  mblength = (mblength < 1) ? 1 : mblength;
1342
++
1343
++	  sep = ptr + mblength;
1344
++	  while (sep < lim)
1345
++	    {
1346
++	      state_bak = state;
1347
++	      mblength = mbrtowc (&wc, sep, lim - sep + 1, &state);
1348
++	      if (mblength == (size_t)-1 || mblength == (size_t)-2)
1349
++		{
1350
++		  mblength = 1;
1351
++		  state = state_bak;
1352
++		  break;
1353
++		}
1354
++	      mblength = (mblength < 1) ? 1 : mblength;
1355
++
1356
++	      if (iswblank (wc) || wc == '\n')
1357
++		break;
1358
++
1359
++	      sep += mblength;
1360
++	    }
1361
++
1362
++	  extract_field (line, ptr, sep - ptr);
1363
++	  if (sep >= lim)
1364
++	    return;
1365
++
1366
++	  state_bak = state;
1367
++	  mblength = mbrtowc (&wc, sep, lim - sep + 1, &state);
1368
++	  if (mblength == (size_t)-1 || mblength == (size_t)-2)
1369
++	    {
1370
++	      mblength = 1;
1371
++	      state = state_bak;
1372
++	      break;
1373
++	    }
1374
++	  mblength = (mblength < 1) ? 1 : mblength;
1375
++
1376
++	  ptr = sep + mblength;
1377
++	  while (ptr < lim)
1378
++	    {
1379
++	      state_bak = state;
1380
++	      mblength = mbrtowc (&wc, ptr, lim - ptr + 1, &state);
1381
++	      if (mblength == (size_t)-1 || mblength == (size_t)-2)
1382
++		{
1383
++		  mblength = 1;
1384
++		  state = state_bak;
1385
++		  break;
1386
++		}
1387
++	      mblength = (mblength < 1) ? 1 : mblength;
1388
++
1389
++	      if (!iswblank (wc) && wc != '\n')
1390
++		break;
1391
++
1392
++	      ptr += mblength;
1393
++	    }
1394
++	}
1395
++      while (ptr < lim);
1396
++    }
1397
++
1398
++  extract_field (line, ptr, lim - ptr);
1399
++}
1400
++#endif
1401
++
1402
+ static void
1403
+ freeline (struct line *line)
1404
+ {
1405
+@@ -326,56 +484,133 @@ keycmp (struct line const *line1, struct
1406
+         size_t jf_1, size_t jf_2)
1407
+ {
1408
+   /* Start of field to compare in each file.  */
1409
+-  char *beg1;
1410
+-  char *beg2;
1411
+-
1412
+-  size_t len1;
1413
+-  size_t len2;		/* Length of fields to compare.  */
1414
++  char *beg[2];
1415
++  char *copy[2];
1416
++  size_t len[2]; 	/* Length of fields to compare.  */
1417
+   int diff;
1418
++  int i, j;
1419
++  int mallocd = 0;
1420
+ 
1421
+   if (jf_1 < line1->nfields)
1422
+     {
1423
+-      beg1 = line1->fields[jf_1].beg;
1424
+-      len1 = line1->fields[jf_1].len;
1425
++      beg[0] = line1->fields[jf_1].beg;
1426
++      len[0] = line1->fields[jf_1].len;
1427
+     }
1428
+   else
1429
+     {
1430
+-      beg1 = NULL;
1431
+-      len1 = 0;
1432
++      beg[0] = NULL;
1433
++      len[0] = 0;
1434
+     }
1435
+ 
1436
+   if (jf_2 < line2->nfields)
1437
+     {
1438
+-      beg2 = line2->fields[jf_2].beg;
1439
+-      len2 = line2->fields[jf_2].len;
1440
++      beg[1] = line2->fields[jf_2].beg;
1441
++      len[1] = line2->fields[jf_2].len;
1442
+     }
1443
+   else
1444
+     {
1445
+-      beg2 = NULL;
1446
+-      len2 = 0;
1447
++      beg[1] = NULL;
1448
++      len[1] = 0;
1449
+     }
1450
+ 
1451
+-  if (len1 == 0)
1452
+-    return len2 == 0 ? 0 : -1;
1453
+-  if (len2 == 0)
1454
++  if (len[0] == 0)
1455
++    return len[1] == 0 ? 0 : -1;
1456
++  if (len[1] == 0)
1457
+     return 1;
1458
+ 
1459
+   if (ignore_case)
1460
+     {
1461
+-      /* FIXME: ignore_case does not work with NLS (in particular,
1462
+-         with multibyte chars).  */
1463
+-      diff = memcasecmp (beg1, beg2, MIN (len1, len2));
1464
++#ifdef HAVE_MBRTOWC
1465
++      if (MB_CUR_MAX > 1)
1466
++      {
1467
++        size_t mblength;
1468
++        wchar_t wc, uwc;
1469
++        mbstate_t state, state_bak;
1470
++
1471
++        memset (&state, '\0', sizeof (mbstate_t));
1472
++
1473
++        for (i = 0; i < 2; i++)
1474
++          {
1475
++            mallocd = 1;
1476
++            copy[i] = xmalloc (len[i] + 1);
1477
++            memset (copy[i], '\0',len[i] + 1);
1478
++
1479
++            for (j = 0; j < MIN (len[0], len[1]);)
1480
++              {
1481
++                state_bak = state;
1482
++                mblength = mbrtowc (&wc, beg[i] + j, len[i] - j, &state);
1483
++
1484
++                switch (mblength)
1485
++                  {
1486
++                  case (size_t) -1:
1487
++                  case (size_t) -2:
1488
++                    state = state_bak;
1489
++                    /* Fall through */
1490
++                  case 0:
1491
++                    mblength = 1;
1492
++                    break;
1493
++
1494
++                  default:
1495
++                    uwc = towupper (wc);
1496
++
1497
++                    if (uwc != wc)
1498
++                      {
1499
++                        mbstate_t state_wc;
1500
++                        size_t mblen;
1501
++
1502
++                        memset (&state_wc, '\0', sizeof (mbstate_t));
1503
++                        mblen = wcrtomb (copy[i] + j, uwc, &state_wc);
1504
++                        assert (mblen != (size_t)-1);
1505
++                      }
1506
++                    else
1507
++                      memcpy (copy[i] + j, beg[i] + j, mblength);
1508
++                  }
1509
++                j += mblength;
1510
++              }
1511
++            copy[i][j] = '\0';
1512
++          }
1513
++      }
1514
++      else
1515
++#endif
1516
++      {
1517
++        for (i = 0; i < 2; i++)
1518
++          {
1519
++            mallocd = 1;
1520
++            copy[i] = xmalloc (len[i] + 1);
1521
++
1522
++            for (j = 0; j < MIN (len[0], len[1]); j++)
1523
++              copy[i][j] = toupper (beg[i][j]);
1524
++
1525
++            copy[i][j] = '\0';
1526
++          }
1527
++      }
1528
+     }
1529
+   else
1530
+     {
1531
+-      if (hard_LC_COLLATE)
1532
+-        return xmemcoll (beg1, len1, beg2, len2);
1533
+-      diff = memcmp (beg1, beg2, MIN (len1, len2));
1534
++      copy[0] = beg[0];
1535
++      copy[1] = beg[1];
1536
++    }
1537
++
1538
++  if (hard_LC_COLLATE)
1539
++    {
1540
++      diff = xmemcoll ((char *) copy[0], len[0], (char *) copy[1], len[1]);
1541
++
1542
++      if (mallocd)
1543
++        for (i = 0; i < 2; i++)
1544
++          free (copy[i]);
1545
++
1546
++      return diff;
1547
+     }
1548
++  diff = memcmp (copy[0], copy[1], MIN (len[0], len[1]));
1549
++
1550
++  if (mallocd)
1551
++    for (i = 0; i < 2; i++)
1552
++      free (copy[i]);
1553
++
1554
+ 
1555
+   if (diff)
1556
+     return diff;
1557
+-  return len1 < len2 ? -1 : len1 != len2;
1558
++  return len[0] - len[1];
1559
+ }
1560
+ 
1561
+ /* Check that successive input lines PREV and CURRENT from input file
1562
+@@ -467,6 +702,11 @@ get_line (FILE *fp, struct line **linep,
1563
+     }
1564
+   ++line_no[which - 1];
1565
+ 
1566
++#if HAVE_MBRTOWC
1567
++  if (MB_CUR_MAX > 1)
1568
++    xfields_multibyte (line);
1569
++  else
1570
++#endif
1571
+   xfields (line);
1572
+ 
1573
+   if (prevline[which - 1])
1574
+@@ -566,21 +806,28 @@ prfield (size_t n, struct line const *li
1575
+ 
1576
+ /* Output all the fields in line, other than the join field.  */
1577
+ 
1578
++#define PUT_TAB_CHAR							\
1579
++  do									\
1580
++    {									\
1581
++      (tab != NULL) ?							\
1582
++	fwrite(tab, sizeof(char), tablen, stdout) : putchar (' ');	\
1583
++    }									\
1584
++  while (0)
1585
++
1586
+ static void
1587
+ prfields (struct line const *line, size_t join_field, size_t autocount)
1588
+ {
1589
+   size_t i;
1590
+   size_t nfields = autoformat ? autocount : line->nfields;
1591
+-  char output_separator = tab < 0 ? ' ' : tab;
1592
+ 
1593
+   for (i = 0; i < join_field && i < nfields; ++i)
1594
+     {
1595
+-      putchar (output_separator);
1596
++      PUT_TAB_CHAR;
1597
+       prfield (i, line);
1598
+     }
1599
+   for (i = join_field + 1; i < nfields; ++i)
1600
+     {
1601
+-      putchar (output_separator);
1602
++      PUT_TAB_CHAR;
1603
+       prfield (i, line);
1604
+     }
1605
+ }
1606
+@@ -591,7 +838,6 @@ static void
1607
+ prjoin (struct line const *line1, struct line const *line2)
1608
+ {
1609
+   const struct outlist *outlist;
1610
+-  char output_separator = tab < 0 ? ' ' : tab;
1611
+   size_t field;
1612
+   struct line const *line;
1613
+ 
1614
+@@ -625,7 +871,7 @@ prjoin (struct line const *line1, struct
1615
+           o = o->next;
1616
+           if (o == NULL)
1617
+             break;
1618
+-          putchar (output_separator);
1619
++          PUT_TAB_CHAR;
1620
+         }
1621
+       putchar (eolchar);
1622
+     }
1623
+@@ -1103,21 +1349,46 @@ main (int argc, char **argv)
1624
+ 
1625
+         case 't':
1626
+           {
1627
+-            unsigned char newtab = optarg[0];
1628
++            char *newtab = NULL;
1629
++            size_t newtablen;
1630
++            newtab = xstrdup (optarg);
1631
++#if HAVE_MBRTOWC
1632
++            if (MB_CUR_MAX > 1)
1633
++              {
1634
++                mbstate_t state;
1635
++
1636
++                memset (&state, 0, sizeof (mbstate_t));
1637
++                newtablen = mbrtowc (NULL, newtab,
1638
++                                     strnlen (newtab, MB_LEN_MAX),
1639
++                                     &state);
1640
++                if (newtablen == (size_t) 0
1641
++                    || newtablen == (size_t) -1
1642
++                    || newtablen == (size_t) -2)
1643
++                  newtablen = 1;
1644
++              }
1645
++            else
1646
++#endif
1647
++              newtablen = 1;
1648
+             if (! newtab)
1649
+-              newtab = '\n'; /* '' => process the whole line.  */
1650
++            {
1651
++              newtab = (char*)"\n"; /* '' => process the whole line.  */
1652
++            }
1653
+             else if (optarg[1])
1654
+               {
1655
+-                if (STREQ (optarg, "\\0"))
1656
+-                  newtab = '\0';
1657
+-                else
1658
+-                  error (EXIT_FAILURE, 0, _("multi-character tab %s"),
1659
+-                         quote (optarg));
1660
++                if (newtablen == 1 && newtab[1])
1661
++                {
1662
++                  if (STREQ (newtab, "\\0"))
1663
++                     newtab[0] = '\0';
1664
++                }
1665
++              }
1666
++            if (tab != NULL && strcmp (tab, newtab))
1667
++              {
1668
++                free (newtab);
1669
++                error (EXIT_FAILURE, 0, _("incompatible tabs"));
1670
+               }
1671
+-            if (0 <= tab && tab != newtab)
1672
+-              error (EXIT_FAILURE, 0, _("incompatible tabs"));
1673
+             tab = newtab;
1674
+-          }
1675
++            tablen = newtablen;
1676
++           }
1677
+           break;
1678
+ 
1679
+         case 'z':
1680
+diff -Naurp coreutils-8.25-orig/src/pr.c coreutils-8.25/src/pr.c
1681
+--- coreutils-8.25-orig/src/pr.c	2016-01-01 07:48:50.000000000 -0600
1682
+@@ -311,6 +311,24 @@
1683
+ 
1684
+ #include <getopt.h>
1685
+ #include <sys/types.h>
1686
++
1687
++/* Get MB_LEN_MAX.  */
1688
++#include <limits.h>
1689
++/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
1690
++   installation; work around this configuration error.  */
1691
++#if !defined MB_LEN_MAX || MB_LEN_MAX == 1
1692
++# define MB_LEN_MAX 16
1693
++#endif
1694
++
1695
++/* Get MB_CUR_MAX.  */
1696
++#include <stdlib.h>
1697
++
1698
++/* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.  */
1699
++/* Get mbstate_t, mbrtowc(), wcwidth().  */
1700
++#if HAVE_WCHAR_H
1701
++# include <wchar.h>
1702
++#endif
1703
++
1704
+ #include "system.h"
1705
+ #include "error.h"
1706
+ #include "fadvise.h"
1707
+@@ -323,6 +341,18 @@
1708
+ #include "xstrtol.h"
1709
+ #include "xdectoint.h"
1710
+ 
1711
++/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
1712
++#if HAVE_MBRTOWC && defined mbstate_t
1713
++# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
1714
++#endif
1715
++
1716
++#ifndef HAVE_DECL_WCWIDTH
1717
++"this configure-time declaration test was not run"
1718
++#endif
1719
++#if !HAVE_DECL_WCWIDTH
1720
++extern int wcwidth ();
1721
++#endif
1722
++
1723
+ /* The official name of this program (e.g., no 'g' prefix).  */
1724
+ #define PROGRAM_NAME "pr"
1725
+ 
1726
+@@ -415,7 +445,20 @@ struct COLUMN
1727
+ 
1728
+ typedef struct COLUMN COLUMN;
1729
+ 
1730
+-static int char_to_clump (char c);
1731
++/* Funtion pointers to switch functions for single byte locale or for
1732
++   multibyte locale. If multibyte functions do not exist in your sysytem,
1733
++   these pointers always point the function for single byte locale. */
1734
++static void (*print_char) (char c);
1735
++static int (*char_to_clump) (char c);
1736
++
1737
++/* Functions for single byte locale. */
1738
++static void print_char_single (char c);
1739
++static int char_to_clump_single (char c);
1740
++
1741
++/* Functions for multibyte locale. */
1742
++static void print_char_multi (char c);
1743
++static int char_to_clump_multi (char c);
1744
++
1745
+ static bool read_line (COLUMN *p);
1746
+ static bool print_page (void);
1747
+ static bool print_stored (COLUMN *p);
1748
+@@ -427,6 +470,7 @@ static void add_line_number (COLUMN *p);
1749
+ static void getoptnum (const char *n_str, int min, int *num,
1750
+                        const char *errfmt);
1751
+ static void getoptarg (char *arg, char switch_char, char *character,
1752
++                       int *character_length, int *character_width,
1753
+                        int *number);
1754
+ static void print_files (int number_of_files, char **av);
1755
+ static void init_parameters (int number_of_files);
1756
+@@ -440,7 +484,6 @@ static void store_char (char c);
1757
+ static void pad_down (unsigned int lines);
1758
+ static void read_rest_of_line (COLUMN *p);
1759
+ static void skip_read (COLUMN *p, int column_number);
1760
+-static void print_char (char c);
1761
+ static void cleanup (void);
1762
+ static void print_sep_string (void);
1763
+ static void separator_string (const char *optarg_S);
1764
+@@ -452,7 +495,7 @@ static COLUMN *column_vector;
1765
+    we store the leftmost columns contiguously in buff.
1766
+    To print a line from buff, get the index of the first character
1767
+    from line_vector[i], and print up to line_vector[i + 1]. */
1768
+-static char *buff;
1769
++static unsigned char *buff;
1770
+ 
1771
+ /* Index of the position in buff where the next character
1772
+    will be stored. */
1773
+@@ -556,7 +599,7 @@ static int chars_per_column;
1774
+ static bool untabify_input = false;
1775
+ 
1776
+ /* (-e) The input tab character. */
1777
+-static char input_tab_char = '\t';
1778
++static char input_tab_char[MB_LEN_MAX] = "\t";
1779
+ 
1780
+ /* (-e) Tabstops are at chars_per_tab, 2*chars_per_tab, 3*chars_per_tab, ...
1781
+    where the leftmost column is 1. */
1782
+@@ -566,7 +609,10 @@ static int chars_per_input_tab = 8;
1783
+ static bool tabify_output = false;
1784
+ 
1785
+ /* (-i) The output tab character. */
1786
+-static char output_tab_char = '\t';
1787
++static char output_tab_char[MB_LEN_MAX] = "\t";
1788
++
1789
++/* (-i) The byte length of output tab character. */
1790
++static int output_tab_char_length = 1;
1791
+ 
1792
+ /* (-i) The width of the output tab. */
1793
+ static int chars_per_output_tab = 8;
1794
+@@ -636,7 +682,13 @@ static int line_number;
1795
+ static bool numbered_lines = false;
1796
+ 
1797
+ /* (-n) Character which follows each line number. */
1798
+-static char number_separator = '\t';
1799
++static char number_separator[MB_LEN_MAX] = "\t";
1800
++
1801
++/* (-n) The byte length of the character which follows each line number. */
1802
++static int number_separator_length = 1;
1803
++
1804
++/* (-n) The character width of the character which follows each line number. */
1805
++static int number_separator_width = 0;
1806
+ 
1807
+ /* (-n) line counting starts with 1st line of input file (not with 1st
1808
+    line of 1st page printed). */
1809
+@@ -689,6 +741,7 @@ static bool use_col_separator = false;
1810
+    -a|COLUMN|-m is a 'space' and with the -J option a 'tab'. */
1811
+ static char *col_sep_string = (char *) "";
1812
+ static int col_sep_length = 0;
1813
++static int col_sep_width = 0;
1814
+ static char *column_separator = (char *) " ";
1815
+ static char *line_separator = (char *) "\t";
1816
+ 
1817
+@@ -839,6 +892,13 @@ separator_string (const char *optarg_S)
1818
+   col_sep_length = (int) strlen (optarg_S);
1819
+   col_sep_string = xmalloc (col_sep_length + 1);
1820
+   strcpy (col_sep_string, optarg_S);
1821
++
1822
++#if HAVE_MBRTOWC
1823
++  if (MB_CUR_MAX > 1)
1824
++    col_sep_width = mbswidth (col_sep_string, 0);
1825
++  else
1826
++#endif
1827
++    col_sep_width = col_sep_length;
1828
+ }
1829
+ 
1830
+ int
1831
+@@ -863,6 +923,21 @@ main (int argc, char **argv)
1832
+ 
1833
+   atexit (close_stdout);
1834
+ 
1835
++/* Define which functions are used, the ones for single byte locale or the ones
1836
++   for multibyte locale. */
1837
++#if HAVE_MBRTOWC
1838
++  if (MB_CUR_MAX > 1)
1839
++    {
1840
++      print_char = print_char_multi;
1841
++      char_to_clump = char_to_clump_multi;
1842
++    }
1843
++  else
1844
++#endif
1845
++    {
1846
++      print_char = print_char_single;
1847
++      char_to_clump = char_to_clump_single;
1848
++    }
1849
++
1850
+   n_files = 0;
1851
+   file_names = (argc > 1
1852
+                 ? xmalloc ((argc - 1) * sizeof (char *))
1853
+@@ -939,8 +1014,12 @@ main (int argc, char **argv)
1854
+           break;
1855
+         case 'e':
1856
+           if (optarg)
1857
+-            getoptarg (optarg, 'e', &input_tab_char,
1858
+-                       &chars_per_input_tab);
1859
++            {
1860
++              int dummy_length, dummy_width;
1861
++
1862
++              getoptarg (optarg, 'e', input_tab_char, &dummy_length,
1863
++                         &dummy_width, &chars_per_input_tab);
1864
++            }
1865
+           /* Could check tab width > 0. */
1866
+           untabify_input = true;
1867
+           break;
1868
+@@ -953,8 +1032,12 @@ main (int argc, char **argv)
1869
+           break;
1870
+         case 'i':
1871
+           if (optarg)
1872
+-            getoptarg (optarg, 'i', &output_tab_char,
1873
+-                       &chars_per_output_tab);
1874
++            {
1875
++              int dummy_width;
1876
++
1877
++              getoptarg (optarg, 'i', output_tab_char, &output_tab_char_length,
1878
++                         &dummy_width, &chars_per_output_tab);
1879
++            }
1880
+           /* Could check tab width > 0. */
1881
+           tabify_output = true;
1882
+           break;
1883
+@@ -972,8 +1055,8 @@ main (int argc, char **argv)
1884
+         case 'n':
1885
+           numbered_lines = true;
1886
+           if (optarg)
1887
+-            getoptarg (optarg, 'n', &number_separator,
1888
+-                       &chars_per_number);
1889
++            getoptarg (optarg, 'n', number_separator, &number_separator_length,
1890
++                       &number_separator_width, &chars_per_number);
1891
+           break;
1892
+         case 'N':
1893
+           skip_count = false;
1894
+@@ -997,7 +1080,7 @@ main (int argc, char **argv)
1895
+           old_s = false;
1896
+           /* Reset an additional input of -s, -S dominates -s */
1897
+           col_sep_string = bad_cast ("");
1898
+-          col_sep_length = 0;
1899
++          col_sep_length = col_sep_width = 0;
1900
+           use_col_separator = true;
1901
+           if (optarg)
1902
+             separator_string (optarg);
1903
+@@ -1152,10 +1235,45 @@ getoptnum (const char *n_str, int min, i
1904
+    a number. */
1905
+ 
1906
+ static void
1907
+-getoptarg (char *arg, char switch_char, char *character, int *number)
1908
++getoptarg (char *arg, char switch_char, char *character, int *character_length,
1909
++           int *character_width, int *number)
1910
+ {
1911
+   if (!ISDIGIT (*arg))
1912
+-    *character = *arg++;
1913
++    {
1914
++#ifdef HAVE_MBRTOWC
1915
++      if (MB_CUR_MAX > 1)        /* for multibyte locale. */
1916
++        {
1917
++          wchar_t wc;
1918
++          size_t mblength;
1919
++          int width;
1920
++          mbstate_t state = {'\0'};
1921
++
1922
++          mblength = mbrtowc (&wc, arg, strnlen(arg, MB_LEN_MAX), &state);
1923
++
1924
++          if (mblength == (size_t)-1 || mblength == (size_t)-2)
1925
++            {
1926
++              *character_length = 1;
1927
++              *character_width = 1;
1928
++            }
1929
++          else
1930
++            {
1931
++              *character_length = (mblength < 1) ? 1 : mblength;
1932
++              width = wcwidth (wc);
1933
++              *character_width = (width < 0) ? 0 : width;
1934
++            }
1935
++
1936
++          strncpy (character, arg, *character_length);
1937
++          arg += *character_length;
1938
++        }
1939
++      else                        /* for single byte locale. */
1940
++#endif
1941
++        {
1942
++          *character = *arg++;
1943
++          *character_length = 1;
1944
++          *character_width = 1;
1945
++        }
1946
++    }
1947
++
1948
+   if (*arg)
1949
+     {
1950
+       long int tmp_long;
1951
+@@ -1177,6 +1295,11 @@ static void
1952
+ init_parameters (int number_of_files)
1953
+ {
1954
+   int chars_used_by_number = 0;
1955
++  int mb_len = 1;
1956
++#if HAVE_MBRTOWC
1957
++  if (MB_CUR_MAX > 1)
1958
++    mb_len = MB_LEN_MAX;
1959
++#endif
1960
+ 
1961
+   lines_per_body = lines_per_page - lines_per_header - lines_per_footer;
1962
+   if (lines_per_body <= 0)
1963
+@@ -1214,7 +1337,7 @@ init_parameters (int number_of_files)
1964
+           else
1965
+             col_sep_string = column_separator;
1966
+ 
1967
+-          col_sep_length = 1;
1968
++          col_sep_length = col_sep_width = 1;
1969
+           use_col_separator = true;
1970
+         }
1971
+       /* It's rather pointless to define a TAB separator with column
1972
+@@ -1244,11 +1367,11 @@ init_parameters (int number_of_files)
1973
+              + TAB_WIDTH (chars_per_input_tab, chars_per_number);   */
1974
+ 
1975
+       /* Estimate chars_per_text without any margin and keep it constant. */
1976
+-      if (number_separator == '\t')
1977
++      if (number_separator[0] == '\t')
1978
+         number_width = (chars_per_number
1979
+                         + TAB_WIDTH (chars_per_default_tab, chars_per_number));
1980
+       else
1981
+-        number_width = chars_per_number + 1;
1982
++        number_width = chars_per_number + number_separator_width;
1983
+ 
1984
+       /* The number is part of the column width unless we are
1985
+          printing files in parallel. */
1986
+@@ -1257,7 +1380,7 @@ init_parameters (int number_of_files)
1987
+     }
1988
+ 
1989
+   chars_per_column = (chars_per_line - chars_used_by_number
1990
+-                      - (columns - 1) * col_sep_length) / columns;
1991
++                      - (columns - 1) * col_sep_width) / columns;
1992
+ 
1993
+   if (chars_per_column < 1)
1994
+     error (EXIT_FAILURE, 0, _("page width too narrow"));
1995
+@@ -1275,7 +1398,7 @@ init_parameters (int number_of_files)
1996
+      We've to use 8 as the lower limit, if we use chars_per_default_tab = 8
1997
+      to expand a tab which is not an input_tab-char. */
1998
+   free (clump_buff);
1999
+-  clump_buff = xmalloc (MAX (8, chars_per_input_tab));
2000
++  clump_buff = xmalloc (mb_len * MAX (8, chars_per_input_tab));
2001
+ }
2002
+ 
2003
+ /* Open the necessary files,
2004
+@@ -1383,7 +1506,7 @@ init_funcs (void)
2005
+ 
2006
+   /* Enlarge p->start_position of first column to use the same form of
2007
+      padding_not_printed with all columns. */
2008
+-  h = h + col_sep_length;
2009
++  h = h + col_sep_width;
2010
+ 
2011
+   /* This loop takes care of all but the rightmost column. */
2012
+ 
2013
+@@ -1417,7 +1540,7 @@ init_funcs (void)
2014
+         }
2015
+       else
2016
+         {
2017
+-          h = h_next + col_sep_length;
2018
++          h = h_next + col_sep_width;
2019
+           h_next = h + chars_per_column;
2020
+         }
2021
+     }
2022
+@@ -1708,9 +1831,9 @@ static void
2023
+ align_column (COLUMN *p)
2024
+ {
2025
+   padding_not_printed = p->start_position;
2026
+-  if (padding_not_printed - col_sep_length > 0)
2027
++  if (padding_not_printed - col_sep_width > 0)
2028
+     {
2029
+-      pad_across_to (padding_not_printed - col_sep_length);
2030
++      pad_across_to (padding_not_printed - col_sep_width);
2031
+       padding_not_printed = ANYWHERE;
2032
+     }
2033
+ 
2034
+@@ -1981,13 +2104,13 @@ store_char (char c)
2035
+       /* May be too generous. */
2036
+       buff = X2REALLOC (buff, &buff_allocated);
2037
+     }
2038
+-  buff[buff_current++] = c;
2039
++  buff[buff_current++] = (unsigned char) c;
2040
+ }
2041
+ 
2042
+ static void
2043
+ add_line_number (COLUMN *p)
2044
+ {
2045
+-  int i;
2046
++  int i, j;
2047
+   char *s;
2048
+   int num_width;
2049
+ 
2050
+@@ -2004,22 +2127,24 @@ add_line_number (COLUMN *p)
2051
+       /* Tabification is assumed for multiple columns, also for n-separators,
2052
+          but 'default n-separator = TAB' hasn't been given priority over
2053
+          equal column_width also specified by POSIX. */
2054
+-      if (number_separator == '\t')
2055
++      if (number_separator[0] == '\t')
2056
+         {
2057
+           i = number_width - chars_per_number;
2058
+           while (i-- > 0)
2059
+             (p->char_func) (' ');
2060
+         }
2061
+       else
2062
+-        (p->char_func) (number_separator);
2063
++        for (j = 0; j < number_separator_length; j++)
2064
++          (p->char_func) (number_separator[j]);
2065
+     }
2066
+   else
2067
+     /* To comply with POSIX, we avoid any expansion of default TAB
2068
+        separator with a single column output. No column_width requirement
2069
+        has to be considered. */
2070
+     {
2071
+-      (p->char_func) (number_separator);
2072
+-      if (number_separator == '\t')
2073
++      for (j = 0; j < number_separator_length; j++)
2074
++        (p->char_func) (number_separator[j]);
2075
++      if (number_separator[0] == '\t')
2076
+         output_position = POS_AFTER_TAB (chars_per_output_tab,
2077
+                           output_position);
2078
+     }
2079
+@@ -2180,7 +2305,7 @@ print_white_space (void)
2080
+   while (goal - h_old > 1
2081
+          && (h_new = POS_AFTER_TAB (chars_per_output_tab, h_old)) <= goal)
2082
+     {
2083
+-      putchar (output_tab_char);
2084
++      fwrite (output_tab_char, sizeof(char), output_tab_char_length, stdout);
2085
+       h_old = h_new;
2086
+     }
2087
+   while (++h_old <= goal)
2088
+@@ -2200,6 +2325,7 @@ print_sep_string (void)
2089
+ {
2090
+   char *s;
2091
+   int l = col_sep_length;
2092
++  int not_space_flag;
2093
+ 
2094
+   s = col_sep_string;
2095
+ 
2096
+@@ -2213,6 +2339,7 @@ print_sep_string (void)
2097
+     {
2098
+       for (; separators_not_printed > 0; --separators_not_printed)
2099
+         {
2100
++          not_space_flag = 0;
2101
+           while (l-- > 0)
2102
+             {
2103
+               /* 3 types of sep_strings: spaces only, spaces and chars,
2104
+@@ -2226,12 +2353,15 @@ print_sep_string (void)
2105
+                 }
2106
+               else
2107
+                 {
2108
++                  not_space_flag = 1;
2109
+                   if (spaces_not_printed > 0)
2110
+                     print_white_space ();
2111
+                   putchar (*s++);
2112
+-                  ++output_position;
2113
+                 }
2114
+             }
2115
++          if (not_space_flag)
2116
++            output_position += col_sep_width;
2117
++
2118
+           /* sep_string ends with some spaces */
2119
+           if (spaces_not_printed > 0)
2120
+             print_white_space ();
2121
+@@ -2259,7 +2389,7 @@ print_clump (COLUMN *p, int n, char *clu
2122
+    required number of tabs and spaces. */
2123
+ 
2124
+ static void
2125
+-print_char (char c)
2126
++print_char_single (char c)
2127
+ {
2128
+   if (tabify_output)
2129
+     {
2130
+@@ -2283,6 +2413,74 @@ print_char (char c)
2131
+   putchar (c);
2132
+ }
2133
+ 
2134
++#ifdef HAVE_MBRTOWC
2135
++static void
2136
++print_char_multi (char c)
2137
++{
2138
++  static size_t mbc_pos = 0;
2139
++  static char mbc[MB_LEN_MAX] = {'\0'};
2140
++  static mbstate_t state = {'\0'};
2141
++  mbstate_t state_bak;
2142
++  wchar_t wc;
2143
++  size_t mblength;
2144
++  int width;
2145
++
2146
++  if (tabify_output)
2147
++    {
2148
++      state_bak = state;
2149
++      mbc[mbc_pos++] = c;
2150
++      mblength = mbrtowc (&wc, mbc, mbc_pos, &state);
2151
++
2152
++      while (mbc_pos > 0)
2153
++        {
2154
++          switch (mblength)
2155
++            {
2156
++            case (size_t)-2:
2157
++              state = state_bak;
2158
++              return;
2159
++
2160
++            case (size_t)-1:
2161
++              state = state_bak;
2162
++              ++output_position;
2163
++              putchar (mbc[0]);
2164
++              memmove (mbc, mbc + 1, MB_CUR_MAX - 1);
2165
++              --mbc_pos;
2166
++              break;
2167
++
2168
++            case 0:
2169
++              mblength = 1;
2170
++
2171
++            default:
2172
++              if (wc == L' ')
2173
++                {
2174
++                  memmove (mbc, mbc + mblength, MB_CUR_MAX - mblength);
2175
++                  --mbc_pos;
2176
++                  ++spaces_not_printed;
2177
++                  return;
2178
++                }
2179
++              else if (spaces_not_printed > 0)
2180
++                print_white_space ();
2181
++
2182
++              /* Nonprintables are assumed to have width 0, except L'\b'. */
2183
++              if ((width = wcwidth (wc)) < 1)
2184
++                {
2185
++                  if (wc == L'\b')
2186
++                    --output_position;
2187
++                }
2188
++              else
2189
++                output_position += width;
2190
++
2191
++              fwrite (mbc, sizeof(char), mblength, stdout);
2192
++              memmove (mbc, mbc + mblength, MB_CUR_MAX - mblength);
2193
++              mbc_pos -= mblength;
2194
++            }
2195
++        }
2196
++      return;
2197
++    }
2198
++  putchar (c);
2199
++}
2200
++#endif
2201
++
2202
+ /* Skip to page PAGE before printing.
2203
+    PAGE may be larger than total number of pages. */
2204
+ 
2205
+@@ -2462,9 +2660,9 @@ read_line (COLUMN *p)
2206
+           align_empty_cols = false;
2207
+         }
2208
+ 
2209
+-      if (padding_not_printed - col_sep_length > 0)
2210
++      if (padding_not_printed - col_sep_width > 0)
2211
+         {
2212
+-          pad_across_to (padding_not_printed - col_sep_length);
2213
++          pad_across_to (padding_not_printed - col_sep_width);
2214
+           padding_not_printed = ANYWHERE;
2215
+         }
2216
+ 
2217
+@@ -2534,7 +2732,7 @@ print_stored (COLUMN *p)
2218
+   int i;
2219
+ 
2220
+   int line = p->current_line++;
2221
+-  char *first = &buff[line_vector[line]];
2222
++  unsigned char *first = &buff[line_vector[line]];
2223
+   /* FIXME
2224
+      UMR: Uninitialized memory read:
2225
+      * This is occurring while in:
2226
+@@ -2546,7 +2744,7 @@ print_stored (COLUMN *p)
2227
+      xmalloc        [xmalloc.c:94]
2228
+      init_store_cols [pr.c:1648]
2229
+      */
2230
+-  char *last = &buff[line_vector[line + 1]];
2231
++  unsigned char *last = &buff[line_vector[line + 1]];
2232
+ 
2233
+   pad_vertically = true;
2234
+ 
2235
+@@ -2565,9 +2763,9 @@ print_stored (COLUMN *p)
2236
+         }
2237
+     }
2238
+ 
2239
+-  if (padding_not_printed - col_sep_length > 0)
2240
++  if (padding_not_printed - col_sep_width > 0)
2241
+     {
2242
+-      pad_across_to (padding_not_printed - col_sep_length);
2243
++      pad_across_to (padding_not_printed - col_sep_width);
2244
+       padding_not_printed = ANYWHERE;
2245
+     }
2246
+ 
2247
+@@ -2580,8 +2778,8 @@ print_stored (COLUMN *p)
2248
+   if (spaces_not_printed == 0)
2249
+     {
2250
+       output_position = p->start_position + end_vector[line];
2251
+-      if (p->start_position - col_sep_length == chars_per_margin)
2252
+-        output_position -= col_sep_length;
2253
++      if (p->start_position - col_sep_width == chars_per_margin)
2254
++        output_position -= col_sep_width;
2255
+     }
2256
+ 
2257
+   return true;
2258
+@@ -2600,7 +2798,7 @@ print_stored (COLUMN *p)
2259
+    number of characters is 1.) */
2260
+ 
2261
+ static int
2262
+-char_to_clump (char c)
2263
++char_to_clump_single (char c)
2264
+ {
2265
+   unsigned char uc = c;
2266
+   char *s = clump_buff;
2267
+@@ -2610,10 +2808,10 @@ char_to_clump (char c)
2268
+   int chars;
2269
+   int chars_per_c = 8;
2270
+ 
2271
+-  if (c == input_tab_char)
2272
++  if (c == input_tab_char[0])
2273
+     chars_per_c = chars_per_input_tab;
2274
+ 
2275
+-  if (c == input_tab_char || c == '\t')
2276
++  if (c == input_tab_char[0] || c == '\t')
2277
+     {
2278
+       width = TAB_WIDTH (chars_per_c, input_position);
2279
+ 
2280
+@@ -2694,6 +2892,164 @@ char_to_clump (char c)
2281
+   return chars;
2282
+ }
2283
+ 
2284
++#ifdef HAVE_MBRTOWC
2285
++static int
2286
++char_to_clump_multi (char c)
2287
++{
2288
++  static size_t mbc_pos = 0;
2289
++  static char mbc[MB_LEN_MAX] = {'\0'};
2290
++  static mbstate_t state = {'\0'};
2291
++  mbstate_t state_bak;
2292
++  wchar_t wc;
2293
++  size_t mblength;
2294
++  int wc_width;
2295
++  register char *s = clump_buff;
2296
++  register int i, j;
2297
++  char esc_buff[4];
2298
++  int width;
2299
++  int chars;
2300
++  int chars_per_c = 8;
2301
++
2302
++  state_bak = state;
2303
++  mbc[mbc_pos++] = c;
2304
++  mblength = mbrtowc (&wc, mbc, mbc_pos, &state);
2305
++
2306
++  width = 0;
2307
++  chars = 0;
2308
++  while (mbc_pos > 0)
2309
++    {
2310
++      switch (mblength)
2311
++        {
2312
++        case (size_t)-2:
2313
++          state = state_bak;
2314
++          return 0;
2315
++
2316
++        case (size_t)-1:
2317
++          state = state_bak;
2318
++          mblength = 1;
2319
++
2320
++          if (use_esc_sequence || use_cntrl_prefix)
2321
++            {
2322
++              width = +4;
2323
++              chars = +4;
2324
++              *s++ = '\\';
2325
++              sprintf (esc_buff, "%03o", (unsigned char) mbc[0]);
2326
++              for (i = 0; i <= 2; ++i)
2327
++                *s++ = (int) esc_buff[i];
2328
++            }
2329
++          else
2330
++            {
2331
++              width += 1;
2332
++              chars += 1;
2333
++              *s++ = mbc[0];
2334
++            }
2335
++          break;
2336
++
2337
++        case 0:
2338
++          mblength = 1;
2339
++                /* Fall through */
2340
++
2341
++        default:
2342
++          if (memcmp (mbc, input_tab_char, mblength) == 0)
2343
++            chars_per_c = chars_per_input_tab;
2344
++
2345
++          if (memcmp (mbc, input_tab_char, mblength) == 0 || c == '\t')
2346
++            {
2347
++              int  width_inc;
2348
++
2349
++              width_inc = TAB_WIDTH (chars_per_c, input_position);
2350
++              width += width_inc;
2351
++
2352
++              if (untabify_input)
2353
++                {
2354
++                  for (i = width_inc; i; --i)
2355
++                    *s++ = ' ';
2356
++                  chars += width_inc;
2357
++                }
2358
++              else
2359
++                {
2360
++                  for (i = 0; i <  mblength; i++)
2361
++                    *s++ = mbc[i];
2362
++                  chars += mblength;
2363
++                }
2364
++            }
2365
++          else if ((wc_width = wcwidth (wc)) < 1)
2366
++            {
2367
++              if (use_esc_sequence)
2368
++                {
2369
++                  for (i = 0; i < mblength; i++)
2370
++                    {
2371
++                      width += 4;
2372
++                      chars += 4;
2373
++                      *s++ = '\\';
2374
++                      sprintf (esc_buff, "%03o", (unsigned char) mbc[i]);
2375
++                      for (j = 0; j <= 2; ++j)
2376
++                        *s++ = (int) esc_buff[j];
2377
++                    }
2378
++                }
2379
++              else if (use_cntrl_prefix)
2380
++                {
2381
++                  if (wc < 0200)
2382
++                    {
2383
++                      width += 2;
2384
++                      chars += 2;
2385
++                      *s++ = '^';
2386
++                      *s++ = wc ^ 0100;
2387
++                    }
2388
++                  else
2389
++                    {
2390
++                      for (i = 0; i < mblength; i++)
2391
++                        {
2392
++                          width += 4;
2393
++                          chars += 4;
2394
++                          *s++ = '\\';
2395
++                          sprintf (esc_buff, "%03o", (unsigned char) mbc[i]);
2396
++                          for (j = 0; j <= 2; ++j)
2397
++                            *s++ = (int) esc_buff[j];
2398
++                        }
2399
++                    }
2400
++                }
2401
++              else if (wc == L'\b')
2402
++                {
2403
++                  width += -1;
2404
++                  chars += 1;
2405
++                  *s++ = c;
2406
++                }
2407
++              else
2408
++                {
2409
++                  width += 0;
2410
++                  chars += mblength;
2411
++                  for (i = 0; i < mblength; i++)
2412
++                    *s++ = mbc[i];
2413
++                }
2414
++            }
2415
++          else
2416
++            {
2417
++              width += wc_width;
2418
++              chars += mblength;
2419
++              for (i = 0; i < mblength; i++)
2420
++                *s++ = mbc[i];
2421
++            }
2422
++        }
2423
++      memmove (mbc, mbc + mblength, MB_CUR_MAX - mblength);
2424
++      mbc_pos -= mblength;
2425
++    }
2426
++
2427
++  /* Too many backspaces must put us in position 0 -- never negative. */
2428
++  if (width < 0 && input_position == 0)
2429
++    {
2430
++      chars = 0;
2431
++      input_position = 0;
2432
++    }
2433
++  else if (width < 0 && input_position <= -width)
2434
++    input_position = 0;
2435
++  else
2436
++   input_position += width;
2437
++
2438
++  return chars;
2439
++}
2440
++#endif
2441
++
2442
+ /* We've just printed some files and need to clean up things before
2443
+    looking for more options and printing the next batch of files.
2444
+ 
2445
+diff -Naurp coreutils-8.25-orig/src/sort.c coreutils-8.25/src/sort.c
2446
+--- coreutils-8.25-orig/src/sort.c	2016-01-16 13:09:33.000000000 -0600
2447
+@@ -29,6 +29,14 @@
2448
+ #include <sys/wait.h>
2449
+ #include <signal.h>
2450
+ #include <assert.h>
2451
++#if HAVE_WCHAR_H
2452
++# include <wchar.h>
2453
++#endif
2454
++/* Get isw* functions. */
2455
++#if HAVE_WCTYPE_H
2456
++# include <wctype.h>
2457
++#endif
2458
++
2459
+ #include "system.h"
2460
+ #include "argmatch.h"
2461
+ #include "error.h"
2462
+@@ -163,14 +171,39 @@ static int decimal_point;
2463
+ /* Thousands separator; if -1, then there isn't one.  */
2464
+ static int thousands_sep;
2465
+ 
2466
++/* True if -f is specified.  */
2467
++static bool folding;
2468
++
2469
+ /* Nonzero if the corresponding locales are hard.  */
2470
+ static bool hard_LC_COLLATE;
2471
+-#if HAVE_NL_LANGINFO
2472
++#if HAVE_LANGINFO_CODESET
2473
+ static bool hard_LC_TIME;
2474
+ #endif
2475
+ 
2476
+ #define NONZERO(x) ((x) != 0)
2477
+ 
2478
++/* get a multibyte character's byte length. */
2479
++#define GET_BYTELEN_OF_CHAR(LIM, PTR, MBLENGTH, STATE)                        \
2480
++  do                                                                        \
2481
++    {                                                                        \
2482
++      wchar_t wc;                                                        \
2483
++      mbstate_t state_bak;                                                \
2484
++                                                                        \
2485
++      state_bak = STATE;                                                \
2486
++      mblength = mbrtowc (&wc, PTR, LIM - PTR, &STATE);                        \
2487
++                                                                        \
2488
++      switch (MBLENGTH)                                                        \
2489
++        {                                                                \
2490
++        case (size_t)-1:                                                \
2491
++        case (size_t)-2:                                                \
2492
++          STATE = state_bak;                                                \
2493
++                /* Fall through. */                                        \
2494
++        case 0:                                                                \
2495
++          MBLENGTH = 1;                                                        \
2496
++      }                                                                        \
2497
++    }                                                                        \
2498
++  while (0)
2499
++
2500
+ /* The kind of blanks for '-b' to skip in various options. */
2501
+ enum blanktype { bl_start, bl_end, bl_both };
2502
+ 
2503
+@@ -344,13 +377,11 @@ static bool reverse;
2504
+    they were read if all keys compare equal.  */
2505
+ static bool stable;
2506
+ 
2507
+-/* If TAB has this value, blanks separate fields.  */
2508
+-enum { TAB_DEFAULT = CHAR_MAX + 1 };
2509
+-
2510
+-/* Tab character separating fields.  If TAB_DEFAULT, then fields are
2511
++/* Tab character separating fields.  If tab_length is 0, then fields are
2512
+    separated by the empty string between a non-blank character and a blank
2513
+    character. */
2514
+-static int tab = TAB_DEFAULT;
2515
++static char tab[MB_LEN_MAX + 1];
2516
++static size_t tab_length = 0;
2517
+ 
2518
+ /* Flag to remove consecutive duplicate lines from the output.
2519
+    Only the last of a sequence of equal lines will be output. */
2520
+@@ -810,6 +841,46 @@ reap_all (void)
2521
+     reap (-1);
2522
+ }
2523
+ 
2524
++/* Function pointers. */
2525
++static void
2526
++(*inittables) (void);
2527
++static char *
2528
++(*begfield) (const struct line*, const struct keyfield *);
2529
++static char *
2530
++(*limfield) (const struct line*, const struct keyfield *);
2531
++static void
2532
++(*skipblanks) (char **ptr, char *lim);
2533
++static int
2534
++(*getmonth) (char const *, size_t, char **);
2535
++static int
2536
++(*keycompare) (const struct line *, const struct line *);
2537
++static int
2538
++(*numcompare) (const char *, const char *);
2539
++
2540
++/* Test for white space multibyte character.
2541
++   Set LENGTH the byte length of investigated multibyte character. */
2542
++#if HAVE_MBRTOWC
2543
++static int
2544
++ismbblank (const char *str, size_t len, size_t *length)
2545
++{
2546
++  size_t mblength;
2547
++  wchar_t wc;
2548
++  mbstate_t state;
2549
++
2550
++  memset (&state, '\0', sizeof(mbstate_t));
2551
++  mblength = mbrtowc (&wc, str, len, &state);
2552
++
2553
++  if (mblength == (size_t)-1 || mblength == (size_t)-2)
2554
++    {
2555
++      *length = 1;
2556
++      return 0;
2557
++    }
2558
++
2559
++  *length = (mblength < 1) ? 1 : mblength;
2560
++  return iswblank (wc) || wc == '\n';
2561
++}
2562
++#endif
2563
++
2564
+ /* Clean up any remaining temporary files.  */
2565
+ 
2566
+ static void
2567
+@@ -1254,7 +1325,7 @@ zaptemp (char const *name)
2568
+   free (node);
2569
+ }
2570
+ 
2571
+-#if HAVE_NL_LANGINFO
2572
++#if HAVE_LANGINFO_CODESET
2573
+ 
2574
+ static int
2575
+ struct_month_cmp (void const *m1, void const *m2)
2576
+@@ -1269,7 +1340,7 @@ struct_month_cmp (void const *m1, void c
2577
+ /* Initialize the character class tables. */
2578
+ 
2579
+ static void
2580
+-inittables (void)
2581
++inittables_uni (void)
2582
+ {
2583
+   size_t i;
2584
+ 
2585
+@@ -1281,7 +1352,7 @@ inittables (void)
2586
+       fold_toupper[i] = toupper (i);
2587
+     }
2588
+ 
2589
+-#if HAVE_NL_LANGINFO
2590
++#if HAVE_LANGINFO_CODESET
2591
+   /* If we're not in the "C" locale, read different names for months.  */
2592
+   if (hard_LC_TIME)
2593
+     {
2594
+@@ -1363,6 +1434,84 @@ specify_nmerge (int oi, char c, char con
2595
+     xstrtol_fatal (e, oi, c, long_options, s);
2596
+ }
2597
+ 
2598
++#if HAVE_MBRTOWC
2599
++static void
2600
++inittables_mb (void)
2601
++{
2602
++  int i, j, k, l;
2603
++  char *name, *s, *lc_time, *lc_ctype;
2604
++  size_t s_len, mblength;
2605
++  char mbc[MB_LEN_MAX];
2606
++  wchar_t wc, pwc;
2607
++  mbstate_t state_mb, state_wc;
2608
++
2609
++  lc_time = setlocale (LC_TIME, "");
2610
++  if (lc_time)
2611
++    lc_time = xstrdup (lc_time);
2612
++
2613
++  lc_ctype = setlocale (LC_CTYPE, "");
2614
++  if (lc_ctype)
2615
++    lc_ctype = xstrdup (lc_ctype);
2616
++
2617
++  if (lc_time && lc_ctype)
2618
++    /* temporarily set LC_CTYPE to match LC_TIME, so that we can convert
2619
++     * the names of months to upper case */
2620
++    setlocale (LC_CTYPE, lc_time);
2621
++
2622
++  for (i = 0; i < MONTHS_PER_YEAR; i++)
2623
++    {
2624
++      s = (char *) nl_langinfo (ABMON_1 + i);
2625
++      s_len = strlen (s);
2626
++      monthtab[i].name = name = (char *) xmalloc (s_len + 1);
2627
++      monthtab[i].val = i + 1;
2628
++
2629
++      memset (&state_mb, '\0', sizeof (mbstate_t));
2630
++      memset (&state_wc, '\0', sizeof (mbstate_t));
2631
++
2632
++      for (j = 0; j < s_len;)
2633
++        {
2634
++          if (!ismbblank (s + j, s_len - j, &mblength))
2635
++            break;
2636
++          j += mblength;
2637
++        }
2638
++
2639
++      for (k = 0; j < s_len;)
2640
++        {
2641
++          mblength = mbrtowc (&wc, (s + j), (s_len - j), &state_mb);
2642
++          assert (mblength != (size_t)-1 && mblength != (size_t)-2);
2643
++          if (mblength == 0)
2644
++            break;
2645
++
2646
++          pwc = towupper (wc);
2647
++          if (pwc == wc)
2648
++            {
2649
++              memcpy (mbc, s + j, mblength);
2650
++              j += mblength;
2651
++            }
2652
++          else
2653
++            {
2654
++              j += mblength;
2655
++              mblength = wcrtomb (mbc, pwc, &state_wc);
2656
++              assert (mblength != (size_t)0 && mblength != (size_t)-1);
2657
++            }
2658
++
2659
++          for (l = 0; l < mblength; l++)
2660
++            name[k++] = mbc[l];
2661
++        }
2662
++      name[k] = '\0';
2663
++    }
2664
++  qsort ((void *) monthtab, MONTHS_PER_YEAR,
2665
++      sizeof (struct month), struct_month_cmp);
2666
++
2667
++  if (lc_time && lc_ctype)
2668
++    /* restore the original locales */
2669
++    setlocale (LC_CTYPE, lc_ctype);
2670
++
2671
++  free (lc_ctype);
2672
++  free (lc_time);
2673
++}
2674
++#endif
2675
++
2676
+ /* Specify the amount of main memory to use when sorting.  */
2677
+ static void
2678
+ specify_sort_size (int oi, char c, char const *s)
2679
+@@ -1596,7 +1745,7 @@ buffer_linelim (struct buffer const *buf
2680
+    by KEY in LINE. */
2681
+ 
2682
+ static char *
2683
+-begfield (struct line const *line, struct keyfield const *key)
2684
++begfield_uni (const struct line *line, const struct keyfield *key)
2685
+ {
2686
+   char *ptr = line->text, *lim = ptr + line->length - 1;
2687
+   size_t sword = key->sword;
2688
+@@ -1605,10 +1754,10 @@ begfield (struct line const *line, struc
2689
+   /* The leading field separator itself is included in a field when -t
2690
+      is absent.  */
2691
+ 
2692
+-  if (tab != TAB_DEFAULT)
2693
++  if (tab_length)
2694
+     while (ptr < lim && sword--)
2695
+       {
2696
+-        while (ptr < lim && *ptr != tab)
2697
++        while (ptr < lim && *ptr != tab[0])
2698
+           ++ptr;
2699
+         if (ptr < lim)
2700
+           ++ptr;
2701
+@@ -1634,11 +1783,70 @@ begfield (struct line const *line, struc
2702
+   return ptr;
2703
+ }
2704
+ 
2705
++#if HAVE_MBRTOWC
2706
++static char *
2707
++begfield_mb (const struct line *line, const struct keyfield *key)
2708
++{
2709
++  int i;
2710
++  char *ptr = line->text, *lim = ptr + line->length - 1;
2711
++  size_t sword = key->sword;
2712
++  size_t schar = key->schar;
2713
++  size_t mblength;
2714
++  mbstate_t state;
2715
++
2716
++  memset (&state, '\0', sizeof(mbstate_t));
2717
++
2718
++  if (tab_length)
2719
++    while (ptr < lim && sword--)
2720
++      {
2721
++        while (ptr < lim && memcmp (ptr, tab, tab_length) != 0)
2722
++          {
2723
++            GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2724
++            ptr += mblength;
2725
++          }
2726
++        if (ptr < lim)
2727
++          {
2728
++            GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2729
++            ptr += mblength;
2730
++          }
2731
++      }
2732
++  else
2733
++    while (ptr < lim && sword--)
2734
++      {
2735
++        while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
2736
++          ptr += mblength;
2737
++        if (ptr < lim)
2738
++          {
2739
++            GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2740
++            ptr += mblength;
2741
++          }
2742
++        while (ptr < lim && !ismbblank (ptr, lim - ptr, &mblength))
2743
++          ptr += mblength;
2744
++      }
2745
++
2746
++  if (key->skipsblanks)
2747
++    while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
2748
++      ptr += mblength;
2749
++
2750
++  for (i = 0; i < schar; i++)
2751
++    {
2752
++      GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2753
++
2754
++      if (ptr + mblength > lim)
2755
++        break;
2756
++      else
2757
++        ptr += mblength;
2758
++    }
2759
++
2760
++  return ptr;
2761
++}
2762
++#endif
2763
++
2764
+ /* Return the limit of (a pointer to the first character after) the field
2765
+    in LINE specified by KEY. */
2766
+ 
2767
+ static char *
2768
+-limfield (struct line const *line, struct keyfield const *key)
2769
++limfield_uni (const struct line *line, const struct keyfield *key)
2770
+ {
2771
+   char *ptr = line->text, *lim = ptr + line->length - 1;
2772
+   size_t eword = key->eword, echar = key->echar;
2773
+@@ -1653,10 +1861,10 @@ limfield (struct line const *line, struc
2774
+      'beginning' is the first character following the delimiting TAB.
2775
+      Otherwise, leave PTR pointing at the first 'blank' character after
2776
+      the preceding field.  */
2777
+-  if (tab != TAB_DEFAULT)
2778
++  if (tab_length)
2779
+     while (ptr < lim && eword--)
2780
+       {
2781
+-        while (ptr < lim && *ptr != tab)
2782
++        while (ptr < lim && *ptr != tab[0])
2783
+           ++ptr;
2784
+         if (ptr < lim && (eword || echar))
2785
+           ++ptr;
2786
+@@ -1702,10 +1910,10 @@ limfield (struct line const *line, struc
2787
+      */
2788
+ 
2789
+   /* Make LIM point to the end of (one byte past) the current field.  */
2790
+-  if (tab != TAB_DEFAULT)
2791
++  if (tab_length)
2792
+     {
2793
+       char *newlim;
2794
+-      newlim = memchr (ptr, tab, lim - ptr);
2795
++      newlim = memchr (ptr, tab[0], lim - ptr);
2796
+       if (newlim)
2797
+         lim = newlim;
2798
+     }
2799
+@@ -1736,6 +1944,130 @@ limfield (struct line const *line, struc
2800
+   return ptr;
2801
+ }
2802
+ 
2803
++#if HAVE_MBRTOWC
2804
++static char *
2805
++limfield_mb (const struct line *line, const struct keyfield *key)
2806
++{
2807
++  char *ptr = line->text, *lim = ptr + line->length - 1;
2808
++  size_t eword = key->eword, echar = key->echar;
2809
++  int i;
2810
++  size_t mblength;
2811
++  mbstate_t state;
2812
++
2813
++  if (echar == 0)
2814
++    eword++; /* skip all of end field. */
2815
++
2816
++  memset (&state, '\0', sizeof(mbstate_t));
2817
++
2818
++  if (tab_length)
2819
++    while (ptr < lim && eword--)
2820
++      {
2821
++        while (ptr < lim && memcmp (ptr, tab, tab_length) != 0)
2822
++          {
2823
++            GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2824
++            ptr += mblength;
2825
++          }
2826
++        if (ptr < lim && (eword | echar))
2827
++          {
2828
++            GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2829
++            ptr += mblength;
2830
++          }
2831
++      }
2832
++  else
2833
++    while (ptr < lim && eword--)
2834
++      {
2835
++        while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
2836
++          ptr += mblength;
2837
++        if (ptr < lim)
2838
++          {
2839
++            GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2840
++            ptr += mblength;
2841
++          }
2842
++        while (ptr < lim && !ismbblank (ptr, lim - ptr, &mblength))
2843
++          ptr += mblength;
2844
++      }
2845
++
2846
++
2847
++# ifdef POSIX_UNSPECIFIED
2848
++  /* Make LIM point to the end of (one byte past) the current field.  */
2849
++  if (tab_length)
2850
++    {
2851
++      char *newlim, *p;
2852
++
2853
++      newlim = NULL;
2854
++      for (p = ptr; p < lim;)
2855
++         {
2856
++          if (memcmp (p, tab, tab_length) == 0)
2857
++            {
2858
++              newlim = p;
2859
++              break;
2860
++            }
2861
++
2862
++          GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2863
++          p += mblength;
2864
++        }
2865
++    }
2866
++  else
2867
++    {
2868
++      char *newlim;
2869
++      newlim = ptr;
2870
++
2871
++      while (newlim < lim && ismbblank (newlim, lim - newlim, &mblength))
2872
++        newlim += mblength;
2873
++      if (ptr < lim)
2874
++        {
2875
++          GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2876
++          ptr += mblength;
2877
++        }
2878
++      while (newlim < lim && !ismbblank (newlim, lim - newlim, &mblength))
2879
++        newlim += mblength;
2880
++      lim = newlim;
2881
++    }
2882
++# endif
2883
++
2884
++  if (echar != 0)
2885
++  {
2886
++    /* If we're skipping leading blanks, don't start counting characters
2887
++     *      until after skipping past any leading blanks.  */
2888
++    if (key->skipeblanks)
2889
++      while (ptr < lim && ismbblank (ptr, lim - ptr, &mblength))
2890
++        ptr += mblength;
2891
++
2892
++    memset (&state, '\0', sizeof(mbstate_t));
2893
++
2894
++    /* Advance PTR by ECHAR (if possible), but no further than LIM.  */
2895
++    for (i = 0; i < echar; i++)
2896
++     {
2897
++        GET_BYTELEN_OF_CHAR (lim, ptr, mblength, state);
2898
++
2899
++        if (ptr + mblength > lim)
2900
++          break;
2901
++        else
2902
++          ptr += mblength;
2903
++      }
2904
++  }
2905
++
2906
++  return ptr;
2907
++}
2908
++#endif
2909
++
2910
++static void
2911
++skipblanks_uni (char **ptr, char *lim)
2912
++{
2913
++  while (*ptr < lim && blanks[to_uchar (**ptr)])
2914
++    ++(*ptr);
2915
++}
2916
++
2917
++#if HAVE_MBRTOWC
2918
++static void
2919
++skipblanks_mb (char **ptr, char *lim)
2920
++{
2921
++  size_t mblength;
2922
++  while (*ptr < lim && ismbblank (*ptr, lim - *ptr, &mblength))
2923
++    (*ptr) += mblength;
2924
++}
2925
++#endif
2926
++
2927
+ /* Fill BUF reading from FP, moving buf->left bytes from the end
2928
+    of buf->buf to the beginning first.  If EOF is reached and the
2929
+    file wasn't terminated by a newline, supply one.  Set up BUF's line
2930
+@@ -1822,8 +2154,22 @@ fillbuf (struct buffer *buf, FILE *fp, c
2931
+                   else
2932
+                     {
2933
+                       if (key->skipsblanks)
2934
+-                        while (blanks[to_uchar (*line_start)])
2935
+-                          line_start++;
2936
++                        {
2937
++#if HAVE_MBRTOWC
2938
++                          if (MB_CUR_MAX > 1)
2939
++                            {
2940
++                              size_t mblength;
2941
++                              while (line_start < line->keylim &&
2942
++                                     ismbblank (line_start,
2943
++                                                line->keylim - line_start,
2944
++                                                &mblength))
2945
++                                line_start += mblength;
2946
++                            }
2947
++                          else
2948
++#endif
2949
++                          while (blanks[to_uchar (*line_start)])
2950
++                            line_start++;
2951
++                        }
2952
+                       line->keybeg = line_start;
2953
+                     }
2954
+                 }
2955
+@@ -1944,7 +2290,7 @@ human_numcompare (char const *a, char co
2956
+    hideously fast. */
2957
+ 
2958
+ static int
2959
+-numcompare (char const *a, char const *b)
2960
++numcompare_uni (const char *a, const char *b)
2961
+ {
2962
+   while (blanks[to_uchar (*a)])
2963
+     a++;
2964
+@@ -1954,6 +2300,25 @@ numcompare (char const *a, char const *b
2965
+   return strnumcmp (a, b, decimal_point, thousands_sep);
2966
+ }
2967
+ 
2968
++#if HAVE_MBRTOWC
2969
++static int
2970
++numcompare_mb (const char *a, const char *b)
2971
++{
2972
++  size_t mblength, len;
2973
++  len = strlen (a); /* okay for UTF-8 */
2974
++  while (*a && ismbblank (a, len > MB_CUR_MAX ? MB_CUR_MAX : len, &mblength))
2975
++    {
2976
++      a += mblength;
2977
++      len -= mblength;
2978
++    }
2979
++  len = strlen (b); /* okay for UTF-8 */
2980
++  while (*b && ismbblank (b, len > MB_CUR_MAX ? MB_CUR_MAX : len, &mblength))
2981
++    b += mblength;
2982
++
2983
++  return strnumcmp (a, b, decimal_point, thousands_sep);
2984
++}
2985
++#endif /* HAV_EMBRTOWC */
2986
++
2987
+ /* Work around a problem whereby the long double value returned by glibc's
2988
+    strtold ("NaN", ...) contains uninitialized bits: clear all bytes of
2989
+    A and B before calling strtold.  FIXME: remove this function once
2990
+@@ -2004,7 +2369,7 @@ general_numcompare (char const *sa, char
2991
+    Return 0 if the name in S is not recognized.  */
2992
+ 
2993
+ static int
2994
+-getmonth (char const *month, char **ea)
2995
++getmonth_uni (char const *month, size_t len, char **ea)
2996
+ {
2997
+   size_t lo = 0;
2998
+   size_t hi = MONTHS_PER_YEAR;
2999
+@@ -2280,15 +2645,14 @@ debug_key (struct line const *line, stru
3000
+           char saved = *lim;
3001
+           *lim = '\0';
3002
+ 
3003
+-          while (blanks[to_uchar (*beg)])
3004
+-            beg++;
3005
++          skipblanks (&beg, lim);
3006
+ 
3007
+           char *tighter_lim = beg;
3008
+ 
3009
+           if (lim < beg)
3010
+             tighter_lim = lim;
3011
+           else if (key->month)
3012
+-            getmonth (beg, &tighter_lim);
3013
++            getmonth (beg, lim-beg, &tighter_lim);
3014
+           else if (key->general_numeric)
3015
+             ignore_value (strtold (beg, &tighter_lim));
3016
+           else if (key->numeric || key->human_numeric)
3017
+@@ -2432,7 +2796,7 @@ key_warnings (struct keyfield const *gke
3018
+       bool maybe_space_aligned = !hard_LC_COLLATE && default_key_compare (key)
3019
+                                  && !(key->schar || key->echar);
3020
+       bool line_offset = key->eword == 0 && key->echar != 0; /* -k1.x,1.y  */
3021
+-      if (!gkey_only && tab == TAB_DEFAULT && !line_offset
3022
++      if (!gkey_only && !tab_length && !line_offset
3023
+           && ((!key->skipsblanks && !(implicit_skip || maybe_space_aligned))
3024
+               || (!key->skipsblanks && key->schar)
3025
+               || (!key->skipeblanks && key->echar)))
3026
+@@ -2490,11 +2854,87 @@ key_warnings (struct keyfield const *gke
3027
+     error (0, 0, _("option '-r' only applies to last-resort comparison"));
3028
+ }
3029
+ 
3030
++#if HAVE_MBRTOWC
3031
++static int
3032
++getmonth_mb (const char *s, size_t len, char **ea)
3033
++{
3034
++  char *month;
3035
++  register size_t i;
3036
++  register int lo = 0, hi = MONTHS_PER_YEAR, result;
3037
++  char *tmp;
3038
++  size_t wclength, mblength;
3039
++  const char *pp;
3040
++  const wchar_t *wpp;
3041
++  wchar_t *month_wcs;
3042
++  mbstate_t state;
3043
++
3044
++  while (len > 0 && ismbblank (s, len, &mblength))
3045
++    {
3046
++      s += mblength;
3047
++      len -= mblength;
3048
++    }
3049
++
3050
++  if (len == 0)
3051
++    return 0;
3052
++
3053
++  if (SIZE_MAX - len < 1)
3054
++    xalloc_die ();
3055
++
3056
++  month = (char *) xnmalloc (len + 1, MB_CUR_MAX);
3057
++
3058
++  pp = tmp = (char *) xnmalloc (len + 1, MB_CUR_MAX);
3059
++  memcpy (tmp, s, len);
3060
++  tmp[len] = '\0';
3061
++  wpp = month_wcs = (wchar_t *) xnmalloc (len + 1, sizeof (wchar_t));
3062
++  memset (&state, '\0', sizeof (mbstate_t));
3063
++
3064
++  wclength = mbsrtowcs (month_wcs, &pp, len + 1, &state);
3065
++  if (wclength == (size_t)-1 || pp != NULL)
3066
++    error (SORT_FAILURE, 0, _("Invalid multibyte input %s."), quote(s));
3067
++
3068
++  for (i = 0; i < wclength; i++)
3069
++    {
3070
++      month_wcs[i] = towupper(month_wcs[i]);
3071
++      if (iswblank (month_wcs[i]))
3072
++        {
3073
++          month_wcs[i] = L'\0';
3074
++          break;
3075
++        }
3076
++    }
3077
++
3078
++  mblength = wcsrtombs (month, &wpp, (len + 1) * MB_CUR_MAX, &state);
3079
++  assert (mblength != (-1) && wpp == NULL);
3080
++
3081
++  do
3082
++    {
3083
++      int ix = (lo + hi) / 2;
3084
++
3085
++      if (strncmp (month, monthtab[ix].name, strlen (monthtab[ix].name)) < 0)
3086
++        hi = ix;
3087
++      else
3088
++        lo = ix;
3089
++    }
3090
++  while (hi - lo > 1);
3091
++
3092
++  result = (!strncmp (month, monthtab[lo].name, strlen (monthtab[lo].name))
3093
++      ? monthtab[lo].val : 0);
3094
++
3095
++  if (ea && result)
3096
++     *ea = (char*) s + strlen (monthtab[lo].name);
3097
++
3098
++  free (month);
3099
++  free (tmp);
3100
++  free (month_wcs);
3101
++
3102
++  return result;
3103
++}
3104
++#endif
3105
++
3106
+ /* Compare two lines A and B trying every key in sequence until there
3107
+    are no more keys or a difference is found. */
3108
+ 
3109
+ static int
3110
+-keycompare (struct line const *a, struct line const *b)
3111
++keycompare_uni (const struct line *a, const struct line *b)
3112
+ {
3113
+   struct keyfield *key = keylist;
3114
+ 
3115
+@@ -2579,7 +3019,7 @@ keycompare (struct line const *a, struct
3116
+           else if (key->human_numeric)
3117
+             diff = human_numcompare (ta, tb);
3118
+           else if (key->month)
3119
+-            diff = getmonth (ta, NULL) - getmonth (tb, NULL);
3120
++            diff = getmonth (ta, tlena, NULL) - getmonth (tb, tlenb, NULL);
3121
+           else if (key->random)
3122
+             diff = compare_random (ta, tlena, tb, tlenb);
3123
+           else if (key->version)
3124
+@@ -2695,6 +3135,211 @@ keycompare (struct line const *a, struct
3125
+   return key->reverse ? -diff : diff;
3126
+ }
3127
+ 
3128
++#if HAVE_MBRTOWC
3129
++static int
3130
++keycompare_mb (const struct line *a, const struct line *b)
3131
++{
3132
++  struct keyfield *key = keylist;
3133
++
3134
++  /* For the first iteration only, the key positions have been
3135
++     precomputed for us. */
3136
++  char *texta = a->keybeg;
3137
++  char *textb = b->keybeg;
3138
++  char *lima = a->keylim;
3139
++  char *limb = b->keylim;
3140
++
3141
++  size_t mblength_a, mblength_b;
3142
++  wchar_t wc_a, wc_b;
3143
++  mbstate_t state_a, state_b;
3144
++
3145
++  int diff = 0;
3146
++
3147
++  memset (&state_a, '\0', sizeof(mbstate_t));
3148
++  memset (&state_b, '\0', sizeof(mbstate_t));
3149
++  /* Ignore keys with start after end.  */
3150
++  if (a->keybeg - a->keylim > 0)
3151
++    return 0;
3152
++
3153
++
3154
++              /* Ignore and/or translate chars before comparing.  */
3155
++# define IGNORE_CHARS(NEW_LEN, LEN, TEXT, COPY, WC, MBLENGTH, STATE)        \
3156
++  do                                                                        \
3157
++    {                                                                        \
3158
++      wchar_t uwc;                                                        \
3159
++      char mbc[MB_LEN_MAX];                                                \
3160
++      mbstate_t state_wc;                                                \
3161
++                                                                        \
3162
++      for (NEW_LEN = i = 0; i < LEN;)                                        \
3163
++        {                                                                \
3164
++          mbstate_t state_bak;                                                \
3165
++                                                                        \
3166
++          state_bak = STATE;                                                \
3167
++          MBLENGTH = mbrtowc (&WC, TEXT + i, LEN - i, &STATE);                \
3168
++                                                                        \
3169
++          if (MBLENGTH == (size_t)-2 || MBLENGTH == (size_t)-1                \
3170
++              || MBLENGTH == 0)                                                \
3171
++            {                                                                \
3172
++              if (MBLENGTH == (size_t)-2 || MBLENGTH == (size_t)-1)        \
3173
++                STATE = state_bak;                                        \
3174
++              if (!ignore)                                                \
3175
++                COPY[NEW_LEN++] = TEXT[i];                                \
3176
++              i++;                                                         \
3177
++              continue;                                                        \
3178
++            }                                                                \
3179
++                                                                        \
3180
++          if (ignore)                                                        \
3181
++            {                                                                \
3182
++              if ((ignore == nonprinting && !iswprint (WC))                \
3183
++                   || (ignore == nondictionary                                \
3184
++                       && !iswalnum (WC) && !iswblank (WC)))                \
3185
++                {                                                        \
3186
++                  i += MBLENGTH;                                        \
3187
++                  continue;                                                \
3188
++                }                                                        \
3189
++            }                                                                \
3190
++                                                                        \
3191
++          if (translate)                                                \
3192
++            {                                                                \
3193
++                                                                        \
3194
++              uwc = towupper(WC);                                        \
3195
++              if (WC == uwc)                                                \
3196
++                {                                                        \
3197
++                  memcpy (mbc, TEXT + i, MBLENGTH);                        \
3198
++                  i += MBLENGTH;                                        \
3199
++                }                                                        \
3200
++              else                                                        \
3201
++                {                                                        \
3202
++                  i += MBLENGTH;                                        \
3203
++                  WC = uwc;                                                \
3204
++                  memset (&state_wc, '\0', sizeof (mbstate_t));                \
3205
++                                                                        \
3206
++                  MBLENGTH = wcrtomb (mbc, WC, &state_wc);                \
3207
++                  assert (MBLENGTH != (size_t)-1 && MBLENGTH != 0);        \
3208
++                }                                                        \
3209
++                                                                        \
3210
++              for (j = 0; j < MBLENGTH; j++)                                \
3211
++                COPY[NEW_LEN++] = mbc[j];                                \
3212
++            }                                                                \
3213
++          else                                                                \
3214
++            for (j = 0; j < MBLENGTH; j++)                                \
3215
++              COPY[NEW_LEN++] = TEXT[i++];                                \
3216
++        }                                                                \
3217
++      COPY[NEW_LEN] = '\0';                                                \
3218
++    }                                                                        \
3219
++  while (0)
3220
++
3221
++      /* Actually compare the fields. */
3222
++
3223
++  for (;;)
3224
++    {
3225
++      /* Find the lengths. */
3226
++      size_t lena = lima <= texta ? 0 : lima - texta;
3227
++      size_t lenb = limb <= textb ? 0 : limb - textb;
3228
++
3229
++      char enda IF_LINT (= 0);
3230
++      char endb IF_LINT (= 0);
3231
++
3232
++      char const *translate = key->translate;
3233
++      bool const *ignore = key->ignore;
3234
++
3235
++      if (ignore || translate)
3236
++        {
3237
++          if (SIZE_MAX - lenb - 2 < lena)
3238
++            xalloc_die ();
3239
++          char *copy_a = (char *) xnmalloc (lena + lenb + 2, MB_CUR_MAX);
3240
++          char *copy_b = copy_a + lena * MB_CUR_MAX + 1;
3241
++          size_t new_len_a, new_len_b;
3242
++          size_t i, j;
3243
++
3244
++          IGNORE_CHARS (new_len_a, lena, texta, copy_a,
3245
++                        wc_a, mblength_a, state_a);
3246
++          IGNORE_CHARS (new_len_b, lenb, textb, copy_b,
3247
++                        wc_b, mblength_b, state_b);
3248
++          texta = copy_a; textb = copy_b;
3249
++          lena = new_len_a; lenb = new_len_b;
3250
++        }
3251
++      else
3252
++        {
3253
++          /* Use the keys in-place, temporarily null-terminated.  */
3254
++          enda = texta[lena]; texta[lena] = '\0';
3255
++          endb = textb[lenb]; textb[lenb] = '\0';
3256
++        }
3257
++
3258
++      if (key->random)
3259
++        diff = compare_random (texta, lena, textb, lenb);
3260
++      else if (key->numeric | key->general_numeric | key->human_numeric)
3261
++        {
3262
++          char savea = *lima, saveb = *limb;
3263
++
3264
++          *lima = *limb = '\0';
3265
++          diff = (key->numeric ? numcompare (texta, textb)
3266
++                  : key->general_numeric ? general_numcompare (texta, textb)
3267
++                  : human_numcompare (texta, textb));
3268
++          *lima = savea, *limb = saveb;
3269
++        }
3270
++      else if (key->version)
3271
++        diff = filevercmp (texta, textb);
3272
++      else if (key->month)
3273
++        diff = getmonth (texta, lena, NULL) - getmonth (textb, lenb, NULL);
3274
++      else if (lena == 0)
3275
++        diff = - NONZERO (lenb);
3276
++      else if (lenb == 0)
3277
++        diff = 1;
3278
++      else if (hard_LC_COLLATE && !folding)
3279
++        {
3280
++          diff = xmemcoll0 (texta, lena + 1, textb, lenb + 1);
3281
++        }
3282
++      else
3283
++        {
3284
++          diff = memcmp (texta, textb, MIN (lena, lenb));
3285
++          if (diff == 0)
3286
++            diff = lena < lenb ? -1 : lena != lenb;
3287
++        }
3288
++
3289
++      if (ignore || translate)
3290
++        free (texta);
3291
++      else
3292
++        {
3293
++          texta[lena] = enda;
3294
++          textb[lenb] = endb;
3295
++        }
3296
++
3297
++      if (diff)
3298
++        goto not_equal;
3299
++
3300
++      key = key->next;
3301
++      if (! key)
3302
++        break;
3303
++
3304
++      /* Find the beginning and limit of the next field.  */
3305
++      if (key->eword != -1)
3306
++        lima = limfield (a, key), limb = limfield (b, key);
3307
++      else
3308
++        lima = a->text + a->length - 1, limb = b->text + b->length - 1;
3309
++
3310
++      if (key->sword != -1)
3311
++        texta = begfield (a, key), textb = begfield (b, key);
3312
++      else
3313
++        {
3314
++          texta = a->text, textb = b->text;
3315
++          if (key->skipsblanks)
3316
++            {
3317
++              while (texta < lima && ismbblank (texta, lima - texta, &mblength_a))
3318
++                texta += mblength_a;
3319
++              while (textb < limb && ismbblank (textb, limb - textb, &mblength_b))
3320
++                textb += mblength_b;
3321
++            }
3322
++        }
3323
++    }
3324
++
3325
++not_equal:
3326
++  if (key && key->reverse)
3327
++    return -diff;
3328
++  else
3329
++    return diff;
3330
++}
3331
++#endif
3332
++
3333
+ /* Compare two lines A and B, returning negative, zero, or positive
3334
+    depending on whether A compares less than, equal to, or greater than B. */
3335
+ 
3336
+@@ -2722,7 +3367,7 @@ compare (struct line const *a, struct li
3337
+     diff = - NONZERO (blen);
3338
+   else if (blen == 0)
3339
+     diff = 1;
3340
+-  else if (hard_LC_COLLATE)
3341
++  else if (hard_LC_COLLATE && !folding)
3342
+     {
3343
+       /* Note xmemcoll0 is a performance enhancement as
3344
+          it will not unconditionally write '\0' after the
3345
+@@ -4121,6 +4766,7 @@ set_ordering (char const *s, struct keyf
3346
+           break;
3347
+         case 'f':
3348
+           key->translate = fold_toupper;
3349
++          folding = true;
3350
+           break;
3351
+         case 'g':
3352
+           key->general_numeric = true;
3353
+@@ -4199,7 +4845,7 @@ main (int argc, char **argv)
3354
+   initialize_exit_failure (SORT_FAILURE);
3355
+ 
3356
+   hard_LC_COLLATE = hard_locale (LC_COLLATE);
3357
+-#if HAVE_NL_LANGINFO
3358
++#if HAVE_LANGINFO_CODESET
3359
+   hard_LC_TIME = hard_locale (LC_TIME);
3360
+ #endif
3361
+ 
3362
+@@ -4220,6 +4866,29 @@ main (int argc, char **argv)
3363
+       thousands_sep = -1;
3364
+   }
3365
+ 
3366
++#if HAVE_MBRTOWC
3367
++  if (MB_CUR_MAX > 1)
3368
++    {
3369
++      inittables = inittables_mb;
3370
++      begfield = begfield_mb;
3371
++      limfield = limfield_mb;
3372
++      skipblanks = skipblanks_mb;
3373
++      getmonth = getmonth_mb;
3374
++      keycompare = keycompare_mb;
3375
++      numcompare = numcompare_mb;
3376
++    }
3377
++  else
3378
++#endif
3379
++    {
3380
++      inittables = inittables_uni;
3381
++      begfield = begfield_uni;
3382
++      limfield = limfield_uni;
3383
++      skipblanks = skipblanks_uni;
3384
++      getmonth = getmonth_uni;
3385
++      keycompare = keycompare_uni;
3386
++      numcompare = numcompare_uni;
3387
++    }
3388
++
3389
+   have_read_stdin = false;
3390
+   inittables ();
3391
+ 
3392
+@@ -4494,13 +5163,34 @@ main (int argc, char **argv)
3393
+ 
3394
+         case 't':
3395
+           {
3396
+-            char newtab = optarg[0];
3397
+-            if (! newtab)
3398
++            char newtab[MB_LEN_MAX + 1];
3399
++            size_t newtab_length = 1;
3400
++            strncpy (newtab, optarg, MB_LEN_MAX);
3401
++            if (! newtab[0])
3402
+               error (SORT_FAILURE, 0, _("empty tab"));
3403
+-            if (optarg[1])
3404
++#if HAVE_MBRTOWC
3405
++            if (MB_CUR_MAX > 1)
3406
++              {
3407
++                wchar_t wc;
3408
++                mbstate_t state;
3409
++
3410
++                memset (&state, '\0', sizeof (mbstate_t));
3411
++                newtab_length = mbrtowc (&wc, newtab, strnlen (newtab,
3412
++                                                               MB_LEN_MAX),
3413
++                                         &state);
3414
++                switch (newtab_length)
3415
++                  {
3416
++                  case (size_t) -1:
3417
++                  case (size_t) -2:
3418
++                  case 0:
3419
++                    newtab_length = 1;
3420
++                  }
3421
++              }
3422
++#endif
3423
++            if (newtab_length == 1 && optarg[1])
3424
+               {
3425
+                 if (STREQ (optarg, "\\0"))
3426
+-                  newtab = '\0';
3427
++                  newtab[0] = '\0';
3428
+                 else
3429
+                   {
3430
+                     /* Provoke with 'sort -txx'.  Complain about
3431
+@@ -4511,9 +5201,12 @@ main (int argc, char **argv)
3432
+                            quote (optarg));
3433
+                   }
3434
+               }
3435
+-            if (tab != TAB_DEFAULT && tab != newtab)
3436
++            if (tab_length
3437
++                && (tab_length != newtab_length
3438
++                    || memcmp (tab, newtab, tab_length) != 0))
3439
+               error (SORT_FAILURE, 0, _("incompatible tabs"));
3440
+-            tab = newtab;
3441
++            memcpy (tab, newtab, newtab_length);
3442
++            tab_length = newtab_length;
3443
+           }
3444
+           break;
3445
+ 
3446
+@@ -4751,12 +5444,10 @@ main (int argc, char **argv)
3447
+       sort (files, nfiles, outfile, nthreads);
3448
+     }
3449
+ 
3450
+-#ifdef lint
3451
+   if (files_from)
3452
+     readtokens0_free (&tok);
3453
+   else
3454
+     free (files);
3455
+-#endif
3456
+ 
3457
+   if (have_read_stdin && fclose (stdin) == EOF)
3458
+     die (_("close failed"), "-");
3459
+diff -Naurp coreutils-8.25-orig/src/unexpand.c coreutils-8.25/src/unexpand.c
3460
+--- coreutils-8.25-orig/src/unexpand.c	2016-01-01 07:48:50.000000000 -0600
3461
+@@ -38,12 +38,29 @@
3462
+ #include <stdio.h>
3463
+ #include <getopt.h>
3464
+ #include <sys/types.h>
3465
++
3466
++/* Get mbstate_t, mbrtowc(), wcwidth(). */
3467
++#if HAVE_WCHAR_H
3468
++# include <wchar.h>
3469
++#endif
3470
++
3471
+ #include "system.h"
3472
+ #include "error.h"
3473
+ #include "fadvise.h"
3474
+ #include "quote.h"
3475
+ #include "xstrndup.h"
3476
+ 
3477
++/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
3478
++      installation; work around this configuration error.  */
3479
++#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
3480
++# define MB_LEN_MAX 16
3481
++#endif
3482
++
3483
++/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
3484
++#if HAVE_MBRTOWC && defined mbstate_t
3485
++# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
3486
++#endif
3487
++
3488
+ /* The official name of this program (e.g., no 'g' prefix).  */
3489
+ #define PROGRAM_NAME "unexpand"
3490
+ 
3491
+@@ -103,6 +120,210 @@ static struct option const longopts[] =
3492
+   {NULL, 0, NULL, 0}
3493
+ };
3494
+ 
3495
++static FILE *next_file (FILE *fp);
3496
++
3497
++#if HAVE_MBRTOWC
3498
++static void
3499
++unexpand_multibyte (void)
3500
++{
3501
++  FILE *fp;			/* Input stream. */
3502
++  mbstate_t i_state;		/* Current shift state of the input stream. */
3503
++  mbstate_t i_state_bak;	/* Back up the I_STATE. */
3504
++  mbstate_t o_state;		/* Current shift state of the output stream. */
3505
++  char buf[MB_LEN_MAX + BUFSIZ];  /* For spooling a read byte sequence. */
3506
++  char *bufpos = buf;			/* Next read position of BUF. */
3507
++  size_t buflen = 0;		/* The length of the byte sequence in buf. */
3508
++  wint_t wc;			/* A gotten wide character. */
3509
++  size_t mblength;		/* The byte size of a multibyte character
3510
++				   which shows as same character as WC. */
3511
++  bool prev_tab = false;
3512
++
3513
++  /* Index in `tab_list' of next tabstop: */
3514
++  int tab_index = 0;		/* For calculating width of pending tabs. */
3515
++  int print_tab_index = 0;	/* For printing as many tabs as possible. */
3516
++  unsigned int column = 0;	/* Column on screen of next char. */
3517
++  int next_tab_column;		/* Column the next tab stop is on. */
3518
++  int convert = 1;		/* If nonzero, perform translations. */
3519
++  unsigned int pending = 0;	/* Pending columns of blanks. */
3520
++
3521
++  fp = next_file ((FILE *) NULL);
3522
++  if (fp == NULL)
3523
++    return;
3524
++
3525
++  memset (&o_state, '\0', sizeof(mbstate_t));
3526
++  memset (&i_state, '\0', sizeof(mbstate_t));
3527
++
3528
++  for (;;)
3529
++    {
3530
++      if (buflen < MB_LEN_MAX && !feof(fp) && !ferror(fp))
3531
++	{
3532
++	  memmove (buf, bufpos, buflen);
3533
++	  buflen += fread (buf + buflen, sizeof(char), BUFSIZ, fp);
3534
++	  bufpos = buf;
3535
++	}
3536
++
3537
++      /* Get a wide character. */
3538
++      if (buflen < 1)
3539
++	{
3540
++	  mblength = 1;
3541
++	  wc = WEOF;
3542
++	}
3543
++      else
3544
++	{
3545
++	  i_state_bak = i_state;
3546
++	  mblength = mbrtowc ((wchar_t *)&wc, bufpos, buflen, &i_state);
3547
++	}
3548
++
3549
++      if (mblength == (size_t)-1 || mblength == (size_t)-2)
3550
++	{
3551
++	  i_state = i_state_bak;
3552
++	  wc = L'\0';
3553
++	}
3554
++
3555
++      if (wc == L' ' && convert && column < INT_MAX)
3556
++	{
3557
++	  ++pending;
3558
++	  ++column;
3559
++	}
3560
++      else if (wc == L'\t' && convert)
3561
++	{
3562
++	  if (tab_size == 0)
3563
++	    {
3564
++	      /* Do not let tab_index == first_free_tab;
3565
++		 stop when it is 1 less. */
3566
++	      while (tab_index < first_free_tab - 1
3567
++		  && column >= tab_list[tab_index])
3568
++		tab_index++;
3569
++	      next_tab_column = tab_list[tab_index];
3570
++	      if (tab_index < first_free_tab - 1)
3571
++		tab_index++;
3572
++	      if (column >= next_tab_column)
3573
++		{
3574
++		  convert = 0;	/* Ran out of tab stops. */
3575
++		  goto flush_pend_mb;
3576
++		}
3577
++	    }
3578
++	  else
3579
++	    {
3580
++	      next_tab_column = column + tab_size - column % tab_size;
3581
++	    }
3582
++	  pending += next_tab_column - column;
3583
++	  column = next_tab_column;
3584
++	}
3585
++      else
3586
++	{
3587
++flush_pend_mb:
3588
++	  /* Flush pending spaces.  Print as many tabs as possible,
3589
++	     then print the rest as spaces. */
3590
++	  if (pending == 1 && column != 1 && !prev_tab)
3591
++	    {
3592
++	      putchar (' ');
3593
++	      pending = 0;
3594
++	    }
3595
++	  column -= pending;
3596
++	  while (pending > 0)
3597
++	    {
3598
++	      if (tab_size == 0)
3599
++		{
3600
++		  /* Do not let print_tab_index == first_free_tab;
3601
++		     stop when it is 1 less. */
3602
++		  while (print_tab_index < first_free_tab - 1
3603
++		      && column >= tab_list[print_tab_index])
3604
++		    print_tab_index++;
3605
++		  next_tab_column = tab_list[print_tab_index];
3606
++		  if (print_tab_index < first_free_tab - 1)
3607
++		    print_tab_index++;
3608
++		}
3609
++	      else
3610
++		{
3611
++		  next_tab_column =
3612
++		    column + tab_size - column % tab_size;
3613
++		}
3614
++	      if (next_tab_column - column <= pending)
3615
++		{
3616
++		  putchar ('\t');
3617
++		  pending -= next_tab_column - column;
3618
++		  column = next_tab_column;
3619
++		}
3620
++	      else
3621
++		{
3622
++		  --print_tab_index;
3623
++		  column += pending;
3624
++		  while (pending != 0)
3625
++		    {
3626
++		      putchar (' ');
3627
++		      pending--;
3628
++		    }
3629
++		}
3630
++	    }
3631
++
3632
++	  if (wc == WEOF)
3633
++	    {
3634
++	      fp = next_file (fp);
3635
++	      if (fp == NULL)
3636
++		break;          /* No more files. */
3637
++	      else
3638
++		{
3639
++		  memset (&i_state, '\0', sizeof(mbstate_t));
3640
++		  continue;
3641
++		}
3642
++	    }
3643
++
3644
++	  if (mblength == (size_t)-1 || mblength == (size_t)-2)
3645
++	    {
3646
++	      if (convert)
3647
++		{
3648
++		  ++column;
3649
++		  if (convert_entire_line == 0)
3650
++		    convert = 0;
3651
++		}
3652
++	      mblength = 1;
3653
++	      putchar (buf[0]);
3654
++	    }
3655
++	  else if (mblength == 0)
3656
++	    {
3657
++	      if (convert && convert_entire_line == 0)
3658
++		convert = 0;
3659
++	      mblength = 1;
3660
++	      putchar ('\0');
3661
++	    }
3662
++	  else
3663
++	    {
3664
++	      if (convert)
3665
++		{
3666
++		  if (wc == L'\b')
3667
++		    {
3668
++		      if (column > 0)
3669
++			--column;
3670
++		    }
3671
++		  else
3672
++		    {
3673
++		      int width;            /* The width of WC. */
3674
++
3675
++		      width = wcwidth (wc);
3676
++		      column += (width > 0) ? width : 0;
3677
++		      if (convert_entire_line == 0)
3678
++			convert = 0;
3679
++		    }
3680
++		}
3681
++
3682
++	      if (wc == L'\n')
3683
++		{
3684
++		  tab_index = print_tab_index = 0;
3685
++		  column = pending = 0;
3686
++		  convert = 1;
3687
++		}
3688
++	      fwrite (bufpos, sizeof(char), mblength, stdout);
3689
++	    }
3690
++	}
3691
++      prev_tab = wc == L'\t';
3692
++      buflen -= mblength;
3693
++      bufpos += mblength;
3694
++    }
3695
++}
3696
++#endif
3697
++
3698
++
3699
+ void
3700
+ usage (int status)
3701
+ {
3702
+@@ -523,7 +744,12 @@ main (int argc, char **argv)
3703
+ 
3704
+   file_list = (optind < argc ? &argv[optind] : stdin_argv);
3705
+ 
3706
+-  unexpand ();
3707
++#if HAVE_MBRTOWC
3708
++  if (MB_CUR_MAX > 1)
3709
++    unexpand_multibyte ();
3710
++  else
3711
++#endif
3712
++    unexpand ();
3713
+ 
3714
+   if (have_read_stdin && fclose (stdin) != 0)
3715
+     error (EXIT_FAILURE, errno, "-");
3716
+diff -Naurp coreutils-8.25-orig/src/uniq.c coreutils-8.25/src/uniq.c
3717
+--- coreutils-8.25-orig/src/uniq.c	2016-01-13 05:08:59.000000000 -0600
3718
+@@ -21,6 +21,17 @@
3719
+ #include <getopt.h>
3720
+ #include <sys/types.h>
3721
+ 
3722
++/* Get mbstate_t, mbrtowc(). */
3723
++#if HAVE_WCHAR_H
3724
++# include <wchar.h>
3725
++#endif
3726
++
3727
++/* Get isw* functions. */
3728
++#if HAVE_WCTYPE_H
3729
++# include <wctype.h>
3730
++#endif
3731
++#include <assert.h>
3732
++
3733
+ #include "system.h"
3734
+ #include "argmatch.h"
3735
+ #include "linebuffer.h"
3736
+@@ -33,6 +44,18 @@
3737
+ #include "xstrtol.h"
3738
+ #include "memcasecmp.h"
3739
+ #include "quote.h"
3740
++#include "xmemcoll.h"
3741
++
3742
++/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
3743
++   installation; work around this configuration error.  */
3744
++#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
3745
++# define MB_LEN_MAX 16
3746
++#endif
3747
++
3748
++/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
3749
++#if HAVE_MBRTOWC && defined mbstate_t
3750
++# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
3751
++#endif
3752
+ 
3753
+ /* The official name of this program (e.g., no 'g' prefix).  */
3754
+ #define PROGRAM_NAME "uniq"
3755
+@@ -143,6 +166,10 @@ enum
3756
+   GROUP_OPTION = CHAR_MAX + 1
3757
+ };
3758
+ 
3759
++/* Function pointers. */
3760
++static char *
3761
++(*find_field) (struct linebuffer *line);
3762
++
3763
+ static struct option const longopts[] =
3764
+ {
3765
+   {"count", no_argument, NULL, 'c'},
3766
+@@ -252,7 +279,7 @@ size_opt (char const *opt, char const *m
3767
+    return a pointer to the beginning of the line's field to be compared. */
3768
+ 
3769
+ static char * _GL_ATTRIBUTE_PURE
3770
+-find_field (struct linebuffer const *line)
3771
++find_field_uni (struct linebuffer *line)
3772
+ {
3773
+   size_t count;
3774
+   char const *lp = line->buffer;
3775
+@@ -272,6 +299,83 @@ find_field (struct linebuffer const *lin
3776
+   return line->buffer + i;
3777
+ }
3778
+ 
3779
++#if HAVE_MBRTOWC
3780
++
3781
++# define MBCHAR_TO_WCHAR(WC, MBLENGTH, LP, POS, SIZE, STATEP, CONVFAIL)  \
3782
++  do                                                                        \
3783
++    {                                                                        \
3784
++      mbstate_t state_bak;                                                \
3785
++                                                                        \
3786
++      CONVFAIL = 0;                                                        \
3787
++      state_bak = *STATEP;                                                \
3788
++                                                                        \
3789
++      MBLENGTH = mbrtowc (&WC, LP + POS, SIZE - POS, STATEP);                \
3790
++                                                                        \
3791
++      switch (MBLENGTH)                                                        \
3792
++        {                                                                \
3793
++        case (size_t)-2:                                                \
3794
++        case (size_t)-1:                                                \
3795
++          *STATEP = state_bak;                                                \
3796
++          CONVFAIL++;                                                        \
3797
++          /* Fall through */                                                \
3798
++        case 0:                                                                \
3799
++          MBLENGTH = 1;                                                        \
3800
++        }                                                                \
3801
++    }                                                                        \
3802
++  while (0)
3803
++
3804
++static char *
3805
++find_field_multi (struct linebuffer *line)
3806
++{
3807
++  size_t count;
3808
++  char *lp = line->buffer;
3809
++  size_t size = line->length - 1;
3810
++  size_t pos;
3811
++  size_t mblength;
3812
++  wchar_t wc;
3813
++  mbstate_t *statep;
3814
++  int convfail = 0;
3815
++
3816
++  pos = 0;
3817
++  statep = &(line->state);
3818
++
3819
++  /* skip fields. */
3820
++  for (count = 0; count < skip_fields && pos < size; count++)
3821
++    {
3822
++      while (pos < size)
3823
++        {
3824
++          MBCHAR_TO_WCHAR (wc, mblength, lp, pos, size, statep, convfail);
3825
++
3826
++          if (convfail || !(iswblank (wc) || wc == '\n'))
3827
++            {
3828
++              pos += mblength;
3829
++              break;
3830
++            }
3831
++          pos += mblength;
3832
++        }
3833
++
3834
++      while (pos < size)
3835
++        {
3836
++          MBCHAR_TO_WCHAR (wc, mblength, lp, pos, size, statep, convfail);
3837
++
3838
++          if (!convfail && (iswblank (wc) || wc == '\n'))
3839
++            break;
3840
++
3841
++          pos += mblength;
3842
++        }
3843
++    }
3844
++
3845
++  /* skip fields. */
3846
++  for (count = 0; count < skip_chars && pos < size; count++)
3847
++    {
3848
++      MBCHAR_TO_WCHAR (wc, mblength, lp, pos, size, statep, convfail);
3849
++      pos += mblength;
3850
++    }
3851
++
3852
++  return lp + pos;
3853
++}
3854
++#endif
3855
++
3856
+ /* Return false if two strings OLD and NEW match, true if not.
3857
+    OLD and NEW point not to the beginnings of the lines
3858
+    but rather to the beginnings of the fields to compare.
3859
+@@ -280,6 +384,8 @@ find_field (struct linebuffer const *lin
3860
+ static bool
3861
+ different (char *old, char *new, size_t oldlen, size_t newlen)
3862
+ {
3863
++  char *copy_old, *copy_new;
3864
++
3865
+   if (check_chars < oldlen)
3866
+     oldlen = check_chars;
3867
+   if (check_chars < newlen)
3868
+@@ -287,15 +393,104 @@ different (char *old, char *new, size_t
3869
+ 
3870
+   if (ignore_case)
3871
+     {
3872
+-      /* FIXME: This should invoke strcoll somehow.  */
3873
+-      return oldlen != newlen || memcasecmp (old, new, oldlen);
3874
++      size_t i;
3875
++
3876
++      copy_old = xmalloc (oldlen + 1);
3877
++      copy_new = xmalloc (oldlen + 1);
3878
++
3879
++      for (i = 0; i < oldlen; i++)
3880
++        {
3881
++          copy_old[i] = toupper (old[i]);
3882
++          copy_new[i] = toupper (new[i]);
3883
++        }
3884
++      bool rc = xmemcoll (copy_old, oldlen, copy_new, newlen);
3885
++      free (copy_old);
3886
++      free (copy_new);
3887
++      return rc;
3888
+     }
3889
+-  else if (hard_LC_COLLATE)
3890
+-    return xmemcoll (old, oldlen, new, newlen) != 0;
3891
+   else
3892
+-    return oldlen != newlen || memcmp (old, new, oldlen);
3893
++    {
3894
++      copy_old = (char *)old;
3895
++      copy_new = (char *)new;
3896
++    }
3897
++
3898
++  return xmemcoll (copy_old, oldlen, copy_new, newlen);
3899
++
3900
+ }
3901
+ 
3902
++#if HAVE_MBRTOWC
3903
++static int
3904
++different_multi (const char *old, const char *new, size_t oldlen, size_t newlen, mbstate_t oldstate, mbstate_t newstate)
3905
++{
3906
++  size_t i, j, chars;
3907
++  const char *str[2];
3908
++  char *copy[2];
3909
++  size_t len[2];
3910
++  mbstate_t state[2];
3911
++  size_t mblength;
3912
++  wchar_t wc, uwc;
3913
++  mbstate_t state_bak;
3914
++
3915
++  str[0] = old;
3916
++  str[1] = new;
3917
++  len[0] = oldlen;
3918
++  len[1] = newlen;
3919
++  state[0] = oldstate;
3920
++  state[1] = newstate;
3921
++
3922
++  for (i = 0; i < 2; i++)
3923
++    {
3924
++      copy[i] = xmalloc (len[i] + 1);
3925
++      memset (copy[i], '\0', len[i] + 1);
3926
++
3927
++      for (j = 0, chars = 0; j < len[i] && chars < check_chars; chars++)
3928
++        {
3929
++          state_bak = state[i];
3930
++          mblength = mbrtowc (&wc, str[i] + j, len[i] - j, &(state[i]));
3931
++
3932
++          switch (mblength)
3933
++            {
3934
++            case (size_t)-1:
3935
++            case (size_t)-2:
3936
++              state[i] = state_bak;
3937
++              /* Fall through */
3938
++            case 0:
3939
++              mblength = 1;
3940
++              break;
3941
++
3942
++            default:
3943
++              if (ignore_case)
3944
++                {
3945
++                  uwc = towupper (wc);
3946
++
3947
++                  if (uwc != wc)
3948
++                    {
3949
++                      mbstate_t state_wc;
3950
++                      size_t mblen;
3951
++
3952
++                      memset (&state_wc, '\0', sizeof(mbstate_t));
3953
++                      mblen = wcrtomb (copy[i] + j, uwc, &state_wc);
3954
++                      assert (mblen != (size_t)-1);
3955
++                    }
3956
++                  else
3957
++                    memcpy (copy[i] + j, str[i] + j, mblength);
3958
++                }
3959
++              else
3960
++                memcpy (copy[i] + j, str[i] + j, mblength);
3961
++            }
3962
++          j += mblength;
3963
++        }
3964
++      copy[i][j] = '\0';
3965
++      len[i] = j;
3966
++    }
3967
++  int rc = xmemcoll (copy[0], len[0], copy[1], len[1]);
3968
++  free (copy[0]);
3969
++  free (copy[1]);
3970
++  return rc;
3971
++
3972
++}
3973
++#endif
3974
++
3975
+ /* Output the line in linebuffer LINE to standard output
3976
+    provided that the switches say it should be output.
3977
+    MATCH is true if the line matches the previous line.
3978
+@@ -359,19 +554,38 @@ check_file (const char *infile, const ch
3979
+       char *prevfield IF_LINT ( = NULL);
3980
+       size_t prevlen IF_LINT ( = 0);
3981
+       bool first_group_printed = false;
3982
++#if HAVE_MBRTOWC
3983
++      mbstate_t prevstate;
3984
++
3985
++      memset (&prevstate, '\0', sizeof (mbstate_t));
3986
++#endif
3987
+ 
3988
+       while (!feof (stdin))
3989
+         {
3990
+           char *thisfield;
3991
+           size_t thislen;
3992
+           bool new_group;
3993
++#if HAVE_MBRTOWC
3994
++          mbstate_t thisstate;
3995
++#endif
3996
+ 
3997
+           if (readlinebuffer_delim (thisline, stdin, delimiter) == 0)
3998
+             break;
3999
+ 
4000
+           thisfield = find_field (thisline);
4001
+           thislen = thisline->length - 1 - (thisfield - thisline->buffer);
4002
++#if HAVE_MBRTOWC
4003
++          if (MB_CUR_MAX > 1)
4004
++            {
4005
++              thisstate = thisline->state;
4006
+ 
4007
++              new_group = (prevline->length == 0
4008
++                           || different_multi (thisfield, prevfield,
4009
++                                               thislen, prevlen,
4010
++                                               thisstate, prevstate));
4011
++            }
4012
++          else
4013
++#endif
4014
+           new_group = (prevline->length == 0
4015
+                        || different (thisfield, prevfield, thislen, prevlen));
4016
+ 
4017
+@@ -389,6 +603,10 @@ check_file (const char *infile, const ch
4018
+               SWAP_LINES (prevline, thisline);
4019
+               prevfield = thisfield;
4020
+               prevlen = thislen;
4021
++#if HAVE_MBRTOWC
4022
++              if (MB_CUR_MAX > 1)
4023
++                prevstate = thisstate;
4024
++#endif
4025
+               first_group_printed = true;
4026
+             }
4027
+         }
4028
+@@ -401,17 +619,26 @@ check_file (const char *infile, const ch
4029
+       size_t prevlen;
4030
+       uintmax_t match_count = 0;
4031
+       bool first_delimiter = true;
4032
++#if HAVE_MBRTOWC
4033
++      mbstate_t prevstate;
4034
++#endif
4035
+ 
4036
+       if (readlinebuffer_delim (prevline, stdin, delimiter) == 0)
4037
+         goto closefiles;
4038
+       prevfield = find_field (prevline);
4039
+       prevlen = prevline->length - 1 - (prevfield - prevline->buffer);
4040
++#if HAVE_MBRTOWC
4041
++      prevstate = prevline->state;
4042
++#endif
4043
+ 
4044
+       while (!feof (stdin))
4045
+         {
4046
+           bool match;
4047
+           char *thisfield;
4048
+           size_t thislen;
4049
++#if HAVE_MBRTOWC
4050
++          mbstate_t thisstate = thisline->state;
4051
++#endif
4052
+           if (readlinebuffer_delim (thisline, stdin, delimiter) == 0)
4053
+             {
4054
+               if (ferror (stdin))
4055
+@@ -420,6 +647,14 @@ check_file (const char *infile, const ch
4056
+             }
4057
+           thisfield = find_field (thisline);
4058
+           thislen = thisline->length - 1 - (thisfield - thisline->buffer);
4059
++#if HAVE_MBRTOWC
4060
++          if (MB_CUR_MAX > 1)
4061
++            {
4062
++              match = !different_multi (thisfield, prevfield,
4063
++                                thislen, prevlen, thisstate, prevstate);
4064
++            }
4065
++          else
4066
++#endif
4067
+           match = !different (thisfield, prevfield, thislen, prevlen);
4068
+           match_count += match;
4069
+ 
4070
+@@ -452,6 +687,9 @@ check_file (const char *infile, const ch
4071
+               SWAP_LINES (prevline, thisline);
4072
+               prevfield = thisfield;
4073
+               prevlen = thislen;
4074
++#if HAVE_MBRTOWC
4075
++              prevstate = thisstate;
4076
++#endif
4077
+               if (!match)
4078
+                 match_count = 0;
4079
+             }
4080
+@@ -498,6 +736,19 @@ main (int argc, char **argv)
4081
+ 
4082
+   atexit (close_stdout);
4083
+ 
4084
++#if HAVE_MBRTOWC
4085
++  if (MB_CUR_MAX > 1)
4086
++    {
4087
++      find_field = find_field_multi;
4088
++    }
4089
++  else
4090
++#endif
4091
++    {
4092
++      find_field = find_field_uni;
4093
++    }
4094
++
4095
++
4096
++
4097
+   skip_chars = 0;
4098
+   skip_fields = 0;
4099
+   check_chars = SIZE_MAX;
4100
+diff -Naurp coreutils-8.25-orig/tests/i18n/sort-month.sh coreutils-8.25/tests/i18n/sort-month.sh
4101
+--- coreutils-8.25-orig/tests/i18n/sort-month.sh	1969-12-31 18:00:00.000000000 -0600
4102
+@@ -0,0 +1,34 @@
4103
++#!/bin/sh
4104
++# Verify sort -M multi-byte support.
4105
++
4106
++. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
4107
++print_ver_ sort
4108
++require_valgrind_
4109
++
4110
++# Skip this test if some deallocations are
4111
++# avoided at process end.
4112
++grep '^#define lint 1' $CONFIG_HEADER > /dev/null ||
4113
++  skip_ 'Allocation checks only work reliably in "lint" mode'
4114
++
4115
++export LC_ALL=en_US.UTF-8
4116
++locale -k LC_CTYPE | grep -q "charmap.*UTF-8" \
4117
++  || skip_ "No UTF-8 locale available"
4118
++
4119
++# Note the use of É‘ here which expands to
4120
++# a wider representation upon case conversion
4121
++# which triggered an assertion in sort -M
4122
++cat <<EOF > exp
4123
++.
4124
++É‘
4125
++EOF
4126
++
4127
++
4128
++# check large mem leak with --month-sort
4129
++# https://bugzilla.redhat.com/show_bug.cgi?id=1259942
4130
++valgrind --leak-check=full \
4131
++         --error-exitcode=1 --errors-for-leak-kinds=definite \
4132
++         sort -M < exp > out || fail=1
4133
++compare exp out || { fail=1; cat out; }
4134
++
4135
++
4136
++Exit $fail
4137
+diff -Naurp coreutils-8.25-orig/tests/i18n/sort.sh coreutils-8.25/tests/i18n/sort.sh
4138
+--- coreutils-8.25-orig/tests/i18n/sort.sh	1969-12-31 18:00:00.000000000 -0600
4139
+@@ -0,0 +1,29 @@
4140
++#!/bin/sh
4141
++# Verify sort's multi-byte support.
4142
++
4143
++. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
4144
++print_ver_ sort
4145
++
4146
++export LC_ALL=en_US.UTF-8
4147
++locale -k LC_CTYPE | grep -q "charmap.*UTF-8" \
4148
++  || skip_ "No UTF-8 locale available"
4149
++
4150
++# Enable heap consistency checkng on older systems
4151
++export MALLOC_CHECK_=2
4152
++
4153
++
4154
++# check buffer overflow issue due to
4155
++# expanding multi-byte representation due to case conversion
4156
++# https://bugzilla.suse.com/show_bug.cgi?id=928749
4157
++cat <<EOF > exp
4158
++.
4159
++É‘
4160
++EOF
4161
++cat <<EOF | sort -f > out || fail=1
4162
++.
4163
++É‘
4164
++EOF
4165
++compare exp out || { fail=1; cat out; }
4166
++
4167
++
4168
++Exit $fail
4169
+diff -Naurp coreutils-8.25-orig/tests/local.mk coreutils-8.25/tests/local.mk
4170
+--- coreutils-8.25-orig/tests/local.mk	2016-01-16 12:18:13.000000000 -0600
4171
+@@ -344,6 +344,9 @@ all_tests =					\
4172
+   tests/misc/sort-discrim.sh			\
4173
+   tests/misc/sort-files0-from.pl		\
4174
+   tests/misc/sort-float.sh			\
4175
++  tests/misc/sort-mb-tests.sh			\
4176
++  tests/i18n/sort.sh				\
4177
++  tests/i18n/sort-month.sh			\
4178
+   tests/misc/sort-merge.pl			\
4179
+   tests/misc/sort-merge-fdlimit.sh		\
4180
+   tests/misc/sort-month.sh			\
4181
+diff -Naurp coreutils-8.25-orig/tests/misc/cut.pl coreutils-8.25/tests/misc/cut.pl
4182
+--- coreutils-8.25-orig/tests/misc/cut.pl	2016-01-16 12:18:13.000000000 -0600
4183
+@@ -23,9 +23,11 @@ use strict;
4184
+ # Turn off localization of executable's output.
4185
+ @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
4186
+ 
4187
+-my $mb_locale = $ENV{LOCALE_FR_UTF8};
4188
++my $mb_locale;
4189
++# uncommented enable multibyte paths
4190
++$mb_locale = $ENV{LOCALE_FR_UTF8};
4191
+ ! defined $mb_locale || $mb_locale eq 'none'
4192
+-  and $mb_locale = 'C';
4193
++ and $mb_locale = 'C';
4194
+ 
4195
+ my $prog = 'cut';
4196
+ my $try = "Try '$prog --help' for more information.\n";
4197
+@@ -240,6 +242,7 @@ if ($mb_locale ne 'C')
4198
+         my @new_t = @$t;
4199
+         my $test_name = shift @new_t;
4200
+ 
4201
++        next if ($test_name =~ "newline-[12][0-9]");
4202
+         push @new, ["$test_name-mb", @new_t, {ENV => "LC_ALL=$mb_locale"}];
4203
+       }
4204
+     push @Tests, @new;
4205
+diff -Naurp coreutils-8.25-orig/tests/misc/expand.pl coreutils-8.25/tests/misc/expand.pl
4206
+--- coreutils-8.25-orig/tests/misc/expand.pl	2016-01-16 12:18:13.000000000 -0600
4207
+@@ -23,6 +23,15 @@ use strict;
4208
+ # Turn off localization of executable's output.
4209
+ @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
4210
+ 
4211
++#comment out next line to disable multibyte tests
4212
++my $mb_locale = $ENV{LOCALE_FR_UTF8};
4213
++! defined $mb_locale || $mb_locale eq 'none'
4214
++ and $mb_locale = 'C';
4215
++
4216
++my $prog = 'expand';
4217
++my $try = "Try \`$prog --help' for more information.\n";
4218
++my $inval = "$prog: invalid byte, character or field list\n$try";
4219
++
4220
+ my @Tests =
4221
+   (
4222
+    ['t1', '--tabs=3',     {IN=>"a\tb"}, {OUT=>"a  b"}],
4223
+@@ -31,6 +40,37 @@ my @Tests =
4224
+    ['i2', '--tabs=3 -i', {IN=>" \ta\tb"}, {OUT=>"   a\tb"}],
4225
+   );
4226
+ 
4227
++if ($mb_locale ne 'C')
4228
++  {
4229
++    # Duplicate each test vector, appending "-mb" to the test name and
4230
++    # inserting {ENV => "LC_ALL=$mb_locale"} in the copy, so that we
4231
++    # provide coverage for the distro-added multi-byte code paths.
4232
++    my @new;
4233
++    foreach my $t (@Tests)
4234
++      {
4235
++        my @new_t = @$t;
4236
++        my $test_name = shift @new_t;
4237
++
4238
++        # Depending on whether expand is multi-byte-patched,
4239
++        # it emits different diagnostics:
4240
++        #   non-MB: invalid byte or field list
4241
++        #   MB:     invalid byte, character or field list
4242
++        # Adjust the expected error output accordingly.
4243
++        if (grep {ref $_ eq 'HASH' && exists $_->{ERR} && $_->{ERR} eq $inval}
4244
++            (@new_t))
4245
++          {
4246
++            my $sub = {ERR_SUBST => 's/, character//'};
4247
++            push @new_t, $sub;
4248
++            push @$t, $sub;
4249
++          }
4250
++        push @new, ["$test_name-mb", @new_t, {ENV => "LC_ALL=$mb_locale"}];
4251
++      }
4252
++    push @Tests, @new;
4253
++  }
4254
++
4255
++
4256
++@Tests = triple_test \@Tests;
4257
++
4258
+ my $save_temps = $ENV{DEBUG};
4259
+ my $verbose = $ENV{VERBOSE};
4260
+ 
4261
+diff -Naurp coreutils-8.25-orig/tests/misc/fold.pl coreutils-8.25/tests/misc/fold.pl
4262
+--- coreutils-8.25-orig/tests/misc/fold.pl	2016-01-16 12:18:13.000000000 -0600
4263
+@@ -20,9 +20,18 @@ use strict;
4264
+ 
4265
+ (my $program_name = $0) =~ s|.*/||;
4266
+ 
4267
++my $prog = 'fold';
4268
++my $try = "Try \`$prog --help' for more information.\n";
4269
++my $inval = "$prog: invalid byte, character or field list\n$try";
4270
++
4271
+ # Turn off localization of executable's output.
4272
+ @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
4273
+ 
4274
++# uncommented to enable multibyte paths
4275
++my $mb_locale = $ENV{LOCALE_FR_UTF8};
4276
++! defined $mb_locale || $mb_locale eq 'none'
4277
++ and $mb_locale = 'C';
4278
++
4279
+ my @Tests =
4280
+   (
4281
+    ['s1', '-w2 -s', {IN=>"a\t"}, {OUT=>"a\n\t"}],
4282
+@@ -31,9 +40,48 @@ my @Tests =
4283
+    ['s4', '-w4 -s', {IN=>"abc ef\n"}, {OUT=>"abc \nef\n"}],
4284
+   );
4285
+ 
4286
++# Add _POSIX2_VERSION=199209 to the environment of each test
4287
++# that uses an old-style option like +1.
4288
++if ($mb_locale ne 'C')
4289
++  {
4290
++    # Duplicate each test vector, appending "-mb" to the test name and
4291
++    # inserting {ENV => "LC_ALL=$mb_locale"} in the copy, so that we
4292
++    # provide coverage for the distro-added multi-byte code paths.
4293
++    my @new;
4294
++    foreach my $t (@Tests)
4295
++      {
4296
++        my @new_t = @$t;
4297
++        my $test_name = shift @new_t;
4298
++
4299
++        # Depending on whether fold is multi-byte-patched,
4300
++        # it emits different diagnostics:
4301
++        #   non-MB: invalid byte or field list
4302
++        #   MB:     invalid byte, character or field list
4303
++        # Adjust the expected error output accordingly.
4304
++        if (grep {ref $_ eq 'HASH' && exists $_->{ERR} && $_->{ERR} eq $inval}
4305
++            (@new_t))
4306
++          {
4307
++            my $sub = {ERR_SUBST => 's/, character//'};
4308
++            push @new_t, $sub;
4309
++            push @$t, $sub;
4310
++          }
4311
++        push @new, ["$test_name-mb", @new_t, {ENV => "LC_ALL=$mb_locale"}];
4312
++      }
4313
++    push @Tests, @new;
4314
++  }
4315
++
4316
++@Tests = triple_test \@Tests;
4317
++
4318
++# Remember that triple_test creates from each test with exactly one "IN"
4319
++# file two more tests (.p and .r suffix on name) corresponding to reading
4320
++# input from a file and from a pipe.  The pipe-reading test would fail
4321
++# due to a race condition about 1 in 20 times.
4322
++# Remove the IN_PIPE version of the "output-is-input" test above.
4323
++# The others aren't susceptible because they have three inputs each.
4324
++@Tests = grep {$_->[0] ne 'output-is-input.p'} @Tests;
4325
++
4326
+ my $save_temps = $ENV{DEBUG};
4327
+ my $verbose = $ENV{VERBOSE};
4328
+ 
4329
+-my $prog = 'fold';
4330
+ my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
4331
+ exit $fail;
4332
+diff -Naurp coreutils-8.25-orig/tests/misc/join.pl coreutils-8.25/tests/misc/join.pl
4333
+--- coreutils-8.25-orig/tests/misc/join.pl	2016-01-16 12:18:13.000000000 -0600
4334
+@@ -25,6 +25,15 @@ my $limits = getlimits ();
4335
+ 
4336
+ my $prog = 'join';
4337
+ 
4338
++my $try = "Try \`$prog --help' for more information.\n";
4339
++my $inval = "$prog: invalid byte, character or field list\n$try";
4340
++
4341
++my $mb_locale;
4342
++#Comment out next line to disable multibyte tests
4343
++$mb_locale = $ENV{LOCALE_FR_UTF8};
4344
++! defined $mb_locale || $mb_locale eq 'none'
4345
++  and $mb_locale = 'C';
4346
++
4347
+ my $delim = chr 0247;
4348
+ sub t_subst ($)
4349
+ {
4350
+@@ -329,8 +338,49 @@ foreach my $t (@tv)
4351
+     push @Tests, $new_ent;
4352
+   }
4353
+ 
4354
++# Add _POSIX2_VERSION=199209 to the environment of each test
4355
++# that uses an old-style option like +1.
4356
++if ($mb_locale ne 'C')
4357
++  {
4358
++    # Duplicate each test vector, appending "-mb" to the test name and
4359
++    # inserting {ENV => "LC_ALL=$mb_locale"} in the copy, so that we
4360
++    # provide coverage for the distro-added multi-byte code paths.
4361
++    my @new;
4362
++    foreach my $t (@Tests)
4363
++      {
4364
++        my @new_t = @$t;
4365
++        my $test_name = shift @new_t;
4366
++
4367
++        # Depending on whether join is multi-byte-patched,
4368
++        # it emits different diagnostics:
4369
++        #   non-MB: invalid byte or field list
4370
++        #   MB:     invalid byte, character or field list
4371
++        # Adjust the expected error output accordingly.
4372
++        if (grep {ref $_ eq 'HASH' && exists $_->{ERR} && $_->{ERR} eq $inval}
4373
++            (@new_t))
4374
++          {
4375
++            my $sub = {ERR_SUBST => 's/, character//'};
4376
++            push @new_t, $sub;
4377
++            push @$t, $sub;
4378
++          }
4379
++        #Adjust the output some error messages including test_name for mb
4380
++        if (grep {ref $_ eq 'HASH' && exists $_->{ERR}}
4381
++             (@new_t))
4382
++          {
4383
++            my $sub2 = {ERR_SUBST => "s/$test_name-mb/$test_name/"};
4384
++            push @new_t, $sub2;
4385
++            push @$t, $sub2;
4386
++          }
4387
++        push @new, ["$test_name-mb", @new_t, {ENV => "LC_ALL=$mb_locale"}];
4388
++      }
4389
++    push @Tests, @new;
4390
++  }
4391
++
4392
+ @Tests = triple_test \@Tests;
4393
+ 
4394
++#skip invalid-j-mb test, it is failing because of the format
4395
++@Tests = grep {$_->[0] ne 'invalid-j-mb'} @Tests;
4396
++
4397
+ my $save_temps = $ENV{DEBUG};
4398
+ my $verbose = $ENV{VERBOSE};
4399
+ 
4400
+diff -Naurp coreutils-8.25-orig/tests/misc/sort-mb-tests.sh coreutils-8.25/tests/misc/sort-mb-tests.sh
4401
+--- coreutils-8.25-orig/tests/misc/sort-mb-tests.sh	1969-12-31 18:00:00.000000000 -0600
4402
+@@ -0,0 +1,45 @@
4403
++#!/bin/sh
4404
++# Verify sort's multi-byte support.
4405
++
4406
++. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
4407
++print_ver_ sort
4408
++
4409
++export LC_ALL=en_US.UTF-8
4410
++locale -k LC_CTYPE | grep -q "charmap.*UTF-8" \
4411
++  || skip_ "No UTF-8 locale available"
4412
++
4413
++
4414
++cat <<EOF > exp
4415
++Bananaï¼ 5
4416
++Appleï¼ 10
4417
++Citrusï¼ 20
4418
++Cherryï¼ 30
4419
++EOF
4420
++
4421
++cat <<EOF | sort -t ï¼  -k2 -n > out || fail=1
4422
++Appleï¼ 10
4423
++Bananaï¼ 5
4424
++Citrusï¼ 20
4425
++Cherryï¼ 30
4426
++EOF
4427
++
4428
++compare exp out || { fail=1; cat out; }
4429
++
4430
++
4431
++cat <<EOF > exp
4432
++Citrus@AA20@@5
4433
++Cherry@AA30@@10
4434
++Apple@AA10@@20
4435
++Banana@AA5@@30
4436
++EOF
4437
++
4438
++cat <<EOF | sort -t ï¼  -k4 -n > out || fail=1
4439
++Apple@AA10@@20
4440
++Banana@AA5@@30
4441
++Citrus@AA20@@5
4442
++Cherry@AA30@@10
4443
++EOF
4444
++
4445
++compare exp out || { fail=1; cat out; }
4446
++
4447
++Exit $fail
4448
+diff -Naurp coreutils-8.25-orig/tests/misc/sort-merge.pl coreutils-8.25/tests/misc/sort-merge.pl
4449
+--- coreutils-8.25-orig/tests/misc/sort-merge.pl	2016-01-16 12:18:14.000000000 -0600
4450
+@@ -26,6 +26,15 @@ my $prog = 'sort';
4451
+ # Turn off localization of executable's output.
4452
+ @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
4453
+ 
4454
++my $mb_locale;
4455
++# uncommented according to upstream commit enabling multibyte paths
4456
++$mb_locale = $ENV{LOCALE_FR_UTF8};
4457
++! defined $mb_locale || $mb_locale eq 'none'
4458
++ and $mb_locale = 'C';
4459
++
4460
++my $try = "Try \`$prog --help' for more information.\n";
4461
++my $inval = "$prog: invalid byte, character or field list\n$try";
4462
++
4463
+ # three empty files and one that says 'foo'
4464
+ my @inputs = (+(map{{IN=> {"empty$_"=> ''}}}1..3), {IN=> {foo=> "foo\n"}});
4465
+ 
4466
+@@ -77,6 +86,39 @@ my @Tests =
4467
+         {OUT=>$big_input}],
4468
+     );
4469
+ 
4470
++# Add _POSIX2_VERSION=199209 to the environment of each test
4471
++# that uses an old-style option like +1.
4472
++if ($mb_locale ne 'C')
4473
++  {
4474
++    # Duplicate each test vector, appending "-mb" to the test name and
4475
++    # inserting {ENV => "LC_ALL=$mb_locale"} in the copy, so that we
4476
++    # provide coverage for the distro-added multi-byte code paths.
4477
++    my @new;
4478
++    foreach my $t (@Tests)
4479
++      {
4480
++        my @new_t = @$t;
4481
++        my $test_name = shift @new_t;
4482
++
4483
++        # Depending on whether sort is multi-byte-patched,
4484
++        # it emits different diagnostics:
4485
++        #   non-MB: invalid byte or field list
4486
++        #   MB:     invalid byte, character or field list
4487
++        # Adjust the expected error output accordingly.
4488
++        if (grep {ref $_ eq 'HASH' && exists $_->{ERR} && $_->{ERR} eq $inval}
4489
++            (@new_t))
4490
++          {
4491
++            my $sub = {ERR_SUBST => 's/, character//'};
4492
++            push @new_t, $sub;
4493
++            push @$t, $sub;
4494
++          }
4495
++        next if ($test_name =~ "nmerge-.");
4496
++        push @new, ["$test_name-mb", @new_t, {ENV => "LC_ALL=$mb_locale"}];
4497
++      }
4498
++    push @Tests, @new;
4499
++  }
4500
++
4501
++@Tests = triple_test \@Tests;
4502
++
4503
+ my $save_temps = $ENV{DEBUG};
4504
+ my $verbose = $ENV{VERBOSE};
4505
+ 
4506
+diff -Naurp coreutils-8.25-orig/tests/misc/sort.pl coreutils-8.25/tests/misc/sort.pl
4507
+--- coreutils-8.25-orig/tests/misc/sort.pl	2016-01-16 12:18:14.000000000 -0600
4508
+@@ -24,10 +24,15 @@ my $prog = 'sort';
4509
+ # Turn off localization of executable's output.
4510
+ @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
4511
+ 
4512
+-my $mb_locale = $ENV{LOCALE_FR_UTF8};
4513
++my $mb_locale;
4514
++#Comment out next line to disable multibyte tests
4515
++$mb_locale = $ENV{LOCALE_FR_UTF8};
4516
+ ! defined $mb_locale || $mb_locale eq 'none'
4517
+   and $mb_locale = 'C';
4518
+ 
4519
++my $try = "Try \`$prog --help' for more information.\n";
4520
++my $inval = "$prog: invalid byte, character or field list\n$try";
4521
++
4522
+ # Since each test is run with a file name and with redirected stdin,
4523
+ # the name in the diagnostic is either the file name or "-".
4524
+ # Normalize each diagnostic to use '-'.
4525
+@@ -424,6 +429,38 @@ foreach my $t (@Tests)
4526
+       }
4527
+   }
4528
+ 
4529
++if ($mb_locale ne 'C')
4530
++   {
4531
++    # Duplicate each test vector, appending "-mb" to the test name and
4532
++    # inserting {ENV => "LC_ALL=$mb_locale"} in the copy, so that we
4533
++    # provide coverage for the distro-added multi-byte code paths.
4534
++    my @new;
4535
++    foreach my $t (@Tests)
4536
++       {
4537
++        my @new_t = @$t;
4538
++        my $test_name = shift @new_t;
4539
++
4540
++        # Depending on whether sort is multi-byte-patched,
4541
++        # it emits different diagnostics:
4542
++        #   non-MB: invalid byte or field list
4543
++        #   MB:     invalid byte, character or field list
4544
++        # Adjust the expected error output accordingly.
4545
++        if (grep {ref $_ eq 'HASH' && exists $_->{ERR} && $_->{ERR} eq $inval}
4546
++            (@new_t))
4547
++          {
4548
++            my $sub = {ERR_SUBST => 's/, character//'};
4549
++            push @new_t, $sub;
4550
++            push @$t, $sub;
4551
++          }
4552
++        #disable several failing tests until investigation, disable all tests with envvars set
4553
++        next if (grep {ref $_ eq 'HASH' && exists $_->{ENV}} (@new_t));
4554
++        next if ($test_name =~ "18g" or $test_name =~ "sort-numeric" or $test_name =~ "08[ab]" or $test_name =~ "03[def]" or $test_name =~ "h4" or $test_name =~ "n1" or $test_name =~ "2[01]a");
4555
++        next if ($test_name =~ "11[ab]"); # avoid FP: expected result differs to MB result due to collation rules.
4556
++        push @new, ["$test_name-mb", @new_t, {ENV => "LC_ALL=$mb_locale"}];
4557
++       }
4558
++    push @Tests, @new;
4559
++   }
4560
++
4561
+ @Tests = triple_test \@Tests;
4562
+ 
4563
+ # Remember that triple_test creates from each test with exactly one "IN"
4564
+@@ -433,6 +470,7 @@ foreach my $t (@Tests)
4565
+ # Remove the IN_PIPE version of the "output-is-input" test above.
4566
+ # The others aren't susceptible because they have three inputs each.
4567
+ @Tests = grep {$_->[0] ne 'output-is-input.p'} @Tests;
4568
++@Tests = grep {$_->[0] ne 'output-is-input-mb.p'} @Tests;
4569
+ 
4570
+ my $save_temps = $ENV{DEBUG};
4571
+ my $verbose = $ENV{VERBOSE};
4572
+diff -Naurp coreutils-8.25-orig/tests/misc/unexpand.pl coreutils-8.25/tests/misc/unexpand.pl
4573
+--- coreutils-8.25-orig/tests/misc/unexpand.pl	2016-01-16 12:18:14.000000000 -0600
4574
+@@ -27,6 +27,14 @@ my $limits = getlimits ();
4575
+ 
4576
+ my $prog = 'unexpand';
4577
+ 
4578
++# comment out next line to disable multibyte tests
4579
++my $mb_locale = $ENV{LOCALE_FR_UTF8};
4580
++! defined $mb_locale || $mb_locale eq 'none'
4581
++ and $mb_locale = 'C';
4582
++
4583
++my $try = "Try \`$prog --help' for more information.\n";
4584
++my $inval = "$prog: invalid byte, character or field list\n$try";
4585
++
4586
+ my @Tests =
4587
+     (
4588
+      ['a1', {IN=> ' 'x 1 ."y\n"}, {OUT=> ' 'x 1 ."y\n"}],
4589
+@@ -92,6 +100,37 @@ my @Tests =
4590
+       {EXIT => 1}, {ERR => "$prog: tab stop value is too large\n"}],
4591
+     );
4592
+ 
4593
++if ($mb_locale ne 'C')
4594
++  {
4595
++    # Duplicate each test vector, appending "-mb" to the test name and
4596
++    # inserting {ENV => "LC_ALL=$mb_locale"} in the copy, so that we
4597
++    # provide coverage for the distro-added multi-byte code paths.
4598
++    my @new;
4599
++    foreach my $t (@Tests)
4600
++      {
4601
++        my @new_t = @$t;
4602
++        my $test_name = shift @new_t;
4603
++
4604
++        # Depending on whether unexpand is multi-byte-patched,
4605
++        # it emits different diagnostics:
4606
++        #   non-MB: invalid byte or field list
4607
++        #   MB:     invalid byte, character or field list
4608
++        # Adjust the expected error output accordingly.
4609
++        if (grep {ref $_ eq 'HASH' && exists $_->{ERR} && $_->{ERR} eq $inval}
4610
++            (@new_t))
4611
++          {
4612
++            my $sub = {ERR_SUBST => 's/, character//'};
4613
++            push @new_t, $sub;
4614
++            push @$t, $sub;
4615
++          }
4616
++        next if ($test_name =~ 'b-1');
4617
++        push @new, ["$test_name-mb", @new_t, {ENV => "LC_ALL=$mb_locale"}];
4618
++      }
4619
++    push @Tests, @new;
4620
++  }
4621
++
4622
++@Tests = triple_test \@Tests;
4623
++
4624
+ my $save_temps = $ENV{DEBUG};
4625
+ my $verbose = $ENV{VERBOSE};
4626
+ 
4627
+diff -Naurp coreutils-8.25-orig/tests/misc/uniq.pl coreutils-8.25/tests/misc/uniq.pl
4628
+--- coreutils-8.25-orig/tests/misc/uniq.pl	2016-01-16 12:18:14.000000000 -0600
4629
+@@ -23,9 +23,17 @@ my $limits = getlimits ();
4630
+ my $prog = 'uniq';
4631
+ my $try = "Try '$prog --help' for more information.\n";
4632
+ 
4633
++my $inval = "$prog: invalid byte, character or field list\n$try";
4634
++
4635
+ # Turn off localization of executable's output.
4636
+ @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
4637
+ 
4638
++my $mb_locale;
4639
++#Comment out next line to disable multibyte tests
4640
++$mb_locale = $ENV{LOCALE_FR_UTF8};
4641
++! defined $mb_locale || $mb_locale eq 'none'
4642
++  and $mb_locale = 'C';
4643
++
4644
+ # When possible, create a "-z"-testing variant of each test.
4645
+ sub add_z_variants($)
4646
+ {
4647
+@@ -262,6 +270,53 @@ foreach my $t (@Tests)
4648
+       and push @$t, {ENV=>'_POSIX2_VERSION=199209'};
4649
+   }
4650
+ 
4651
++if ($mb_locale ne 'C')
4652
++  {
4653
++    # Duplicate each test vector, appending "-mb" to the test name and
4654
++    # inserting {ENV => "LC_ALL=$mb_locale"} in the copy, so that we
4655
++    # provide coverage for the distro-added multi-byte code paths.
4656
++    my @new;
4657
++    foreach my $t (@Tests)
4658
++      {
4659
++        my @new_t = @$t;
4660
++        my $test_name = shift @new_t;
4661
++
4662
++        # Depending on whether uniq is multi-byte-patched,
4663
++        # it emits different diagnostics:
4664
++        #   non-MB: invalid byte or field list
4665
++        #   MB:     invalid byte, character or field list
4666
++        # Adjust the expected error output accordingly.
4667
++        if (grep {ref $_ eq 'HASH' && exists $_->{ERR} && $_->{ERR} eq $inval}
4668
++            (@new_t))
4669
++          {
4670
++            my $sub = {ERR_SUBST => 's/, character//'};
4671
++            push @new_t, $sub;
4672
++            push @$t, $sub;
4673
++          }
4674
++        # In test #145, replace the each ‘...’ by '...'.
4675
++        if ($test_name =~ "145")
4676
++          {
4677
++            my $sub = { ERR_SUBST => "s/‘([^’]+)’/'\$1'/g"};
4678
++            push @new_t, $sub;
4679
++            push @$t, $sub;
4680
++          }
4681
++        next if (   $test_name =~ "schar"
4682
++                 or $test_name =~ "^obs-plus"
4683
++                 or $test_name =~ "119");
4684
++        push @new, ["$test_name-mb", @new_t, {ENV => "LC_ALL=$mb_locale"}];
4685
++      }
4686
++    push @Tests, @new;
4687
++   }
4688
++
4689
++# Remember that triple_test creates from each test with exactly one "IN"
4690
++# file two more tests (.p and .r suffix on name) corresponding to reading
4691
++# input from a file and from a pipe.  The pipe-reading test would fail
4692
++# due to a race condition about 1 in 20 times.
4693
++# Remove the IN_PIPE version of the "output-is-input" test above.
4694
++# The others aren't susceptible because they have three inputs each.
4695
++
4696
++@Tests = grep {$_->[0] ne 'output-is-input.p'} @Tests;
4697
++
4698
+ @Tests = add_z_variants \@Tests;
4699
+ @Tests = triple_test \@Tests;
4700
+ 
4701
+diff -Naurp coreutils-8.25-orig/tests/pr/pr-tests.pl coreutils-8.25/tests/pr/pr-tests.pl
4702
+--- coreutils-8.25-orig/tests/pr/pr-tests.pl	2016-01-16 12:18:14.000000000 -0600
4703
+@@ -24,6 +24,15 @@ use strict;
4704
+ my $prog = 'pr';
4705
+ my $normalize_strerror = "s/': .*/'/";
4706
+ 
4707
++my $mb_locale;
4708
++#Uncomment the following line to enable multibyte tests
4709
++$mb_locale = $ENV{LOCALE_FR_UTF8};
4710
++! defined $mb_locale || $mb_locale eq 'none'
4711
++  and $mb_locale = 'C';
4712
++
4713
++my $try = "Try \`$prog --help' for more information.\n";
4714
++my $inval = "$prog: invalid byte, character or field list\n$try";
4715
++
4716
+ my @tv = (
4717
+ 
4718
+ # -b option is no longer an official option. But it's still working to
4719
+@@ -467,8 +476,48 @@ push @Tests,
4720
+     {IN=>{3=>"x\ty\tz\n"}},
4721
+      {OUT=>join("\t", qw(a b c m n o x y z)) . "\n"} ];
4722
+ 
4723
++# Add _POSIX2_VERSION=199209 to the environment of each test
4724
++# that uses an old-style option like +1.
4725
++if ($mb_locale ne 'C')
4726
++  {
4727
++    # Duplicate each test vector, appending "-mb" to the test name and
4728
++    # inserting {ENV => "LC_ALL=$mb_locale"} in the copy, so that we
4729
++    # provide coverage for the distro-added multi-byte code paths.
4730
++    my @new;
4731
++    foreach my $t (@Tests)
4732
++      {
4733
++        my @new_t = @$t;
4734
++        my $test_name = shift @new_t;
4735
++
4736
++        # Depending on whether pr is multi-byte-patched,
4737
++        # it emits different diagnostics:
4738
++        #   non-MB: invalid byte or field list
4739
++        #   MB:     invalid byte, character or field list
4740
++        # Adjust the expected error output accordingly.
4741
++        if (grep {ref $_ eq 'HASH' && exists $_->{ERR} && $_->{ERR} eq $inval}
4742
++            (@new_t))
4743
++          {
4744
++            my $sub = {ERR_SUBST => 's/, character//'};
4745
++            push @new_t, $sub;
4746
++            push @$t, $sub;
4747
++          }
4748
++        #temporarily skip some failing tests
4749
++        next if ($test_name =~ "col-0" or $test_name =~ "col-inval");
4750
++        push @new, ["$test_name-mb", @new_t, {ENV => "LC_ALL=$mb_locale"}];
4751
++      }
4752
++    push @Tests, @new;
4753
++  }
4754
++
4755
+ @Tests = triple_test \@Tests;
4756
+ 
4757
++# Remember that triple_test creates from each test with exactly one "IN"
4758
++# file two more tests (.p and .r suffix on name) corresponding to reading
4759
++# input from a file and from a pipe.  The pipe-reading test would fail
4760
++# due to a race condition about 1 in 20 times.
4761
++# Remove the IN_PIPE version of the "output-is-input" test above.
4762
++# The others aren't susceptible because they have three inputs each.
4763
++@Tests = grep {$_->[0] ne 'output-is-input.p'} @Tests;
4764
++
4765
+ my $save_temps = $ENV{DEBUG};
4766
+ my $verbose = $ENV{VERBOSE};
4767
+ 
... ...
@@ -1,6 +1,6 @@
1 1
 Summary:	Basic system utilities
2 2
 Name:		coreutils
3
-Version:	8.24
3
+Version:	8.25
4 4
 Release:	1%{?dist}
5 5
 License:	GPLv3
6 6
 URL:		http://www.gnu.org/software/coreutils
... ...
@@ -8,8 +8,8 @@ Group:		System Environment/Base
8 8
 Vendor:		VMware, Inc.
9 9
 Distribution: Photon
10 10
 Source0:	http://ftp.gnu.org/gnu/coreutils/%{name}-%{version}.tar.xz
11
-%define sha1 coreutils=cf3d9983461c2b0c074a76804c18464e9a474883
12
-Patch0:		http://http://www.linuxfromscratch.org/patches/downloads/coreutils/coreutils-8.24-i18n-1.patch
11
+%define sha1 coreutils=301f186c24afb882a3ca73d19a102a2ce6f456c3
12
+Patch0:		http://www.linuxfromscratch.org/patches/downloads/coreutils/coreutils-8.25-i18n-2.patch
13 13
 Requires:	gmp
14 14
 Provides:	sh-utils
15 15
 %description
... ...
@@ -19,7 +19,7 @@ the basic system
19 19
 %package lang
20 20
 Summary: Additional language files for coreutils
21 21
 Group: System Environment/Base
22
-Requires: coreutils >= 8.24
22
+Requires: coreutils >= %{version}
23 23
 %description lang
24 24
 These are the additional language files of coreutils.
25 25
 
... ...
@@ -63,6 +63,8 @@ make -k NON_ROOT_USERNAME=nobody check |& tee %{_specdir}/%{name}-check-log || %
63 63
 %defattr(-,root,root)
64 64
 
65 65
 %changelog
66
+*	Tue May 17 2016 Divya Thaluru <dthaluru@vmware.com> 8.25-1
67
+- 	Updated to version 8.25
66 68
 * 	Tue Jan 12 2016 Xiaolin Li <xiaolinl@vmware.com> 8.24-1
67 69
 - 	Updated to version 8.24
68 70
 *	Wed Nov 5 2014 Divya Thaluru <dthaluru@vmware.com> 8.22-1