From 1ced9b35b040714bf4bbf3fab8d5f9e8170d50a5 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Mon, 22 Aug 2022 09:13:00 +0300 Subject: cmd_insert: replace errprint_*() with print_error() Handle errors using goto instead of "do { ... } while(0)". This is one of the steps on the way to get rid of errprint_*() macros and handle errors by yourself. For more context see other patches under the same topic specified below. TOPIC=drop_errprint --- src/exec-cmd.c | 87 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/exec-cmd.c b/src/exec-cmd.c index 621acd1..5ad312c 100644 --- a/src/exec-cmd.c +++ b/src/exec-cmd.c @@ -95,8 +95,10 @@ int cmd_insert(int argc, char *argv[]) } result = check_sneaky_paths(path); - if(result) - errprint_r(1, "You have used forbidden paths\n"); + if(result) { + print_error("Error: You have used forbidden paths\n"); + return 1; + } if(file_exist(path) == F_ISFILE) { if(!flag_force) { @@ -106,53 +108,54 @@ int cmd_insert(int argc, char *argv[]) } char *f_pass, *s_pass; - do { // START DO - - if(!flag_echo) { - visible_enter(0); - - printf("Type your password: "); - f_pass = get_input(minlen_pass, maxlen_pass); - printf("\n"); - if(f_pass == NULL) { - errprint_ptr(&retval, 1, "Incorrect password\n"); - break; - } - - printf("Type your password again: "); - s_pass = get_input(minlen_pass, maxlen_pass); - printf("\n"); - if(s_pass == NULL) { - errprint_ptr(&retval, 1, "Incorrect password\n"); - break; - } - - if(strcmp(f_pass, s_pass) != 0) { - errprint_ptr(&retval, 1, "Password do not match\n"); - break; - } + if(!flag_echo) { + visible_enter(0); + + printf("Type your password: "); + f_pass = get_input(minlen_pass, maxlen_pass); + printf("\n"); + if(f_pass == NULL) { + print_error("Error: Incorrect password\n"); + retval = 1; + goto out; } - else { - printf("Type your password: "); - f_pass = get_input(minlen_pass, maxlen_pass); - if(f_pass == NULL) { - errprint_ptr(&retval, 1, "Incorrect password\n"); - break; - } + + printf("Type your password again: "); + s_pass = get_input(minlen_pass, maxlen_pass); + printf("\n"); + if(s_pass == NULL) { + print_error("Error: Incorrect password\n"); + retval = 1; + goto out; } - result = insert_pass(path, f_pass); - if(result) { - errprint_ptr(&retval, 1, "Can't add password to LockPassword\n"); - break; + if(strcmp(f_pass, s_pass) != 0) { + print_error("Error: Password do not match\n"); + retval = 1; + goto out; + } + } + else { + printf("Type your password: "); + f_pass = get_input(minlen_pass, maxlen_pass); + if(f_pass == NULL) { + print_error("Error: Incorrect password\n"); + return 1; } - if(flag_copy) - copy_outside(f_pass); + } - printf("Password added successfully for %s\n", path); + result = insert_pass(path, f_pass); + if(result) { + print_error("Error: Can't add password to LockPassword\n"); + retval = 1; + goto out; + } + if(flag_copy) + copy_outside(f_pass); - } while(0); // END DO + printf("Password added successfully for %s\n", path); +out: visible_enter(1); if(f_pass) free(f_pass); -- cgit v1.2.3-18-g5258