Version 1.0

This commit is contained in:
benstrb 2025-11-01 20:52:03 +01:00
parent e566be4122
commit d7c1195a1a
2 changed files with 62 additions and 20 deletions

BIN
a.out Executable file

Binary file not shown.

View file

@ -3,10 +3,17 @@
#include <time.h>
#include <string.h>
#define error "minimal_length maximal_length options custom_characters\n"
int str_to_int(char* str) {
int output = 0;
for (int i = 0; i < strlen(str); i++) {
if (str[i] < '0' || str[i] > '9') {
printf("That is not a number.\n");
exit(1);
}
output *= 10;
output += str[i] - '0';
}
@ -14,6 +21,13 @@ int str_to_int(char* str) {
return output;
}
void not_avaliable_options(char** argv) {
if (strcmp(argv[3], "-c") != 0) {
printf(error);
exit(1);
}
}
void invalid_args(int argc) {
if (argc < 4 && argc > 5) {
printf("nuhuh\n");
@ -37,7 +51,7 @@ char* character_list(int argc, char** argv) {
case 'U':
final_size += sizeof(uppercase) - 1;
break;
case 'L':
case 'l':
final_size += sizeof(lowercase) - 1;
break;
case 's':
@ -46,42 +60,60 @@ char* character_list(int argc, char** argv) {
case 'n':
final_size += sizeof(numbers) - 1;
break;
default:
printf(error);
exit(1);
}
}
output = malloc(sizeof(char) * (final_size + 1));
int copy_index = 0;
int last_index = 0;
const char* current_list;
int size;
for (int i = 0; argv[3][i] != '\0'; i++) {
const char* current_list;
int size;
switch (argv[3][i]) {
case 'U':
current_list = uppercase;
size = sizeof(uppercase);
size = sizeof(uppercase) - 1;
break;
case 'L':
case 'l':
current_list = lowercase;
size = sizeof(lowercase);
size = sizeof(lowercase) - 1;
break;
case 's':
current_list = symbols;
size = sizeof(symbols);
size = sizeof(symbols) - 1;
break;
case 'n':
current_list = numbers;
size = sizeof(numbers);
size = sizeof(numbers) - 1;
break;
}
for (int j = 0; j < size - 1; j++) {
output[copy_index] = current_list[j];
copy_index++;
for (int j = 0; j < size; j++) {
output[last_index] = current_list[j];
last_index++;
}
output[final_size] = '\0';
}
output[final_size] = '\0';
}
else if (argc == 5) {
int final_size = 0;
for (int i = 0; argv[4][i] != '\0'; i++) {
final_size++;
}
output = malloc(sizeof(char) * (final_size + 1));
for (int i = 0; argv[4][i] != '\0'; i++) {
output[i] = argv[4][i];
}
output[final_size] = '\0';
}
return output;
@ -106,16 +138,26 @@ char* random_str(int length, char* symbols) {
int main(int argc, char** argv) {
invalid_args(argc);
if (argc == 5) {
not_avaliable_options(argv);
}
srand(time(NULL));
int min_length = str_to_int(argv[1]);
int max_length = str_to_int(argv[2]);
if (min_length > max_length) {
printf("Your minimal length is longer than your maximal length.\n");
exit(1);
}
int length = random_len(min_length, max_length);
if (argc == 4) {
char* password = random_str(length, character_list(argc, argv));
puts(password);
free(password);
}
char* symbols = character_list(argc, argv);
char* password = random_str(length, symbols);
free(symbols);
puts(password);
free(password);
}