From 84180756da688591de8324733d344bd14d15ddef Mon Sep 17 00:00:00 2001 From: Joursoir Date: Thu, 1 Oct 2020 21:30:15 +0300 Subject: a little bit chore --- main.c | 116 +++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 55 insertions(+), 61 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index c83cea8..20dee39 100644 --- a/main.c +++ b/main.c @@ -28,17 +28,17 @@ static void clearStdinBuff() ; } -static char* get_password(char *path, char *password) +static char* getPassword(char *path, char *password) { FILE *filePass; filePass = fopen(path, "r"); if(filePass == NULL) { - call_error(110); + callError(110); } char sign[MAXLEN_PASSWORD]; if(!fgets(sign, sizeof(sign), filePass)) { - call_error(111); + callError(111); } strcpy(password, sign); @@ -46,12 +46,12 @@ static char* get_password(char *path, char *password) return password; } -static void show_tree(char *path) +static void showTree(char *path) { if(opendir(path) != NULL) { // if it's directory int pid; pid = fork(); - if(pid == -1) call_error(101); + if(pid == -1) callError(101); if(pid == 0) { /* new process */ execlp("tree", "tree", "-C", "--noreport", path, NULL); perror("tree"); @@ -61,7 +61,7 @@ static void show_tree(char *path) } else { char password[MAXLEN_PASSWORD]; - get_password(path, password); + getPassword(path, password); printf("%s\n", password); } } @@ -73,11 +73,11 @@ static void changePass(char *root_path, char *add_path, char *password) main_path = banks/france file_path = [number] */ - char *main_path = malloc(sizeof(char) * strlen(add_path) + 1); - char *file_path = malloc(sizeof(char) * strlen(add_path) + 1); + char *main_path = malloc(sizeof(char) * strlen(add_path) + sizeof(char)); + char *file_path = malloc(sizeof(char) * strlen(add_path) + sizeof(char)); if(splitPath(add_path, main_path, file_path) == NULL) { - print_error("[Error] The path you specified is incorrect\n"); + printError("lpass: The path you specified is incorrect\n"); } int pass_buf = strlen(root_path) + strlen(add_path); @@ -88,16 +88,16 @@ static void changePass(char *root_path, char *add_path, char *password) strcat(final_path, main_path); if(chdir(final_path) != 0) { - call_error(107); + callError(107); } FILE *filePass; filePass = fopen(file_path, "w"); if(filePass == NULL) { if(errno == ENOENT) { // file doesn't exist - print_error("Error: No such file exists\n"); + printError("lpass: No such file exists\n"); } - call_error(114); + callError(114); } fputs(password, filePass); @@ -117,7 +117,7 @@ static void insertPass(char *root_path, char *add_path, char *password) char *file_path = malloc(sizeof(char) * strlen(add_path) + 1); if(splitPath(add_path, main_path, file_path) == NULL) { - print_error("[Error] The path you specified is incorrect\n"); + printError("lpass: The path you specified is incorrect\n"); } int pass_buf = strlen(root_path) + strlen(add_path); @@ -128,7 +128,7 @@ static void insertPass(char *root_path, char *add_path, char *password) strcat(final_path, main_path); int pid = fork(); - if(pid == -1) call_error(103); + if(pid == -1) callError(103); if(pid == 0) { /* new process */ execlp("mkdir", "mkdir", "-p", main_path, NULL); perror("mkdir"); @@ -137,14 +137,14 @@ static void insertPass(char *root_path, char *add_path, char *password) wait(&pid); if(chdir(final_path) != 0) { - call_error(107); + callError(107); } // create file, copy password there FILE *filePass; filePass = fopen(file_path, "w"); if(filePass == NULL) { - call_error(108); + callError(108); } fputs(password, filePass); @@ -153,7 +153,7 @@ static void insertPass(char *root_path, char *add_path, char *password) fclose(filePass); } -static char *generate_password(char *dest, int amount) +static char *generatePassword(char *dest, int amount) { char allowed_symbols[] = { 'A','E','I','J','O','U','B','C','D','F','G','H', @@ -180,7 +180,7 @@ static char *generate_password(char *dest, int amount) return dest; } -static void nonvisible_enter(int status) +static void nonvisibleEnter(int status) { struct termios term_settings; tcgetattr(0, &term_settings); // get current settings @@ -193,7 +193,7 @@ static void nonvisible_enter(int status) tcsetattr(0, TCSANOW, &term_settings); } -static int get_answer(char *text) +static int getAnswer(char *text) { char answer; printf("%s\n", text); @@ -214,7 +214,7 @@ static int get_answer(char *text) } case EOF: { - print_error("Unexpected end of file\n"); + printError("lpass: Unexpected end of file\n"); } default: { @@ -231,14 +231,14 @@ static char *typePass(char *text, char *dest) { printf("%s", text); if(fgets(dest, (sizeof(char)*MAXLEN_PASSWORD + 1), stdin) == NULL) { - nonvisible_enter(0); - print_error("Unexpected end of file\n"); + nonvisibleEnter(0); + printError("lpass: Unexpected end of file\n"); } int len = strlen(dest); if(len < 2) { - nonvisible_enter(0); - print_error("Error: uncorrect password\n"); + nonvisibleEnter(0); + printError("lpass: incorrect password\n"); } if(dest[len-1] == '\n') { @@ -257,7 +257,7 @@ static char *typePass(char *text, char *dest) int main(int argc, char *argv[]) { if(!isatty(0)) { // stdin - print_error("Please, use a terminal to run this program\n"); + printError("lpass: Please, use a terminal to run this program\n"); } #if defined(DEBUG) @@ -286,21 +286,21 @@ int main(int argc, char *argv[]) if(result != 'I') // if doesn't init { if(errno == ENOENT) { // file doesn't exist - print_error("Before starting work, you must initialize LockPassword\n\ + printError("lpass: Before starting work, you must initialize LockPassword\n\ Use: lpass --init\n"); } - call_error(102); + callError(102); } } else { if(result == 'I') { - print_error("You already initialized LockPassword\n"); + printError("lpass: You already initialized LockPassword\n"); } else if(result != -1) { strcat(rootPath, ADDNAMETOFULL); // complements to full path if(chdir(rootPath) != 0) { - call_error(112); + callError(112); } } } @@ -316,26 +316,26 @@ int main(int argc, char *argv[]) char *passPath = optarg; if(checkFileExist(passPath) == 0) { - print_error("Error: No such file exists\n"); + printError("lpass: No such file exists\n"); } /* ask user about change pass */ - int buffSize = (strlen("Do you want edit password in '' (Y/N)?") + strlen(passPath)) * sizeof(char) + 1; + int buffSize = (strlen("Do you want edit password in '' (Y/N)?") + strlen(passPath)) * sizeof(char) + sizeof(char); char *str = malloc(buffSize); - snprintf(str, buffSize, "Do you want edit password in '%s' (Y/N)?", passPath); - if(get_answer(str) != 1) { - // free(str); + + if(getAnswer(str) != 1) { + free(str); return 0; } free(str); /* enter password */ - nonvisible_enter(1); // change terminal work + nonvisibleEnter(1); // change terminal work char pass_one[MAXLEN_PASSWORD], pass_two[MAXLEN_PASSWORD]; typePass("Please type your new password: ", pass_one); typePass("Please type your new password again: ", pass_two); - nonvisible_enter(0); + nonvisibleEnter(0); if(strcmp(pass_one, pass_two) == 0) { printf("Password correct\n"); @@ -351,13 +351,13 @@ int main(int argc, char *argv[]) char *passPath = argv[argc-1]; char *lenPass = optarg; - if(checkFileExist(passPath) == 0) { + if(checkFileExist(passPath) == 1) { /* ask user about change pass */ int buffSize = (strlen("Do you want generate new password and paste it in '' (Y/N)?") + strlen(passPath)) * sizeof(char) + 1; char *str = malloc(buffSize); snprintf(str, buffSize, "Do you want generate new password and paste it in '%s' (Y/N)?", passPath); - if(get_answer(str) != 1) { + if(getAnswer(str) != 1) { // free(str); return 0; } @@ -371,12 +371,12 @@ int main(int argc, char *argv[]) if(n_symbols < 1 || n_symbols > 127) { - print_error("Error: you typed an incorrect number"); + printError("lpass: you typed an incorrect number"); } /* generate password */ char gpass[MAXLEN_PASSWORD]; - generate_password(gpass, n_symbols); + generatePassword(gpass, n_symbols); insertPass(rootPath, passPath, gpass); printf("Generated password: %s\n", gpass); @@ -386,16 +386,16 @@ int main(int argc, char *argv[]) } case 'i': { if(checkFileExist(optarg) == 1) { - printf("To change pass use lpass -e\n"); + printf("lpass: To change pass use argument '-e'\n"); return 0; } /* enter password */ - nonvisible_enter(1); // change terminal work + nonvisibleEnter(1); // change terminal work char pass_one[MAXLEN_PASSWORD], pass_two[MAXLEN_PASSWORD]; typePass("Please type your password: ", pass_one); typePass("Please type your password again: ", pass_two); - nonvisible_enter(0); + nonvisibleEnter(0); if(strcmp(pass_one, pass_two) == 0) { printf("Password correct\n"); @@ -403,24 +403,24 @@ int main(int argc, char *argv[]) insertPass(rootPath, optarg, pass_one); printf("Password added successfully for %s\n", optarg); } - else printf("Passwords do not match\n"); + else printf("lpass: Passwords do not match\n"); break; } case 'R': { if(checkFileExist(optarg) == 0) { - print_error("Error: No such file exists\n"); + printError("lpass: No such file exists\n"); } char *main_path = malloc(sizeof(char) * strlen(optarg) + 1); char *file_path = malloc(sizeof(char) * strlen(optarg) + 1); if(splitPath(optarg, main_path, file_path) == NULL) { // check correct input - print_error("[Error] The path you specified is incorrect\n"); + printError("lpass: The path you specified is incorrect\n"); } - if(delete_file(optarg)) { - delete_emptydir(main_path); + if(deleteFile(optarg)) { + deleteEmptyDir(main_path); } free(main_path); @@ -428,29 +428,23 @@ int main(int argc, char *argv[]) break; } case 'I': { - int pid; - // create direction: - pid = fork(); - if(pid == -1) call_error(100); + int pid = fork(); + if(pid == -1) callError(100); if(pid == 0) { /* new process */ execlp("mkdir", "mkdir", "-vp", rootPath, NULL); perror("mkdir"); exit(4); } wait(&pid); - printf("LockPassword initialized\n"); + printf("LockPassword initialized successfully\n"); break; } #if defined(DEBUG) case 't': { - char *main_path = malloc(sizeof(char) * strlen(optarg) + 1); - char *file_path = malloc(sizeof(char) * strlen(optarg) + 1); - - if(splitPath(optarg, main_path, file_path) == NULL) - printf("NULL\n"); + // for check new features: break; } #endif @@ -463,17 +457,17 @@ int main(int argc, char *argv[]) { char *ptr; if(strcmp(argv[argc-1], argv[0]) == 0) { - ptr = malloc(sizeof(char) * strlen(NAMEFILE) + 1); + ptr = malloc(sizeof(char) * strlen(NAMEFILE) + sizeof(char)); strcpy(ptr, NAMEFILE); } else { int buff_tree = strlen(NAMEFILE) + strlen(argv[argc-1]); - ptr = malloc(sizeof(char) * buff_tree + 1 + 1); + ptr = malloc(sizeof(char) * buff_tree + sizeof(char) + sizeof(char)); strcpy(ptr, NAMEFILE); strcat(ptr, "/"); strcat(ptr, argv[argc-1]); } - show_tree(ptr); + showTree(ptr); free(ptr); } -- cgit v1.2.3-18-g5258