1
0

separate out rot13(1)

This commit is contained in:
dtb
2022-09-12 23:28:28 -04:00
parent 7441fa08ea
commit 261f2d46d7
6 changed files with 99 additions and 158 deletions

26
rot13/Makefile Normal file
View File

@@ -0,0 +1,26 @@
all: rot13
clean:
rm -rf ../dist/rot13 ../dist/rot13.tar ../dist/rot13.tar.gz rot13
dist: ../dist/rot13.tar.gz
sane: rot13.c ../include/sysexits.h
$(CC) -DDONT_USE_SYSTEM_SYSEXITS -o rot13 rot13.c
rot13: rot13.c
$(CC) -o rot13 rot13.c
../dist/rot13: rot13
mkdir -p ../dist/rot13.tmp/bin/
cp rot13 ../dist/rot13.tmp/bin/rot13
mv ../dist/rot13.tmp ../dist/rot13
../dist/rot13.tar: ../dist/rot13
cd ../dist/rot13 && pax -w -x ustar . >../rot13.tar.tmp
mv ../dist/rot13.tar.tmp ../dist/rot13.tar
../dist/rot13.tar.gz: ../dist/rot13.tar
gzip -c <../dist/rot13.tar >../dist/rot13.tar.gz.tmp
mv ../dist/rot13.tar.gz.tmp ../dist/rot13.tar.gz
.PHONY: all clean sane

68
rot13/rot13.c Normal file
View File

@@ -0,0 +1,68 @@
#include <stddef.h> /* stdin, stdout */
/* When rot13(1) is a part of ~trinity/src, which doesn't assume the system has
* <sysexits.h> and generates it out of a table. */
#ifdef DONT_USE_SYSTEM_SYSEXITS
# include "../include/sysexits.h" /* EX_OK, EX_USAGE */
#else
# include <sysexits.h> /* EX_OK, EX_USAGE */
#endif
#ifdef U_UNICODE_VERSION /* libicu Unicode support */
# define CHARACTER UChar32
# include <uchar.h> /* u_isupper(3) */
# define ISUPPER u_isupper
# include <ustdio.h> /* U_EOF */
# define ENDOFFILE U_EOF
#else /* ifdef U_UNICODE_VERSION */
# define CHARACTER int
# include <ctype.h> /* isalpha(3), isupper(3) */
# define ISALPHA isalpha
# define ISUPPER isupper
# include <stdio.h> /* getc(3), putc(3) */
# define ENDOFFILE EOF
# define GETC getc
# define PUTC putc
#endif /* ifdef U_UNICODE_VERSION */
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. */
// #include <string.h> /* strchr(3), strlen(3) */
// static int
// caesar(char c, int r, const char *a){
// return a[(strchr(a, c) - a + r) % strlen(a)];
// }
/* Or... make some static arrays of the alphabet * 1.5, so c + '\0' + 13 is
* always a valid index and always reflects the correct rotation (13 places).
*/
static char alpha_upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM";
static char alpha_lower[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklm";
int main(int argc, char *argv[]){
char *a; /* easier than doing freaky math */
CHARACTER c;
if(argc > 1){
fprintf(stderr, "Usage: %s\n",
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);
return EX_OK;
}