Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/misc/rot13.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <ctype.h>
#include <libc.h>
#include <bio.h>

Biobuf bin, bout;

static int
rot13(Biobuf *bp, char *file)
{
	long got;
	char buf[1024], *p;

	while((got = Bread(bp, buf, sizeof(buf))) > 0){
		for (p = buf; p < buf + got; p++){
			if(!isprint(*p) && !isspace(*p)){
				fprint(2, "%s: %s - '%c' bad character\n", argv0, file, *p);
			}
			if (isalpha(*p))
				if (tolower(*p) <= 'm')
					*p += 13;
				else
					*p -= 13;
		}
		Bwrite(&bout, buf, got);
	}
	return 0;
}

void
main(int argc, char *argv[])
{
	Biobuf *bp;
	
	Binit(&bout, 1, OWRITE);

	argv0 = argv[0];
	if(argc == 1){
		Binit(&bin, 0, OREAD);
		rot13(&bin, "stdin");
		exits(0);
	}

	while(argv++, --argc){
		if((bp = Bopen(*argv, OREAD)) == nil){
			fprint(2, "%s: %s - cannot open - %r\n", argv0, *argv);
			continue;
		}
		rot13(bp, *argv);
		Bterm(bp);
	}
	exits(0);
}


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.