learning experience
This commit is contained in:
commit
42d5b1fea8
1 changed files with 59 additions and 0 deletions
59
password_gen.c
Normal file
59
password_gen.c
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int str_to_int(char* str) {
|
||||||
|
int output = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < strlen(str); i++) {
|
||||||
|
output *= 10;
|
||||||
|
output += str[i] - '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
void invalid_args(int argc) {
|
||||||
|
if (argc < 4 && argc > 5) {
|
||||||
|
printf("nuhuh\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int random_len(int min_length, int max_length) {
|
||||||
|
return rand() % (max_length - min_length + 1) + min_length;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* random_str(int length, char* symbols) {
|
||||||
|
char* str = malloc(sizeof(char) * (length + 1));
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
int index = rand() % strlen(symbols);
|
||||||
|
str[i] = symbols[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
str[length] = '\0';
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
invalid_args(argc);
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
int min_length = str_to_int(argv[1]);
|
||||||
|
int max_length = str_to_int(argv[2]);
|
||||||
|
|
||||||
|
int length = random_len(min_length, max_length);
|
||||||
|
|
||||||
|
if (argc != 4) {
|
||||||
|
char* password = random_str(length, argv[4]);
|
||||||
|
puts(password);
|
||||||
|
free(password);
|
||||||
|
} else {
|
||||||
|
char* password = random_str(length, argv[3]);
|
||||||
|
puts(password);
|
||||||
|
free(password);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue