sh: Do parameter expansion on ENV before using it.

This is required by POSIX, and allows things like ENV=\$HOME/.shrc.

Note that tilde expansion is explicitly not performed.
This commit is contained in:
Jilles Tjoelker 2011-06-10 22:42:00 +00:00
parent e4a06afe93
commit 1a62d8843d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=222957
3 changed files with 24 additions and 7 deletions

View File

@ -78,7 +78,7 @@ int rootshell;
struct jmploc main_handler; struct jmploc main_handler;
int localeisutf8, initial_localeisutf8; int localeisutf8, initial_localeisutf8;
static void read_profile(const char *); static void read_profile(char *);
static char *find_dot_file(char *); static char *find_dot_file(char *);
/* /*
@ -92,7 +92,7 @@ static char *find_dot_file(char *);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct stackmark smark; struct stackmark smark, smark2;
volatile int state; volatile int state;
char *shinit; char *shinit;
@ -139,6 +139,7 @@ main(int argc, char *argv[])
rootshell = 1; rootshell = 1;
init(); init();
setstackmark(&smark); setstackmark(&smark);
setstackmark(&smark2);
procargs(argc, argv); procargs(argc, argv);
pwd_init(iflag); pwd_init(iflag);
if (iflag) if (iflag)
@ -163,6 +164,7 @@ main(int argc, char *argv[])
} }
state3: state3:
state = 4; state = 4;
popstackmark(&smark2);
if (minusc) { if (minusc) {
evalstring(minusc, sflag ? 0 : EV_EXIT); evalstring(minusc, sflag ? 0 : EV_EXIT);
} }
@ -235,12 +237,16 @@ cmdloop(int top)
*/ */
static void static void
read_profile(const char *name) read_profile(char *name)
{ {
int fd; int fd;
const char *expandedname;
expandedname = expandstr(name);
if (expandedname == NULL)
return;
INTOFF; INTOFF;
if ((fd = open(name, O_RDONLY)) >= 0) if ((fd = open(expandedname, O_RDONLY)) >= 0)
setinputfd(fd, 1); setinputfd(fd, 1);
INTON; INTON;
if (fd < 0) if (fd < 0)

View File

@ -32,7 +32,7 @@
.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 .\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd June 9, 2011 .Dd June 10, 2011
.Dt SH 1 .Dt SH 1
.Os .Os
.Sh NAME .Sh NAME
@ -124,8 +124,8 @@ If the environment variable
.Ev ENV .Ev ENV
is set on entry to a shell, or is set in the is set on entry to a shell, or is set in the
.Pa .profile .Pa .profile
of a login shell, the shell then reads commands from the file named in of a login shell, the shell then subjects its value to parameter expansion
.Ev ENV . and arithmetic expansion and reads commands from the named file.
Therefore, a user should place commands that are to be executed only Therefore, a user should place commands that are to be executed only
at login time in the at login time in the
.Pa .profile .Pa .profile

View File

@ -0,0 +1,11 @@
# $FreeBSD$
export key='must contain this'
unset x
r=$(ENV="\${x?\$key}" ${SH} -i +m 2>&1 >/dev/null <<\EOF
exit 0
EOF
) && case $r in
*"$key"*) true ;;
*) false ;;
esac