Please consider a donation to the Higher Intellect project. See https://preterhuman.net/donate.php or the Donate to Higher Intellect page for more info.

DOS2UNIX.C

From Higher Intellect Vintage Wiki
Revision as of 10:45, 23 July 2020 by Netfreak (talk | contribs) (Created page with "<pre> →‎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 *fn...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
/* 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);
}