diff --git a/Makefile b/Makefile index 9cc73ad..bce0cd4 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ cleanprograms: $(RM) bin/multiply $(RM) bin/nonzero $(RM) bin/pscat + $(RM) bin/rot13 $(RM) bin/simexec $(RM) bin/sleep $(RM) bin/streq @@ -35,6 +36,7 @@ cleanprograms: $(RM) bin/rldecode $(RM) bin/rlencode $(RM) bin/roll + $(RM) bin/roll_stdio $(RM) bin/tail $(RM) bin/true @@ -109,6 +111,12 @@ roll_stdio.o: lib/libio.h src/roll.c sysexits roll_stdio: libio sysexits roll_stdio.o $(CC) $(CFLAGS) -o bin/roll_stdio build/libio.o build/roll_stdio.o +rot13.o: libio sysexits src/rot13.c + $(CC) $(CFLAGS) -c -o build/rot13.o src/rot13.c + +rot13: libio rot13.o + $(CC) $(CFLAGS) -o bin/rot13 build/libio.o build/rot13.o + rldecode.o: sysexits src/runlength.c $(CC) $(CFLAGS) -Df=decode -c -o build/rldecode.o src/runlength.c diff --git a/src/rot13.c b/src/rot13.c new file mode 100644 index 0000000..cc99dc1 --- /dev/null +++ b/src/rot13.c @@ -0,0 +1,41 @@ +#include /* isalpha(3), isupper(3) */ +#include /* getc(3), putc(3) */ +#include /* stdin, stdout, EOF */ +#include /* strchr(3), strlen(3) */ +#include /* EX_OK, EX_USAGE */ +#include /* write(2) */ +#include "libio.h" /* fdputs(3) */ +#include "usefulmacros.h" /* ARRAYLEN */ + +static char *program_name = "rot13"; + +/* Assumes this is a character in the provided glyph system. + * Probably not Unicode-capable but doesn't assume Alphabet. + * Returns `c` rotated around `a` by `r` characters. */ +static int +caesar(char c, int r, const char *a){ + return a[(strchr(a, c) - a + r) % strlen(a)]; +} + +/* It's faster to assume Alphabet. */ +static char alpha_upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM"; +static char alpha_lower[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklm"; + +int main(int argc, char *argv[]){ + char *a; + int c; + + if(argc > 1){ + write(2, "Usage: ", 7); + fdputs(2, argv[0] == NULL ? program_name : argv[0]); + return EX_USAGE; + } + + while((c = getc(stdin)) != EOF){ + if(isalpha(c)){ + a = isupper(c) ? alpha_upper : alpha_lower; + putc(a[c - a[0] + 13], stdout); + }else + putc(c, stdout); + } +}