sh: Constify internal representation in expand.c.
Forbid (temporary or permanent) modifications of the strings in NARG nodes during expansion. Tilde expansion now needs to copy the username for the terminating '\0'.
This commit is contained in:
parent
18a2ccd22d
commit
52d5897d50
@ -91,13 +91,13 @@ struct worddest {
|
|||||||
static char *expdest; /* output of current string */
|
static char *expdest; /* output of current string */
|
||||||
static struct nodelist *argbackq; /* list of back quote expressions */
|
static struct nodelist *argbackq; /* list of back quote expressions */
|
||||||
|
|
||||||
static char *argstr(char *, int, struct worddest *);
|
static const char *argstr(const char *, int, struct worddest *);
|
||||||
static char *exptilde(char *, int);
|
static const char *exptilde(const char *, int);
|
||||||
static char *expari(char *, int, struct worddest *);
|
static const char *expari(const char *, int, struct worddest *);
|
||||||
static void expbackq(union node *, int, int, struct worddest *);
|
static void expbackq(union node *, int, int, struct worddest *);
|
||||||
static void subevalvar_trim(char *, int, int, int);
|
static void subevalvar_trim(const char *, int, int, int);
|
||||||
static int subevalvar_misc(char *, const char *, int, int, int);
|
static int subevalvar_misc(const char *, const char *, int, int, int);
|
||||||
static char *evalvar(char *, int, struct worddest *);
|
static const char *evalvar(const char *, int, struct worddest *);
|
||||||
static int varisset(const char *, int);
|
static int varisset(const char *, int);
|
||||||
static void strtodest(const char *, int, int, int, struct worddest *);
|
static void strtodest(const char *, int, int, int, struct worddest *);
|
||||||
static void reprocess(int, int, int, int, struct worddest *);
|
static void reprocess(int, int, int, int, struct worddest *);
|
||||||
@ -262,8 +262,8 @@ expandarg(union node *arg, struct arglist *arglist, int flag)
|
|||||||
*
|
*
|
||||||
* If EXP_SPLIT is set, dst receives any complete words produced.
|
* If EXP_SPLIT is set, dst receives any complete words produced.
|
||||||
*/
|
*/
|
||||||
static char *
|
static const char *
|
||||||
argstr(char *p, int flag, struct worddest *dst)
|
argstr(const char *p, int flag, struct worddest *dst)
|
||||||
{
|
{
|
||||||
char c;
|
char c;
|
||||||
int quotes = flag & (EXP_GLOB | EXP_CASE); /* do CTLESC */
|
int quotes = flag & (EXP_GLOB | EXP_CASE); /* do CTLESC */
|
||||||
@ -352,12 +352,15 @@ argstr(char *p, int flag, struct worddest *dst)
|
|||||||
* Perform tilde expansion, placing the result in the stack string and
|
* Perform tilde expansion, placing the result in the stack string and
|
||||||
* returning the next position in the input string to process.
|
* returning the next position in the input string to process.
|
||||||
*/
|
*/
|
||||||
static char *
|
static const char *
|
||||||
exptilde(char *p, int flag)
|
exptilde(const char *p, int flag)
|
||||||
{
|
{
|
||||||
char c, *startp = p;
|
char c;
|
||||||
|
const char *startp = p;
|
||||||
|
const char *user;
|
||||||
struct passwd *pw;
|
struct passwd *pw;
|
||||||
char *home;
|
char *home;
|
||||||
|
int len;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
c = *p;
|
c = *p;
|
||||||
@ -377,14 +380,17 @@ exptilde(char *p, int flag)
|
|||||||
case '\0':
|
case '\0':
|
||||||
case '/':
|
case '/':
|
||||||
case CTLENDVAR:
|
case CTLENDVAR:
|
||||||
*p = '\0';
|
len = p - startp - 1;
|
||||||
if (*(startp+1) == '\0') {
|
STPUTBIN(startp + 1, len, expdest);
|
||||||
|
STACKSTRNUL(expdest);
|
||||||
|
user = expdest - len;
|
||||||
|
if (*user == '\0') {
|
||||||
home = lookupvar("HOME");
|
home = lookupvar("HOME");
|
||||||
} else {
|
} else {
|
||||||
pw = getpwnam(startp+1);
|
pw = getpwnam(user);
|
||||||
home = pw != NULL ? pw->pw_dir : NULL;
|
home = pw != NULL ? pw->pw_dir : NULL;
|
||||||
}
|
}
|
||||||
*p = c;
|
STADJUST(-len, expdest);
|
||||||
if (home == NULL || *home == '\0')
|
if (home == NULL || *home == '\0')
|
||||||
return (startp);
|
return (startp);
|
||||||
strtodest(home, flag, VSNORMAL, 1, NULL);
|
strtodest(home, flag, VSNORMAL, 1, NULL);
|
||||||
@ -398,8 +404,8 @@ exptilde(char *p, int flag)
|
|||||||
/*
|
/*
|
||||||
* Expand arithmetic expression.
|
* Expand arithmetic expression.
|
||||||
*/
|
*/
|
||||||
static char *
|
static const char *
|
||||||
expari(char *p, int flag, struct worddest *dst)
|
expari(const char *p, int flag, struct worddest *dst)
|
||||||
{
|
{
|
||||||
char *q, *start;
|
char *q, *start;
|
||||||
arith_t result;
|
arith_t result;
|
||||||
@ -532,7 +538,7 @@ recordleft(const char *str, const char *loc, char *startp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
subevalvar_trim(char *p, int strloc, int subtype, int startloc)
|
subevalvar_trim(const char *p, int strloc, int subtype, int startloc)
|
||||||
{
|
{
|
||||||
char *startp;
|
char *startp;
|
||||||
char *loc = NULL;
|
char *loc = NULL;
|
||||||
@ -606,7 +612,7 @@ subevalvar_trim(char *p, int strloc, int subtype, int startloc)
|
|||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
subevalvar_misc(char *p, const char *var, int subtype, int startloc,
|
subevalvar_misc(const char *p, const char *var, int subtype, int startloc,
|
||||||
int varflags)
|
int varflags)
|
||||||
{
|
{
|
||||||
char *startp;
|
char *startp;
|
||||||
@ -645,12 +651,12 @@ subevalvar_misc(char *p, const char *var, int subtype, int startloc,
|
|||||||
* input string.
|
* input string.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static char *
|
static const char *
|
||||||
evalvar(char *p, int flag, struct worddest *dst)
|
evalvar(const char *p, int flag, struct worddest *dst)
|
||||||
{
|
{
|
||||||
int subtype;
|
int subtype;
|
||||||
int varflags;
|
int varflags;
|
||||||
char *var;
|
const char *var;
|
||||||
const char *val;
|
const char *val;
|
||||||
int patloc;
|
int patloc;
|
||||||
int c;
|
int c;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user