/* EOL2CR */
/* By Bill Kendrick */   /* thanks to Brian Wilson and Sam Vincent for */
/* NBS 1995 */           /* happening to be online late the night I wrote */
                         /* this and willing to answer stupid forgetful */
                         /* questions */

/* This program is a simple Atari ATASCII EOL (End Of Line) (char #155 dec)
/* to Unix NL (New Line) (char #10 dec, aka LF (Line Feed) on PC's) converter
/* written with emacs and compiled with GNU C (gcc).   5-10-1995 12:26am PST
/* */

#include "stdio.h"

/* prototypes: */
int convert(char *source, char *dest);
void main(int argc, char *argv[]);

/* code: */

int convert(char *source, char *dest)
{
  FILE *fin;
  FILE *fout;
  int a,lines;

  lines=0;
  fin=fopen(source,"r");
  fout=fopen(dest,"w");

  if (fin==NULL)
    {
      printf("Error opening input file.\n");
      exit(1);
    }

  if (fout==NULL)
    {
      printf("Error opening output file.\n");
      exit(1);
    }

  a=0;
  while(a!=-1)
    {
      a=fgetc(fin);
      if (a==155)
      {
	a=10;
        lines++;
      }
      fprintf(fout,"%c",a); 
    }
  fclose(fin);
  fclose(fout);

  return(lines);
}

void main(int argc, char *argv[])
{
  char in[100],out[100];

  printf("\nEOL2CR\n------\nBy Bill Kendrick\nNBS 1995\n\n");
  printf("Converts AtariASCII text files (with EOLs) into\n");
  printf("Unix text files (with NLs).\n\n");

  if (argc==1)
    {
      printf("In: ");
      scanf("%s",in);
      if (strcmp(in,"\0")==0)
        exit(1);
    }
  else
    {
      strcpy(in,argv[1]);
      printf("In: %s\n",in);
    }

  if (argc<=2)
    {
      printf("Out:");
      scanf("%s",out);
      if (strcmp(out,"\0")==0)
        exit(1);   /* for now until I make it determine a new name */
                   /* derived from the 'in' name..  perhaps just dumping */
                   /* to stdout is the best idea (so ">" piping can be done) */
    }
  else
    {
      strcpy(out,argv[2]);
      printf("Out:%s\n",out);
    }

  if (argc>3)
    {
      printf("\nUsage:  eol2cr [infile [outfile]]\n");
      return;
    }

  printf("\nConverting...\n");
  printf("\nLines converted:%d.\n\n",convert(in,out));
}
