From 7410086b60833da849b678548e4c659fc680f610 Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 4 Dec 2023 19:58:53 -0700 Subject: [PATCH] make rot13 into rot --- rot13/Makefile | 31 +++---------------- rot13/rot-ascii.c | 30 +++++++++++++++++++ rot13/rot-ascii.h | 10 +++++++ rot13/rot13.c | 76 +++++++++++++---------------------------------- 4 files changed, 65 insertions(+), 82 deletions(-) create mode 100644 rot13/rot-ascii.c create mode 100644 rot13/rot-ascii.h diff --git a/rot13/Makefile b/rot13/Makefile index 1a6603e..1b3d83a 100644 --- a/rot13/Makefile +++ b/rot13/Makefile @@ -1,28 +1,5 @@ -all: rot13 +rot-ascii.o: rot-ascii.c rot-ascii.h -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/ ../dist/rot13.tmp/share/man/man1/ - cp rot13 ../dist/rot13.tmp/bin/rot13 - cp rot13.1 ../dist/rot13.tmp/share/man/man1/rot13.1 - 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 +rot-ascii: rot-ascii.o + $(CC) $(CFLAGS) -c -o rot.o -include "rot-ascii.h" rot.c + $(CC) $(CFLAGS) -o rot-ascii rot.o rot-ascii.o diff --git a/rot13/rot-ascii.c b/rot13/rot-ascii.c new file mode 100644 index 0000000..94668f0 --- /dev/null +++ b/rot13/rot-ascii.c @@ -0,0 +1,30 @@ +#include "rot-ascii.h" + +#include /* size_t */ + +static CHAR +caesar_lower(CHAR c, size_t r){ + static char alpha_lower[] = "abcdefghijklmnopqrstuvwxyz" + "abcdefghijklmnopqrstuvwxyz"; + + return : alpha_lower[c - alpha_lower[0] + + r % 26 /* length of alphabet */]; +} + +static CHAR +caesar_upper(CHAR c, size_t r){ + static char alpha_upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + return alpha_upper[c - alpha_upper[0] + + r % 26 /* length of alphabet */]; +} + +CHAR +caesar(CHAR c, size_t r){ + return !ISALPHA(c) + ? c + : ISLOWER(c) + ? caesar_lower(c, r) + : caesar_upper(c, r); +} diff --git a/rot13/rot-ascii.h b/rot13/rot-ascii.h new file mode 100644 index 0000000..6ff2a5d --- /dev/null +++ b/rot13/rot-ascii.h @@ -0,0 +1,10 @@ +#define CHHR int +#include /* isalpha(3), islower(3), isupper(3) */ +#define ISALPHA(a) (((a) < 0x80) && isalpha(a)) +#define ISLOWER(a) (((a) < 0x80) && islower(a)) +#define ISUPPER(a) (((a) < 0x80) && isupper(a)) +#include /* getc(3), putc(3), stdin, stdout, EOF */ +#define ENDOFFILE EOF +#define GETC getc +#define PUTC putc +/* #include /* size_t */ diff --git a/rot13/rot13.c b/rot13/rot13.c index eec1117..a174a2b 100644 --- a/rot13/rot13.c +++ b/rot13/rot13.c @@ -1,71 +1,37 @@ -#include /* stdin, stdout */ - -/* When rot13(1) is a part of ~trinity/src, which doesn't assume the system has - * and generates it out of a table. */ -#ifdef DONT_USE_SYSTEM_SYSEXITS -# include "../include/sysexits.h" /* EX_OK, EX_USAGE */ -#else +#include /* errno */ +#include /* strtol(3), size_t */ +#if !defined EX_OK || !defined EX_USAGE # include /* EX_OK, EX_USAGE */ #endif -#ifdef U_UNICODE_VERSION /* libicu Unicode support */ -# define CHARACTER UChar32 +CHAR caesar(CHAR c, size_t r); -# include /* u_isupper(3) */ -# define ISUPPER u_isupper - -# include /* U_EOF */ -# define ENDOFFILE U_EOF -#else /* ifdef U_UNICODE_VERSION */ -# define CHARACTER int - -# include /* isalpha(3), isupper(3) */ -# define ISALPHA isalpha -# define ISUPPER isupper - -# include /* 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 /* 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"; +static char *program_name = "rot"; int main(int argc, char *argv[]){ + CHAR c; + size_t r; - if(argc > 1){ - fprintf(stderr, "Usage: %s\n", + if(argc > 2){ + fprintf(stderr, "Usage: %s (rotation)\n", argv[0] == NULL ? program_name : argv[0] ); return EX_USAGE; } - { /* rot13 */ - char *a; /* easier than doing freaky math */ - CHARACTER c; + if(argc == 2){ + r = strtol(argv[1], &argv[1], 10); + if(*argv[1] != '\0' || errno != 0){ + fprintf(stderr, "%s: Invalid rotation\n", argv[0]); + return EX_USAGE; + } + }else + r = 13; /* default to rot13 */ - while((c = GETC(stdin)) != ENDOFFILE) - if(isalpha(c)){ - a = isupper(c) ? alpha_upper : alpha_lower; - PUTC(a[c - a[0] + 13], stdout); - }else - PUTC(c, stdout); - } + while((c = GETC(stdin)) != ENDOFFILE) + if(PUTC(caesar(c, r), stdout) == ENDOFFILE) + break; + return EX_OK; }