Program constantly crashes requiring an argument count of at least 3

Brian Ho Source

Im suppose to write a program that validates CLI command. My program sort of works when there are two parameters but when i add the third, it crashes. I've been stuck for hours.Your help will be greatly appreciated! :D

Commands

This is what im supposed to do. Instructor recommended me to break my solution into 3 functions:

  • validate command: this function takes the command line arguments and validates that they represent a valid command for the system. Return a Boolean indicating if the command was valid or not.

  • print help: this function prints a complete usage (help) message for the user

  • main: the main function uses the above to validate the command, and then perform an n-way branch or switch to select the correct option and print a suitable message.

Ive put a comment block in the validArgue function trying to isolate and debug it

Heres my code

/*
 * Assignment 2 Encryption part 1
 * Author : Brian HO
 * Date: 12/02/2018
 *
 */

 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <string.h>

 bool validArgue(char* input, int length,char* string);
 bool helpManual(char* input);

     int main(int argc, char* argv[]) {

    char* input = argv[1];
    int length = atoi(argv[2]);
    char* string = argv[2];

    if(helpManual(input)) {
        printf("\nUsage:   encrypt -k 123\n   or:   encrypt -f textfile.txt\n   or:   enctypt -e/d textfile.txt keyfile.txt\n\n\tOptions:\n\t\t-h\t\t\tPrint this help manual.\n\t\t-k <length>\t\tGenerate encryption key for given length (length > 0).\n\t\t-f <filename>\t\tGenerate frequency histogram for filename.\n\t\t-e <filename> <keyfile> Encrypt cleartxt in filename with given keyfile.\n\t\t-d <filename> <keyfile> Decipher ciphertext filename with given keyfile.\n\n");
    }
    if(validArgue(input,length,string)){
        printf("Command is valid!");
    }
    else {
        printf("Invalid arguments...\n\nUsage:   encrypt -k 123\n   or:   e 
    ncrypt -f textfile.txt\n   or:   enctypt -e/d textfile.txt 
    keyfile.txt\n\n\tOptions:\n\t\t-h\t\t\tPrint this help manual.\n\t\t-k 
    <length>\t\tGenerate encryption key for given length (length > 0).\n\t\t-f 
    <filename>\t\tGenerate frequency histogram for filename.\n\t\t-e <filename> 
    <keyfile> Encrypt cleartxt in filename with given keyfile.\n\t\t-d 
    <filename> <keyfile> Decipher ciphertext filename with given keyfile.");
    }
    return 0;
}


bool helpManual(char* input) {
    char ch1 = input[0];


    char ch2 = input[1];
    if(ch1 == '-' && ch2 == 'h'){
        return true;
    }
    else {
        return false;
     }
 }

bool validArgue(char* input, int length, char* string){
    char ch = string[1];
    if (strcmp(input, "-h")==0){
        return true;
    }
    else if (strcmp(input, "-k")==0) {
        if (length >0) {
            return true;
        }
        else 
            return false;
    }
    else if (strcmp(input, "-f")==0) {
        if (ch>0) {
            return true;
        }
        else
            return false;
    }
      else 
        return false;
}
c

Answers

comments powered by Disqus