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