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