Version 1.0
This commit is contained in:
parent
e566be4122
commit
d7c1195a1a
2 changed files with 62 additions and 20 deletions
BIN
a.out
Executable file
BIN
a.out
Executable file
Binary file not shown.
|
|
@ -3,10 +3,17 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#define error "minimal_length maximal_length options custom_characters\n"
|
||||||
|
|
||||||
int str_to_int(char* str) {
|
int str_to_int(char* str) {
|
||||||
int output = 0;
|
int output = 0;
|
||||||
|
|
||||||
for (int i = 0; i < strlen(str); i++) {
|
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 *= 10;
|
||||||
output += str[i] - '0';
|
output += str[i] - '0';
|
||||||
}
|
}
|
||||||
|
|
@ -14,6 +21,13 @@ int str_to_int(char* str) {
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void not_avaliable_options(char** argv) {
|
||||||
|
if (strcmp(argv[3], "-c") != 0) {
|
||||||
|
printf(error);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void invalid_args(int argc) {
|
void invalid_args(int argc) {
|
||||||
if (argc < 4 && argc > 5) {
|
if (argc < 4 && argc > 5) {
|
||||||
printf("nuhuh\n");
|
printf("nuhuh\n");
|
||||||
|
|
@ -37,7 +51,7 @@ char* character_list(int argc, char** argv) {
|
||||||
case 'U':
|
case 'U':
|
||||||
final_size += sizeof(uppercase) - 1;
|
final_size += sizeof(uppercase) - 1;
|
||||||
break;
|
break;
|
||||||
case 'L':
|
case 'l':
|
||||||
final_size += sizeof(lowercase) - 1;
|
final_size += sizeof(lowercase) - 1;
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
|
|
@ -46,42 +60,60 @@ char* character_list(int argc, char** argv) {
|
||||||
case 'n':
|
case 'n':
|
||||||
final_size += sizeof(numbers) - 1;
|
final_size += sizeof(numbers) - 1;
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
printf(error);
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
output = malloc(sizeof(char) * (final_size + 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++) {
|
for (int i = 0; argv[3][i] != '\0'; i++) {
|
||||||
const char* current_list;
|
|
||||||
int size;
|
|
||||||
|
|
||||||
switch (argv[3][i]) {
|
switch (argv[3][i]) {
|
||||||
case 'U':
|
case 'U':
|
||||||
current_list = uppercase;
|
current_list = uppercase;
|
||||||
size = sizeof(uppercase);
|
size = sizeof(uppercase) - 1;
|
||||||
break;
|
break;
|
||||||
case 'L':
|
case 'l':
|
||||||
current_list = lowercase;
|
current_list = lowercase;
|
||||||
size = sizeof(lowercase);
|
size = sizeof(lowercase) - 1;
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
current_list = symbols;
|
current_list = symbols;
|
||||||
size = sizeof(symbols);
|
size = sizeof(symbols) - 1;
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
current_list = numbers;
|
current_list = numbers;
|
||||||
size = sizeof(numbers);
|
size = sizeof(numbers) - 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int j = 0; j < size - 1; j++) {
|
for (int j = 0; j < size; j++) {
|
||||||
output[copy_index] = current_list[j];
|
output[last_index] = current_list[j];
|
||||||
copy_index++;
|
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;
|
return output;
|
||||||
|
|
@ -106,16 +138,26 @@ char* random_str(int length, char* symbols) {
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
invalid_args(argc);
|
invalid_args(argc);
|
||||||
|
|
||||||
|
if (argc == 5) {
|
||||||
|
not_avaliable_options(argv);
|
||||||
|
}
|
||||||
|
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
int min_length = str_to_int(argv[1]);
|
int min_length = str_to_int(argv[1]);
|
||||||
int max_length = str_to_int(argv[2]);
|
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);
|
int length = random_len(min_length, max_length);
|
||||||
|
|
||||||
if (argc == 4) {
|
char* symbols = character_list(argc, argv);
|
||||||
char* password = random_str(length, character_list(argc, argv));
|
char* password = random_str(length, symbols);
|
||||||
puts(password);
|
free(symbols);
|
||||||
free(password);
|
puts(password);
|
||||||
}
|
free(password);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue