make rot13 into rot
This commit is contained in:
parent
19438441d2
commit
7410086b60
@ -1,28 +1,5 @@
|
|||||||
all: rot13
|
rot-ascii.o: rot-ascii.c rot-ascii.h
|
||||||
|
|
||||||
clean:
|
rot-ascii: rot-ascii.o
|
||||||
rm -rf ../dist/rot13 ../dist/rot13.tar ../dist/rot13.tar.gz rot13
|
$(CC) $(CFLAGS) -c -o rot.o -include "rot-ascii.h" rot.c
|
||||||
|
$(CC) $(CFLAGS) -o rot-ascii rot.o rot-ascii.o
|
||||||
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
|
|
||||||
|
30
rot13/rot-ascii.c
Normal file
30
rot13/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
rot13/rot-ascii.h
Normal file
10
rot13/rot-ascii.h
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#define CHHR int
|
||||||
|
#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 */
|
@ -1,71 +1,37 @@
|
|||||||
#include <stddef.h> /* stdin, stdout */
|
#include <errno.h> /* errno */
|
||||||
|
#include <stdlib.h> /* strtol(3), size_t */
|
||||||
/* When rot13(1) is a part of ~trinity/src, which doesn't assume the system has
|
#if !defined EX_OK || !defined EX_USAGE
|
||||||
* <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 */
|
# include <sysexits.h> /* EX_OK, EX_USAGE */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef U_UNICODE_VERSION /* libicu Unicode support */
|
CHAR caesar(CHAR c, size_t r);
|
||||||
# define CHARACTER UChar32
|
|
||||||
|
|
||||||
# include <uchar.h> /* u_isupper(3) */
|
static char *program_name = "rot";
|
||||||
# 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[]){
|
int main(int argc, char *argv[]){
|
||||||
|
CHAR c;
|
||||||
|
size_t r;
|
||||||
|
|
||||||
if(argc > 1){
|
if(argc > 2){
|
||||||
fprintf(stderr, "Usage: %s\n",
|
fprintf(stderr, "Usage: %s (rotation)\n",
|
||||||
argv[0] == NULL ? program_name : argv[0]
|
argv[0] == NULL ? program_name : argv[0]
|
||||||
);
|
);
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
{ /* rot13 */
|
if(argc == 2){
|
||||||
char *a; /* easier than doing freaky math */
|
r = strtol(argv[1], &argv[1], 10);
|
||||||
CHARACTER c;
|
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;
|
return EX_OK;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user