Subject: bug#7085: fmt (GNU coreutils) 6.10

From: William Plusnick <[email-masked]>
Date: Mon, 27 Sep 2010 17:26:09 -0500

On Wed, Sep 22, 2010 at 2:59 PM, Denis M. Wilson <[email-masked]>wrote:

> This program does not deal properly with CRLF terminators.
> Gratuitous CRs are left in joined lines; they should be
> removed. The user may want to keep the CRLF style or change
> to Unix (LF). There should be an option for this.
>
> Denis M. Wilson
>
> --
>
>
>
> Here is a patch that I wrote because I was bored today (I finished all my
homework this weekend :^p) that will remove CRs from lines ending in CRLF. I
must warn you that I didn't add documentation to the --help (and by
implication the man page) or to the texi files. I did this to minimize the
number of changes, so as to become less likely to conflict with future
commits, since this is most likely not going into the mainstream repository.
You invoke it like this:
fmt -d [file]

or alternatively:
fmt --dos [file]

The reason I sent this to everyone and not just Denis Wilson is that I
wanted to be able to point to it in the future if people want this feature
and are willing to risk future incompatibility.

I believe it works, though I haven't tested it too throughly. Though
Valgrind doesn't complain and it seems to do the job. (I downloaded a file
written on MS-DOS and it ran it through via: 'fmt -d file.txt' and it works
on it.)

Hope this helps someone,
William
>From 12a2bee879e3c803f872fe1960a1dedaed485d10 Mon Sep 17 00:00:00 2001
From: Patrick W. Plusnick II <[email-masked]>
Date: Mon, 27 Sep 2010 16:57:06 -0500
Subject: [PATCH] fmt: added the -d option so that it removes Carriage
Returns from MS-DOS files

* src/fmt.c: simply removes the Carriage Returns from lines ending with
CRLF.
---
 src/fmt.c |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/fmt.c b/src/fmt.c
index 8a5d8bd..9150f43 100644
--- a/src/fmt.c
+++ b/src/fmt.c
@@ -173,6 +173,8 @@ static void put_space (int space);
 /* If true, first 2 lines may have different indent (default false).  */
 static bool crown;

+/* If true, Removes the CR out of CRLFs. Mainly for MS-DOS files */
+static bool trunc_crlf;
 /* If true, first 2 lines _must_ have different indent (default false).  */
 static bool tagged;

@@ -304,6 +306,7 @@ With no FILE, or when FILE is -, read standard
input.\n"),
 static struct option const long_options[] =
 {
   {"crown-margin", no_argument, NULL, 'c'},
+  {"dos", no_argument, NULL, 'd'},
   {"prefix", required_argument, NULL, 'p'},
   {"split-only", no_argument, NULL, 's'},
   {"tagged-paragraph", no_argument, NULL, 't'},
@@ -329,7 +332,7 @@ main (int argc, char **argv)

   atexit (close_stdout);

-  crown = tagged = split = uniform = false;
+  crown = trunc_crlf = tagged = split = uniform = false;
   max_width = WIDTH;
   prefix = "";
   prefix_length = prefix_lead_space = prefix_full_length = 0;
@@ -345,7 +348,7 @@ main (int argc, char **argv)
       argc--;
     }

-  while ((optchar = getopt_long (argc, argv, "0123456789cstuw:p:",
+  while ((optchar = getopt_long (argc, argv, "0123456789cdstuw:p:",
                                  long_options, NULL))
          != -1)
     switch (optchar)
@@ -361,6 +364,10 @@ main (int argc, char **argv)
         crown = true;
         break;

+      case 'd':
+        trunc_crlf = true;
+    break;
+
       case 's':
         split = true;
         break;
@@ -691,6 +698,8 @@ get_line (FILE *f, int c)
       word_limit++;
     }
   while (c != '\n' && c != EOF);
+  if (c == '\n' && *(wptr-1) == '\r' && trunc_crlf)
+    *--wptr = '\n';
   return get_prefix (f);
 }

-- 
1.7.0.4