简体   繁体   中英

How can I generate a list of all possible password combinations with argv?

I am a beginner so if I miss something vital to the code please forgive me haha. I am making a program which gets input from the console that says how many characters are going to be used, the characters (single letters), and then how long you want the password to be. I feel like I am pretty close, but every time I run the program, it seems to generate a random pattern of letters and I can't follow it.

Here is my code, including the main function.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100


int generatePassword(char* characters[], int i, char s[], int numCharacters, int passwordLength) {

        if (i==0) {
                printf("%s\n", s);
                return 0;
        }

        for (int j = 0; j < passwordLength; j++) {
                strcat(s, characters[j]);
                generatePassword(characters, i-1, s, numCharacters, passwordLength);
        }

        return 0;

}


void homeFunction(char* characters[], int numCharacters, int passwordLength) {
        for (int i = 1; i <= numCharacters; i++) {
                char s[MAX] = "";
                int c = generatePassword(characters, i, s, numCharacters, passwordLength);
        }
}


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

        if (argc < 2) {
                printf("ERROR: Program did not execute due to lack of arguments.\n");
                return -1;
        }

        int numCharacters;
        numCharacters = atoi(argv[1]);
        int passwordLength;
        passwordLength = atoi(argv[numCharacters+2]);

        for (int i = 0; i < numCharacters; i++) {
                if (strlen(argv[i+1]) > 1) {
                        printf("ERROR: You can only input one character at a time.\n");
                        return -1;
                }
        }

        if (argv != numCharacters + 3) {
                 printf("ERROR: Invalid number of arguments.\n");
                 return -1;
        }

        char *charArray[numCharacters];
        charArray[numCharacters] = (char*)malloc(numCharacters * sizeof(char));

        for (int i = 0; i < numCharacters; i++) {
                charArray[i] = argv[i+2];
        }

        homeFunction(charArray, numCharacters, passwordLength);

        return 0;

}

In theory, if the user ran the program with "./NAME 2 ab 2" the result should be

a
b
aa
ab
ba
bb

This is my current output. How can I make it look like the output above?

a
ab
abaa
abaab
abaabba
abaabbab

You need to learn the fundamentals of arrays and strings.

Imagine if I run your code as app.exe 99

This is undefined behavior:

    numCharacters = atoi(argv[1]);
    int passwordLength;
    passwordLength = atoi(argv[numCharacters+2]);

If argv[1] is say, "99" , then numCharacters will be 99 .

Then passwordLength will be assigned the atoi conversion of argv[101] . But there's only two valid elements in argv : argv[0] , which is the executable path, and argv[1] , which is "99" . argv[2] and up is random memory that's not yours to look at.

This is also wrong:

    char *charArray[numCharacters];
    charArray[numCharacters] = (char*)malloc(MAX * sizeof(char));

You are allocating an array of (string) pointers, then assigning to an index one past the valid range of the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM