/*
 * crypt: a file encryption utility
 *
 * by Andrew Scott Beals
 * 9178 Centerway Rd.
 * Gaithersburg MD 20879
 * (301) 926-0911
 * last update->26 June 1982
 *
 */

#include "a:bdscio.h"

#ifndef FILE
#define FILE struct _buf
#endif

main(argc,argv)
int	argc;
char	*argv[]; {
	char	rotor[128];
	char	rotsiz;
	int	byte;
	char	rotptr;
	char	filout;
	FILE	input,output;

	if(argc == 2 && argv[1][0] == '-') {
		printf("crypt is a program designed to provide\n");
		printf("the user with a means to protect his data\n");
		printf("from the perusal of others, even if they\n");
		printf("can get their hands on his file\n");
		exit(1);
	}

	if(argc > 5 || argc < 2) {
		printf("usage: crypt [ -K <key> ] <input file> [ <output file> ]\nfor more information, enter: crypt -");
		exit(1);
	}

	if(argv[1][0] == '-' && argv[1][1] == 'K') {
		strcpy(rotor,argv[2]);
		argc-=3;	/* make argc == # of files spec */
		argv+=3;	/* argv now points to the first file name */
	} else {
		printf("key? ");
		scanf("%s",rotor);
		argc--;
		argv++;
	}

	if(fopen(argv[0],input) == ERROR) {
		printf("can't open %s for input",argv[0]);
		exit(1);
	}

	if(argc == 2) {		/* open up the output file */
		filout=1;
		if(fcreat(argv[1],output) == ERROR) {
			printf("can't open %s for output",argv[1]);
			exit(1);
		}
	} else
		filout=0;

	rotsiz=strlen(rotor);
	if(!rotsiz) {
		printf("funny guy...the key must be at least 1 character");
		exit(1);
	}

	rotptr=1;

	printf("%sWorking...",CLEARS);

	while((byte=getc(input)) != EOF) {
		byte ^= rotor[rotptr++];
		rotptr %= rotsiz;
		if(filout)
			putc(byte,output);
		else
			putchar(byte);
	}

	if(filout) {
		fflush(output);
		fclose(output);
	}

	exit(0);
}
   