Blame SOURCES/coreutils-i18n-cut-old.patch

f5e30c
diff --git a/src/cut.c b/src/cut.c
f5e30c
index 7ab6be4..022d0ad 100644
f5e30c
--- a/src/cut.c
f5e30c
+++ b/src/cut.c
f5e30c
@@ -28,6 +28,11 @@
f5e30c
 #include <assert.h>
f5e30c
 #include <getopt.h>
f5e30c
 #include <sys/types.h>
f5e30c
+
f5e30c
+/* Get mbstate_t, mbrtowc().  */
f5e30c
+#if HAVE_WCHAR_H
f5e30c
+# include <wchar.h>
f5e30c
+#endif
f5e30c
 #include "system.h"
f5e30c
 
f5e30c
 #include "error.h"
f5e30c
@@ -38,6 +43,18 @@
f5e30c
 
f5e30c
 #include "set-fields.h"
f5e30c
 
f5e30c
+/* MB_LEN_MAX is incorrectly defined to be 1 in at least one GCC
f5e30c
+   installation; work around this configuration error.        */
f5e30c
+#if !defined MB_LEN_MAX || MB_LEN_MAX < 2
f5e30c
+# undef MB_LEN_MAX
f5e30c
+# define MB_LEN_MAX 16
f5e30c
+#endif
f5e30c
+
f5e30c
+/* Some systems, like BeOS, have multibyte encodings but lack mbstate_t.  */
f5e30c
+#if HAVE_MBRTOWC && defined mbstate_t
f5e30c
+# define mbrtowc(pwc, s, n, ps) (mbrtowc) (pwc, s, n, 0)
f5e30c
+#endif
f5e30c
+
f5e30c
 /* The official name of this program (e.g., no 'g' prefix).  */
f5e30c
 #define PROGRAM_NAME "cut"
f5e30c
 
f5e30c
@@ -54,6 +71,52 @@
f5e30c
     }									\
f5e30c
   while (0)
f5e30c
 
f5e30c
+/* Refill the buffer BUF to get a multibyte character. */
f5e30c
+#define REFILL_BUFFER(BUF, BUFPOS, BUFLEN, STREAM)                        \
f5e30c
+  do                                                                        \
f5e30c
+    {                                                                        \
f5e30c
+      if (BUFLEN < MB_LEN_MAX && !feof (STREAM) && !ferror (STREAM))        \
f5e30c
+        {                                                                \
f5e30c
+          memmove (BUF, BUFPOS, BUFLEN);                                \
f5e30c
+          BUFLEN += fread (BUF + BUFLEN, sizeof(char), BUFSIZ, STREAM); \
f5e30c
+          BUFPOS = BUF;                                                        \
f5e30c
+        }                                                                \
f5e30c
+    }                                                                        \
f5e30c
+  while (0)
f5e30c
+
f5e30c
+/* Get wide character on BUFPOS. BUFPOS is not included after that.
f5e30c
+   If byte sequence is not valid as a character, CONVFAIL is true. Otherwise false. */
f5e30c
+#define GET_NEXT_WC_FROM_BUFFER(WC, BUFPOS, BUFLEN, MBLENGTH, STATE, CONVFAIL) \
f5e30c
+  do                                                                        \
f5e30c
+    {                                                                        \
f5e30c
+      mbstate_t state_bak;                                                \
f5e30c
+                                                                        \
f5e30c
+      if (BUFLEN < 1)                                                        \
f5e30c
+        {                                                                \
f5e30c
+          WC = WEOF;                                                        \
f5e30c
+          break;                                                        \
f5e30c
+        }                                                                \
f5e30c
+                                                                        \
f5e30c
+      /* Get a wide character. */                                        \
f5e30c
+      CONVFAIL = false;                                                        \
f5e30c
+      state_bak = STATE;                                                \
f5e30c
+      MBLENGTH = mbrtowc ((wchar_t *)&WC, BUFPOS, BUFLEN, &STATE);        \
f5e30c
+                                                                        \
f5e30c
+      switch (MBLENGTH)                                                        \
f5e30c
+        {                                                                \
f5e30c
+        case (size_t)-1:                                                \
f5e30c
+        case (size_t)-2:                                                \
f5e30c
+          CONVFAIL = true;                                                        \
f5e30c
+          STATE = state_bak;                                                \
f5e30c
+          /* Fall througn. */                                                \
f5e30c
+                                                                        \
f5e30c
+        case 0:                                                                \
f5e30c
+          MBLENGTH = 1;                                                        \
f5e30c
+          break;                                                        \
f5e30c
+        }                                                                \
f5e30c
+    }                                                                        \
f5e30c
+  while (0)
f5e30c
+
f5e30c
 
f5e30c
 /* Pointer inside RP.  When checking if a byte or field is selected
f5e30c
    by a finite range, we check if it is between CURRENT_RP.LO
f5e30c
@@ -61,6 +124,9 @@
f5e30c
    CURRENT_RP.HI then we make CURRENT_RP to point to the next range pair. */
f5e30c
 static struct field_range_pair *current_rp;
f5e30c
 
f5e30c
+/* Length of the delimiter given as argument to -d.  */
f5e30c
+size_t delimlen;
f5e30c
+
f5e30c
 /* This buffer is used to support the semantics of the -s option
f5e30c
    (or lack of same) when the specified field list includes (does
f5e30c
    not include) the first field.  In both of those cases, the entire
f5e30c
@@ -77,15 +143,25 @@ enum operating_mode
f5e30c
   {
f5e30c
     undefined_mode,
f5e30c
 
f5e30c
-    /* Output characters that are in the given bytes. */
f5e30c
+    /* Output bytes that are at the given positions. */
f5e30c
     byte_mode,
f5e30c
 
f5e30c
+    /* Output characters that are at the given positions. */
f5e30c
+    character_mode,
f5e30c
+
f5e30c
     /* Output the given delimiter-separated fields. */
f5e30c
     field_mode
f5e30c
   };
f5e30c
 
f5e30c
 static enum operating_mode operating_mode;
f5e30c
 
f5e30c
+/* If nonzero, when in byte mode, don't split multibyte characters.  */
f5e30c
+static int byte_mode_character_aware;
f5e30c
+
f5e30c
+/* If nonzero, the function for single byte locale is work
f5e30c
+   if this program runs on multibyte locale. */
f5e30c
+static int force_singlebyte_mode;
f5e30c
+
f5e30c
 /* If true do not output lines containing no delimiter characters.
f5e30c
    Otherwise, all such lines are printed.  This option is valid only
f5e30c
    with field mode.  */
f5e30c
@@ -97,6 +173,9 @@ static bool complement;
f5e30c
 
f5e30c
 /* The delimiter character for field mode. */
f5e30c
 static unsigned char delim;
f5e30c
+#if HAVE_WCHAR_H
f5e30c
+static wchar_t wcdelim;
f5e30c
+#endif
f5e30c
 
f5e30c
 /* The delimiter for each line/record. */
f5e30c
 static unsigned char line_delim = '\n';
f5e30c
@@ -164,7 +243,7 @@ Print selected parts of lines from each FILE to standard output.\n\
f5e30c
   -f, --fields=LIST       select only these fields;  also print any line\n\
f5e30c
                             that contains no delimiter character, unless\n\
f5e30c
                             the -s option is specified\n\
f5e30c
-  -n                      (ignored)\n\
f5e30c
+  -n                      with -b: don't split multibyte characters\n\
f5e30c
 "), stdout);
f5e30c
       fputs (_("\
f5e30c
       --complement        complement the set of selected bytes, characters\n\
f5e30c
@@ -280,6 +359,82 @@ cut_bytes (FILE *stream)
f5e30c
     }
f5e30c
 }
f5e30c
 
f5e30c
+#if HAVE_MBRTOWC
f5e30c
+/* This function is in use for the following case.
f5e30c
+
f5e30c
+   1. Read from the stream STREAM, printing to standard output any selected
f5e30c
+   characters.
f5e30c
+
f5e30c
+   2. Read from stream STREAM, printing to standard output any selected bytes,
f5e30c
+   without splitting multibyte characters.  */
f5e30c
+
f5e30c
+static void
f5e30c
+cut_characters_or_cut_bytes_no_split (FILE *stream)
f5e30c
+{
f5e30c
+  uintmax_t idx;             /* number of bytes or characters in the line so far. */
f5e30c
+  char buf[MB_LEN_MAX + BUFSIZ];  /* For spooling a read byte sequence. */
f5e30c
+  char *bufpos;                /* Next read position of BUF. */
f5e30c
+  size_t buflen;        /* The length of the byte sequence in buf. */
f5e30c
+  wint_t wc;                /* A gotten wide character. */
f5e30c
+  size_t mblength;        /* The byte size of a multibyte character which shows
f5e30c
+                           as same character as WC. */
f5e30c
+  mbstate_t state;        /* State of the stream. */
f5e30c
+  bool convfail = false;  /* true, when conversion failed. Otherwise false. */
f5e30c
+  /* Whether to begin printing delimiters between ranges for the current line.
f5e30c
+     Set after we've begun printing data corresponding to the first range.  */
f5e30c
+  bool print_delimiter = false;
f5e30c
+
f5e30c
+  idx = 0;
f5e30c
+  buflen = 0;
f5e30c
+  bufpos = buf;
f5e30c
+  memset (&state, '\0', sizeof(mbstate_t));
f5e30c
+
f5e30c
+  current_rp = frp;
f5e30c
+
f5e30c
+  while (1)
f5e30c
+    {
f5e30c
+      REFILL_BUFFER (buf, bufpos, buflen, stream);
f5e30c
+
f5e30c
+      GET_NEXT_WC_FROM_BUFFER (wc, bufpos, buflen, mblength, state, convfail);
f5e30c
+      (void) convfail;  /* ignore unused */
f5e30c
+
f5e30c
+      if (wc == WEOF)
f5e30c
+        {
f5e30c
+          if (idx > 0)
f5e30c
+            putchar (line_delim);
f5e30c
+          break;
f5e30c
+        }
f5e30c
+      else if (wc == line_delim)
f5e30c
+        {
f5e30c
+          putchar (line_delim);
f5e30c
+          idx = 0;
f5e30c
+          print_delimiter = false;
f5e30c
+          current_rp = frp;
f5e30c
+        }
f5e30c
+      else
f5e30c
+        {
f5e30c
+          next_item (&idx);
f5e30c
+          if (print_kth (idx))
f5e30c
+            {
f5e30c
+              if (output_delimiter_specified)
f5e30c
+                {
f5e30c
+                  if (print_delimiter && is_range_start_index (idx))
f5e30c
+                    {
f5e30c
+                      fwrite (output_delimiter_string, sizeof (char),
f5e30c
+                              output_delimiter_length, stdout);
f5e30c
+                    }
f5e30c
+                  print_delimiter = true;
f5e30c
+                }
f5e30c
+              fwrite (bufpos, mblength, sizeof(char), stdout);
f5e30c
+            }
f5e30c
+        }
f5e30c
+
f5e30c
+      buflen -= mblength;
f5e30c
+      bufpos += mblength;
f5e30c
+    }
f5e30c
+}
f5e30c
+#endif
f5e30c
+
f5e30c
 /* Read from stream STREAM, printing to standard output any selected fields.  */
f5e30c
 
f5e30c
 static void
f5e30c
@@ -425,13 +580,211 @@ cut_fields (FILE *stream)
f5e30c
     }
f5e30c
 }
f5e30c
 
f5e30c
+#if HAVE_MBRTOWC
f5e30c
+static void
f5e30c
+cut_fields_mb (FILE *stream)
f5e30c
+{
f5e30c
+  int c;
f5e30c
+  uintmax_t field_idx;
f5e30c
+  int found_any_selected_field;
f5e30c
+  int buffer_first_field;
f5e30c
+  int empty_input;
f5e30c
+  char buf[MB_LEN_MAX + BUFSIZ];  /* For spooling a read byte sequence. */
f5e30c
+  char *bufpos;                /* Next read position of BUF. */
f5e30c
+  size_t buflen;        /* The length of the byte sequence in buf. */
f5e30c
+  wint_t wc = 0;        /* A gotten wide character. */
f5e30c
+  size_t mblength;        /* The byte size of a multibyte character which shows
f5e30c
+                           as same character as WC. */
f5e30c
+  mbstate_t state;        /* State of the stream. */
f5e30c
+  bool convfail = false;  /* true, when conversion failed. Otherwise false. */
f5e30c
+
f5e30c
+  current_rp = frp;
f5e30c
+
f5e30c
+  found_any_selected_field = 0;
f5e30c
+  field_idx = 1;
f5e30c
+  bufpos = buf;
f5e30c
+  buflen = 0;
f5e30c
+  memset (&state, '\0', sizeof(mbstate_t));
f5e30c
+
f5e30c
+  c = getc (stream);
f5e30c
+  empty_input = (c == EOF);
f5e30c
+  if (c != EOF)
f5e30c
+  {
f5e30c
+    ungetc (c, stream);
f5e30c
+    wc = 0;
f5e30c
+  }
f5e30c
+  else
f5e30c
+    wc = WEOF;
f5e30c
+
f5e30c
+  /* To support the semantics of the -s flag, we may have to buffer
f5e30c
+     all of the first field to determine whether it is `delimited.'
f5e30c
+     But that is unnecessary if all non-delimited lines must be printed
f5e30c
+     and the first field has been selected, or if non-delimited lines
f5e30c
+     must be suppressed and the first field has *not* been selected.
f5e30c
+     That is because a non-delimited line has exactly one field.  */
f5e30c
+  buffer_first_field = (suppress_non_delimited ^ !print_kth (1));
f5e30c
+
f5e30c
+  while (1)
f5e30c
+    {
f5e30c
+      if (field_idx == 1 && buffer_first_field)
f5e30c
+        {
f5e30c
+          int len = 0;
f5e30c
+
f5e30c
+          while (1)
f5e30c
+            {
f5e30c
+              REFILL_BUFFER (buf, bufpos, buflen, stream);
f5e30c
+
f5e30c
+              GET_NEXT_WC_FROM_BUFFER
f5e30c
+                (wc, bufpos, buflen, mblength, state, convfail);
f5e30c
+
f5e30c
+              if (wc == WEOF)
f5e30c
+                break;
f5e30c
+
f5e30c
+              field_1_buffer = xrealloc (field_1_buffer, len + mblength);
f5e30c
+              memcpy (field_1_buffer + len, bufpos, mblength);
f5e30c
+              len += mblength;
f5e30c
+              buflen -= mblength;
f5e30c
+              bufpos += mblength;
f5e30c
+
f5e30c
+              if (!convfail && (wc == line_delim || wc == wcdelim))
f5e30c
+                break;
f5e30c
+            }
f5e30c
+
f5e30c
+          if (len <= 0 && wc == WEOF)
f5e30c
+            break;
f5e30c
+
f5e30c
+          /* If the first field extends to the end of line (it is not
f5e30c
+             delimited) and we are printing all non-delimited lines,
f5e30c
+             print this one.  */
f5e30c
+          if (convfail || (!convfail && wc != wcdelim))
f5e30c
+            {
f5e30c
+              if (suppress_non_delimited)
f5e30c
+                {
f5e30c
+                  /* Empty.        */
f5e30c
+                }
f5e30c
+              else
f5e30c
+                {
f5e30c
+                  fwrite (field_1_buffer, sizeof (char), len, stdout);
f5e30c
+                  /* Make sure the output line is newline terminated.  */
f5e30c
+                  if (convfail || (!convfail && wc != line_delim))
f5e30c
+                    putchar (line_delim);
f5e30c
+                }
f5e30c
+              continue;
f5e30c
+            }
f5e30c
+
f5e30c
+          if (print_kth (1))
f5e30c
+            {
f5e30c
+              /* Print the field, but not the trailing delimiter.  */
f5e30c
+              fwrite (field_1_buffer, sizeof (char), len - 1, stdout);
f5e30c
+              found_any_selected_field = 1;
f5e30c
+            }
f5e30c
+          next_item (&field_idx);
f5e30c
+        }
f5e30c
+
f5e30c
+      if (wc != WEOF)
f5e30c
+        {
f5e30c
+          if (print_kth (field_idx))
f5e30c
+            {
f5e30c
+              if (found_any_selected_field)
f5e30c
+                {
f5e30c
+                  fwrite (output_delimiter_string, sizeof (char),
f5e30c
+                          output_delimiter_length, stdout);
f5e30c
+                }
f5e30c
+              found_any_selected_field = 1;
f5e30c
+            }
f5e30c
+
f5e30c
+          while (1)
f5e30c
+            {
f5e30c
+              REFILL_BUFFER (buf, bufpos, buflen, stream);
f5e30c
+
f5e30c
+              GET_NEXT_WC_FROM_BUFFER
f5e30c
+                (wc, bufpos, buflen, mblength, state, convfail);
f5e30c
+
f5e30c
+              if (wc == WEOF)
f5e30c
+                break;
f5e30c
+              else if (!convfail && (wc == wcdelim || wc == line_delim))
f5e30c
+                {
f5e30c
+                  buflen -= mblength;
f5e30c
+                  bufpos += mblength;
f5e30c
+                  break;
f5e30c
+                }
f5e30c
+
f5e30c
+              if (print_kth (field_idx))
f5e30c
+                fwrite (bufpos, mblength, sizeof(char), stdout);
f5e30c
+
f5e30c
+              buflen -= mblength;
f5e30c
+              bufpos += mblength;
f5e30c
+            }
f5e30c
+        }
f5e30c
+
f5e30c
+      if ((!convfail || wc == line_delim) && buflen < 1)
f5e30c
+        wc = WEOF;
f5e30c
+
f5e30c
+      if (!convfail && wc == wcdelim)
f5e30c
+        next_item (&field_idx);
f5e30c
+      else if (wc == WEOF || (!convfail && wc == line_delim))
f5e30c
+        {
f5e30c
+          if (found_any_selected_field
f5e30c
+              || (!empty_input && !(suppress_non_delimited && field_idx == 1)))
f5e30c
+            putchar (line_delim);
f5e30c
+          if (wc == WEOF)
f5e30c
+            break;
f5e30c
+          field_idx = 1;
f5e30c
+          current_rp = frp;
f5e30c
+          found_any_selected_field = 0;
f5e30c
+        }
f5e30c
+    }
f5e30c
+}
f5e30c
+#endif
f5e30c
+
f5e30c
 static void
f5e30c
 cut_stream (FILE *stream)
f5e30c
 {
f5e30c
-  if (operating_mode == byte_mode)
f5e30c
-    cut_bytes (stream);
f5e30c
+#if HAVE_MBRTOWC
f5e30c
+  if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
f5e30c
+    {
f5e30c
+      switch (operating_mode)
f5e30c
+        {
f5e30c
+        case byte_mode:
f5e30c
+          if (byte_mode_character_aware)
f5e30c
+            cut_characters_or_cut_bytes_no_split (stream);
f5e30c
+          else
f5e30c
+            cut_bytes (stream);
f5e30c
+          break;
f5e30c
+
f5e30c
+        case character_mode:
f5e30c
+          cut_characters_or_cut_bytes_no_split (stream);
f5e30c
+          break;
f5e30c
+
f5e30c
+        case field_mode:
f5e30c
+          if (delimlen == 1)
f5e30c
+            {
f5e30c
+              /* Check if we have utf8 multibyte locale, so we can use this
f5e30c
+                 optimization because of uniqueness of characters, which is
f5e30c
+                 not true for e.g. SJIS */
f5e30c
+              char * loc = setlocale(LC_CTYPE, NULL);
f5e30c
+              if (loc && (strstr (loc, "UTF-8") || strstr (loc, "utf-8") ||
f5e30c
+                  strstr (loc, "UTF8") || strstr (loc, "utf8")))
f5e30c
+                {
f5e30c
+                  cut_fields (stream);
f5e30c
+                  break;
f5e30c
+                }
f5e30c
+            }
f5e30c
+          cut_fields_mb (stream);
f5e30c
+          break;
f5e30c
+
f5e30c
+        default:
f5e30c
+          abort ();
f5e30c
+        }
f5e30c
+    }
f5e30c
   else
f5e30c
-    cut_fields (stream);
f5e30c
+#endif
f5e30c
+    {
f5e30c
+      if (operating_mode == field_mode)
f5e30c
+        cut_fields (stream);
f5e30c
+      else
f5e30c
+        cut_bytes (stream);
f5e30c
+    }
f5e30c
 }
f5e30c
 
f5e30c
 /* Process file FILE to standard output.
f5e30c
@@ -483,6 +836,7 @@ main (int argc, char **argv)
f5e30c
   bool ok;
f5e30c
   bool delim_specified = false;
f5e30c
   char *spec_list_string IF_LINT ( = NULL);
f5e30c
+  char mbdelim[MB_LEN_MAX + 1];
f5e30c
 
f5e30c
   initialize_main (&argc, &argv);
f5e30c
   set_program_name (argv[0]);
f5e30c
@@ -505,7 +859,6 @@ main (int argc, char **argv)
f5e30c
       switch (optc)
f5e30c
         {
f5e30c
         case 'b':
f5e30c
-        case 'c':
f5e30c
           /* Build the byte list. */
f5e30c
           if (operating_mode != undefined_mode)
f5e30c
             FATAL_ERROR (_("only one type of list may be specified"));
f5e30c
@@ -513,6 +866,14 @@ main (int argc, char **argv)
f5e30c
           spec_list_string = optarg;
f5e30c
           break;
f5e30c
 
f5e30c
+        case 'c':
f5e30c
+          /* Build the character list. */
f5e30c
+          if (operating_mode != undefined_mode)
f5e30c
+            FATAL_ERROR (_("only one type of list may be specified"));
f5e30c
+          operating_mode = character_mode;
f5e30c
+          spec_list_string = optarg;
f5e30c
+          break;
f5e30c
+
f5e30c
         case 'f':
f5e30c
           /* Build the field list. */
f5e30c
           if (operating_mode != undefined_mode)
f5e30c
@@ -524,10 +885,38 @@ main (int argc, char **argv)
f5e30c
         case 'd':
f5e30c
           /* New delimiter. */
f5e30c
           /* Interpret -d '' to mean 'use the NUL byte as the delimiter.'  */
f5e30c
-          if (optarg[0] != '\0' && optarg[1] != '\0')
f5e30c
-            FATAL_ERROR (_("the delimiter must be a single character"));
f5e30c
-          delim = optarg[0];
f5e30c
-          delim_specified = true;
f5e30c
+            {
f5e30c
+#if HAVE_MBRTOWC
f5e30c
+              if(MB_CUR_MAX > 1)
f5e30c
+                {
f5e30c
+                  mbstate_t state;
f5e30c
+
f5e30c
+                  memset (&state, '\0', sizeof(mbstate_t));
f5e30c
+                  delimlen = mbrtowc (&wcdelim, optarg, strnlen(optarg, MB_LEN_MAX), &state);
f5e30c
+
f5e30c
+                  if (delimlen == (size_t)-1 || delimlen == (size_t)-2)
f5e30c
+                    ++force_singlebyte_mode;
f5e30c
+                  else
f5e30c
+                    {
f5e30c
+                      delimlen = (delimlen < 1) ? 1 : delimlen;
f5e30c
+                      if (wcdelim != L'\0' && *(optarg + delimlen) != '\0')
f5e30c
+                        FATAL_ERROR (_("the delimiter must be a single character"));
f5e30c
+                      memcpy (mbdelim, optarg, delimlen);
f5e30c
+                      mbdelim[delimlen] = '\0';
f5e30c
+                      if (delimlen == 1)
f5e30c
+                        delim = *optarg;
f5e30c
+                    }
f5e30c
+                }
f5e30c
+
f5e30c
+              if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
f5e30c
+#endif
f5e30c
+                {
f5e30c
+                  if (optarg[0] != '\0' && optarg[1] != '\0')
f5e30c
+                    FATAL_ERROR (_("the delimiter must be a single character"));
f5e30c
+                  delim = (unsigned char) optarg[0];
f5e30c
+                }
f5e30c
+            delim_specified = true;
f5e30c
+          }
f5e30c
           break;
f5e30c
 
f5e30c
         case OUTPUT_DELIMITER_OPTION:
f5e30c
@@ -540,6 +929,7 @@ main (int argc, char **argv)
f5e30c
           break;
f5e30c
 
f5e30c
         case 'n':
f5e30c
+          byte_mode_character_aware = 1;
f5e30c
           break;
f5e30c
 
f5e30c
         case 's':
f5e30c
@@ -579,15 +969,34 @@ main (int argc, char **argv)
f5e30c
               | (complement ? SETFLD_COMPLEMENT : 0) );
f5e30c
 
f5e30c
   if (!delim_specified)
f5e30c
-    delim = '\t';
f5e30c
+    {
f5e30c
+      delim = '\t';
f5e30c
+#ifdef HAVE_MBRTOWC
f5e30c
+      wcdelim = L'\t';
f5e30c
+      mbdelim[0] = '\t';
f5e30c
+      mbdelim[1] = '\0';
f5e30c
+      delimlen = 1;
f5e30c
+#endif
f5e30c
+    }
f5e30c
 
f5e30c
   if (output_delimiter_string == NULL)
f5e30c
     {
f5e30c
-      static char dummy[2];
f5e30c
-      dummy[0] = delim;
f5e30c
-      dummy[1] = '\0';
f5e30c
-      output_delimiter_string = dummy;
f5e30c
-      output_delimiter_length = 1;
f5e30c
+#ifdef HAVE_MBRTOWC
f5e30c
+      if (MB_CUR_MAX > 1 && !force_singlebyte_mode)
f5e30c
+        {
f5e30c
+          output_delimiter_string = xstrdup(mbdelim);
f5e30c
+          output_delimiter_length = delimlen;
f5e30c
+        }
f5e30c
+
f5e30c
+      if (MB_CUR_MAX <= 1 || force_singlebyte_mode)
f5e30c
+#endif
f5e30c
+        {
f5e30c
+          static char dummy[2];
f5e30c
+          dummy[0] = delim;
f5e30c
+          dummy[1] = '\0';
f5e30c
+          output_delimiter_string = dummy;
f5e30c
+          output_delimiter_length = 1;
f5e30c
+        }
f5e30c
     }
f5e30c
 
f5e30c
   if (optind == argc)