summaryrefslogtreecommitdiffstats
path: root/crypt_args.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'crypt_args.cpp')
-rw-r--r--crypt_args.cpp195
1 files changed, 195 insertions, 0 deletions
diff --git a/crypt_args.cpp b/crypt_args.cpp
new file mode 100644
index 0000000..7c1dc67
--- /dev/null
+++ b/crypt_args.cpp
@@ -0,0 +1,195 @@
+/*
+ * 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 = 80;
+ char *line_ptr = (char *) malloc(sizeof(char) * line_sz);
+ if (!line_ptr)
+ {
+ perror("malloc");
+ exit(EXIT_FAILURE);
+ }
+
+ 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;
+}
+