Subject: dos2unix program (was: Bash, history file, and Tcl)
From: [email-masked] (Ed Huott)

22 Jan 1997 12:18:17 -0800
Approved: [email-masked]
Distribution: cygnus
Message-ID: <[email-masked]>
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"
Original-To: [email-masked]
Content-ID: <[email-masked]>
Original-Sender: [email-masked]

------- =_aaaaaaaaaa0
Content-Type: text/plain; charset="us-ascii"
Content-ID: <[email-masked]>

In message <[email-masked]>, 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: <[email-masked]>
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
"[email-masked]" with one line of text: "help".