Web lists-archives.com

Re: [Mingw-msys] Description of MSYS in forthcoming C++ Cookbook, by O'Reilly

On Saturday 25 October 2008 02:39:06 John Clizbe wrote:
> > need unix2dos in mingw-utils
> If you've installed perl, which is probably a safe bet:

I'm no perl expert, and I'm sorry to disappoint, but...

> jpclizbe@yogi:~$ cat << EOF > /usr/local/bin/unix2dos
> #!/usr/bin/perl -pi
> s/\n/\r\n/;

...this doesn't work; it is broken, for any file which already has 
CRLF line endings.  Correct behaviour is to insert the missing CR 
*only* for those lines which do not already have it in place.

However, ...

> jpclizbe@yogi:~$ cat << EOF /usr/local/bin/dos2unix
> #!/usr/bin/perl -pi
> s/\r\n/\n/;

...this looks like it should probably be ok.  (The `sed -n p' 
alternative, which I suggested, is also effective).

The script which I actually use is rather more sophisticated, being a 
complete emulation of Cygwin's composite unix2dos/dos2unix tool.  It 
is attached, but for reference, it does the equivalent of:

  <script name=unix2dos>
    awk '{sub("\r$",""); printf "%s\r\n", $0}' "$@"

  <script name=dos2unix>
    awk '{sub("\r$",""); printf "%s\n", $0}' "$@"

Note that this pair may also be written as:

  <script name=unix2dos>
    #!/bin/awk -f
    {sub("\r$",""); printf "%s\r\n", $0}

  <script name=dos2unix>
    #!/bin/awk -f
    {sub("\r$",""); printf "%s\n", $0}

or, for dos2unix, this also works, (but it's fragile and obscure, 
because it relies on a quirk of the current MSYS sed -- it always 
converts CRLF to LF on output, regardless of the input format; this 
could easily break in a future release):

  <script name=dos2unix>
    #!/bin/sed -nf

These are more like John's perl variants in coding structure, and may 
be slightly more efficient than the shell script variants.


Attachment: msys-unix2dos.sh
Description: application/shellscript

This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
Mingw-msys mailing list