/* dos2unix - A program to take a cr/lf delimited ASCII file
   (from DOS, OS/2 or Windows) and change the line delimiters
   to only lf used by Unix. Rick Smereka, Copyright (C) 1998-2003.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, get a copy via the Internet at
   http://gnu.org/copyleft/gpl.html or write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
   MA 02111-1307 USA

   You can contact the author via email at rsmereka@future-lab.com

   Original version for 16bit DOS. Program syntax:

      dos2unix inputfile outputfile

   April 1998, Rick Smereka

   First GPL'ed version for Debian Linux. Sep/2002, Rick Smereka

   Increased line buffer to 'BUFSIZE' (defined in 'fio.h')
   Dec/2002, Rick Smereka

   Ported to QNX, DOS and 32bit Windows. Jan/2003, Rick Smereka */

#include "stdhead.h"

#define VERSION "1.4.01-2003.01.26"
#define APPNAME "dos2unix"

int main(int, char **);
void usage(void);

int main(int argc, char *argv[])
{
   FILE *in, *out;
   char line[BUFSIZE];
   char fin[128];
   char fout[128];
   char lf[2];
   unsigned int ch;
   int len;

   if (argc == 1)
      {
      usage();
      return(0);
      }

   if (argc < 3)
      {
      printf("dos2unix:incorrect number of parameters\n");
      usage();
      return(0);
      }

   /* build logo string based on platform */

#ifndef OS_UNIX
   /* non-Unix */

   printf("%s for %s Version %s\n", APPNAME, PLATFORM_STRING,
           VERSION);
#else
   /* Unix */

   printf("%s for %s Version %s\n", APPNAME, SUB_PLATFORM_STRING,
           VERSION);
#endif

   printf("By Rick Smereka, Copyright (c) 1998-2003\n");
   printf("%s comes with ABSOLUTELY NO WARRANTY\n", APPNAME);
   printf("This is free software, and you are welcome to redistribute it\n");
   printf("under certain conditions; see 'gpl.txt' for information.\n");
   strcpy(fin, *++argv);
   strcpy(fout, *++argv);

   if ((in = fopen(fin, "r")) == NULL)
      {
      printf("%s:unable open input file '%s'\n", APPNAME, fin);
      return(0);
      }

#ifdef OS_UNIX
   /* if we are running on a Unix machine, open output file as
      text, otherwise open as binary */

   if ((out = fopen(fout, "w")) == NULL)
      {
      printf("%s:unable open output file '%s'\n", APPNAME, fout);
      fclose(in);
      return(0);
      }
#else
   if ((out = fopen(fout, "wb")) == NULL)
      {
      printf("%s:unable open output file '%s'\n", APPNAME, fout);
      fclose(in);
      return(0);
      }
#endif

   lf[0] = 10;
   printf("converting, please wait...");
   get_rec(in, line);

   while(!feof(in))
      {
      len = strlen(line);

#ifdef OS_UNIX
      /* if running on Unix machine, just write out with
         standard text delimiter after removing extra
	 carriage return */

      if (len)
	 {
         ch = (unsigned int)line[len - 1];

         if (ch == EOLL)
	    line[len - 1] = EOS;
	 }

      fprintf(out, "%s\n", line);
#else
      fwrite(line, 1, len, out);
      fwrite(lf, 1, 1, out);
#endif
      get_rec(in, line);
      }

   fclose(in);
   fclose(out);
   printf("\nprogram complete\n");
   return(0);
}

void usage(void)
{
   printf("%s - A program to convert a DOS ASCII file for\n", APPNAME);
   printf("           use by Unix\n\n");
   printf("usage: %s infile outfile\n", APPNAME);
   printf("       where 'infile' is the name of the DOS input file\n");
   printf("       and 'outfile' is the name of the output file.\n");
}
