delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1997/01/22/12:18:17

From: huott AT pinebush DOT com (Ed Huott)
Subject: dos2unix program (was: Bash, history file, and Tcl)
22 Jan 1997 12:18:17 -0800 :
Approved: cygnus DOT gnu-win32 AT cygnus DOT com
Distribution: cygnus
Message-ID: <199701221720.MAA05542.cygnus.gnu-win32@sol.pinebush.com>
Mime-Version: 1.0
Original-To: gnu-win32 AT cygnus DOT com
Original-Sender: owner-gnu-win32 AT cygnus DOT com

------- =_aaaaaaaaaa0
Content-Type: text/plain; charset="us-ascii"
Content-ID: <24024 DOT 853953147 DOT 1 AT pinebush DOT com>

In message <32E59D3D DOT 6DCB AT netcom DOT com>, Jim Balter writes:
>>         c = getchar();
>>         while (c != EOF) {
>>                 /* Eat any \r's... they shouldn't be here */
>>                 while (c == '\r') c = getchar();
>>                 if (c == EOF) break;
>>                 putchar(c);
>>                 c = getchar();
>>         }
>
>Uh, sure, if you are into obfuscation and exercising the optimizer, but
>
>        while ((c = getchar()) != EOF)
>                if (c != '\r')
>                        putchar(c);
>
>does exactly the same thing.
>

Here's a DOS to Unix file filter that works a little better by leaving
lone <cr>'s embedded in the file unmolested.  It should also run
faster due to the buffering through the use of fgets() and fprintf().

It's written using Microsoft compiler semantics, not gcc, sorry.

<ED>


------- =_aaaaaaaaaa0
Content-Type: text/plain; charset="us-ascii"
Content-ID: <24024 DOT 853953147 DOT 2 AT pinebush DOT com>
Content-Description: dos2unix.c

/*  Copyright 1996  Ed Huott, Albany, New York, USA
    All rights reserved.

 Redistribution and use of this source code, with or without modification, is
 permitted provided that the following condition is met:

 1. Redistributions of this source code must retain the above copyright
    notice, this condition, and the following disclaimer.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
 * Program to convert DOS style text files (lines end in <cr><lf>) to
 * Unix style text files (lines end in <lf>).
 */

#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <string.h>

#if defined _DEBUG && !defined DEBUG
#define DEBUG
#endif

int main(int argc, char *argv[])
{
  FILE *infp, *outfp;
  char buf[1000], *cp;
  int retn;

  if (argc > 1) {
    printf("Usage: %s < infile > outfile\n", argv[0]);
    exit (2);
  }

  /* get file pointer for input (might come from argv[] someday) */
  infp = stdin;

  /* get file pointer for output (might come from argv[] someday) */
  outfp = stdout;

  /* set input and output file modes to binary */
  retn = _setmode(_fileno(infp), _O_BINARY);
  if (retn != -1) {
    retn = _setmode(_fileno(outfp), _O_BINARY);
  }
  if (retn == -1) {
    perror( "Cannot set mode" );
    exit (1);
  }  

  while (NULL != fgets(buf, sizeof(buf), infp)) {
    cp = strstr(buf, "\r\n");
    if (cp != NULL) {
      *cp = '\0';
      fprintf(outfp, "%s\n", buf);
    }
    else {
      fprintf(outfp, "%s", buf);
    }
  }

  if (feof(infp)) {
    /* finished successfully */
    exit (0);
  }

  if (ferror(infp)) {
    /* uh, oh */
    perror("Read error");
    exit (1);
  }

  exit (0);
} /* main */

------- =_aaaaaaaaaa0--
-
For help on using this list, send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019