This commit is contained in:
tola
2023-03-30 16:14:52 +07:00
parent 23f6e4b88d
commit 0ed01fb1a1
+59 -59
View File
@@ -10,17 +10,17 @@
#define ALPHABET "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM[]{}<>()*#"
#define HELP_MESSAGE "\tpwdgen <quantity> - generates a password of the given size from the standard alphabet\n"\
"\tpwdgen <alphabet> - generates four characters from the given alphabet\n"\
"\tpwdgen <alphabet> <quantity> - generates a password of the given length from the given alphabet\n"\
"\tpwdgen - generates a four character password from the standard alphabet\n"
"\tpwdgen <alphabet> - generates four characters from the given alphabet\n"\
"\tpwdgen <alphabet> <quantity> - generates a password of the given length from the given alphabet\n"\
"\tpwdgen - generates a four character password from the standard alphabet\n"
char *get_random_string(const char *alphabet, int quantity) {
srand(time(NULL));
char *str = malloc(sizeof(char*) * quantity);
for (int i = 0; i < quantity; i++) {
str[i] = alphabet[0+rand()%strlen(alphabet)];
}
return str;
srand(time(NULL));
char *str = malloc(sizeof(char *) * quantity);
for (int i = 0; i < quantity; i++) {
str[i] = alphabet[0 + rand() % strlen(alphabet)];
}
return str;
}
// char *get_file_extension(char *filename) {
@@ -49,56 +49,56 @@ char *get_random_string(const char *alphabet, int quantity) {
// }
int main(int argc, char *argv[]) {
if (argc == 2) {
if (!strcmp(argv[1], "-h")) {
printf("%s\n", HELP_MESSAGE);
return 0;
} else if (atoi(argv[1]) > 0) {
int quantity = atoi(argv[1]);
char *pwd = get_random_string(ALPHABET, quantity);
printf("Password -> %s\n", pwd);
return 0;
} else if (atoi(argv[1]) == 0) {
// char *file_extension = get_file_extension(argv[1]);
// printf("%s\n", file_extension);
// printf("%s\n%d\n%d\n", argv[1], strcmp(file_extension, ""), strcmp(file_extension, "txt"));
// if (!strcmp(file_extension, "txt")) {
// FILE *alphabet_file = fopen(argv[1], "r");
// if (alphabet_file == NULL) {
// printf("Can't open file\n");
// return 1;
// }
// int64_t size = getFileSize(alphabet_file);
// // printf("%ld\n", size);
// char alphabet[size];
// fgets(alphabet, size+1, alphabet_file);
// for (int i = 0; i < size; i++){
// if (alphabet[i] == '\n') {
// printf("nononono");
// }else {
// printf("%c\n", alphabet[i]);
// }
// }
// fclose(alphabet_file);
// char *pwd = get_random_string(alphabet, 4);
// printf("Password -> %s\n", pwd);
// }
// printf("%s\n", get_file_extension(argv[1]));
char *pwd = get_random_string(argv[1], 4);
printf("Password -> %s\n", pwd);
return 0;
}
}
if (argc == 3) {
int quantity = atoi(argv[2]);
char *alphabet = argv[1];
char *pwd = get_random_string(alphabet, quantity);
printf("Password -> %s\n", pwd);
return 0;
}
char *pwd = get_random_string(ALPHABET, 4);
printf("Password -> %s\n", pwd);
return 0;
if (argc == 2) {
if (!strcmp(argv[1], "-h")) {
printf("%s\n", HELP_MESSAGE);
return 0;
} else if (atoi(argv[1]) > 0) {
int quantity = atoi(argv[1]);
char *pwd = get_random_string(ALPHABET, quantity);
printf("Password -> %s\n", pwd);
return 0;
} else if (atoi(argv[1]) == 0) {
// char *file_extension = get_file_extension(argv[1]);
// printf("%s\n", file_extension);
// printf("%s\n%d\n%d\n", argv[1], strcmp(file_extension, ""), strcmp(file_extension, "txt"));
// if (!strcmp(file_extension, "txt")) {
// FILE *alphabet_file = fopen(argv[1], "r");
// if (alphabet_file == NULL) {
// printf("Can't open file\n");
// return 1;
// }
// int64_t size = getFileSize(alphabet_file);
// // printf("%ld\n", size);
// char alphabet[size];
// fgets(alphabet, size+1, alphabet_file);
// for (int i = 0; i < size; i++){
// if (alphabet[i] == '\n') {
// printf("nononono");
// }else {
// printf("%c\n", alphabet[i]);
// }
// }
// fclose(alphabet_file);
// char *pwd = get_random_string(alphabet, 4);
// printf("Password -> %s\n", pwd);
// }
// printf("%s\n", get_file_extension(argv[1]));
char *pwd = get_random_string(argv[1], 4);
printf("Password -> %s\n", pwd);
return 0;
}
}
if (argc == 3) {
int quantity = atoi(argv[2]);
char *alphabet = argv[1];
char *pwd = get_random_string(alphabet, quantity);
printf("Password -> %s\n", pwd);
return 0;
}
char *pwd = get_random_string(ALPHABET, 4);
printf("Password -> %s\n", pwd);
return 0;
}
// int64_t getFileSize(const char* file_name){