From 6cf8c7ade4212094ef5c55e18bd8efd1db3037c6 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Fri, 26 Feb 2021 11:45:04 +0100 Subject: [PATCH] initctl: minor refactor, simplify edit command - Gamble a bit by redirecting stderr to /dev/null instead of command -v - Drop vi from VISUAL/EDITOR expansion alternatives, tested later anyway Signed-off-by: Joachim Wiberg --- src/serv.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/serv.c b/src/serv.c index a54f022f..a78ca674 100644 --- a/src/serv.c +++ b/src/serv.c @@ -202,6 +202,13 @@ int serv_touch(char *arg) int serv_edit(char *arg) { + char *editor[] = { + "sensible-editor", + "editor", + "${VISUAL:-${EDITOR}}", + "mg", + "vi" + }; char corr[40]; char path[256]; @@ -221,11 +228,13 @@ int serv_edit(char *arg) if (!fexist(path)) return serv_list(NULL); - return !systemf("[ -x \"$(command -v sensible-editor)\" ] && sensible-editor %s", path) || \ - !systemf("[ -x \"$(command -v editor)\" ] && editor %s", path) || \ - !systemf("${VISUAL:-${EDITOR:-$(command -v vi)}} %s", path) || \ - !systemf("[ -x \"$(command -v mg)\" ] && mg %s", path) || \ - !systemf("[ -x \"$(command -v vi)\" ] && vi %s", path); + for (size_t i = 0; i < NELEMS(editor); i++) { + if (systemf("%s %s 2>/dev/null", editor[i], path)) + continue; + return 0; + } + + return 1; } /**