From d16925e3b7910735586a36d6f197f624783dbfd3 Mon Sep 17 00:00:00 2001 From: Seppia Date: Thu, 9 Jun 2016 17:16:10 +0200 Subject: added option and help to code --- onetimebluh.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 10 deletions(-) diff --git a/onetimebluh.c b/onetimebluh.c index f838db3..b8f3f8c 100644 --- a/onetimebluh.c +++ b/onetimebluh.c @@ -1,22 +1,75 @@ #include #include +#include + +void xor(int ac, char* av[]); +void help(char* av[]); int main(int argc, char* argv[]) { - FILE* uno = fopen(argv[1], "r"); - FILE* due = fopen(argv[2], "r"); - FILE* tre = fopen(argv[3], "w"); + if (atoi(argv[1]) == 1){ + xor(argc, argv); + printf("Message successfully encrypted! \n"); + } else if (atoi(argv[1]) == 2) { + printf("Working \n"); + } else { + printf("WARNING no command specified!! \n"); + help(argv); + fprintf(stderr,"Exiting... \n"); + exit(EXIT_FAILURE); + } + + exit(EXIT_SUCCESS); +} + +void xor(int ac, char* av[]) { + + int opt; + int defout = 0; + char* output; + while ((opt = getopt(ac, av, "o:")) != -1) { + switch (opt) { + case 'o': + output = av[(optind-1)]; + defout = 1; + break; + default: + fprintf(stderr,"Usage %s %s [-o output] \n", av[0], av[1]); + exit(EXIT_FAILURE); + } + } + + if (defout == 0) { + printf("WARNING no command specified using default value critt \n"); + output = "critt"; + } + FILE* mess_uno = fopen(av[2], "r"); + FILE* mess_due = fopen(av[3], "r"); + FILE* critt = fopen(output, "w"); int i = 1; char a, b; while(i != EOF) { - i = fscanf(uno, "%c", &a); - fscanf(due, "%c", &b); + i = fscanf(mess_uno, "%c", &a); + fscanf(mess_due, "%c", &b); if(i != EOF) { - fprintf(tre, "%c", a^b); + fprintf(critt, "%c", a^b); } } - fclose(uno); - fclose(due); - fclose(tre); + fclose(mess_uno); + fclose(mess_due); + fclose(critt); + + return; +} + +void help(char* av[]) { + printf("ONETIMEBLUH USAGE: \n"); + printf("%s [COMMAND] [OPTIONS] \n \n", av[0]); + printf("COMMANDS: \n"); + printf("1 -> encrypt encrypt message (argv[1]) using key (argv[2]) \n"); + printf("2 -> keygen create key file \n \n"); + printf("OPTIONS \n"); + printf("-b number of bytes \n"); + printf("-o output name \n"); - return 0; + return; } -- cgit v1.2.3