Home | History | Annotate | Line # | Download | only in sh
var.c revision 1.65
      1 /*	$NetBSD: var.c,v 1.65 2017/07/12 19:06:16 kre Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kenneth Almquist.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
     39 #else
     40 __RCSID("$NetBSD: var.c,v 1.65 2017/07/12 19:06:16 kre Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <stdio.h>
     45 #include <unistd.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 #include <paths.h>
     49 #include <limits.h>
     50 #include <time.h>
     51 #include <pwd.h>
     52 #include <fcntl.h>
     53 
     54 /*
     55  * Shell variables.
     56  */
     57 
     58 #include "shell.h"
     59 #include "output.h"
     60 #include "expand.h"
     61 #include "nodes.h"	/* for other headers */
     62 #include "eval.h"	/* defines cmdenviron */
     63 #include "exec.h"
     64 #include "syntax.h"
     65 #include "options.h"
     66 #include "builtins.h"
     67 #include "mail.h"
     68 #include "var.h"
     69 #include "memalloc.h"
     70 #include "error.h"
     71 #include "mystring.h"
     72 #include "parser.h"
     73 #include "show.h"
     74 #include "machdep.h"
     75 #ifndef SMALL
     76 #include "myhistedit.h"
     77 #endif
     78 
     79 #ifdef SMALL
     80 #define VTABSIZE 39
     81 #else
     82 #define VTABSIZE 517
     83 #endif
     84 
     85 
     86 struct varinit {
     87 	struct var *var;
     88 	int flags;
     89 	const char *text;
     90 	union var_func_union v_u;
     91 };
     92 #define	func v_u.set_func
     93 #define	rfunc v_u.ref_func
     94 
     95 char *get_lineno(struct var *);
     96 
     97 #ifndef SMALL
     98 char *get_tod(struct var *);
     99 char *get_hostname(struct var *);
    100 char *get_seconds(struct var *);
    101 char *get_euser(struct var *);
    102 char *get_random(struct var *);
    103 #endif
    104 
    105 struct localvar *localvars;
    106 
    107 #ifndef SMALL
    108 struct var vhistsize;
    109 struct var vterm;
    110 struct var editrc;
    111 struct var ps_lit;
    112 #endif
    113 struct var vifs;
    114 struct var vmail;
    115 struct var vmpath;
    116 struct var vpath;
    117 struct var vps1;
    118 struct var vps2;
    119 struct var vps4;
    120 struct var vvers;
    121 struct var voptind;
    122 struct var line_num;
    123 #ifndef SMALL
    124 struct var tod;
    125 struct var host_name;
    126 struct var seconds;
    127 struct var euname;
    128 struct var random_num;
    129 
    130 intmax_t sh_start_time;
    131 #endif
    132 
    133 struct var line_num;
    134 int line_number;
    135 int funclinebase = 0;
    136 int funclineabs = 0;
    137 
    138 char ifs_default[] = " \t\n";
    139 
    140 const struct varinit varinit[] = {
    141 #ifndef SMALL
    142 	{ &vhistsize,	VSTRFIXED|VTEXTFIXED|VUNSET,	"HISTSIZE=",
    143 	   { .set_func= sethistsize } },
    144 #endif
    145 	{ &vifs,	VSTRFIXED|VTEXTFIXED,		"IFS= \t\n",
    146 	   { NULL } },
    147 	{ &vmail,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAIL=",
    148 	   { NULL } },
    149 	{ &vmpath,	VSTRFIXED|VTEXTFIXED|VUNSET,	"MAILPATH=",
    150 	   { NULL } },
    151 	{ &vvers,	VSTRFIXED|VTEXTFIXED|VNOEXPORT, "NETBSD_SHELL=",
    152 	   { NULL } },
    153 	{ &vpath,	VSTRFIXED|VTEXTFIXED,		"PATH=" _PATH_DEFPATH,
    154 	   { .set_func= changepath } },
    155 	/*
    156 	 * vps1 depends on uid
    157 	 */
    158 	{ &vps2,	VSTRFIXED|VTEXTFIXED,		"PS2=> ",
    159 	   { NULL } },
    160 	{ &vps4,	VSTRFIXED|VTEXTFIXED,		"PS4=+ ",
    161 	   { NULL } },
    162 #ifndef SMALL
    163 	{ &vterm,	VSTRFIXED|VTEXTFIXED|VUNSET,	"TERM=",
    164 	   { .set_func= setterm } },
    165 	{ &editrc, 	VSTRFIXED|VTEXTFIXED|VUNSET,	"EDITRC=",
    166 	   { .set_func= set_editrc } },
    167 	{ &ps_lit, 	VSTRFIXED|VTEXTFIXED|VUNSET,	"PSlit=",
    168 	   { .set_func= set_prompt_lit } },
    169 #endif
    170 	{ &voptind,	VSTRFIXED|VTEXTFIXED|VNOFUNC,	"OPTIND=1",
    171 	   { .set_func= getoptsreset } },
    172 	{ &line_num,	VSTRFIXED|VTEXTFIXED|VFUNCREF,	"LINENO=1",
    173 	   { .ref_func= get_lineno } },
    174 #ifndef SMALL
    175 	{ &tod,		VSTRFIXED|VTEXTFIXED|VFUNCREF,	"ToD=",
    176 	   { .ref_func= get_tod } },
    177 	{ &host_name,	VSTRFIXED|VTEXTFIXED|VFUNCREF,	"HOSTNAME=",
    178 	   { .ref_func= get_hostname } },
    179 	{ &seconds,	VSTRFIXED|VTEXTFIXED|VFUNCREF,	"SECONDS=",
    180 	   { .ref_func= get_seconds } },
    181 	{ &euname,	VSTRFIXED|VTEXTFIXED|VFUNCREF,	"EUSER=",
    182 	   { .ref_func= get_euser } },
    183 	{ &random_num,	VSTRFIXED|VTEXTFIXED|VFUNCREF,	"RANDOM=",
    184 	   { .ref_func= get_random } },
    185 #endif
    186 	{ NULL,	0,				NULL,
    187 	   { NULL } }
    188 };
    189 
    190 struct var *vartab[VTABSIZE];
    191 
    192 STATIC int strequal(const char *, const char *);
    193 STATIC struct var *find_var(const char *, struct var ***, int *);
    194 
    195 /*
    196  * Initialize the varable symbol tables and import the environment
    197  */
    198 
    199 #ifdef mkinit
    200 INCLUDE <stdio.h>
    201 INCLUDE <unistd.h>
    202 INCLUDE <time.h>
    203 INCLUDE "var.h"
    204 INCLUDE "version.h"
    205 MKINIT char **environ;
    206 INIT {
    207 	char **envp;
    208 	char buf[64];
    209 
    210 #ifndef SMALL
    211 	sh_start_time = (intmax_t)time((time_t *)0);
    212 #endif
    213 	/*
    214 	 * Set up our default variables and their values.
    215 	 */
    216 	initvar();
    217 
    218 	/*
    219 	 * Import variables from the environment, which will
    220 	 * override anything initialised just previously.
    221 	 */
    222 	for (envp = environ ; *envp ; envp++) {
    223 		if (strchr(*envp, '=')) {
    224 			setvareq(*envp, VEXPORT|VTEXTFIXED);
    225 		}
    226 	}
    227 
    228 	/*
    229 	 * Set variables which override anything read from environment.
    230 	 *
    231 	 * PPID is readonly
    232 	 * Always default IFS
    233 	 * PSc indicates the root/non-root status of this shell.
    234 	 * NETBSD_SHELL is a constant (readonly), and is never exported
    235 	 * START_TIME belongs only to this shell.
    236 	 * LINENO is simply magic...
    237 	 */
    238 	snprintf(buf, sizeof(buf), "%d", (int)getppid());
    239 	setvar("PPID", buf, VREADONLY);
    240 	setvar("IFS", ifs_default, VTEXTFIXED);
    241 	setvar("PSc", (geteuid() == 0 ? "#" : "$"), VTEXTFIXED);
    242 
    243 #ifndef SMALL
    244 	snprintf(buf, sizeof(buf), "%jd", sh_start_time);
    245 	setvar("START_TIME", buf, VTEXTFIXED);
    246 #endif
    247 
    248 	setvar("NETBSD_SHELL", NETBSD_SHELL
    249 #ifdef BUILD_DATE
    250 		" BUILD:" BUILD_DATE
    251 #endif
    252 #ifdef DEBUG
    253 		" DEBUG"
    254 #endif
    255 #if !defined(JOBS) || JOBS == 0
    256 		" -JOBS"
    257 #endif
    258 #ifndef DO_SHAREDVFORK
    259 		" -VFORK"
    260 #endif
    261 #ifdef SMALL
    262 		" SMALL"
    263 #endif
    264 #ifdef TINY
    265 		" TINY"
    266 #endif
    267 #ifdef OLD_TTY_DRIVER
    268 		" OLD_TTY"
    269 #endif
    270 #ifdef SYSV
    271 		" SYSV"
    272 #endif
    273 #ifndef BSD
    274 		" -BSD"
    275 #endif
    276 #ifdef BOGUS_NOT_COMMAND
    277 		" BOGUS_NOT"
    278 #endif
    279 		    , VTEXTFIXED|VREADONLY|VNOEXPORT);
    280 
    281 	setvar("LINENO", "1", VTEXTFIXED);
    282 }
    283 #endif
    284 
    285 
    286 /*
    287  * This routine initializes the builtin variables.  It is called when the
    288  * shell is initialized and again when a shell procedure is spawned.
    289  */
    290 
    291 void
    292 initvar(void)
    293 {
    294 	const struct varinit *ip;
    295 	struct var *vp;
    296 	struct var **vpp;
    297 
    298 	for (ip = varinit ; (vp = ip->var) != NULL ; ip++) {
    299 		if (find_var(ip->text, &vpp, &vp->name_len) != NULL)
    300 			continue;
    301 		vp->next = *vpp;
    302 		*vpp = vp;
    303 		vp->text = strdup(ip->text);
    304 		vp->flags = (ip->flags & ~VTEXTFIXED) | VSTRFIXED;
    305 		vp->v_u = ip->v_u;
    306 	}
    307 	/*
    308 	 * PS1 depends on uid
    309 	 */
    310 	if (find_var("PS1", &vpp, &vps1.name_len) == NULL) {
    311 		vps1.next = *vpp;
    312 		*vpp = &vps1;
    313 		vps1.flags = VSTRFIXED;
    314 		vps1.text = NULL;
    315 		choose_ps1();
    316 	}
    317 }
    318 
    319 void
    320 choose_ps1(void)
    321 {
    322 	if ((vps1.flags & (VTEXTFIXED|VSTACK)) == 0)
    323 		free(vps1.text);
    324 	vps1.text = strdup(geteuid() ? "PS1=$ " : "PS1=# ");
    325 	vps1.flags &= ~(VTEXTFIXED|VSTACK);
    326 
    327 	/*
    328 	 * Update PSc whenever we feel the need to update PS1
    329 	 */
    330 	setvarsafe("PSc", (geteuid() == 0 ? "#" : "$"), 0);
    331 }
    332 
    333 /*
    334  * Safe version of setvar, returns 1 on success 0 on failure.
    335  */
    336 
    337 int
    338 setvarsafe(const char *name, const char *val, int flags)
    339 {
    340 	struct jmploc jmploc;
    341 	struct jmploc * const savehandler = handler;
    342 	int volatile err = 0;
    343 
    344 	if (setjmp(jmploc.loc))
    345 		err = 1;
    346 	else {
    347 		handler = &jmploc;
    348 		setvar(name, val, flags);
    349 	}
    350 	handler = savehandler;
    351 	return err;
    352 }
    353 
    354 /*
    355  * Set the value of a variable.  The flags argument is ored with the
    356  * flags of the variable.  If val is NULL, the variable is unset.
    357  *
    358  * This always copies name and val when setting a variable, so
    359  * the source strings can be from anywhere, and are no longer needed
    360  * after this function returns.  The VTEXTFIXED and VSTACK flags should
    361  * not be used (but just in case they were, clear them.)
    362  */
    363 
    364 void
    365 setvar(const char *name, const char *val, int flags)
    366 {
    367 	const char *p;
    368 	const char *q;
    369 	char *d;
    370 	int len;
    371 	int namelen;
    372 	char *nameeq;
    373 	int isbad;
    374 
    375 	isbad = 0;
    376 	p = name;
    377 	if (! is_name(*p))
    378 		isbad = 1;
    379 	p++;
    380 	for (;;) {
    381 		if (! is_in_name(*p)) {
    382 			if (*p == '\0' || *p == '=')
    383 				break;
    384 			isbad = 1;
    385 		}
    386 		p++;
    387 	}
    388 	namelen = p - name;
    389 	if (isbad)
    390 		error("%.*s: bad variable name", namelen, name);
    391 	len = namelen + 2;		/* 2 is space for '=' and '\0' */
    392 	if (val == NULL) {
    393 		flags |= VUNSET;
    394 	} else {
    395 		len += strlen(val);
    396 	}
    397 	d = nameeq = ckmalloc(len);
    398 	q = name;
    399 	while (--namelen >= 0)
    400 		*d++ = *q++;
    401 	*d++ = '=';
    402 	*d = '\0';
    403 	if (val)
    404 		scopy(val, d);
    405 	setvareq(nameeq, flags & ~(VTEXTFIXED | VSTACK));
    406 }
    407 
    408 
    409 
    410 /*
    411  * Same as setvar except that the variable and value are passed in
    412  * the first argument as name=value.  Since the first argument will
    413  * be actually stored in the table, it should not be a string that
    414  * will go away.   The flags (VTEXTFIXED or VSTACK) can be used to
    415  * indicate the source of the string (if neither is set, the string will
    416  * eventually be free()d when a replacement value is assigned.)
    417  */
    418 
    419 void
    420 setvareq(char *s, int flags)
    421 {
    422 	struct var *vp, **vpp;
    423 	int nlen;
    424 
    425 	if (aflag && !(flags & VNOEXPORT))
    426 		flags |= VEXPORT;
    427 	vp = find_var(s, &vpp, &nlen);
    428 	if (vp != NULL) {
    429 		if (vp->flags & VREADONLY) {
    430 			if ((flags & (VTEXTFIXED|VSTACK)) == 0)
    431 				ckfree(s);
    432 			if (flags & VNOERROR)
    433 				return;
    434 			error("%.*s: is read only", vp->name_len, vp->text);
    435 		}
    436 		if (flags & VNOSET) {
    437 			if ((flags & (VTEXTFIXED|VSTACK)) == 0)
    438 				ckfree(s);
    439 			return;
    440 		}
    441 
    442 		INTOFF;
    443 
    444 		if (vp->func && !(vp->flags & VFUNCREF) && !(flags & VNOFUNC))
    445 			(*vp->func)(s + vp->name_len + 1);
    446 
    447 		if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
    448 			ckfree(vp->text);
    449 
    450 		vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
    451 		if (flags & VNOEXPORT)
    452 			vp->flags &= ~VEXPORT;
    453 		if (vp->flags & VNOEXPORT)
    454 			flags &= ~VEXPORT;
    455 		vp->flags |= flags & ~VNOFUNC;
    456 		vp->text = s;
    457 
    458 		/*
    459 		 * We could roll this to a function, to handle it as
    460 		 * a regular variable function callback, but why bother?
    461 		 */
    462 		if (vp == &vmpath || (vp == &vmail && ! mpathset()))
    463 			chkmail(1);
    464 
    465 		INTON;
    466 		return;
    467 	}
    468 	/* not found */
    469 	if (flags & VNOSET) {
    470 		if ((flags & (VTEXTFIXED|VSTACK)) == 0)
    471 			ckfree(s);
    472 		return;
    473 	}
    474 	vp = ckmalloc(sizeof (*vp));
    475 	vp->flags = flags & ~(VNOFUNC|VFUNCREF|VSTRFIXED);
    476 	vp->text = s;
    477 	vp->name_len = nlen;
    478 	vp->next = *vpp;
    479 	vp->func = NULL;
    480 	*vpp = vp;
    481 }
    482 
    483 
    484 
    485 /*
    486  * Process a linked list of variable assignments.
    487  */
    488 
    489 void
    490 listsetvar(struct strlist *list, int flags)
    491 {
    492 	struct strlist *lp;
    493 
    494 	INTOFF;
    495 	for (lp = list ; lp ; lp = lp->next) {
    496 		setvareq(savestr(lp->text), flags);
    497 	}
    498 	INTON;
    499 }
    500 
    501 void
    502 listmklocal(struct strlist *list, int flags)
    503 {
    504 	struct strlist *lp;
    505 
    506 	for (lp = list ; lp ; lp = lp->next)
    507 		mklocal(lp->text, flags);
    508 }
    509 
    510 
    511 /*
    512  * Find the value of a variable.  Returns NULL if not set.
    513  */
    514 
    515 char *
    516 lookupvar(const char *name)
    517 {
    518 	struct var *v;
    519 
    520 	v = find_var(name, NULL, NULL);
    521 	if (v == NULL || v->flags & VUNSET)
    522 		return NULL;
    523 	if (v->rfunc && (v->flags & VFUNCREF) != 0)
    524 		return (*v->rfunc)(v) + v->name_len + 1;
    525 	return v->text + v->name_len + 1;
    526 }
    527 
    528 
    529 
    530 /*
    531  * Search the environment of a builtin command.  If the second argument
    532  * is nonzero, return the value of a variable even if it hasn't been
    533  * exported.
    534  */
    535 
    536 char *
    537 bltinlookup(const char *name, int doall)
    538 {
    539 	struct strlist *sp;
    540 	struct var *v;
    541 
    542 	for (sp = cmdenviron ; sp ; sp = sp->next) {
    543 		if (strequal(sp->text, name))
    544 			return strchr(sp->text, '=') + 1;
    545 	}
    546 
    547 	v = find_var(name, NULL, NULL);
    548 
    549 	if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
    550 		return NULL;
    551 	if (v->rfunc && (v->flags & VFUNCREF) != 0)
    552 		return (*v->rfunc)(v) + v->name_len + 1;
    553 	return v->text + v->name_len + 1;
    554 }
    555 
    556 
    557 
    558 /*
    559  * Generate a list of exported variables.  This routine is used to construct
    560  * the third argument to execve when executing a program.
    561  */
    562 
    563 char **
    564 environment(void)
    565 {
    566 	int nenv;
    567 	struct var **vpp;
    568 	struct var *vp;
    569 	char **env;
    570 	char **ep;
    571 
    572 	nenv = 0;
    573 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    574 		for (vp = *vpp ; vp ; vp = vp->next)
    575 			if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT)
    576 				nenv++;
    577 	}
    578 	CTRACE(DBG_VARS, ("environment: %d vars to export\n", nenv));
    579 	ep = env = stalloc((nenv + 1) * sizeof *env);
    580 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    581 		for (vp = *vpp ; vp ; vp = vp->next)
    582 			if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT) {
    583 				if (vp->rfunc && (vp->flags & VFUNCREF))
    584 					*ep++ = (*vp->rfunc)(vp);
    585 				else
    586 					*ep++ = vp->text;
    587 				VTRACE(DBG_VARS, ("environment: %s\n", ep[-1]));
    588 			}
    589 	}
    590 	*ep = NULL;
    591 	return env;
    592 }
    593 
    594 
    595 /*
    596  * Called when a shell procedure is invoked to clear out nonexported
    597  * variables.  It is also necessary to reallocate variables of with
    598  * VSTACK set since these are currently allocated on the stack.
    599  */
    600 
    601 #ifdef mkinit
    602 void shprocvar(void);
    603 
    604 SHELLPROC {
    605 	shprocvar();
    606 }
    607 #endif
    608 
    609 void
    610 shprocvar(void)
    611 {
    612 	struct var **vpp;
    613 	struct var *vp, **prev;
    614 
    615 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    616 		for (prev = vpp ; (vp = *prev) != NULL ; ) {
    617 			if ((vp->flags & VEXPORT) == 0) {
    618 				*prev = vp->next;
    619 				if ((vp->flags & VTEXTFIXED) == 0)
    620 					ckfree(vp->text);
    621 				if ((vp->flags & VSTRFIXED) == 0)
    622 					ckfree(vp);
    623 			} else {
    624 				if (vp->flags & VSTACK) {
    625 					vp->text = savestr(vp->text);
    626 					vp->flags &=~ VSTACK;
    627 				}
    628 				prev = &vp->next;
    629 			}
    630 		}
    631 	}
    632 	initvar();
    633 }
    634 
    635 
    636 
    637 /*
    638  * Command to list all variables which are set.  Currently this command
    639  * is invoked from the set command when the set command is called without
    640  * any variables.
    641  */
    642 
    643 void
    644 print_quoted(const char *p)
    645 {
    646 	const char *q;
    647 
    648 	if (p[0] == '\0') {
    649 		out1fmt("''");
    650 		return;
    651 	}
    652 	if (strcspn(p, "|&;<>()$`\\\"' \t\n*?[]#~=%") == strlen(p)) {
    653 		out1fmt("%s", p);
    654 		return;
    655 	}
    656 	while (*p) {
    657 		if (*p == '\'') {
    658 			out1fmt("\\'");
    659 			p++;
    660 			continue;
    661 		}
    662 		q = strchr(p, '\'');
    663 		if (!q) {
    664 			out1fmt("'%s'", p );
    665 			return;
    666 		}
    667 		out1fmt("'%.*s'", (int)(q - p), p );
    668 		p = q;
    669 	}
    670 }
    671 
    672 static int
    673 sort_var(const void *v_v1, const void *v_v2)
    674 {
    675 	const struct var * const *v1 = v_v1;
    676 	const struct var * const *v2 = v_v2;
    677 	char *t1 = (*v1)->text, *t2 = (*v2)->text;
    678 
    679 	if (*t1 == *t2) {
    680 		char *p, *s;
    681 
    682 		STARTSTACKSTR(p);
    683 
    684 		/*
    685 		 * note: if lengths are equal, strings must be different
    686 		 * so we don't care which string we pick for the \0 in
    687 		 * that case.
    688 		 */
    689 		if ((strchr(t1, '=') - t1) <= (strchr(t2, '=') - t2)) {
    690 			s = t1;
    691 			t1 = p;
    692 		} else {
    693 			s = t2;
    694 			t2 = p;
    695 		}
    696 
    697 		while (*s && *s != '=') {
    698 			STPUTC(*s, p);
    699 			s++;
    700 		}
    701 		STPUTC('\0', p);
    702 	}
    703 
    704 	return strcoll(t1, t2);
    705 }
    706 
    707 /*
    708  * POSIX requires that 'set' (but not export or readonly) output the
    709  * variables in lexicographic order - by the locale's collating order (sigh).
    710  * Maybe we could keep them in an ordered balanced binary tree
    711  * instead of hashed lists.
    712  * For now just roll 'em through qsort for printing...
    713  */
    714 
    715 int
    716 showvars(const char *name, int flag, int show_value, const char *xtra)
    717 {
    718 	struct var **vpp;
    719 	struct var *vp;
    720 	const char *p;
    721 
    722 	static struct var **list;	/* static in case we are interrupted */
    723 	static int list_len;
    724 	int count = 0;
    725 
    726 	if (!list) {
    727 		list_len = 32;
    728 		list = ckmalloc(list_len * sizeof *list);
    729 	}
    730 
    731 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
    732 		for (vp = *vpp ; vp ; vp = vp->next) {
    733 			if (flag && !(vp->flags & flag))
    734 				continue;
    735 			if (vp->flags & VUNSET && !(show_value & 2))
    736 				continue;
    737 			if (count >= list_len) {
    738 				list = ckrealloc(list,
    739 					(list_len << 1) * sizeof *list);
    740 				list_len <<= 1;
    741 			}
    742 			list[count++] = vp;
    743 		}
    744 	}
    745 
    746 	qsort(list, count, sizeof *list, sort_var);
    747 
    748 	for (vpp = list; count--; vpp++) {
    749 		vp = *vpp;
    750 		if (name)
    751 			out1fmt("%s ", name);
    752 		if (xtra)
    753 			out1fmt("%s ", xtra);
    754 		p = vp->text;
    755 		if (vp->rfunc && (vp->flags & VFUNCREF) != 0) {
    756 			p = (*vp->rfunc)(vp);
    757 			if (p == NULL)
    758 				p = vp->text;
    759 		}
    760 		for ( ; *p != '=' ; p++)
    761 			out1c(*p);
    762 		if (!(vp->flags & VUNSET) && show_value) {
    763 			out1fmt("=");
    764 			print_quoted(++p);
    765 		}
    766 		out1c('\n');
    767 	}
    768 	return 0;
    769 }
    770 
    771 
    772 
    773 /*
    774  * The export and readonly commands.
    775  */
    776 
    777 int
    778 exportcmd(int argc, char **argv)
    779 {
    780 	struct var *vp;
    781 	char *name;
    782 	const char *p;
    783 	int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
    784 	int pflg = 0;
    785 	int nflg = 0;
    786 	int xflg = 0;
    787 	int res;
    788 	int c;
    789 	int f;
    790 
    791 	while ((c = nextopt("npx")) != '\0') {
    792 		switch (c) {
    793 		case 'p':
    794 			if (nflg)
    795 				return 1;
    796 			pflg = 3;
    797 			break;
    798 		case 'n':
    799 			if (pflg || xflg || flag == VREADONLY)
    800 				return 1;
    801 			nflg = 1;
    802 			break;
    803 		case 'x':
    804 			if (nflg || flag == VREADONLY)
    805 				return 1;
    806 			flag = VNOEXPORT;
    807 			xflg = 1;
    808 			break;
    809 		default:
    810 			return 1;
    811 		}
    812 	}
    813 
    814 	if (nflg && *argptr == NULL)
    815 		return 1;
    816 
    817 	if (pflg || *argptr == NULL) {
    818 		showvars( pflg ? argv[0] : 0, flag, pflg,
    819 		    pflg && xflg ? "-x" : NULL );
    820 		return 0;
    821 	}
    822 
    823 	res = 0;
    824 	while ((name = *argptr++) != NULL) {
    825 		f = flag;
    826 		if ((p = strchr(name, '=')) != NULL) {
    827 			p++;
    828 		} else {
    829 			vp = find_var(name, NULL, NULL);
    830 			if (vp != NULL) {
    831 				if (nflg)
    832 					vp->flags &= ~flag;
    833 				else if (flag&VEXPORT && vp->flags&VNOEXPORT)
    834 					res = 1;
    835 				else {
    836 					vp->flags |= flag;
    837 					if (flag == VNOEXPORT)
    838 						vp->flags &= ~VEXPORT;
    839 				}
    840 				continue;
    841 			} else
    842 				f |= VUNSET;
    843 		}
    844 		if (!nflg)
    845 			setvar(name, p, f);
    846 	}
    847 	return res;
    848 }
    849 
    850 
    851 /*
    852  * The "local" command.
    853  */
    854 
    855 int
    856 localcmd(int argc, char **argv)
    857 {
    858 	char *name;
    859 	int c;
    860 	int flags = 0;		/*XXX perhaps VUNSET from a -o option value */
    861 
    862 	if (! in_function())
    863 		error("Not in a function");
    864 
    865 	/* upper case options, as bash stole all the good ones ... */
    866 	while ((c = nextopt("INx")) != '\0')
    867 		switch (c) {
    868 		case 'I':	flags &= ~VUNSET;	break;
    869 		case 'N':	flags |= VUNSET;	break;
    870 		case 'x':	flags |= VEXPORT;	break;
    871 		}
    872 
    873 	while ((name = *argptr++) != NULL) {
    874 		mklocal(name, flags);
    875 	}
    876 	return 0;
    877 }
    878 
    879 
    880 /*
    881  * Make a variable a local variable.  When a variable is made local, its
    882  * value and flags are saved in a localvar structure.  The saved values
    883  * will be restored when the shell function returns.  We handle the name
    884  * "-" as a special case.
    885  */
    886 
    887 void
    888 mklocal(const char *name, int flags)
    889 {
    890 	struct localvar *lvp;
    891 	struct var **vpp;
    892 	struct var *vp;
    893 
    894 	INTOFF;
    895 	lvp = ckmalloc(sizeof (struct localvar));
    896 	if (name[0] == '-' && name[1] == '\0') {
    897 		char *p;
    898 		p = ckmalloc(sizeof_optlist);
    899 		lvp->text = memcpy(p, optlist, sizeof_optlist);
    900 		vp = NULL;
    901 	} else {
    902 		vp = find_var(name, &vpp, NULL);
    903 		if (vp == NULL) {
    904 			flags &= ~VNOEXPORT;
    905 			if (strchr(name, '='))
    906 				setvareq(savestr(name),
    907 				    VSTRFIXED | (flags & ~VUNSET));
    908 			else
    909 				setvar(name, NULL, VSTRFIXED|flags);
    910 			vp = *vpp;	/* the new variable */
    911 			lvp->text = NULL;
    912 			lvp->flags = VUNSET;
    913 		} else {
    914 			lvp->text = vp->text;
    915 			lvp->flags = vp->flags;
    916 			vp->flags |= VSTRFIXED|VTEXTFIXED;
    917 			if (vp->flags & VNOEXPORT)
    918 				flags &= ~VEXPORT;
    919 			if (flags & (VNOEXPORT | VUNSET))
    920 				vp->flags &= ~VEXPORT;
    921 			flags &= ~VNOEXPORT;
    922 			if (name[vp->name_len] == '=')
    923 				setvareq(savestr(name), flags & ~VUNSET);
    924 			else if (flags & VUNSET)
    925 				unsetvar(name, 0);
    926 			else
    927 				vp->flags |= flags & (VUNSET|VEXPORT);
    928 
    929 			if (vp == &line_num) {
    930 				if (name[vp->name_len] == '=')
    931 					funclinebase = funclineabs -1;
    932 				else
    933 					funclinebase = 0;
    934 			}
    935 		}
    936 	}
    937 	lvp->vp = vp;
    938 	lvp->next = localvars;
    939 	localvars = lvp;
    940 	INTON;
    941 }
    942 
    943 
    944 /*
    945  * Called after a function returns.
    946  */
    947 
    948 void
    949 poplocalvars(void)
    950 {
    951 	struct localvar *lvp;
    952 	struct var *vp;
    953 
    954 	while ((lvp = localvars) != NULL) {
    955 		localvars = lvp->next;
    956 		vp = lvp->vp;
    957 		VTRACE(DBG_VARS, ("poplocalvar %s", vp ? vp->text : "-"));
    958 		if (vp == NULL) {	/* $- saved */
    959 			memcpy(optlist, lvp->text, sizeof_optlist);
    960 			ckfree(lvp->text);
    961 			optschanged();
    962 		} else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
    963 			(void)unsetvar(vp->text, 0);
    964 		} else {
    965 			if (vp->func && (vp->flags & (VNOFUNC|VFUNCREF)) == 0)
    966 				(*vp->func)(lvp->text + vp->name_len + 1);
    967 			if ((vp->flags & VTEXTFIXED) == 0)
    968 				ckfree(vp->text);
    969 			vp->flags = lvp->flags;
    970 			vp->text = lvp->text;
    971 		}
    972 		ckfree(lvp);
    973 	}
    974 }
    975 
    976 
    977 int
    978 setvarcmd(int argc, char **argv)
    979 {
    980 	if (argc <= 2)
    981 		return unsetcmd(argc, argv);
    982 	else if (argc == 3)
    983 		setvar(argv[1], argv[2], 0);
    984 	else
    985 		error("List assignment not implemented");
    986 	return 0;
    987 }
    988 
    989 
    990 /*
    991  * The unset builtin command.  We unset the function before we unset the
    992  * variable to allow a function to be unset when there is a readonly variable
    993  * with the same name.
    994  */
    995 
    996 int
    997 unsetcmd(int argc, char **argv)
    998 {
    999 	char **ap;
   1000 	int i;
   1001 	int flg_func = 0;
   1002 	int flg_var = 0;
   1003 	int flg_x = 0;
   1004 	int ret = 0;
   1005 
   1006 	while ((i = nextopt("efvx")) != '\0') {
   1007 		switch (i) {
   1008 		case 'f':
   1009 			flg_func = 1;
   1010 			break;
   1011 		case 'e':
   1012 		case 'x':
   1013 			flg_x = (2 >> (i == 'e'));
   1014 			/* FALLTHROUGH */
   1015 		case 'v':
   1016 			flg_var = 1;
   1017 			break;
   1018 		}
   1019 	}
   1020 
   1021 	if (flg_func == 0 && flg_var == 0)
   1022 		flg_var = 1;
   1023 
   1024 	for (ap = argptr; *ap ; ap++) {
   1025 		if (flg_func)
   1026 			ret |= unsetfunc(*ap);
   1027 		if (flg_var)
   1028 			ret |= unsetvar(*ap, flg_x);
   1029 	}
   1030 	return ret;
   1031 }
   1032 
   1033 
   1034 /*
   1035  * Unset the specified variable.
   1036  */
   1037 
   1038 int
   1039 unsetvar(const char *s, int unexport)
   1040 {
   1041 	struct var **vpp;
   1042 	struct var *vp;
   1043 
   1044 	vp = find_var(s, &vpp, NULL);
   1045 	if (vp == NULL)
   1046 		return 0;
   1047 
   1048 	if (vp->flags & VREADONLY && !(unexport & 1))
   1049 		return 1;
   1050 
   1051 	INTOFF;
   1052 	if (unexport & 1) {
   1053 		vp->flags &= ~VEXPORT;
   1054 	} else {
   1055 		if (vp->text[vp->name_len + 1] != '\0')
   1056 			setvar(s, nullstr, 0);
   1057 		if (!(unexport & 2))
   1058 			vp->flags &= ~VEXPORT;
   1059 		vp->flags |= VUNSET;
   1060 		if ((vp->flags&(VEXPORT|VSTRFIXED|VREADONLY|VNOEXPORT)) == 0) {
   1061 			if ((vp->flags & VTEXTFIXED) == 0)
   1062 				ckfree(vp->text);
   1063 			*vpp = vp->next;
   1064 			ckfree(vp);
   1065 		}
   1066 	}
   1067 	INTON;
   1068 	return 0;
   1069 }
   1070 
   1071 
   1072 /*
   1073  * Returns true if the two strings specify the same varable.  The first
   1074  * variable name is terminated by '='; the second may be terminated by
   1075  * either '=' or '\0'.
   1076  */
   1077 
   1078 STATIC int
   1079 strequal(const char *p, const char *q)
   1080 {
   1081 	while (*p == *q++) {
   1082 		if (*p++ == '=')
   1083 			return 1;
   1084 	}
   1085 	if (*p == '=' && *(q - 1) == '\0')
   1086 		return 1;
   1087 	return 0;
   1088 }
   1089 
   1090 /*
   1091  * Search for a variable.
   1092  * 'name' may be terminated by '=' or a NUL.
   1093  * vppp is set to the pointer to vp, or the list head if vp isn't found
   1094  * lenp is set to the number of charactets in 'name'
   1095  */
   1096 
   1097 STATIC struct var *
   1098 find_var(const char *name, struct var ***vppp, int *lenp)
   1099 {
   1100 	unsigned int hashval;
   1101 	int len;
   1102 	struct var *vp, **vpp;
   1103 	const char *p = name;
   1104 
   1105 	hashval = 0;
   1106 	while (*p && *p != '=')
   1107 		hashval = 2 * hashval + (unsigned char)*p++;
   1108 	len = p - name;
   1109 
   1110 	if (lenp)
   1111 		*lenp = len;
   1112 	vpp = &vartab[hashval % VTABSIZE];
   1113 	if (vppp)
   1114 		*vppp = vpp;
   1115 
   1116 	for (vp = *vpp ; vp ; vpp = &vp->next, vp = *vpp) {
   1117 		if (vp->name_len != len)
   1118 			continue;
   1119 		if (memcmp(vp->text, name, len) != 0)
   1120 			continue;
   1121 		if (vppp)
   1122 			*vppp = vpp;
   1123 		return vp;
   1124 	}
   1125 	return NULL;
   1126 }
   1127 
   1128 /*
   1129  * The following are the functions that create the values for
   1130  * shell variables that are dynamically produced when needed.
   1131  *
   1132  * The output strings cannot be malloc'd as there is nothing to
   1133  * free them - callers assume these are ordinary variables where
   1134  * the value returned is vp->text
   1135  *
   1136  * Each function needs its own storage space, as the results are
   1137  * used to create processes' environment, and (if exported) all
   1138  * the values will (might) be needed simultaneously.
   1139  *
   1140  * It is not a problem if a var is updated while nominally in use
   1141  * somewhere, all these are intended to be dynamic, the value they
   1142  * return is not guaranteed, an updated vaue is just as good.
   1143  *
   1144  * So, malloc a single buffer for the result of each function,
   1145  * grow, and even shrink, it as needed, but once we have one that
   1146  * is a suitable size for the actual usage, simply hold it forever.
   1147  *
   1148  * For a SMALL shell we implement only LINENO, none of the others,
   1149  * and give it just a fixed length static buffer for its result.
   1150  */
   1151 
   1152 #ifndef SMALL
   1153 
   1154 struct space_reserved {		/* record of space allocated for results */
   1155 	char *b;
   1156 	int len;
   1157 };
   1158 
   1159 /* rough (over-)estimate of the number of bytes needed to hold a number */
   1160 static int
   1161 digits_in(intmax_t number)
   1162 {
   1163 	int res = 0;
   1164 
   1165 	if (number & ~((1LL << 62) - 1))
   1166 		res = 64;	/* enough for 2^200 and a bit more */
   1167 	else if (number & ~((1LL << 32) - 1))
   1168 		res = 20;	/* enough for 2^64 */
   1169 	else if (number & ~((1 << 23) - 1))
   1170 		res = 10;	/* enough for 2^32 */
   1171 	else
   1172 		res = 8;	/* enough for 2^23 or smaller */
   1173 
   1174 	return res;
   1175 }
   1176 
   1177 static int
   1178 make_space(struct space_reserved *m, int bytes)
   1179 {
   1180 	void *p;
   1181 
   1182 	if (m->len >= bytes && m->len <= (bytes<<2))
   1183 		return 1;
   1184 
   1185 	bytes = SHELL_ALIGN(bytes);
   1186 	/* not ckrealloc() - we want failure, not error() here */
   1187 	p = realloc(m->b, bytes);
   1188 	if (p == NULL)	/* what we had should still be there */
   1189 		return 0;
   1190 
   1191 	m->b = p;
   1192 	m->len = bytes;
   1193 	m->b[bytes - 1] = '\0';
   1194 
   1195 	return 1;
   1196 }
   1197 #endif
   1198 
   1199 char *
   1200 get_lineno(struct var *vp)
   1201 {
   1202 #ifdef SMALL
   1203 #define length (8 + 10)		/* 10 digits is enough for a 32 bit line num */
   1204 	static char result[length];
   1205 #else
   1206 	static struct space_reserved buf;
   1207 #define result buf.b
   1208 #define length buf.len
   1209 #endif
   1210 	int ln = line_number;
   1211 
   1212 	if (vp->flags & VUNSET)
   1213 		return NULL;
   1214 
   1215 	ln -= funclinebase;
   1216 
   1217 #ifndef SMALL
   1218 	if (!make_space(&buf, vp->name_len + 2 + digits_in(ln)))
   1219 		return vp->text;
   1220 #endif
   1221 
   1222 	snprintf(result, length - 1, "%.*s=%d", vp->name_len, vp->text, ln);
   1223 	return result;
   1224 }
   1225 #undef result
   1226 #undef length
   1227 
   1228 #ifndef SMALL
   1229 
   1230 char *
   1231 get_hostname(struct var *vp)
   1232 {
   1233 	static struct space_reserved buf;
   1234 
   1235 	if (vp->flags & VUNSET)
   1236 		return NULL;
   1237 
   1238 	if (!make_space(&buf, vp->name_len + 2 + 256))
   1239 		return vp->text;
   1240 
   1241 	memcpy(buf.b, vp->text, vp->name_len + 1);	/* include '=' */
   1242 	(void)gethostname(buf.b + vp->name_len + 1,
   1243 	    buf.len - vp->name_len - 3);
   1244 	return buf.b;
   1245 }
   1246 
   1247 char *
   1248 get_tod(struct var *vp)
   1249 {
   1250 	static struct space_reserved buf;	/* space for answers */
   1251 	static struct space_reserved tzs;	/* remember TZ last used */
   1252 	static timezone_t last_zone;		/* timezone data for tzs zone */
   1253 	const char *fmt;
   1254 	char *tz;
   1255 	time_t now;
   1256 	struct tm tm_now, *tmp;
   1257 	timezone_t zone = NULL;
   1258 	static char t_err[] = "time error";
   1259 	int len;
   1260 
   1261 	if (vp->flags & VUNSET)
   1262 		return NULL;
   1263 
   1264 	fmt = lookupvar("ToD_FORMAT");
   1265 	if (fmt == NULL)
   1266 		fmt="%T";
   1267 	tz = lookupvar("TZ");
   1268 	(void)time(&now);
   1269 
   1270 	if (tz != NULL) {
   1271 		if (tzs.b == NULL || strcmp(tzs.b, tz) != 0) {
   1272 			if (make_space(&tzs, strlen(tz) + 1)) {
   1273 				INTOFF;
   1274 				strcpy(tzs.b, tz);
   1275 				if (last_zone)
   1276 					tzfree(last_zone);
   1277 				last_zone = zone = tzalloc(tz);
   1278 				INTON;
   1279 			} else
   1280 				zone = tzalloc(tz);
   1281 		} else
   1282 			zone = last_zone;
   1283 
   1284 		tmp = localtime_rz(zone, &now, &tm_now);
   1285 	} else
   1286 		tmp = localtime_r(&now, &tm_now);
   1287 
   1288 	len = (strlen(fmt) * 4) + vp->name_len + 2;
   1289 	while (make_space(&buf, len)) {
   1290 		memcpy(buf.b, vp->text, vp->name_len+1);
   1291 		if (tmp == NULL) {
   1292 			if (buf.len >= vp->name_len+2+(int)(sizeof t_err - 1)) {
   1293 				strcpy(buf.b + vp->name_len + 1, t_err);
   1294 				if (zone && zone != last_zone)
   1295 					tzfree(zone);
   1296 				return buf.b;
   1297 			}
   1298 			len = vp->name_len + 4 + sizeof t_err - 1;
   1299 			continue;
   1300 		}
   1301 		if (strftime_z(zone, buf.b + vp->name_len + 1,
   1302 		     buf.len - vp->name_len - 2, fmt, tmp)) {
   1303 			if (zone && zone != last_zone)
   1304 				tzfree(zone);
   1305 			return buf.b;
   1306 		}
   1307 		if (len >= 4096)	/* Let's be reasonable */
   1308 			break;
   1309 		len <<= 1;
   1310 	}
   1311 	if (zone && zone != last_zone)
   1312 		tzfree(zone);
   1313 	return vp->text;
   1314 }
   1315 
   1316 char *
   1317 get_seconds(struct var *vp)
   1318 {
   1319 	static struct space_reserved buf;
   1320 	intmax_t secs;
   1321 
   1322 	if (vp->flags & VUNSET)
   1323 		return NULL;
   1324 
   1325 	secs = (intmax_t)time((time_t *)0) - sh_start_time;
   1326 	if (!make_space(&buf, vp->name_len + 2 + digits_in(secs)))
   1327 		return vp->text;
   1328 
   1329 	snprintf(buf.b, buf.len-1, "%.*s=%jd", vp->name_len, vp->text, secs);
   1330 	return buf.b;
   1331 }
   1332 
   1333 char *
   1334 get_euser(struct var *vp)
   1335 {
   1336 	static struct space_reserved buf;
   1337 	static uid_t lastuid = 0;
   1338 	uid_t euid;
   1339 	struct passwd *pw;
   1340 
   1341 	if (vp->flags & VUNSET)
   1342 		return NULL;
   1343 
   1344 	euid = geteuid();
   1345 	if (buf.b != NULL && lastuid == euid)
   1346 		return buf.b;
   1347 
   1348 	pw = getpwuid(euid);
   1349 	if (pw == NULL)
   1350 		return vp->text;
   1351 
   1352 	if (make_space(&buf, vp->name_len + 2 + strlen(pw->pw_name))) {
   1353 		lastuid = euid;
   1354 		snprintf(buf.b, buf.len, "%.*s=%s", vp->name_len, vp->text,
   1355 		    pw->pw_name);
   1356 		return buf.b;
   1357 	}
   1358 
   1359 	return vp->text;
   1360 }
   1361 
   1362 char *
   1363 get_random(struct var *vp)
   1364 {
   1365 	static struct space_reserved buf;
   1366 	static intmax_t random_val = 0;
   1367 
   1368 #ifdef USE_LRAND48
   1369 #define random lrand48
   1370 #define srandom srand48
   1371 #endif
   1372 
   1373 	if (vp->flags & VUNSET)
   1374 		return NULL;
   1375 
   1376 	if (vp->text != buf.b) {
   1377 		/*
   1378 		 * Either initialisation, or a new seed has been set
   1379 		 */
   1380 		if (vp->text[vp->name_len + 1] == '\0') {
   1381 			int fd;
   1382 
   1383 			/*
   1384 			 * initialisation (without pre-seeding),
   1385 			 * or explictly requesting a truly random seed.
   1386 			 */
   1387 			fd = open("/dev/urandom", 0);
   1388 			if (fd == -1) {
   1389 				out2str("RANDOM initialisation failed\n");
   1390 				random_val = (getpid()<<3) ^ time((time_t *)0);
   1391 			} else {
   1392 				int n;
   1393 
   1394 				do {
   1395 				    n = read(fd,&random_val,sizeof random_val);
   1396 				} while (n != sizeof random_val);
   1397 				close(fd);
   1398 			}
   1399 		} else
   1400 			/* good enough for today */
   1401 			random_val = atoi(vp->text + vp->name_len + 1);
   1402 
   1403 		srandom((long)random_val);
   1404 	}
   1405 
   1406 #if 0
   1407 	random_val = (random_val + 1) & 0x7FFF;	/* 15 bit "random" numbers */
   1408 #else
   1409 	random_val = (random() >> 5) & 0x7FFF;
   1410 #endif
   1411 
   1412 	if (!make_space(&buf, vp->name_len + 2 + digits_in(random_val)))
   1413 		return vp->text;
   1414 
   1415 	snprintf(buf.b, buf.len-1, "%.*s=%jd", vp->name_len, vp->text,
   1416 	    random_val);
   1417 
   1418 	if (buf.b != vp->text && (vp->flags & (VTEXTFIXED|VSTACK)) == 0)
   1419 		free(vp->text);
   1420 	vp->flags |= VTEXTFIXED;
   1421 	vp->text = buf.b;
   1422 
   1423 	return vp->text;
   1424 #undef random
   1425 #undef srandom
   1426 }
   1427 
   1428 #endif /* SMALL */
   1429