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