rot13(1)
This commit is contained in:
parent
c838c0b304
commit
8c55735e67
8
Makefile
8
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
|
||||
|
||||
|
41
src/rot13.c
Normal file
41
src/rot13.c
Normal file
@ -0,0 +1,41 @@
|
||||
#include <ctype.h> /* isalpha(3), isupper(3) */
|
||||
#include <stdio.h> /* getc(3), putc(3) */
|
||||
#include <stddef.h> /* stdin, stdout, EOF */
|
||||
#include <string.h> /* strchr(3), strlen(3) */
|
||||
#include <sysexits.h> /* EX_OK, EX_USAGE */
|
||||
#include <unistd.h> /* 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user