diff options
-rw-r--r-- | class.c | 2 | ||||
-rw-r--r-- | keygen_args.c | 115 | ||||
-rw-r--r-- | keygen_args.h | 16 |
3 files changed, 132 insertions, 1 deletions
@@ -7,7 +7,7 @@ void printS(char *str) void printF(float *f) { - printf("%f\n", f); + printf("%f\n", *f); } struct GLFrame diff --git a/keygen_args.c b/keygen_args.c new file mode 100644 index 0000000..6c12e9c --- /dev/null +++ b/keygen_args.c @@ -0,0 +1,115 @@ +/* + * keygen_args.c + * + * + */ + +#include "keygen_args.h" + +char *pubkey; +char *prikey; + +int keygen_args(int argc, char **argv) +{ + int fname_f = 0; + int fname_arg = 0; + int random_f = 0; + int usage_f = 0; + int opt_args = 0; + /* 1: -o was first, 0: -c was first */ + int first_f = 0; + + int i; + for (i = 1; i < argc; i++) + { + if (argc > 5) + { + fprintf(stderr, "too many options options\n"); + usage_f = 1; + break; + } + + /* skip non option arguments */ + if (!strchr(argv[i], '-')) + { + opt_args++; + if (!fname_arg) + fname_arg = i; + + continue; + } + + else if (strcmp(argv[i], "-o") == 0) + { + if (i == 1) + first_f = 1; + + fname_f = 1; + } + else if (strcmp(argv[i], "-c") == 0) + { + if (i == 1) + first_f = 0; + + random_f = 1; + } + else + { + fprintf(stderr, "unknown option \"%s\"\n", argv[i]); + usage_f = 1; + break; + } + } + + if (usage_f || opt_args > 2 || opt_args == 1) + { + printf("usage: %s [-o pubkey prikey] [-c]\n", argv[0]); + exit(EXIT_FAILURE); + } + + if (opt_args == 2) + { + pubkey = strdup(argv[fname_arg]); + prikey = strdup(argv[fname_arg+1]); + } + else if (fname_f) /* opt_args equals 0 here */ + { + /* ask user for the filenames */ + puts("please provide pubkey and prikey filenames separated by a space"); + printf("e.g. \"pubkey.xml prikey.xml\": "); + + size_t line_sz = 80; + char *line_ptr = (char *) malloc(sizeof(char) * line_sz); + pubkey = (char *) malloc(sizeof(char) * 100); + prikey = (char *) malloc(sizeof(char) * 100); + if (!line_ptr || !pubkey || !prikey) + { + perror("malloc"); + exit(EXIT_FAILURE); + } + + ssize_t amount_read = 0; + int args_parsed = 0; + do + { + fflush(stdin); + amount_read = getline(&line_ptr, &line_sz, stdin); + args_parsed = sscanf(line_ptr, "%s %s", pubkey, prikey); + if (args_parsed != 2) + fprintf(stderr, "invalid input, please try again: "); + } while (args_parsed != 2); + } + else + { + pubkey = strdup("pubkey.xml"); + prikey = strdup("prikey.xml"); + } + +#ifdef DEBUG + fprintf(stdout, "debug: pubkey = \"%s\"\n", pubkey); + fprintf(stdout, "debug: prikey = \"%s\"\n", prikey); +#endif + + return 0; +} + diff --git a/keygen_args.h b/keygen_args.h new file mode 100644 index 0000000..c1144e5 --- /dev/null +++ b/keygen_args.h @@ -0,0 +1,16 @@ +#ifndef _KEYGEN_ARGS_ +#define _KEYGEN_ARGS_ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* global variables */ +extern char *pubkey; +extern char *prikey; + +/* function protypes */ +int keygen_args(int, char **); + +#endif + |