mirror of https://github.com/dirtbags/moth.git
Nicer argument parsing in tea
This commit is contained in:
parent
d8a3e98803
commit
b08f2747f7
|
@ -1,4 +1,4 @@
|
||||||
TARGETS = bubblebabble arc4
|
TARGETS = bubblebabble arc4 tea
|
||||||
|
|
||||||
all: $(TARGETS)
|
all: $(TARGETS)
|
||||||
|
|
||||||
|
|
33
src/tea.c
33
src/tea.c
|
@ -16,27 +16,40 @@
|
||||||
|
|
||||||
#define min(a,b) (((a)<(b))?(a):(b))
|
#define min(a,b) (((a)<(b))?(a):(b))
|
||||||
|
|
||||||
int
|
|
||||||
usage(const char *prog)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "Usage: %s [-e] <PLAINTEXT\n", prog);
|
|
||||||
fprintf(stderr, "\n");
|
|
||||||
fprintf(stderr, "You must pass in a key on fd 3 or in the environment variable KEY.\n");
|
|
||||||
return EX_USAGE;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
uint8_t *buf = NULL;
|
uint8_t *buf = NULL;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
uint32_t key[4] = {0};
|
uint32_t key[4] = {0};
|
||||||
|
int encode = 0;
|
||||||
|
|
||||||
|
/* Parse args */
|
||||||
|
{
|
||||||
|
int opt;
|
||||||
|
|
||||||
|
while ((opt = getopt(argc, argv, "e")) != -1) {
|
||||||
|
switch (opt) {
|
||||||
|
case 'e':
|
||||||
|
encode = 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "Usage: %s [-e] <PLAINTEXT\n", argv[0]);
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
fprintf(stderr, "You must pass in a key on fd 3 or in the environment variable KEY.\n");
|
||||||
|
return EX_USAGE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read key */
|
||||||
{
|
{
|
||||||
char *ekey = getenv("KEY");
|
char *ekey = getenv("KEY");
|
||||||
|
|
||||||
if (ekey) {
|
if (ekey) {
|
||||||
memcpy(key, ekey, min(strlen(ekey), sizeof(key)));
|
size_t l = strlen(ekey);
|
||||||
|
|
||||||
|
memcpy(key, ekey, min(l, sizeof(key)));
|
||||||
} else {
|
} else {
|
||||||
read(3, key, sizeof(key));
|
read(3, key, sizeof(key));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue