From 5dad7bdc3bdad8e9934d45301d5a8c51e843cd7b Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 6 Jul 2017 19:48:20 +0200 Subject: hush: implement negative start in the ${v: -n[:m]} idiom Signed-off-by: Denys Vlasenko --- shell/hush.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'shell/hush.c') diff --git a/shell/hush.c b/shell/hush.c index 64b33cf..f6b50de 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -5619,8 +5619,12 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha goto arith_err; debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); if (len >= 0) { /* bash compat: len < 0 is illegal */ - if (beg < 0) /* bash compat */ - beg = 0; + if (beg < 0) { + /* negative beg counts from the end */ + beg = (arith_t)strlen(val) + beg; + if (beg < 0) /* ${v: -999999} is "" */ + beg = len = 0; + } debug_printf_varexp("from val:'%s'\n", val); if (len == 0 || !val || beg >= strlen(val)) { arith_err: -- cgit v1.1