oweals/busybox.git
8 years agodnsdomainname: split it from "hostname", make it independently selectable
Denys Vlasenko [Sun, 13 Nov 2016 21:35:10 +0000 (22:35 +0100)]
dnsdomainname: split it from "hostname", make it independently selectable

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotar: handle pax-encoded utf8 filenames and link names. Closes 9406
Denys Vlasenko [Fri, 11 Nov 2016 16:56:45 +0000 (17:56 +0100)]
tar: handle pax-encoded utf8 filenames and link names. Closes 9406

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: make getch/peek functions directly called
Denys Vlasenko [Tue, 8 Nov 2016 21:35:05 +0000 (22:35 +0100)]
hush: make getch/peek functions directly called

Indirect calls are more difficult to predict.
Unfortunately, on x64 direct call is 5 bytes while indirect "call (reg+ofs)"
is 3 bytes:

function                                             old     new   delta
i_getch                                                -      82     +82
i_peek                                                 -      63     +63
parse_stream                                        2531    2579     +48
parse_dollar                                         771     797     +26
parse_redirect                                       296     321     +25
add_till_closing_bracket                             408     420     +12
encode_string                                        256     265      +9
i_peek_and_eat_bkslash_nl                             93      99      +6
add_till_backquote                                   110     114      +4
parse_and_run_stream                                 139     141      +2
expand_vars_to_list                                 1143    1144      +1
static_peek                                            6       -      -6
setup_string_in_str                                   39      18     -21
setup_file_in_str                                     40      19     -21
static_get                                            27       -     -27
file_peek                                             52       -     -52
file_get                                              65       -     -65
------------------------------------------------------------------------------
(add/remove: 2/4 grow/shrink: 9/2 up/down: 278/-192)           Total: 86 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: renumber PIPE_foo, make PIPE_SEQ = 0
Denys Vlasenko [Tue, 8 Nov 2016 19:35:53 +0000 (20:35 +0100)]
hush: renumber PIPE_foo, make PIPE_SEQ = 0

PIPE_SEQ is used most often, having it zero makes code smaller:

function                                             old     new   delta
done_word                                            719     707     -12
parse_stream                                        2546    2531     -15

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: small optimization in run_list
Denys Vlasenko [Tue, 8 Nov 2016 19:26:11 +0000 (20:26 +0100)]
hush: small optimization in run_list

I thought gcc can detect this itself. It doesn't.

function                                             old     new   delta
run_list                                            1030    1021      -9

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: deindent large block of code, no code changes
Denys Vlasenko [Tue, 8 Nov 2016 16:34:44 +0000 (17:34 +0100)]
hush: deindent large block of code, no code changes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: simplify insert_bg_jobs
Denys Vlasenko [Tue, 8 Nov 2016 16:28:45 +0000 (17:28 +0100)]
hush: simplify insert_bg_jobs

function                                             old     new   delta
insert_bg_job                                        366     281     -85

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: make "wait %1" less likely to play with signal mask
Denys Vlasenko [Tue, 8 Nov 2016 03:59:11 +0000 (04:59 +0100)]
hush: make "wait %1" less likely to play with signal mask

Was playing with "sleep 3 | exit 3 & wait %1" and noticed that often
SIGCHLD arrives even before I get to signal masking. Can avoid it in this
case.

function                                             old     new   delta
wait_for_child_or_signal                             228     265     +37

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: rework "wait %jobspec" to work in non-interactive shells too
Denys Vlasenko [Mon, 7 Nov 2016 23:59:29 +0000 (00:59 +0100)]
hush: rework "wait %jobspec" to work in non-interactive shells too

Also add tests. wait5.tests so far fails (but works for ash and dash).

function                                             old     new   delta
builtin_wait                                         305     283     -22

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: "wait $!; echo $?" should return 127 if $! already exited
Denys Vlasenko [Mon, 7 Nov 2016 22:07:21 +0000 (23:07 +0100)]
hush: "wait $!; echo $?" should return 127 if $! already exited

It would be nice to provide bash-like "remember las exitcode"
thingy, but it's a bit complex. For now, match ash and dash.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: implement "wait %jobspec"
Denys Vlasenko [Mon, 7 Nov 2016 21:12:18 +0000 (22:12 +0100)]
hush: implement "wait %jobspec"

function                                             old     new   delta
parse_jobspec                                          -      83     +83
job_exited_or_stopped                                  -      79     +79
builtin_wait                                         236     302     +66
wait_for_child_or_signal                             199     228     +29
checkjobs                                            142     158     +16
builtin_jobs                                          59      68      +9
process_wait_result                                  453     408     -45
builtin_fg_bg                                        272     203     -69
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 4/2 up/down: 282/-114)          Total: 168 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: factor out %jobspec parsing
Denys Vlasenko [Mon, 7 Nov 2016 19:06:40 +0000 (20:06 +0100)]
hush: factor out %jobspec parsing

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: comment fix
Denys Vlasenko [Mon, 7 Nov 2016 18:36:50 +0000 (19:36 +0100)]
hush: comment fix

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: do not allow sh -c '{ echo boo }'
Denys Vlasenko [Mon, 7 Nov 2016 15:22:35 +0000 (16:22 +0100)]
hush: do not allow sh -c '{ echo boo }'

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agocpio: add ownership (-R) test cases
Aaro Koskinen [Wed, 2 Nov 2016 22:25:05 +0000 (00:25 +0200)]
cpio: add ownership (-R) test cases

Add ownership (-R) test cases.

Signed-off-by: Aaro Koskinen <aaro.koskinen@iki.fi>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: case logic for setting $? was still wrong
Denys Vlasenko [Fri, 4 Nov 2016 19:14:04 +0000 (20:14 +0100)]
hush: case logic for setting $? was still wrong

Resetting to 0 should happen in "esac". Matched branch must
still see previous $?.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: non-matching "case" statement sets $? to 0
Denys Vlasenko [Fri, 4 Nov 2016 17:52:48 +0000 (18:52 +0100)]
hush: non-matching "case" statement sets $? to 0

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: allow { cmd } to not be terminated by semicolon in some cases
Denys Vlasenko [Fri, 4 Nov 2016 17:46:14 +0000 (18:46 +0100)]
hush: allow { cmd } to not be terminated by semicolon in some cases

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: fix "duplicate local" code (forgot to re-enable interrupts)
Denys Vlasenko [Fri, 4 Nov 2016 15:43:18 +0000 (16:43 +0100)]
ash: fix "duplicate local" code (forgot to re-enable interrupts)

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agocomment and readme updates
Denys Vlasenko [Fri, 4 Nov 2016 13:13:58 +0000 (14:13 +0100)]
comment and readme updates

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agosu: expand help; simplify passing of -c CMD to run_shell()
Denys Vlasenko [Thu, 3 Nov 2016 21:13:08 +0000 (22:13 +0100)]
su: expand help; simplify passing of -c CMD to run_shell()

Also, added a comment about bug 9401 (TIOCSTI input injection).

function                                             old     new   delta
packed_usage                                       30909   30932     +23
su_main                                              470     487     +17
sulogin_main                                         260     258      -2
run_applet_and_exit                                  681     678      -3
run_shell                                            166     126     -40

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoloadfont: 32k size limit is not enough
Denys Vlasenko [Thu, 3 Nov 2016 19:57:37 +0000 (20:57 +0100)]
loadfont: 32k size limit is not enough

Since our "read to malloced buf" routines only gradually grow
allocations, let's be generous here and allow 128k.

Reported by Alex Henrie <alexhenrie24@gmail.com>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: while (!got_sig) pause() is not reliable, use sigsuspend()
Denys Vlasenko [Thu, 3 Nov 2016 19:17:23 +0000 (20:17 +0100)]
ash: while (!got_sig) pause() is not reliable, use sigsuspend()

dash was doing it for a reason. Unfortunately, it had no comment why...
now I know.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoman: remove -Tascii from nroff invocation
Denys Vlasenko [Mon, 31 Oct 2016 13:05:34 +0000 (14:05 +0100)]
man: remove -Tascii from nroff invocation

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoman: make width selection more thorough; explain how to override it
Denys Vlasenko [Mon, 31 Oct 2016 00:52:18 +0000 (01:52 +0100)]
man: make width selection more thorough; explain how to override it

Fedora's "man CMD >file" still uses terminal width, not 80 (but disables formatting),
this change mimics that.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoman: allow nroff and tbl commands be overridden; unmangle writing to files
Denys Vlasenko [Sun, 30 Oct 2016 21:31:30 +0000 (22:31 +0100)]
man: allow nroff and tbl commands be overridden; unmangle writing to files

Parse this in config files:
 DEFINE col ...
 DEFINE tbl ...
 DEFINE nroff ...
Add width options to nroff command line.
Use "tbl", not "gtbl", as default tbl command.
Export GROFF_NO_SGR=1 and use "col -b -p -x" instead of pager when writing to file.

function                                             old     new   delta
man_main                                             735     863    +128
if_redefined                                           -      64     +64
show_manpage                                         199     169     -30
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 192/-30)           Total: 162 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: if using libc glob(), skip it if no metachars are in word
Denys Vlasenko [Sun, 30 Oct 2016 17:41:01 +0000 (18:41 +0100)]
ash: if using libc glob(), skip it if no metachars are in word

This saves making tons of pointless stat() calls

function                                             old     new   delta
expandarg                                            888     921     +33

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: fix bit-rotten debug infrastructure
Denys Vlasenko [Sun, 30 Oct 2016 17:30:29 +0000 (18:30 +0100)]
ash: fix bit-rotten debug infrastructure

DEBUG = 2 output was a bit messed up

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: make popfile() anfter popallfiles() safe
Denys Vlasenko [Sun, 30 Oct 2016 17:27:14 +0000 (18:27 +0100)]
ash: make popfile() anfter popallfiles() safe

In this example:

ash -c 'readonly x; echo $(command eval x=2)'

evalstring() is called after forkchild(), which calls popallfiles().
On exception, evalstring() will popfile().

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: fix "(sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $?"
Denys Vlasenko [Fri, 28 Oct 2016 20:39:12 +0000 (22:39 +0200)]
hush: fix "(sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $?"

"wait $!" may be just a bit too late: backgrounded $! is gone.
Do not bomb out in this case.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: fix "wait PID"
Denys Vlasenko [Fri, 28 Oct 2016 19:57:31 +0000 (21:57 +0200)]
hush: fix "wait PID"

It was not properly interruptible, and did not update job status
(the exited processes were still thought of as running).

function                                             old     new   delta
process_wait_result                                    -     453    +453
wait_for_child_or_signal                               -     199    +199
run_list                                             996    1002      +6
checkjobs_and_fg_shell                                41      43      +2
builtin_wait                                         328     215    -113
checkjobs                                            516     142    -374
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 2/2 up/down: 660/-487)          Total: 173 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: use pause(), not sigsuspend(), in wait builtin
Denys Vlasenko [Fri, 28 Oct 2016 15:16:11 +0000 (17:16 +0200)]
ash: use pause(), not sigsuspend(), in wait builtin

Same effect, smaller code

function                                             old     new   delta
dowait                                               463     374     -89

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: fix interactive "command eval STRING" exiting on errors.
Denys Vlasenko [Fri, 28 Oct 2016 13:43:50 +0000 (15:43 +0200)]
ash: fix interactive "command eval STRING" exiting on errors.

This bug is also present in current dash

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [JOBS] Fix dowait signal race
Denys Vlasenko [Thu, 27 Oct 2016 21:51:19 +0000 (23:51 +0200)]
ash: [JOBS] Fix dowait signal race

Upstream commit:

    Date: Sun, 22 Feb 2009 18:10:01 +0800
    [JOBS] Fix dowait signal race

    This test program by Alexey Gladkov can cause dash to enter an
    infinite loop in waitcmd.

    #!/bin/dash
    trap "echo TRAP" USR1
    stub() {
        echo ">>> STUB $1" >&2
        sleep $1
        echo "<<< STUB $1" >&2
        kill -USR1 $$
    }
    stub 3 &
    stub 2 &
    until { echo "###"; wait; } do
    echo "*** $?"
    done

    The problem is that if we get a signal after the wait3 system
    call has returned but before we get to INTON in dowait, then
    we can jump back up to the top and lose the exit status.  So
    if we then wait for the job that has just exited, then it'll
    stay there forever.

    I made the original change that caused this bug to fix pretty
    much the same bug but in the opposite direction.  That is, if
    we get a signal after we enter wait3 but before we hit the kernel
    then it too can cause the wait to go on forever (assuming the
    child doesn't exit).

    In fact this is pretty much exactly the scenario that you'll
    find in glibc's documentation on pause().  The solution is given
    there too, in the form of sigsuspend, which is the only way to
    do the check and wait atomically.

    So this patch fixes Alexey's race without reintroducing the old
    bug by converting the blocking wait3 to a sigsuspend.

    In order to do this we need to set a signal handler for SIGCHLD,
    so the code has been modified to always do that.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
I failed to reproduce the bug (it requires precise timing), but it seems real.

function                                             old     new   delta
dowait                                               284     463    +179
setsignal                                            301     326     +25
signal_handler                                        59      76     +17
ash_main                                            1481    1487      +6
localcmd                                             350     348      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/1 up/down: 227/-2)            Total: 225 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [SIGNAL] Remove EXSIG
Denys Vlasenko [Thu, 27 Oct 2016 19:09:01 +0000 (21:09 +0200)]
ash: [SIGNAL] Remove EXSIG

Upstream commit 1:

    Date: Sun, 22 Feb 2009 18:16:13 +0800
    [SIGNAL] Remove EXSIG

    Now that waitcmd no longer uses EXSIG we can remove it.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Upstream commit 2:

    Date: Thu, 2 Oct 2014 21:07:55 +0800
    [ERROR] Set exitstatus in onint

    Currently the exit status when we receive SIGINT is set in evalcommand
    which means that it doesn't always get set.  For example, if you press
    CTRL-C at the prompt of an interactive dash, the exit status is not
    set to 130 as it is in many other Bourne shells.

    This patch fixes this by moving the setting of the exit status into
    onint which also simplifies evalcommand.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Upstream commit 3:

    Date: Fri, 3 Oct 2014 14:07:07 +0800
    [EVAL] Do not clobber exitstatus in evalcommand

    All originators of EXERROR have been setting the exitstatus for
    a while now.  So it is no longer appropriate to set it explicitly
    in evalcommand.

    In fact doing so may cause the original exitstatus to be lost.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Last three coomits:
function                                             old     new   delta
waitcmd                                              186     224     +38
dowait                                               276     284      +8
waitforjob                                           104     107      +3
localcmd                                             348     350      +2
showjobs                                              64      61      -3
forkshell                                            263     260      -3
raise_interrupt                                       93      67     -26
blocking_wait_with_raise_on_sig                       40       -     -40
evalcommand                                         1264    1208     -56
evaltree                                             809     498    -311

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: open-code blocking_dowait_with_raise_on_sig()
Denys Vlasenko [Thu, 27 Oct 2016 18:18:18 +0000 (20:18 +0200)]
ash: open-code blocking_dowait_with_raise_on_sig()

There is in fact only one callsite.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: return to DOWAIT_* constants similar to dash, no logic changes
Denys Vlasenko [Thu, 27 Oct 2016 18:08:28 +0000 (20:08 +0200)]
ash: return to DOWAIT_* constants similar to dash, no logic changes

This loses an insignificant optimization, but may allow backporting
of some recent-ish dash fixes.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: delete leftovers from "simplify EOF/newline handling in list parser" commit
Denys Vlasenko [Thu, 27 Oct 2016 12:49:21 +0000 (14:49 +0200)]
ash: delete leftovers from "simplify EOF/newline handling in list parser" commit

This commit should have deleted these two statements:

    commit c0e007663d30f83b0e5e074db34dcffaa8915e99
    Author: Ron Yorston <rmy@pobox.com>
    Date:   Thu Oct 29 11:30:55 2015 +0000
    ash: simplify EOF/newline handling in list parser

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [EXPAND] Fix ifsfirst/ifslastp leak
Denys Vlasenko [Thu, 27 Oct 2016 12:46:50 +0000 (14:46 +0200)]
ash: [EXPAND] Fix ifsfirst/ifslastp leak

Upstream commit:

    Date: Wed, 8 Sep 2010 20:07:26 +0800
    [EXPAND] Fix ifsfirst/ifslastp leak

    As it stands expandarg may return with a non-NULL ifslastp which
    then confuses any subsequent ifsbreakup user that doesn't clear
    it directly.

    What's worse, if we get interrupted before we hit ifsfree in
    expandarg we will leak memory.

    This patch fixes this by always calling ifsfree in expandarg
    thus ensuring that ifslastp is always NULL on the normal path.
    It also adds an ifsfree call to the RESET path to ensure that
    memory isn't leaked.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Fallout 1:

    Date: Mon, 18 Oct 2010 10:55:42 +0800
    [EXPAND] Fix ifsfirst/ifslastp leak in casematch

    The commit f42e443bb511ed3224f09b4fcf0772438ebdbbfa

        [EXPAND] Fix ifsfirst/ifslastp leak

    revealed yet another ifsfirst/ifslastp leak in casematch.
    Previously it was hidden because ifsfirst/ifslastp was cleared
    unconditionally on entry (which caused the leakage of those
    entries).

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Fallout 2:

    Date: Sun, 28 Nov 2010 21:09:51 +0800
    [EXPAND] Free IFS state in evalbackcmd

    On Sun, Nov 07, 2010 at 04:04:20PM -0600, Jonathan Nieder wrote:
    > Herbert Xu wrote:
    > > commit f42e443bb511ed3224f09b4fcf0772438ebdbbfa
    > > Author: Herbert Xu <herbert@gondor.apana.org.au>
    > > Date:   Wed Sep 8 20:07:26 2010 +0800
    > >
    > >     [EXPAND] Fix ifsfirst/ifslastp leak
    >
    > Another puzzle bisecting to f42e443bb.  This one comes from the
    > grub-mkconfig script:
    >
    >  $ sh -c 'datadir=/usr/share; pkgdatadir=${datadir}/`cat`' 2>&1 | cat -A
    >  cat: M-^\^M^F^HM-4^M^F^HM-(^M^F^H: No such file or directory$
    >  cat: M-(^M^F^H: No such file or directory$
    >
    > Still reproducible with 016b529.  I'll try to find time to look into
    > it, but thought you might like to know nevertheless.

    This is the symptom of another leak.  In this case evalbackcmd
    occurs in the middle of an expansion (as it should) but the forked
    child never clears the previous IFS state.

    This patch adds the missing ifsfree call.

    This wasn't as much of a problem as the previously discovered leaks
    since all it means is that the child gets to carry around the parent's
    expansion state and the child is usually short-lived.

Reported-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Fallout 3:

    Date: Tue, 15 Mar 2011 16:01:34 +0800
    [EXPAND] Free IFS state after here document expansion

    Here's another bug bisecting to f42e443bb ([EXPAND] Fix
    ifsfirst/ifslastp leak, 2010-09-08).  It was found with the following
    test case, based on the configure script for Tracker:

        dash -x -c '
                <<-_ACEOF
                $@
                _ACEOF
                exec
        ' - abcdefgh
        +
        + exec   ?a
        exec: 1: : Permission denied

    The missing ifsfree call is in expandarg when it returns to openhere
    during here document expansion.

Reported-by: Aurelien Jarno <aurel32@debian.org>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
function                                             old     new   delta
ifsfree                                                -      66     +66
ash_main                                            1490    1495      +5
argstr                                              1154    1159      +5
evalcase                                             275     270      -5
expandarg                                            972     888     -84
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/2 up/down: 76/-89)            Total: -13 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: move ifsbreakup() and ifsfree() up
Denys Vlasenko [Thu, 27 Oct 2016 12:45:13 +0000 (14:45 +0200)]
ash: move ifsbreakup() and ifsfree() up

Preparatory patch.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: partially sync with dash on "fork if traps are set" logic
Denys Vlasenko [Thu, 27 Oct 2016 10:55:09 +0000 (12:55 +0200)]
ash: partially sync with dash on "fork if traps are set" logic

Upstream commit "[EVAL] Force fork if any trap is set, not just on EXIT"
had a similar code as our fix to that bug.
Eliminate some superficial differences.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [SHELL] Expand ENV before using it
Denys Vlasenko [Thu, 27 Oct 2016 09:28:59 +0000 (11:28 +0200)]
ash: [SHELL] Expand ENV before using it

Upstream commit:

    Date: Sun, 13 Jul 2008 21:51:52 +0800
    [SHELL] Expand ENV before using it

    Per POSIX ENV needs to undergo parameter expansion.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: comment tweaks, no code changes
Denys Vlasenko [Thu, 27 Oct 2016 00:31:55 +0000 (02:31 +0200)]
ash: comment tweaks, no code changes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: optimize tryexec(): avoid one allocation
Denys Vlasenko [Wed, 26 Oct 2016 20:29:11 +0000 (22:29 +0200)]
ash: optimize tryexec(): avoid one allocation

There was a bug in tryexec which bbox had fixed in 2003.
dash had a smaller fix in 2007. Copy it. It is smaller,
although it is also more quirky (requires argv[-1] to exist).

Upstream commit 1:

    Date: Mon, 15 Oct 2007 20:24:28 +0800
    [EXEC] Fixed execing of scripts with no hash-bang

    The function tryexec used the original name instead of the path found through
    PATH search.  This patch fixes that.

    Test case:

        trap 'rm -f $TMP' EXIT
        TMP=$(tempfile -s nosuchthing)

        cat <<- EOF > $TMP
                echo OK
        EOF
        chmod u+x $TMP

        cd /
        PATH=${TMP%/*} ${TMP##*/}

    Old result:

        /bin/sh: Can't open filelgY4Fanosuchthing

    New result:

        OK

Upstream commit 2:

    Date: Sun, 23 Dec 2007 11:02:26 +0800
    [EVAL] Fix bad pointer arithmetic in evalcommand

    dash dies on sparc with a SIGBUS due to an arithmetic error introduced
    with commit 03b4958, this patch fixes it.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
function                                             old     new   delta
evalcommand                                         1261    1264      +3
dotcmd                                               321     319      -2
tryexec                                              115      64     -51
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 3/-53)             Total: -50 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [CD] Lookup PWD after going through CDPATH
Denys Vlasenko [Wed, 26 Oct 2016 17:56:05 +0000 (19:56 +0200)]
ash: [CD] Lookup PWD after going through CDPATH

Upstream commit:

    Date: Mon, 31 Aug 2009 22:06:41 +1000
    [CD] Lookup PWD after going through CDPATH

    On Tue, Jul 14, 2009 at 09:39:03PM +0000, Eric Blake wrote:
    > For the cd command, POSIX 2008 requires that after all pathnames in CDPATH
    > have been tested and failed in step 5, then step 6 interprets the directory
    > argument relative to PWD.  In other words, this demonstrates a bug:
    >
    > $ dash -c 'cd /tmp; mkdir -p foo; CDPATH=oops; cd foo; echo $?; pwd'
    > cd: 1: can't cd to foo
    > 2
    > /tmp
    >
    > while bash gets it correct:
    >
    > $ bash -c 'cd /tmp; mkdir -p foo; CDPATH=oops; cd foo; echo $?; pwd'
    > 0
    > /tmp/foo

    This patch fixes the problem.

Reported-by: Eric Blake <ebb9@byu.net>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
function                                             old     new   delta
cdcmd                                                667     680     +13

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [MEMALLOC] Made grabstackblock an inline wrapper for stalloc
Denys Vlasenko [Wed, 26 Oct 2016 16:26:27 +0000 (18:26 +0200)]
ash: [MEMALLOC] Made grabstackblock an inline wrapper for stalloc

Upstream patch:

    Date: Fri, 5 Oct 2007 23:26:45 +0800
    [MEMALLOC] Made grabstackblock an inline wrapper for stalloc

    The function grabstackblock is identical in semantics to stalloc within its
    input constraints.

function                                             old     new   delta
dotcmd                                               319     321      +2
grabstackblock                                        19       5     -14

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [VAR] Remove setvarsafe
Denys Vlasenko [Wed, 26 Oct 2016 15:54:32 +0000 (17:54 +0200)]
ash: [VAR] Remove setvarsafe

Upstream commit:

    Date: Sat, 6 Oct 2007 21:18:58 +0800
    [VAR] Remove setvarsafe

    The only user of setvarsafe is getopts.  However, we can achieve the same
    result by pre-setting the value of shellparam.optind.

function                                             old     new   delta
getoptscmd                                           614     515     -99
setvarsafe                                           147       -    -147
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 0/1 up/down: 0/-246)           Total: -246 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: use shellparam.optind/optoff in getopts() directly, not through pointers
Denys Vlasenko [Wed, 26 Oct 2016 15:34:26 +0000 (17:34 +0200)]
ash: use shellparam.optind/optoff in getopts() directly, not through pointers

This is a preparatory patch for next change

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [PARSER] Size optimisations in parameter expansion parser
Denys Vlasenko [Wed, 26 Oct 2016 14:41:13 +0000 (16:41 +0200)]
ash: [PARSER] Size optimisations in parameter expansion parser

Upstream commit:

    Date: Thu, 4 Oct 2007 22:20:38 +0800
    [PARSER] Size optimisations in parameter expansion parser

    Merge flags into subtype.
    Do not write subtype out twice.
    Add likely flag on ${ vs. $NAME.
    Kill unnecessary (and bogus) PEOA check.

function                                             old     new   delta
readtoken1                                          2891    2860     -31

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [PARSER] Recognise here-doc delimiters terminated by EOF
Denys Vlasenko [Wed, 26 Oct 2016 14:26:45 +0000 (16:26 +0200)]
ash: [PARSER] Recognise here-doc delimiters terminated by EOF

Upstream commit 1:

    Date: Wed, 26 Sep 2007 17:14:16 +0800
    [PARSER] Recognise here-doc delimiters terminated by EOF

    Previously dash required a <newline> character to be present in order for
    a here-document delimiter to be detected.  Allowing EOF in the absence of
    a <newline> to play the same purpose allows some intuitive scripts to
    succeed.  POSIX seems to be silence on this so this should be OK.

    Test case:

        eval 'cat <<- NOT
                test
        NOT'
        echo OK

    Old result:

        test
        NOTOK

    New result:

        test
        OK

Upstream commit 2:

    Date: Sat, 20 Oct 2007 18:49:31 +0800
    [PARSER] Fix here-doc corruption

    The change

        [PARSER] Recognise here-doc delimiters terminated by EOF

    introduced a regerssion whereby lines starting with eofmark but are not equal
    to eofmark would be corrupted.  This patch fixes it.

    Test case:

        cat << _ACEOF
        _ASBOX
        _ACEOF

    Old result:

        SASBOX

    New result:

        _ASBOX

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [PARSER] Fix parsing of ${##1}
Denys Vlasenko [Wed, 26 Oct 2016 13:56:53 +0000 (15:56 +0200)]
ash: [PARSER] Fix parsing of ${##1}

Upstream commit:

    Date: Thu, 4 Oct 2007 22:15:10 +0800
    [PARSER] Fix parsing of ${##1}

    Previously dash treated ${##1} as a length operation.  This patch fixes that.

    Test case:

        set -- a
        echo ${##1}OK

    Old result:

        1OK

    New result:

        OK

This was a real bug in ash (but not in hush).

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [REDIR] Remove redundant CLOEXEC calls
Denys Vlasenko [Wed, 26 Oct 2016 13:38:44 +0000 (15:38 +0200)]
ash: [REDIR] Remove redundant CLOEXEC calls

Upstream commit:

    Date: Sun, 6 May 2007 19:28:56 +1000
    [REDIR] Remove redundant CLOEXEC calls

    Now that we're marking file descriptors as CLOEXEC in savefd, we no longer
    need to close them on exec or in setinputfd.

function                                             old     new   delta
ash_main                                            1478    1492     +14
setinputfile                                         224     226      +2
readtoken1                                          2752    2750      -2
shellexec                                            208     198     -10
clearredir                                            30       -     -30
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 2/2 up/down: 16/-42)            Total: -26 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [REDIR] Replace copyfd by savefd and use dup2 elsewhere
Denys Vlasenko [Wed, 26 Oct 2016 13:24:30 +0000 (15:24 +0200)]
ash: [REDIR] Replace copyfd by savefd and use dup2 elsewhere

Upstream commit:

    Date: Sat, 12 May 2007 18:00:57 +1000
    [REDIR] Replace copyfd by savefd and use dup2 elsewhere

    There are two kinds of users to copyfd, those that want to copy an fd to
    an exact value and those that want to move an fd to a value >= 10.  The
    former can simply use dup2 directly while the latter share a lot of common
    code that now constitutes savefd.

This does not change much, just reducing our divergence from dash code.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotypo fixes
Denys Vlasenko [Wed, 26 Oct 2016 11:15:35 +0000 (13:15 +0200)]
typo fixes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [BUILTIN] Treat OPTIND=0 in the same way as OPTIND=1
Denys Vlasenko [Wed, 26 Oct 2016 00:03:37 +0000 (02:03 +0200)]
ash: [BUILTIN] Treat OPTIND=0 in the same way as OPTIND=1

Upstream commit:

    Date: Sat, 6 Oct 2007 18:59:31 +0800
    [BUILTIN] Treat OPTIND=0 in the same way as OPTIND=1

    Previously setting OPTIND to 0 would cause subsequent getopts calls to fail.
    This patch makes dash reset the getopts parameters the same way as OPTIND=1.

    Both behaviours are allowed by POSIX but other common shells do tolerate this
    case.

function                                             old     new   delta
getoptsreset                                          24      30      +6
getoptscmd                                           632     614     -18

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [PARSER] Report substition errors at expansion time
Denys Vlasenko [Tue, 25 Oct 2016 23:55:56 +0000 (01:55 +0200)]
ash: [PARSER] Report substition errors at expansion time

Upstreams commit:

    Date: Mon, 8 Oct 2007 21:32:25 +0800
    [PARSER] Report substition errors at expansion time

    On Wed, Apr 11, 2007 at 01:24:21PM -0700, Micah Cowan wrote:
    > This operation fails on Ubuntu:
    >
    >     $ /bin/sh -c 'if false; then d="${foo/bar}"; fi'
    >     /bin/sh: Syntax error: Bad substitution
    >
    > When used with other POSIX shells it succeeds. While semantically the
    > variable reference ${foo/bar} is not valid, this is not a syntax error
    > according to POSIX, and since the variable assignment expression is
    > never invoked (because it's within an "if false") it should not be seen
    > as an error.
    >
    > I ran into this because after restarting my system I could no longer log
    > in. It turns out that the problem was (a) I had edited .gnomerc to
    > source my .bashrc file so that my environment would be set properly, and
    > (b) I had added some new code to my .bashrc WITHIN A CHECK FOR BASH!
    > that used bash's ${var/match/sub} feature. Even though this code was
    > within a "case $BASH_VERSION; in *[0-9]*) ... esac (so dash would never
    > execute it since that variable is not set), it still caused dash to
    > throw up.
    >
    > FYI, some relevant details from POSIX:
    >
    > Section 2.3, Token Recognition:
    >
    > 5. If the current character is an unquoted '$' or '`', the shell shall
    > identify the start of any candidates for parameter expansion ( Parameter
    > Expansion), command substitution ( Command Substitution), or arithmetic
    > expansion ( Arithmetic Expansion) from their introductory unquoted
    > character sequences: '$' or "${", "$(" or '`', and "$((", respectively.
    > The shell shall read sufficient input to determine the end of the unit
    > to be expanded (as explained in the cited sections).
    >
    > Section 2.6.2, Parameter Expansion:
    >
    > The format for parameter expansion is as follows:
    >
    >     ${expression}
    >
    > where expression consists of all characters until the matching '}'. Any
    > '}' escaped by a backslash or within a quoted string, and characters in
    > embedded arithmetic expansions, command substitutions, and variable
    > expansions, shall not be examined in determining the matching '}'.
    > [...]
    >
    > The parameter name or symbol can be enclosed in braces, which are
    > optional except for positional parameters with more than one digit or
    > when parameter is followed by a character that could be interpreted as
    > part of the name. The matching closing brace shall be determined by
    > counting brace levels, skipping over enclosed quoted strings, and
    > command substitutions.
    > ---
    > In addition to bash I've checked Solaris /bin/sh and ksh and they don't
    > report an error.
    >
    > -----
    > Micah Cowan:
    >
    > The applicable portion of POSIX is in XCU 2.10.1:
    >
    > "The WORD tokens shall have the word expansion rules applied to them
    > immediately before the associated command is executed, not at the time
    > the command is parsed."
    >
    > This seems fairly clear to me.

    This patch moves the error detection to expansion time.

    Test case:

     if false; then
     echo ${a!7}
     fi
     echo OK

    Old result:

dash: Syntax error: Bad substitution

    New result:

OK

function                                             old     new   delta
evalvar                                              574     585     +11
readtoken1                                          2763    2750     -13

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [REDIR] Move null redirect checks into caller
Denys Vlasenko [Tue, 25 Oct 2016 19:46:03 +0000 (21:46 +0200)]
ash: [REDIR] Move null redirect checks into caller

Upstream commit:

    Date: Thu, 27 May 2010 14:21:17 +0800
    [REDIR] Move null redirect checks into caller

    The null redirect checks were added as an optimisation to avoid
    unnecessary memory allocations.  However, we could avoid this
    completely by simply making the caller avoid making a redirection
    unless it is not null.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
function                                             old     new   delta
evaltree                                             784     809     +25
evalcommand                                         1251    1261     +10
hashvar                                               59      62      +3
dotcmd                                               321     319      -2
clearredir                                            37      30      -7
popredir                                             183     162     -21
redirect                                            1264    1233     -31
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/4 up/down: 63/-61)            Total: -23 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [PARSER] Do not show prompts in expandstr
Denys Vlasenko [Tue, 25 Oct 2016 19:17:52 +0000 (21:17 +0200)]
ash: [PARSER] Do not show prompts in expandstr

Upstream patch:

    Date: Thu, 27 Dec 2007 13:57:07 +1100
    [PARSER] Do not show prompts in expandstr

    Once I fixed the previous problem it became apparent that we never dealt
    with prompts with new-lines in them correctly.  The problem is that we
    showed a secondary prompt for each of them.

    This patch disables prompt generation in expandstr.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
function                                             old     new   delta
expandstr                                            102     127     +25

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [EXPAND] Removed herefd hack
Denys Vlasenko [Tue, 25 Oct 2016 19:10:20 +0000 (21:10 +0200)]
ash: [EXPAND] Removed herefd hack

Upstream commit:

    Date: Sun, 11 Nov 2007 15:00:06 +0800
    [EXPAND] Removed herefd hack

    The herefd hack goes back more than a decade.  it limits the amount of
    memory we have to allocate when expanding here-documents by writing the
    result out from time to time.  However, it's no longer safe because the
    stack is used to place intermediate results too and there we certainly
    don't want to write them out should we be short on memory.

    In any case, with today's computers we can afford to keep the entire
    result in memory and write them out at the end.

function                                             old     new   delta
redirect                                            1268    1264      -4
ash_main                                            1485    1478      -7
subevalvar                                          1157    1132     -25
growstackstr                                          54      24     -30
argstr                                              1192    1154     -38
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/5 up/down: 0/-104)           Total: -104 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [SHELL] Move flushall to the point just before _exit
Denys Vlasenko [Tue, 25 Oct 2016 18:49:53 +0000 (20:49 +0200)]
ash: [SHELL] Move flushall to the point just before _exit

Upstream commit:

    We need to flush at the very end in case we've generated any errors
    before that.  The flushall call cannot perform a longjmp so it's
    safe there.

    Date: Sat, 22 Sep 2007 20:50:21 +0800
    [SHELL] Move flushall to the point just before _exit

    We need to flush at the very end in case we've generated any errors
    before that.  The flushall call cannot perform a longjmp so it's
    safe there.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [EVAL] Let funcnode refer to a function definition, not its first command
Denys Vlasenko [Tue, 25 Oct 2016 18:26:02 +0000 (20:26 +0200)]
ash: [EVAL] Let funcnode refer to a function definition, not its first command

Upstream patch:

    Date: Tue, 15 Mar 2011 15:44:47 +0800
    [EVAL] Let funcnode refer to a function definition, not its first command

    It is not unrelated: I changed the meaning of struct funcnode's field n
    to refer to the function definition, rather than the list of the
    function's commands, because I needed to refer to the function
    definition node from evalfun, which only gets passed a funcnode. But it
    is something that could be applied independently (without being useful
    by itself), so I've attached it as a separate patch for easier review.

Signed-off-by: Harald van Dijk <harald@gigawatt.nl>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [REDIR] Remove EMFILE special case
Denys Vlasenko [Tue, 25 Oct 2016 17:04:39 +0000 (19:04 +0200)]
ash: [REDIR] Remove EMFILE special case

Upstream commit:

    Date: Sun, 6 May 2007 12:01:37 +1000
    [REDIR] Remove EMFILE special case

    No caller of copyfd need to ignore EMFILE so we can remove the special
    case and just let it call sh_error on any error.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [EVAL] Check exit for eval NSUBSHELL
Denys Vlasenko [Tue, 25 Oct 2016 16:19:39 +0000 (18:19 +0200)]
ash: [EVAL] Check exit for eval NSUBSHELL

Upstream commit:

    Date: Tue, 6 Jul 2010 17:50:37 +0800
    [PATCH 161/277] [EVAL] Check exit for eval NSUBSHELL

    Example:

    $ dash -c 'set -e; (false); echo here'
    here

    With this commit, dash exits 1 before echo.

    The bug was reported by Stefan Fritsch through
     http://bugs.debian.org/514863

Signed-off-by: Gerrit Pape <pape@smarden.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This was fixed differently in our tree:

    Date:   Fri Sep 16 19:04:02 2016 +0000
    ash: exit after subshell error when errexit option is set

    When "set -e" option is on, shell must exit when any command fails,
    including compound commands of the form (compound-list) executed in a
    subshell. Bash and dash shells have this behaviour.

    Also add a corresponding testcase.

Signed-off-by: Rostislav Skudnov <rostislav@tuxera.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: add comment explaining "set -e; $(cmd)" discrepancy
Denys Vlasenko [Tue, 25 Oct 2016 16:12:15 +0000 (18:12 +0200)]
ash: add comment explaining "set -e; $(cmd)" discrepancy

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: reduce code differences from upstream
Denys Vlasenko [Tue, 25 Oct 2016 15:40:25 +0000 (17:40 +0200)]
ash: reduce code differences from upstream

Upstream commit:

    Date: Wed, 8 Sep 2010 16:21:52 +0800
    [JOBS] Debug compile fix

    No point in tracing a no longer undeclared "ps->cmd", fixes:
    jobs.c: In function \u2018commandtext\u2019:
    jobs.c:1192: error: \u2018ps\u2019 undeclared (first use in this function)
    jobs.c:1192: error: (Each undeclared identifier is reported only once
    jobs.c:1192: error: for each function it appears in.)

Signed-off-by: maximilian attems <max@stro.at>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: remove unused EXSHELLPROC
Denys Vlasenko [Tue, 25 Oct 2016 15:26:56 +0000 (17:26 +0200)]
ash: remove unused EXSHELLPROC

Upstream commit:

    Date: Sun, 28 Nov 2010 20:47:07 +0800
    [BUILTIN] Stop documenting EXSHELLPROC

    At some point between ash 0.3.5-11.0.1 and ash 0.3.8-37, Debian
    ash stopped using the EXSHELLPROC exception to handle shell
    scripts without a magic number.

    Remove all remaining references to it to avoid confusion.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: [BUILTIN] Use EXEXIT in place of EXEXEC
Denys Vlasenko [Tue, 25 Oct 2016 15:24:25 +0000 (17:24 +0200)]
ash: [BUILTIN] Use EXEXIT in place of EXEXEC

Upstream commit:

    Date: Sun, 28 Nov 2010 20:44:37 +0800
    [BUILTIN] Use EXEXIT in place of EXEXEC

    The intended semantics of EXEXEC are identical to EXEXIT, so
    simplify by using EXEXIT directly.

    Functional change: in edge cases (exec within a trap handler),
    this causes the exit status from exec not to be clobbered.
    For example, without this patch:

     $ sh -c 'trap "exec nonexistent" EXIT'; echo $?
     exec: 1: nonexistent: not found
     0

    And with it:

     $ sh -c 'trap "exec nonexistent" EXIT'; echo $?
     exec: 1: nonexistent: not found
     127

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash,hush: set exit code 127 in "sh /does/not/exist" case
Denys Vlasenko [Tue, 25 Oct 2016 15:00:13 +0000 (17:00 +0200)]
ash,hush: set exit code 127 in "sh /does/not/exist" case

Upstream commit 1 for ash:

    [ERROR] Allow the originator of EXERROR to set the exit status

    Some errors have exit status values specified by POSIX and it is
    therefore desirable to be able to set the exit status at the EXERROR
    source rather than in main.c.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Upstream commit 2 for ash:

    [INPUT] Use exit status 127 when the script to run does not exist

    This commit makes dash exit with return code 127 instead of 2 if
    started as non-interactive shell with a non-existent command_file
    specified as argument (or a directory), as documented in
     http://www.opengroup.org/onlinepubs/009695399/utilities/sh.html#tag_04_128_14

    The wrong exit code was reported by Clint Adams and Jari Aalto through
     http://bugs.debian.org/548743
     http://bugs.debian.org/548687

Signed-off-by: Gerrit Pape <pape@smarden.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
NB: in fact, http://bugs.debian.org/548687 was not fixed by this:
"sh /dir/" thinks that EISDIR error on read is EOF, and exits 0.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoudhcpc: check read of overload option data byte to be within packet
Denys Vlasenko [Tue, 25 Oct 2016 12:25:52 +0000 (14:25 +0200)]
udhcpc: check read of overload option data byte to be within packet

function                                             old     new   delta
udhcp_get_option                                     220     225      +5

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoudhcpc: check read of option length byte to be within packet
Brian Foley [Tue, 25 Oct 2016 12:20:55 +0000 (14:20 +0200)]
udhcpc: check read of option length byte to be within packet

function                                             old     new   delta
udhcp_get_option                                     215     220      +5
udhcp_run_script                                     802     803      +1

Signed-off-by: Brian Foley <bpfoley@google.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agostrings: implement -t radix
Tito Ragusa [Mon, 24 Oct 2016 19:52:10 +0000 (21:52 +0200)]
strings: implement -t radix

v2: minor code cleanup, no changes.
v1: Implement -t radix option.
    Fix help text for -o option.

Signed-off-by: Tito Ragusa <farmatito@tiscali.it>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: explain EXP_REDIR and why we (dont) glob redir filenames
Denys Vlasenko [Mon, 24 Oct 2016 19:12:33 +0000 (21:12 +0200)]
ash: explain EXP_REDIR and why we (dont) glob redir filenames

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoawk: fix segfault on for loop syntax error
Brian Foley [Sat, 15 Oct 2016 13:45:40 +0000 (14:45 +0100)]
awk: fix segfault on for loop syntax error

Parsing "for()" segfaults as awk fails to find loop iteration expressions.

Signed-off-by: Brian Foley <bpfoley@google.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agolineedit: fix completion with applet names. closes 9361
Denys Vlasenko [Sun, 23 Oct 2016 23:25:05 +0000 (01:25 +0200)]
lineedit: fix completion with applet names. closes 9361

Patch by Ron Yorston <rmy@pobox.com>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: return exit status of nofork applets
Ron Yorston [Thu, 20 Oct 2016 11:24:02 +0000 (12:24 +0100)]
ash: return exit status of nofork applets

The commit 'ash: eval: Return status in eval functions' changed how
exit status is handled in eval functions.  The case of nofork
applets was missed, resulting in the incorrect status potentially
being returned for nofork applets when FEATURE_SH_NOFORK is enabled.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoexamples: update var_service/README again
Denys Vlasenko [Fri, 14 Oct 2016 17:02:11 +0000 (19:02 +0200)]
examples: update var_service/README again

Added "ps -AH e" example

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotypo fixes in doc
Denys Vlasenko [Fri, 14 Oct 2016 16:48:05 +0000 (18:48 +0200)]
typo fixes in doc

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoexamples: update var_service/README again
Denys Vlasenko [Fri, 14 Oct 2016 16:38:08 +0000 (18:38 +0200)]
examples: update var_service/README again

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoexamples: update var_service/README
Denys Vlasenko [Fri, 14 Oct 2016 16:22:50 +0000 (18:22 +0200)]
examples: update var_service/README

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotelnetd: ifdef out a buggy error handling code path
Denys Vlasenko [Thu, 13 Oct 2016 14:17:06 +0000 (16:17 +0200)]
telnetd: ifdef out a buggy error handling code path

Here, not handling the error is would just eat one input 0xff char.
Correct handling would need even more corner case handling,
as-is buggy handling corrupts the buffer.
Since we just been told by kernel that pty is ready,
EAGAIN should not be happening here anyway.

function                                             old     new   delta
telnetd_main                                        1798    1785     -13

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agocpio: fix restoration of file ownership, closes 9306
Denys Vlasenko [Wed, 12 Oct 2016 18:56:46 +0000 (20:56 +0200)]
cpio: fix restoration of file ownership, closes 9306

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotelnet: code shrink
Denys Vlasenko [Wed, 12 Oct 2016 18:42:58 +0000 (20:42 +0200)]
telnet: code shrink

put_iac2(w,c) is mostly used with constants, fold them into one arg

function                                             old     new   delta
put_iac2_merged                                        -      46     +46
telnet_main                                         1603    1583     -20
con_escape                                           285     257     -28
put_iac2                                              50       -     -50
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 0/2 up/down: 46/-98)            Total: -52 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotelnetd: add another handshake example in comments, no code changes
Denys Vlasenko [Wed, 12 Oct 2016 18:09:22 +0000 (20:09 +0200)]
telnetd: add another handshake example in comments, no code changes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotelnetd: treat all 2-byte IACs in 240..249 range as NOPs.
Denys Vlasenko [Wed, 12 Oct 2016 17:41:33 +0000 (19:41 +0200)]
telnetd: treat all 2-byte IACs in 240..249 range as NOPs.

A bit of future-proofing. Some of them can stand just being ignored.

function                                             old     new   delta
telnetd_main                                        1791    1798      +7

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotelnetd: fix corner case of input processing of 0xff bytes
Denys Vlasenko [Wed, 12 Oct 2016 17:13:46 +0000 (19:13 +0200)]
telnetd: fix corner case of input processing of 0xff bytes

I managed to reproduce the bug, with some difficulty.

function                                             old     new   delta
telnetd_main                                        1780    1791     +11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotelnetd: fix a corner case where CRLF->CR translation can misbehave
Denys Vlasenko [Wed, 12 Oct 2016 15:36:57 +0000 (17:36 +0200)]
telnetd: fix a corner case where CRLF->CR translation can misbehave

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotelnetd: fix handling of short writes to pty
Denys Vlasenko [Wed, 12 Oct 2016 12:54:10 +0000 (14:54 +0200)]
telnetd: fix handling of short writes to pty

If a write to pty is short, remove_iacs() can be run on a buffer repeatedly.
This, for example, can eat 0xff chars (IACs, in telnet terms).

Rework the logic to handle IACs in a special "write to pty" function.

function                                             old     new   delta
telnetd_main                                        1662    1750     +88

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agomore: accept and ignore a bunch of options
Denys Vlasenko [Tue, 11 Oct 2016 13:29:38 +0000 (15:29 +0200)]
more: accept and ignore a bunch of options

Alpine Linux stumbled over "more -s":
http://bugs.alpinelinux.org/issues/5190

function                                             old     new   delta
more_main                                            857     872     +15

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoifupdown: rewrite state file atomically
Denys Vlasenko [Sun, 9 Oct 2016 21:04:16 +0000 (23:04 +0200)]
ifupdown: rewrite state file atomically

By user's request.
Decided to not use fcntl(F_SETLKW) in lieu of problems with locking
on networked filesystems. The existence of /var/run/ifstate.new
is treated as a write lock. rename() provides atomicity.

function                                             old     new   delta
ifupdown_main                                       1019    1122    +103

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agotcpsvd: don't keep shared fd open if fd limit is reached. closes 9331
Denys Vlasenko [Fri, 7 Oct 2016 13:56:47 +0000 (15:56 +0200)]
tcpsvd: don't keep shared fd open if fd limit is reached. closes 9331

Also, much improved help text.

function                                             old     new   delta
packed_usage                                       30652   30851    +199
tcpudpsvd_main                                      1782    1784      +2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: all blocks in function node copying must be SHELL_ALIGNed
Denys Vlasenko [Fri, 7 Oct 2016 02:28:33 +0000 (04:28 +0200)]
ash: all blocks in function node copying must be SHELL_ALIGNed

Previous commit probably introduced a bug:
non-matching size calculation in size counting and
actual copying caused by SHELL_ALIGN being applied differently!
This won't bite if string sizes are also SHELL_ALIGNed.
Thus fixing.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash testsuite: fix false positives
Denys Vlasenko [Fri, 7 Oct 2016 02:07:05 +0000 (04:07 +0200)]
ash testsuite: fix false positives

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: get rid of two global data variables
Denys Vlasenko [Fri, 7 Oct 2016 02:05:15 +0000 (04:05 +0200)]
ash: get rid of two global data variables

function                                             old     new   delta
calcsize                                             126     147     +21
funcstring_end                                         -       4      +4
sizenodelist                                          28      24      -4
funcstringsize                                         4       -      -4
funcstring                                             4       -      -4
funcblocksize                                          4       -      -4
nodeckstrdup                                          48      39      -9
evaltree                                             828     788     -40
------------------------------------------------------------------------------
(add/remove: 1/3 grow/shrink: 1/4 up/down: 25/-105)           Total: -40 bytes
   text    data     bss     dec     hex filename
 943376     916   14292  958584   ea078 busybox_old
 943344     916   14284  958544   ea050 busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoash: comment out free(p) just before _exit, tweak some outdated comments
Denys Vlasenko [Fri, 7 Oct 2016 01:17:28 +0000 (03:17 +0200)]
ash: comment out free(p) just before _exit, tweak some outdated comments

Planned to sync exitshell up to dash, turned out ours is better :)

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agowget: there is no -s option in GNU Wget 1.18, we should be compatible
Denys Vlasenko [Tue, 4 Oct 2016 02:23:09 +0000 (04:23 +0200)]
wget: there is no -s option in GNU Wget 1.18, we should be compatible

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agodhcp: merge leases.c and static_leases.c into dhcpd.c
Denys Vlasenko [Mon, 3 Oct 2016 22:51:38 +0000 (00:51 +0200)]
dhcp: merge leases.c and static_leases.c into dhcpd.c

function                                             old     new   delta
send_offer                                           292     461    +169
udhcpd_main                                         1531    1588     +57
read_leases                                          330     332      +2
add_lease                                            314     312      -2
find_lease_by_mac                                     68       -     -68
find_free_or_expired_nip                             174       -    -174
------------------------------------------------------------------------------
(add/remove: 0/4 grow/shrink: 3/1 up/down: 228/-244)          Total: -16 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agodhcp: merge most of static_leases.c into dhcpd.c
Denys Vlasenko [Mon, 3 Oct 2016 22:43:14 +0000 (00:43 +0200)]
dhcp: merge most of static_leases.c into dhcpd.c

function                                             old     new   delta
read_staticlease                                     121     222    +101
add_static_lease                                      48       -     -48
log_static_leases                                     68       -     -68
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 1/0 up/down: 101/-116)          Total: -15 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agodhcp: merge files.c into dhcpd.c
Denys Vlasenko [Mon, 3 Oct 2016 22:37:50 +0000 (00:37 +0200)]
dhcp: merge files.c into dhcpd.c

Added NOINLINE to two function, since my version of gcc would actualy increase
code size otherwise.

I see no size changes.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agoexamples: add example of a DHCP server
Denys Vlasenko [Mon, 3 Oct 2016 21:43:44 +0000 (23:43 +0200)]
examples: add example of a DHCP server

As usual, by multiplying directories - "dhcpd_eth0", "dhcpd_wlan1"
you can run many servers on different interfaces.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
8 years agohush: add commented-out debug printouts in "memleak" built-in
Denys Vlasenko [Mon, 3 Oct 2016 15:42:53 +0000 (17:42 +0200)]
hush: add commented-out debug printouts in "memleak" built-in

Allocation addresses of malloc() are jittery,
thought I had a mem leak in hush, but it was malloc variability.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>