summaryrefslogtreecommitdiffstats
path: root/keygen_args.c
blob: 2eafa5f584f9d3ddadb6a21602af882287b0d148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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;
}