summaryrefslogtreecommitdiffstats
path: root/crypt_args.cpp
blob: 3fde418aa874644c7468c51ee603556645752719 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * crypt_args.cpp
 *
 * Notes: since argv[] array contains c-style char strings, I chose to use C
 *        functions to work on them instead of dealing with C++ class string
 *        conversions.
 *
 */

#include "crypt_args.h"

char *fname;
char *keyfname;
char *outfname;

int crypt_args(int argc, char **argv)
{
    /* flags */
    int fname_f         = 0;
    int keyfname_f      = 0;
    int resultfile_f    = 0;
    int usage_f         = 0;
    int fname_args      = 0;
    int keyfname_args   = 0;
    int resultfile_args = 0;
    int opt_args        = 0;

    int i;
    for (i = 1; i < argc; i++)
    {
        if (argc > 7)
        {
            fprintf(stderr, "too many options\n");
            usage_f = 1;
            break;
        }

        /* skip non option arguments */
        if (!strchr(argv[i], '-'))
        {
            opt_args++;

            if (fname_f)
            {
                if (i-1 == fname_f)
                    fname_args++;

                /* not allowed to have 2 args */
                if ((i-2 == fname_f) && argv[i-1][0] != '-')
                {
                    fprintf(stderr, "too many arguments to \"-f\" option\n");
                    usage_f = 1;
                    break;
                }
            }

            if (keyfname_f)
            {
                if (i-1 == keyfname_f)
                    keyfname_args++;

                /* not allowed to have 2 args */
                if ((i-2 == keyfname_f) && argv[i-1][0] != '-')
                {
                    fprintf(stderr, "too many arguments to \"-k\" option\n");
                    usage_f = 1;
                    break;
                }
            }

            if (resultfile_f)
            {
                if (i-1 == resultfile_f)
                    resultfile_args++;

                /* not allowed to have 2 args */
                if ((i-2 == resultfile_f) && argv[i-1][0] != '-')
                {
                    fprintf(stderr, "too many arguments to \"-o\" option\n");
                    usage_f = 1;
                    break;
                }
            }

            continue;
        }
        else if (strcmp(argv[i], "-f") == 0)
            fname_f = i;
        else if (strcmp(argv[i], "-k") == 0)
            keyfname_f = i;
        else if (strcmp(argv[i], "-o") == 0)
            resultfile_f = i;
        else
        {
            fprintf(stderr, "unknown option \"%s\"\n", argv[i]);
            usage_f = 1;
            break;
        }
    }

    /* error checking */
    if (usage_f || opt_args > 3 || (fname_f && !fname_args) || (resultfile_f &&
        !resultfile_args) || (keyfname_f && !keyfname_args) || (!fname_f && 
        !keyfname_f && !resultfile_f && opt_args))
    {
        printf("usage: %s [-f] file [-k] key [-o ] output\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* used for line reading */
    ssize_t amount_read = 0;
    int args_parsed = 0;
    size_t line_sz = 0;
    char *line_ptr = NULL;

    if (fname_f)
        fname = strdup(argv[fname_f+1]);
    else
    {
        /* ask user for the filename */
        printf("please provide the file to be encrypted/decrypted: ");
        fname = (char *) malloc(sizeof(char) * 100);
        if (!fname)
        {
            perror("malloc");
            exit(EXIT_FAILURE);
        }

        do
        {
            fflush(stdin);
            amount_read = getline(&line_ptr, &line_sz, stdin);
            args_parsed = sscanf(line_ptr, "%s", fname);
            if (args_parsed != 1)
                fprintf(stderr, "invalid input, please try again: ");
        } while (args_parsed != 1);
    }

    if (keyfname_f)
        keyfname = strdup(argv[keyfname_f+1]);
    else
    {
        /* ask user for the filename */
        printf("please provide the file that contains the key: ");
        keyfname = (char *) malloc(sizeof(char) * 100);
        if (!keyfname)
        {
            perror("malloc");
            exit(EXIT_FAILURE);
        }

        do
        {
            fflush(stdin);
            amount_read = getline(&line_ptr, &line_sz, stdin);
            args_parsed = sscanf(line_ptr, "%s", keyfname);
            if (args_parsed != 1)
                fprintf(stderr, "invalid input, please try again: ");
        } while (args_parsed != 1);
    }

    if (resultfile_f)
        outfname = strdup(argv[resultfile_f+1]);
    else
    {
        /* ask user for the filename */
        printf("please specify the output file: ");
        outfname = (char *) malloc(sizeof(char) * 100);
        if (!outfname)
        {
            perror("malloc");
            exit(EXIT_FAILURE);
        }

        do
        {
            fflush(stdin);
            amount_read = getline(&line_ptr, &line_sz, stdin);
            args_parsed = sscanf(line_ptr, "%s", outfname);
            if (args_parsed != 1)
                fprintf(stderr, "invalid input, please try again: ");
        } while (args_parsed != 1);
    }

    if (line_ptr)
        free(line_ptr);

    return 0;
}