Another Silly programming puzzle....

The bored one! hanson at mergvax
Tue Apr 11 05:21:20 AEST 1989


	Well, since so many other people sent their own rot13 decryption 
	programs, here's one I wrote a while back.  This version will let
	you do the usual piping bit as well as working from vi.  It will
	also let you specify source and destination filenames on the command
	line.

#include	<stdio.h>

#ifndef	MSDOS
#include	<sys/file.h>
#else
#include	<fcntl.h>
#include	<sys/types.h>
#include	<sys/stat.h>
#include	<io.h>
#endif

#define		BUFSIZE	1024			/* Buffer I/O to gain speed*/

main(ac,av)
char **av;
{
	char	*c,				/* Pointer to current char */
		tmpbuf[BUFSIZE];		/* I/O buffer		   */
	int	in,				/* Input file handle	   */
		out,				/* Output file handle	   */
		numbytes,			/* Number of bytes read	   */
		i,				/* Generic index	   */
		mode = 0666,			/* File permission mode	   */
		usrmask = 0;			/* File mode mask	   */

	usrmask = umask(usrmask);		/* Get users umask	   */
	umask(usrmask);				/* Reset it to its orig val*/
	mode ^= usrmask;			/* Create file permission  */

	/*
	 * Find out where I/O is coming from/going to.
	 */

	in  = (ac == 1 ? 0 : open(av[1],O_RDONLY));
	out = (ac <  3 ? 1 : open(av[2],O_CREAT|O_TRUNC|O_WRONLY,mode));
	if (in < 0 || out < 0) {
		fprintf(stderr,"%s: File access error\n",*av);
		exit(-1);
	}

	/*
	 * Let's rot away!  (Doesn't that sound morbid?!)
	 */

	while ((numbytes = read(in,tmpbuf,BUFSIZE)) > 0) {
		for (c = tmpbuf, i = 0; i < numbytes ; ++c, ++i) 
			if ((*c >= 'A') && (*c <= 'Z')) 
				*c = ((*c - 'A' + 13) % 26) + 'A';
			else if ((*c >= 'a') && (*c <= 'z'))
				*c = ((*c - 'a' + 13) % 26) + 'a';
		write(out,tmpbuf,numbytes);
	}
	close(in);
	close(out);
	exit(0);
}
-- 
Kevin Hanson		(516) 434-3071	| To know me is to discover how truly
Linotype Co. R & D  Dept.		| 	demented a person can be ;-).
425 Oser Ave.  Hauppauge, NY  11788	|
...!philabs!mergvax			|



More information about the Comp.lang.c mailing list