From fc73a96f8d6ee8ec2deb6414a8cb54bd588fc590 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Tue, 23 Aug 2022 20:02:00 +0300 Subject: cmd_edit: replace errprint_*() with print_error() 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 | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/src/exec-cmd.c b/src/exec-cmd.c index 1e76980..014012e 100644 --- a/src/exec-cmd.c +++ b/src/exec-cmd.c @@ -176,27 +176,36 @@ int cmd_edit(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; + } result = file_exist(path); - if(result == F_NOEXIST) - errprint_r(1, "No such file exists\n"); - if(result == F_ISDIR) - errprint_r(1, "It's a directory\n"); + if(result == F_NOEXIST) { + print_error("Error: No such file exists\n"); + return 1; + } + if(result == F_ISDIR) { + print_error("Error: It's a directory\n"); + return 1; + } editor = getenv("EDITOR"); if(!editor) editor = STD_TEXT_EDITOR; password = get_password(path); - if(password == NULL) - errprint_r(1, "Decrypt password failed\n"); + if(password == NULL) { + print_error("Error: Decrypt password failed\n"); + return 1; + } fd = mkstemp(path_tmpfile); if(fd == -1) { + print_error("Error: Create temporary file failed: %s\n", strerror(errno)); free(password); - errprint_r(1, "Create temporary file failed: %s\n", strerror(errno)); + return 1; } dbgprint("tmp file: %s\n", path_tmpfile); @@ -205,16 +214,18 @@ int cmd_edit(int argc, char *argv[]) free(password); close(fd); if(result != len_pass) { + print_error("Error: Write password to temporary file failed\n"); unlink(path_tmpfile); - errprint_r(1, "Write password to temporary file failed\n"); + return 1; } // fork for text editor char *editor_arg[] = {editor, path_tmpfile, NULL}; pid = fork(); if(pid == -1) { + print_error("Error: Start %s failed: %s\n", editor, strerror(errno)); unlink(path_tmpfile); - errprint_r(1, "Start %s failed: %s\n", editor, strerror(errno)); + return 1; } if(pid == 0) { /* new process */ execvp(editor, editor_arg); @@ -225,8 +236,9 @@ int cmd_edit(int argc, char *argv[]) fd = open(path_tmpfile, O_RDONLY); if(fd == -1) { + print_error("Error: Open temporary file failed: %s\n", strerror(errno)); unlink(path_tmpfile); - errprint_r(1, "Open temporary file failed: %s\n", strerror(errno)); + return 1; } password = malloc(sizeof(char) * (maxlen_pass + 1)); @@ -235,11 +247,12 @@ int cmd_edit(int argc, char *argv[]) close(fd); unlink(path_tmpfile); if(len_pass < minlen_pass) { - free(password); if(len_pass == -1) - errprint_r(1, "Read temporary file failed: %s\n", strerror(save_errno)); + print_error("Error: Read temporary file failed: %s\n", strerror(save_errno)); else - errprint_r(1, "Min. password length is %d\n", minlen_pass); + print_error("Error: Min. password length is %d\n", minlen_pass); + free(password); + return 1; } password[len_pass-1] = '\0'; dbgprint("new pass: %s\n", password); @@ -247,8 +260,10 @@ int cmd_edit(int argc, char *argv[]) // encrypt result = insert_pass(path, password); free(password); - if(result) - errprint_r(1, "Can't add password to LockPassword\n"); + if(result) { + print_error("Error: Can't add password to LockPassword\n"); + return 1; + } return 0; } -- cgit v1.2.3-18-g5258