NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

bin/45430: ash uses argv[0] as $0 for scripts without #!



>Number:         45430
>Category:       bin
>Synopsis:       ash uses argv[0] as $0 for scripts without #!
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Oct 05 13:40:01 +0000 2011
>Originator:     Nikolai Kondrashov
>Release:        
>Organization:
NVIDIA
>Environment:
>Description:
Initially discovered in Android branch.
Problem in NetBSD upstream confirmed by inspecting the source code.

ash shell uses argv[0] as $0 in scripts lacking shebang line, located during 
$PATH lookup, while using full file path for the scripts with shebang line.
>How-To-Repeat:
cat > /usr/local/bin/shebang-test <<END
#!/bin/sh
echo \$0
END
chmod 755 /usr/local/bin/shebang-test
cat > /usr/local/bin/no-shebang-test <<END
echo \$0
END
chmod 755 /usr/local/bin/no-shebang-test

# Expected

# shebang-test
/usr/local/bin/shebang-test
# no-shebang-test
/usr/local/bin/no-shebang-test

# Actual

# shebang-test
/usr/local/bin/shebang-test
# no-shebang-test
no-shebang-test

>Fix:
The following patch fixes the problem.

diff -ru a/exec.c b/exec.c
--- a/exec.c    2008-10-16 18:31:05.000000000 +0300
+++ b/exec.c    2011-10-05 16:33:49.937465310 +0300
@@ -187,7 +187,7 @@
 #endif
                initshellproc();
                setinputfile(cmd, 0);
-               commandname = arg0 = savestr(argv[0]);
+               commandname = arg0 = savestr(cmd);
 #ifndef BSD
                pgetc(); pungetc();             /* fill up input buffer */
                p = parsenextc;



Home | Main Index | Thread Index | Old Index