Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!bionet!ames!pacbell!att!chinet!mcdchg!clyde!motown!mergvax!hanson From: hanson@mergvax (The bored one!) Newsgroups: comp.lang.c Subject: Re: Another Silly programming puzzle.... Summary: My Rot13 decryption program in C Keywords: contest silly Message-ID: <5406@mergvax> Date: 10 Apr 89 19:21:20 GMT References: <1210@microsoft.UUCP> <2714@ritcsh.UUCP> Organization: Linotype Co., Hauppauge NY Lines: 68 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 #ifndef MSDOS #include #else #include #include #include #include #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 |