1
0
src/rot13/rot13.c
2022-10-16 18:43:27 -04:00

72 lines
1.9 KiB
C

#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[]){
if(argc > 1){
fprintf(stderr, "Usage: %s\n",
argv[0] == NULL ? program_name : argv[0]
);
return EX_USAGE;
}
{ /* rot13 */
char *a; /* easier than doing freaky math */
CHARACTER c;
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);
}
return EX_OK;
}