/*
* dos2unix and unix2dos file format converter
* <venglin@FreeBSD.lublin.pl>
*
* $Log: conv.c,v $
* Revision 1.1.1.1  2001/05/21 15:38:58  venglin
* initial import into CVS
*
* Revision 1.2  1999/08/12 21:01:49  venglin
* Syntax Bug :)
*
* Revision 1.1  1999/08/12 21:00:29  venglin
* Initial revision
*
*/

#include <stdio.h>
#include <err.h>
#include <string.h>
#include <errno.h>

#define CR 13
#define LF 10

static const char rcsid[] =
  "$Id: conv.c,v 1.1.1.1 2001/05/21 15:38:58 venglin Exp $";

char *av0;

void convert(fp)
FILE *fp;
{
  if (!strcmp(av0, "dos2unix"))
  {   
    char b = getc(fp);
    while (b != EOF) 
    {   
      if (b != CR)
      putchar(b);
      b = getc(fp);
    }  
   }
   
  if (!strcmp(av0, "unix2dos"))
  {
    char b = getc(fp);
    while (b != EOF)
    {
      if (b == LF)
        putchar(CR);
      putchar(b);
      b = getc(fp);
    }
  }
}

void main(argc, argv)
int argc;
char **argv;
{
  int i;
  FILE *phile;

  if (strchr(argv[0], '/'))
    av0 = strrchr(argv[0], '/') + 1;
  else
    av0 = argv[0];

  if (strcmp(av0, "unix2dos") && strcmp(av0, "dos2unix"))
    errx(1, "cannot determine argv[0].\n");

  if (argc < 2) { convert(stdin); exit(0); }

  for (i=1;i<argc;i++)
  {
    if ((phile=fopen(argv[i], "r")) == NULL)
      errx(1, "cannot open file %s: %s", argv[i], strerror(errno)); 

    convert(phile); 

    if ((phile=fopen(argv[i], "r")) == NULL)
      errx(1, "cannot close file %s: %s", argv[i], strerror(errno));
  }

  exit(0);
}

