rot13->rot
This commit is contained in:
9
rot/Makefile
Normal file
9
rot/Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
rot-ascii.o: rot-ascii.c rot-ascii.h
|
||||
|
||||
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
|
||||
|
||||
rot-unicode: rot-unicode.o
|
||||
$(CC) $(CFLAGS) -c -o rot.o -include "rot-unicode.h" rot.c
|
||||
$(CC) $(CFLAGS) -o rot-unicode rot.o rot-unicode.o
|
||||
30
rot/rot-ascii.c
Normal file
30
rot/rot-ascii.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "rot-ascii.h"
|
||||
|
||||
#include <stdlib.h> /* 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);
|
||||
}
|
||||
10
rot/rot-ascii.h
Normal file
10
rot/rot-ascii.h
Normal file
@@ -0,0 +1,10 @@
|
||||
typedef int CHAR;
|
||||
#include <ctype.h> /* 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 <stdio.h> /* getc(3), putc(3), stdin, stdout, EOF */
|
||||
#define ENDOFFILE EOF
|
||||
#define GETC getc
|
||||
#define PUTC putc
|
||||
/* #include <stdlib.h> /* size_t */
|
||||
33
rot/rot-unicode.c
Normal file
33
rot/rot-unicode.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include "rot-unicode.h"
|
||||
|
||||
#include <stdlib.h> /* size_t */
|
||||
|
||||
/* this file is essentially the same as rot-ascii.c */
|
||||
/* TODO: multilingual rot */
|
||||
|
||||
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);
|
||||
}
|
||||
13
rot/rot-unicode.h
Normal file
13
rot/rot-unicode.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#include <utypes.h> /* UChar32 */
|
||||
typedef UChar32 CHAR;
|
||||
#include <uchar.h> /* u_islower(3), u_isupper(3) */
|
||||
#include <ctype.h> /* isalpha(3) */
|
||||
#define ISALPHA(a) (((a) < 0x80) && isalpha(a))
|
||||
#define ISLOWER u_islower
|
||||
#define ISUPPER u_isupper
|
||||
#include <ustdio.h> /* u_fgetc(3), u_fputc(3), U_EOF */
|
||||
#define ENDOFFILE U_EOF
|
||||
#define GETC u_fgetc
|
||||
#define PUTC u_fputc
|
||||
#include <stdio.h> /* stdin, stdout */
|
||||
/* #include <stdlib.h> /* size_t */
|
||||
37
rot/rot.c
Normal file
37
rot/rot.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <errno.h> /* errno */
|
||||
#include <stdlib.h> /* strtol(3), size_t */
|
||||
#if !defined EX_OK || !defined EX_USAGE
|
||||
# include <sysexits.h> /* EX_OK, EX_USAGE */
|
||||
#endif
|
||||
|
||||
CHAR caesar(CHAR c, size_t r);
|
||||
|
||||
static char *program_name = "rot";
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
CHAR c;
|
||||
size_t r;
|
||||
|
||||
if(argc > 2){
|
||||
fprintf(stderr, "Usage: %s (rotation)\n",
|
||||
argv[0] == NULL ? program_name : argv[0]
|
||||
);
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
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(PUTC(caesar(c, r), stdout) == ENDOFFILE)
|
||||
break;
|
||||
|
||||
return EX_OK;
|
||||
}
|
||||
32
rot/rot13.1
Normal file
32
rot/rot13.1
Normal file
@@ -0,0 +1,32 @@
|
||||
.TH ROT13 1
|
||||
|
||||
.SH NAME
|
||||
|
||||
rot13 \- decrypt/encrypt 13-degree caesar-rotated text
|
||||
|
||||
.SH DESCRIPTION
|
||||
|
||||
.B rot13
|
||||
is a filter program that writes to standard output each Alphabet character from standard input, rotated 13 places.
|
||||
Non-Alphabet characters are ignored.
|
||||
|
||||
.SH DIAGNOSTICS
|
||||
|
||||
.B rot13
|
||||
will exit with the appropriate status from sysexits(3) if executed improperly.
|
||||
Otherwise,
|
||||
.B rot13
|
||||
will always exit with status 0, unless there are any unforeseen errors, in which case it will crash without grace.
|
||||
|
||||
|
||||
.SH BUGS
|
||||
|
||||
Not Unicode compatible.
|
||||
|
||||
.SH COPYRIGHT
|
||||
|
||||
Public domain.
|
||||
|
||||
.SH SEE ALSO
|
||||
|
||||
caesar(1)
|
||||
Reference in New Issue
Block a user