#include <iostream>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
const char LF=10;
const char CR=13;
const char FF=12;
FILE *in,*out;
main(int argc, char *argv[]) {
cout << "     ******** DOS TO UNIX TEXT FILES FILTER ******** \n\r\n\r";
cout << "THIS SIMPLE PROGRAM  TRANSLATES STANDARD DOS TEXT FILES INTO\n\r";
cout << "UNIX ONES BY TRANSFORMING CR/LF TO 'NEWLINES' DELETING\n\r";
cout << "ALL 'FF' CODES. OTHER ONES ARE NOT CHANGED.\n\r\n\r";
cout <<  "! DO NOT CONVERT FILES BEING ALERADY UNIX TYPE !\n\r";
if ((argc != 3) && (argc != 2))	{
	cout << "\n\r -----> Syntax:\n\r\n\r";
	cout << "dos2unix in_file [out_file]\n\r\n\r";
	cout << "(if you use only in_file, the out_file will be the input one)\n\r\a";
	exit(1);
	}
if ((in = fopen(argv[1], "rb")) == NULL) {
      cout << "\n\rCannot open input file.\n\r\a";
      exit(2);
 }
char *temp_name=tempnam(".","xyz");
if (argc == 3) 
	out = fopen(argv[2], "wb"); 
else
	out = fopen(temp_name, "wb");
if (out == NULL)    {
     cout << "\n\rCannot open output file.\n\r\a";
     exit(3);
}

while (!feof(in)) {
  char c=fgetc(in);
  if ( c==FF) 
  	continue;
   else
     if (c != LF ) fwrite(&c,1,1,out);
}
fclose(in);
fclose(out);
if (argc==2)  {
  unlink(argv[1]);
  rename(temp_name,argv[1]);
  }
cout << "\n\r\n\rDone!    :-) from Piotr Wroblewski\n\r";
return 0;
}
