/* Converts DOS text file to UNIX/AMIGA format by removing CR's */


#include <stdio.h>
#include <conio.h>
#include <dir.h>
#include <alloc.h>
#include <io.h>


char *fnames[1024];


main(int argc,char **argv)
{
       static struct ffblk ffblk;
       int i, done, num_found = 0;

       if(argc < 2) {
               printf("DOS2UNIX  converts DOS textfiles to UNIX/Amiga format\n");
               printf("usage: dos2unix <DOSFile>\n");
               printf("Wildcards allowed\n");
               exit(0);
       }

       done = findfirst(argv[1], &ffblk, 0);
       while (done == 0) {
               fnames[num_found] = malloc(strlen(ffblk.ff_name) + 1);
               strcpy(fnames[num_found++], ffblk.ff_name);
               done = findnext(&ffblk);
       }

       for(i = 0; i < num_found; i++)
               convert(fnames[i]);

       printf("OK.\n");

       for(i = 0; i < num_found; i++)
               free(fnames[i]);
}


convert(char *dosfname)
{
       static unsigned char buffer[32768];
       FILE *unixf, *dosf;
       unsigned i, numread;

       dosf = fopen(dosfname, "rb");
       unixf = fopen("DOSTEMP.$$$", "wb");

       printf("Converting %s...\n", dosfname);

       do {
               numread = fread(buffer, 1, sizeof(buffer), dosf);
               for(i = 0; i < numread; i++) {
                       if(buffer[i] != 0x0D)
                               fwrite(&buffer[i], 1, 1, unixf);
               }
       } while(numread == sizeof(buffer));

       fclose(unixf);
       fclose(dosf);

       remove(dosfname);
       rename("DOSTEMP.$$$", dosfname);
}