Converting DOS text files

ken ken at metaware.metaware.com
Thu Oct 18 02:16:00 AEST 1990


In article <1990Oct16.134008.22319 at esegue.segue.boston.ma.us> johnl at esegue.segue.boston.ma.us (John R. Levine) writes:
>In article <1477 at pai.UUCP> erc at pai.UUCP (Eric Johnson) writes:
>>The problem is this: when you use a DOS-based copy command to copy a text
>>file onto your system (from a PC floppy, say), that DOS text file
>>is full of CR/LFs (instead of the UNIX line feed) and has a trailing
>>Ctrl-Z.  [172 line program follows]



Here's yet another solution. This closely emulates Sun's dos2unix program.

#include <stdio.h>
#include <ctype.h>
#include <errno.h>

main(argc, argv)
int argc;
char *argv[];
{
	int c;
	FILE *ifp=NULL, *ofp=NULL;
	extern void exit();

	if (argc != 3 && argc != 2 && argc != 1)
		printf("\n\tUsage:  %s [infile [outfile]]\n\n", argv[0]);
	else {
		switch (argc) {
		case 1:
			ifp = stdin;
			ofp = stdout;
			break;
		case 2:
			if ((ifp = fopen(argv[1], "r")) == NULL) {
				perror(argv[1]);
				exit(errno);
			}
			ofp = stdout;
			break;
		case 3:
			if ((ifp = fopen(argv[1], "r")) == NULL) {
				perror(argv[1]);
				exit(errno);
			}
			if ((ofp = fopen(argv[2], "w")) == NULL) {
				perror(argv[1]);
				exit(errno);
			}
			break;
		}
		while ((c = getc(ifp)) != EOF) {
			if ((c != '
				putc(c, ofp);
		}
		if (ifp != NULL)
			fclose(ifp);
		if (ofp != NULL)
			fclose(ofp);
		exit(0);
	}
	exit(-1);
}



More information about the Comp.unix.sysv386 mailing list