From 175b115e233c2181759c2f8a291a42724c0c549e Mon Sep 17 00:00:00 2001 From: Joursoir Date: Wed, 24 Aug 2022 14:37:00 +0300 Subject: cmd_showtree: 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 | 71 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/src/exec-cmd.c b/src/exec-cmd.c index 19bacec..c574b7b 100644 --- a/src/exec-cmd.c +++ b/src/exec-cmd.c @@ -506,8 +506,10 @@ int cmd_showtree(int argc, char *argv[]) if(argv[optind]) { result = check_sneaky_paths(argv[optind]); - if(result) - errprint_r(1, "You have used forbidden paths\n"); + if(result) { + print_error("Error: You have used forbidden paths\n"); + return 1; + } path = malloc(sizeof(char) * (strlen(argv[optind]) + 1)); strcpy(path, argv[optind]); } @@ -516,44 +518,43 @@ int cmd_showtree(int argc, char *argv[]) strcpy(path, "."); } - do { // START_DO - - result = file_exist(path); - if(result == F_ISDIR) - { - if(flag_copy) { - errprint_ptr(&retval, 1, - "You must type a passname, not a directory\n"); - break; - } - - if(strcmp(path, ".") == 0) - printf("Password Manager\n"); - else - printf("Password Manager/%s\n", path); - tree(path, "", flag_color); + result = file_exist(path); + if(result == F_ISDIR) + { + if(flag_copy) { + print_error("Error: You must type a passname, not a directory\n"); + retval = 1; + goto out; } - else if(result == F_ISFILE) - { - char *pass = get_password(path); - if(!pass) { - errprint_ptr(&retval, 1, "Decrypt password failed\n"); - break; - } - - if(flag_copy) - copy_outside(pass); - else - printf("%s\n", pass); - - free(pass); + + if(strcmp(path, ".") == 0) + printf("Password Manager\n"); + else + printf("Password Manager/%s\n", path); + tree(path, "", flag_color); + } + else if(result == F_ISFILE) + { + char *pass = get_password(path); + if(!pass) { + print_error("Error: Decrypt password failed\n"); + retval = 1; + goto out; } + + if(flag_copy) + copy_outside(pass); else - errprint_ptr(&retval, 1, - "This path is not in the password storage\n"); + printf("%s\n", pass); - } while(0); // END_DO + free(pass); + } + else { + print_error("Error: This path is not in the password storage\n"); + retval = 1; + } +out: free(path); return retval; } -- cgit v1.2.3-18-g5258