summaryrefslogtreecommitdiffstats
path: root/keygen_args.c
diff options
context:
space:
mode:
Diffstat (limited to 'keygen_args.c')
-rw-r--r--keygen_args.c115
1 files changed, 115 insertions, 0 deletions
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;
+}
+