/* Converts UNIX type text file to DOS format by adding CR's to the LF'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("UNIX2DOS  converts UNIX/Amiga type textfiles to DOS format\n");
               printf("usage: unix2dos <UNIXFile>\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 *unixfname)
{
       static unsigned char buffer[32768];
       FILE *unixf, *dosf;
       unsigned i, numread;

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

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

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

       fclose(unixf);
       fclose(dosf);

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