2002-12-02  Jan Nieuwenhuizen  <janneke@gnu.org>

	* src/cut.c (print_kth): Also return whether char is first of
	range or field.
	(set_fields): Mark start of field or range.
	(cut_bytes): If user specified output_delimiter, output
	output_delimiter_string between ranges.
	(main): Initialize new file-scope global
	output_delimiter_specified.

diff -p'urNx*~' -x'ChangeLog*' ../coreutils-4.5.3/src/cut.c ./src/cut.c
--- ../coreutils-4.5.3/src/cut.c	2002-10-13 16:03:06.000000000 +0200
+++ ./src/cut.c	2002-12-02 14:58:27.000000000 +0100
@@ -126,6 +126,9 @@ static int suppress_non_delimited;
 /* The delimeter character for field mode. */
 static int delim;
 
+/* Was output_delimiter specified?  */
+static int output_delimiter_specified;
+
 /* The length of output_delimiter_string.  */
 static size_t output_delimiter_length;
 
@@ -210,11 +213,19 @@ With no FILE, or when FILE is -, read st
   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
+/* Return nonzero if k is printable, 2 for first position in range or
+   field, 1 for other positions. */
+
 static int
 print_kth (unsigned int k)
 {
-  return ((0 < eol_range_start && eol_range_start <= k)
-	  || (k <= max_range_endpoint && printable_field[k]));
+  if (eol_range_start > 0 && k >= eol_range_start)
+    return 1 + (eol_range_start == k);
+  
+  if (k <= max_range_endpoint)
+    return printable_field[k];
+  
+  return 0;
 }
 
 /* Given the list of field or byte range specifications FIELDSTR, set
@@ -371,8 +382,12 @@ set_fields (const char *fieldstr)
   /* Set the array entries corresponding to integers in the ranges of RP.  */
   for (i = 0; i < n_rp; i++)
     {
-      unsigned int j;
-      for (j = rp[i].lo; j <= rp[i].hi; j++)
+      unsigned int j = rp[i].lo;
+
+      /* Mark first position of field or range with a 2. */
+      if (j <= rp[i].hi)
+	printable_field[j] = 2;
+      for (++j; j <= rp[i].hi; j++)
 	{
 	  printable_field[j] = 1;
 	}
@@ -389,8 +404,10 @@ static void
 cut_bytes (FILE *stream)
 {
   unsigned int byte_idx;	/* Number of chars in the line so far. */
-
+  int found_any_selected_range; /* Was a range output in the line so far.  */
+    
   byte_idx = 0;
+  found_any_selected_range = 0;
   while (1)
     {
       register int c;		/* Each character from the file. */
@@ -401,6 +418,7 @@ cut_bytes (FILE *stream)
 	{
 	  putchar ('\n');
 	  byte_idx = 0;
+	  found_any_selected_range = 0;
 	}
       else if (c == EOF)
 	{
@@ -410,16 +428,22 @@ cut_bytes (FILE *stream)
 	}
       else
 	{
-	  ++byte_idx;
-	  if (print_kth (byte_idx))
+	  int idx_status = print_kth (++byte_idx);
+	  if (idx_status)
 	    {
+	      if (idx_status == 2 && found_any_selected_range
+		  && output_delimiter_specified)
+		fwrite (output_delimiter_string, sizeof (char),
+			output_delimiter_length, stdout);
+	      found_any_selected_range = 1;
 	      putchar (c);
 	    }
 	}
     }
 }
 
-/* Read from stream STREAM, printing to standard output any selected fields.  */
+/* Read from stream STREAM, printing to standard output any selected
+   fields.  */
 
 static void
 cut_fields (FILE *stream)
@@ -611,6 +635,7 @@ main (int argc, char **argv)
 
   delim = '\0';
   have_read_stdin = 0;
+  output_delimiter_specified = 0;
 
   while ((optc = getopt_long (argc, argv, "b:c:d:f:ns", longopts, NULL)) != -1)
     {
@@ -648,6 +673,7 @@ main (int argc, char **argv)
 	  break;
 
 	case OUTPUT_DELIMITER_OPTION:
+	  output_delimiter_specified = 1;
 	  /* Interpret --output-delimiter='' to mean
 	     `use the NUL byte as the delimiter.'  */
 	  output_delimiter_length = (optarg[0] == '\0'
@@ -675,7 +701,8 @@ main (int argc, char **argv)
     FATAL_ERROR (_("you must specify a list of bytes, characters, or fields"));
 
   if (delim != '\0' && operating_mode != field_mode)
-    FATAL_ERROR (_("a delimiter may be specified only when operating on fields"));
+    FATAL_ERROR (_("a delimiter may be specified only when operating on \
+fields"));
 
   if (suppress_non_delimited && operating_mode != field_mode)
     FATAL_ERROR (_("suppressing non-delimited lines makes sense\n\

