17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate 237c478bd9Sstevel@tonic-gate /* 247c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 28*965005c8Schin /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 29*965005c8Schin /* All Rights Reserved */ 30*965005c8Schin 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate /* 337c478bd9Sstevel@tonic-gate * UNIX shell 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include "defs.h" 377c478bd9Sstevel@tonic-gate #include <stropts.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate extern BOOL chkid(); 407c478bd9Sstevel@tonic-gate extern unsigned char *simple(); 417c478bd9Sstevel@tonic-gate extern int mailchk; 427c478bd9Sstevel@tonic-gate 43*965005c8Schin static void setname(unsigned char *, int); 447c478bd9Sstevel@tonic-gate static void set_builtins_path(); 457c478bd9Sstevel@tonic-gate static int patheq(); 46*965005c8Schin static void namwalk(struct namnod *); 47*965005c8Schin static void dolocale(); 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate struct namnod ps2nod = 507c478bd9Sstevel@tonic-gate { 517c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 527c478bd9Sstevel@tonic-gate &acctnod, 537c478bd9Sstevel@tonic-gate (unsigned char *)ps2name 547c478bd9Sstevel@tonic-gate }; 557c478bd9Sstevel@tonic-gate struct namnod cdpnod = 567c478bd9Sstevel@tonic-gate { 577c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 587c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 597c478bd9Sstevel@tonic-gate (unsigned char *)cdpname 607c478bd9Sstevel@tonic-gate }; 617c478bd9Sstevel@tonic-gate struct namnod pathnod = 627c478bd9Sstevel@tonic-gate { 637c478bd9Sstevel@tonic-gate &mailpnod, 647c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 657c478bd9Sstevel@tonic-gate (unsigned char *)pathname 667c478bd9Sstevel@tonic-gate }; 677c478bd9Sstevel@tonic-gate struct namnod ifsnod = 687c478bd9Sstevel@tonic-gate { 697c478bd9Sstevel@tonic-gate &homenod, 707c478bd9Sstevel@tonic-gate &mailnod, 717c478bd9Sstevel@tonic-gate (unsigned char *)ifsname 727c478bd9Sstevel@tonic-gate }; 737c478bd9Sstevel@tonic-gate struct namnod ps1nod = 747c478bd9Sstevel@tonic-gate { 757c478bd9Sstevel@tonic-gate &pathnod, 767c478bd9Sstevel@tonic-gate &ps2nod, 777c478bd9Sstevel@tonic-gate (unsigned char *)ps1name 787c478bd9Sstevel@tonic-gate }; 797c478bd9Sstevel@tonic-gate struct namnod homenod = 807c478bd9Sstevel@tonic-gate { 817c478bd9Sstevel@tonic-gate &cdpnod, 827c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 837c478bd9Sstevel@tonic-gate (unsigned char *)homename 847c478bd9Sstevel@tonic-gate }; 857c478bd9Sstevel@tonic-gate struct namnod mailnod = 867c478bd9Sstevel@tonic-gate { 877c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 887c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 897c478bd9Sstevel@tonic-gate (unsigned char *)mailname 907c478bd9Sstevel@tonic-gate }; 917c478bd9Sstevel@tonic-gate struct namnod mchknod = 927c478bd9Sstevel@tonic-gate { 937c478bd9Sstevel@tonic-gate &ifsnod, 947c478bd9Sstevel@tonic-gate &ps1nod, 957c478bd9Sstevel@tonic-gate (unsigned char *)mchkname 967c478bd9Sstevel@tonic-gate }; 977c478bd9Sstevel@tonic-gate struct namnod acctnod = 987c478bd9Sstevel@tonic-gate { 997c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 1007c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 1017c478bd9Sstevel@tonic-gate (unsigned char *)acctname 1027c478bd9Sstevel@tonic-gate }; 1037c478bd9Sstevel@tonic-gate struct namnod mailpnod = 1047c478bd9Sstevel@tonic-gate { 1057c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 1067c478bd9Sstevel@tonic-gate (struct namnod *)NIL, 1077c478bd9Sstevel@tonic-gate (unsigned char *)mailpname 1087c478bd9Sstevel@tonic-gate }; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate struct namnod *namep = &mchknod; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate /* ======== variable and string handling ======== */ 1147c478bd9Sstevel@tonic-gate 115*965005c8Schin int 116*965005c8Schin syslook(unsigned char *w, struct sysnod syswds[], int n) 1177c478bd9Sstevel@tonic-gate { 1187c478bd9Sstevel@tonic-gate int low; 1197c478bd9Sstevel@tonic-gate int high; 1207c478bd9Sstevel@tonic-gate int mid; 121*965005c8Schin int cond; 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate if (w == 0 || *w == 0) 1247c478bd9Sstevel@tonic-gate return(0); 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate low = 0; 1277c478bd9Sstevel@tonic-gate high = n - 1; 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate while (low <= high) 1307c478bd9Sstevel@tonic-gate { 1317c478bd9Sstevel@tonic-gate mid = (low + high) / 2; 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if ((cond = cf(w, syswds[mid].sysnam)) < 0) 1347c478bd9Sstevel@tonic-gate high = mid - 1; 1357c478bd9Sstevel@tonic-gate else if (cond > 0) 1367c478bd9Sstevel@tonic-gate low = mid + 1; 1377c478bd9Sstevel@tonic-gate else 1387c478bd9Sstevel@tonic-gate return(syswds[mid].sysval); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate return(0); 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 143*965005c8Schin void 144*965005c8Schin setlist(struct argnod *arg, int xp) 1457c478bd9Sstevel@tonic-gate { 1467c478bd9Sstevel@tonic-gate if (flags & exportflg) 1477c478bd9Sstevel@tonic-gate xp |= N_EXPORT; 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate while (arg) 1507c478bd9Sstevel@tonic-gate { 151*965005c8Schin unsigned char *s = mactrim(arg->argval); 1527c478bd9Sstevel@tonic-gate setname(s, xp); 1537c478bd9Sstevel@tonic-gate arg = arg->argnxt; 1547c478bd9Sstevel@tonic-gate if (flags & execpr) 1557c478bd9Sstevel@tonic-gate { 1567c478bd9Sstevel@tonic-gate prs(s); 1577c478bd9Sstevel@tonic-gate if (arg) 1587c478bd9Sstevel@tonic-gate blank(); 1597c478bd9Sstevel@tonic-gate else 1607c478bd9Sstevel@tonic-gate newline(); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 165*965005c8Schin static void 166*965005c8Schin setname(unsigned char *argi, int xp) /* does parameter assignments */ 1677c478bd9Sstevel@tonic-gate { 168*965005c8Schin unsigned char *argscan = argi; 169*965005c8Schin struct namnod *n; 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate if (letter(*argscan)) 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate while (alphanum(*argscan)) 1747c478bd9Sstevel@tonic-gate argscan++; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate if (*argscan == '=') 1777c478bd9Sstevel@tonic-gate { 1787c478bd9Sstevel@tonic-gate *argscan = 0; /* make name a cohesive string */ 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate n = lookup(argi); 1817c478bd9Sstevel@tonic-gate *argscan++ = '='; 1827c478bd9Sstevel@tonic-gate attrib(n, xp); 1837c478bd9Sstevel@tonic-gate if (xp & N_ENVNAM) 1847c478bd9Sstevel@tonic-gate { 1857c478bd9Sstevel@tonic-gate n->namenv = n->namval = argscan; 1867c478bd9Sstevel@tonic-gate if (n == &pathnod) 1877c478bd9Sstevel@tonic-gate set_builtins_path(); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate else 1907c478bd9Sstevel@tonic-gate assign(n, argscan); 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate dolocale(n->namid); 1937c478bd9Sstevel@tonic-gate return; 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate 198*965005c8Schin void 199*965005c8Schin replace(unsigned char **a, unsigned char *v) 2007c478bd9Sstevel@tonic-gate { 2017c478bd9Sstevel@tonic-gate free(*a); 2027c478bd9Sstevel@tonic-gate *a = make(v); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 205*965005c8Schin void 206*965005c8Schin dfault(struct namnod *n, unsigned char *v) 2077c478bd9Sstevel@tonic-gate { 2087c478bd9Sstevel@tonic-gate if (n->namval == 0) 2097c478bd9Sstevel@tonic-gate assign(n, v); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 212*965005c8Schin void 213*965005c8Schin assign(struct namnod *n, unsigned char *v) 2147c478bd9Sstevel@tonic-gate { 2157c478bd9Sstevel@tonic-gate if (n->namflg & N_RDONLY) 2167c478bd9Sstevel@tonic-gate failed(n->namid, wtfailed); 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate #ifndef RES 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate else if (flags & rshflg) 2217c478bd9Sstevel@tonic-gate { 2227c478bd9Sstevel@tonic-gate if (n == &pathnod || eq(n->namid,"SHELL")) 2237c478bd9Sstevel@tonic-gate failed(n->namid, restricted); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate #endif 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate else if (n->namflg & N_FUNCTN) 2287c478bd9Sstevel@tonic-gate { 2297c478bd9Sstevel@tonic-gate func_unhash(n->namid); 2307c478bd9Sstevel@tonic-gate freefunc(n); 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate n->namenv = 0; 2337c478bd9Sstevel@tonic-gate n->namflg = N_DEFAULT; 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate if (n == &mchknod) 2377c478bd9Sstevel@tonic-gate { 2387c478bd9Sstevel@tonic-gate mailchk = stoi(v); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate replace(&n->namval, v); 2427c478bd9Sstevel@tonic-gate attrib(n, N_ENVCHG); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (n == &pathnod) 2457c478bd9Sstevel@tonic-gate { 2467c478bd9Sstevel@tonic-gate zaphash(); 2477c478bd9Sstevel@tonic-gate set_dotpath(); 2487c478bd9Sstevel@tonic-gate set_builtins_path(); 2497c478bd9Sstevel@tonic-gate return; 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate if (flags & prompt) 2537c478bd9Sstevel@tonic-gate { 2547c478bd9Sstevel@tonic-gate if ((n == &mailpnod) || (n == &mailnod && mailpnod.namflg == N_DEFAULT)) 2557c478bd9Sstevel@tonic-gate setmail(n->namval); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate static void 2607c478bd9Sstevel@tonic-gate set_builtins_path() 2617c478bd9Sstevel@tonic-gate { 262*965005c8Schin unsigned char *path; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate ucb_builtins = 0; 2657c478bd9Sstevel@tonic-gate path = getpath(""); 2667c478bd9Sstevel@tonic-gate while (path && *path) 2677c478bd9Sstevel@tonic-gate { 2687c478bd9Sstevel@tonic-gate if (patheq(path, "/usr/ucb")) 2697c478bd9Sstevel@tonic-gate { 2707c478bd9Sstevel@tonic-gate ucb_builtins++; 2717c478bd9Sstevel@tonic-gate break; 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate else if (patheq(path, "/usr/bin")) 2747c478bd9Sstevel@tonic-gate break; 2757c478bd9Sstevel@tonic-gate else if (patheq(path, "/bin")) 2767c478bd9Sstevel@tonic-gate break; 2777c478bd9Sstevel@tonic-gate else if (patheq(path, "/usr/5bin")) 2787c478bd9Sstevel@tonic-gate break; 2797c478bd9Sstevel@tonic-gate path = nextpath(path); 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate static int 284*965005c8Schin patheq(unsigned char *component, char *dir) 2857c478bd9Sstevel@tonic-gate { 286*965005c8Schin unsigned char c; 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate for (;;) 2897c478bd9Sstevel@tonic-gate { 2907c478bd9Sstevel@tonic-gate c = *component++; 2917c478bd9Sstevel@tonic-gate if (c == COLON) 2927c478bd9Sstevel@tonic-gate c = '\0'; /* end of component of path */ 293*965005c8Schin if (c != *dir++) 294*965005c8Schin return (0); 2957c478bd9Sstevel@tonic-gate if (c == '\0') 2967c478bd9Sstevel@tonic-gate return(1); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 300*965005c8Schin int 301*965005c8Schin readvar(unsigned char **names) 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate struct fileblk fb; 304*965005c8Schin struct fileblk *f = &fb; 3057c478bd9Sstevel@tonic-gate unsigned char c[MULTI_BYTE_MAX+1]; 306*965005c8Schin int rc = 0; 3077c478bd9Sstevel@tonic-gate struct namnod *n = lookup(*names++); /* done now to avoid storage mess */ 3087c478bd9Sstevel@tonic-gate unsigned char *rel = (unsigned char *)relstak(); 3097c478bd9Sstevel@tonic-gate unsigned char *oldstak; 310*965005c8Schin unsigned char *pc, *rest; 3117c478bd9Sstevel@tonic-gate int d; 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate push(f); 3147c478bd9Sstevel@tonic-gate initf(dup(0)); 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate /* 3177c478bd9Sstevel@tonic-gate * If stdin is a pipe then this lseek(2) will fail with ESPIPE, so 3187c478bd9Sstevel@tonic-gate * the read buffer size is set to 1 because we will not be able 3197c478bd9Sstevel@tonic-gate * lseek(2) back towards the beginning of the file, so we have 3207c478bd9Sstevel@tonic-gate * to read a byte at a time instead 3217c478bd9Sstevel@tonic-gate * 3227c478bd9Sstevel@tonic-gate */ 3237c478bd9Sstevel@tonic-gate if (lseek(0, (off_t)0, SEEK_CUR) == -1) 3247c478bd9Sstevel@tonic-gate f->fsiz = 1; 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate /* 3277c478bd9Sstevel@tonic-gate * If stdin is a socket then this isastream(3C) will return 1, so 3287c478bd9Sstevel@tonic-gate * the read buffer size is set to 1 because we will not be able 3297c478bd9Sstevel@tonic-gate * lseek(2) back towards the beginning of the file, so we have 3307c478bd9Sstevel@tonic-gate * to read a byte at a time instead 3317c478bd9Sstevel@tonic-gate * 3327c478bd9Sstevel@tonic-gate */ 3337c478bd9Sstevel@tonic-gate if (isastream(0) == 1) 3347c478bd9Sstevel@tonic-gate f->fsiz = 1; 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate /* 3377c478bd9Sstevel@tonic-gate * strip leading IFS characters 3387c478bd9Sstevel@tonic-gate */ 3397c478bd9Sstevel@tonic-gate for (;;) 3407c478bd9Sstevel@tonic-gate { 3417c478bd9Sstevel@tonic-gate d = nextwc(); 3427c478bd9Sstevel@tonic-gate if(eolchar(d)) 3437c478bd9Sstevel@tonic-gate break; 3447c478bd9Sstevel@tonic-gate rest = readw(d); 3457c478bd9Sstevel@tonic-gate pc = c; 3467c478bd9Sstevel@tonic-gate while(*pc++ = *rest++); 3477c478bd9Sstevel@tonic-gate if(!anys(c, ifsnod.namval)) 3487c478bd9Sstevel@tonic-gate break; 3497c478bd9Sstevel@tonic-gate } 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate oldstak = curstak(); 3527c478bd9Sstevel@tonic-gate for (;;) 3537c478bd9Sstevel@tonic-gate { 3547c478bd9Sstevel@tonic-gate if ((*names && anys(c, ifsnod.namval)) || eolchar(d)) 3557c478bd9Sstevel@tonic-gate { 3567c478bd9Sstevel@tonic-gate if (staktop >= brkend) 3577c478bd9Sstevel@tonic-gate growstak(staktop); 3587c478bd9Sstevel@tonic-gate zerostak(); 3597c478bd9Sstevel@tonic-gate assign(n, absstak(rel)); 3607c478bd9Sstevel@tonic-gate setstak(rel); 3617c478bd9Sstevel@tonic-gate if (*names) 3627c478bd9Sstevel@tonic-gate n = lookup(*names++); 3637c478bd9Sstevel@tonic-gate else 3647c478bd9Sstevel@tonic-gate n = 0; 3657c478bd9Sstevel@tonic-gate if (eolchar(d)) 3667c478bd9Sstevel@tonic-gate { 3677c478bd9Sstevel@tonic-gate break; 3687c478bd9Sstevel@tonic-gate } 3697c478bd9Sstevel@tonic-gate else /* strip imbedded IFS characters */ 3707c478bd9Sstevel@tonic-gate while(1) { 3717c478bd9Sstevel@tonic-gate d = nextwc(); 3727c478bd9Sstevel@tonic-gate if(eolchar(d)) 3737c478bd9Sstevel@tonic-gate break; 3747c478bd9Sstevel@tonic-gate rest = readw(d); 3757c478bd9Sstevel@tonic-gate pc = c; 3767c478bd9Sstevel@tonic-gate while(*pc++ = *rest++); 3777c478bd9Sstevel@tonic-gate if(!anys(c, ifsnod.namval)) 3787c478bd9Sstevel@tonic-gate break; 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate else 3827c478bd9Sstevel@tonic-gate { 3837c478bd9Sstevel@tonic-gate if(d == '\\') { 3847c478bd9Sstevel@tonic-gate d = readwc(); 3857c478bd9Sstevel@tonic-gate rest = readw(d); 3867c478bd9Sstevel@tonic-gate while(d = *rest++) { 3877c478bd9Sstevel@tonic-gate if (staktop >= brkend) 3887c478bd9Sstevel@tonic-gate growstak(staktop); 3897c478bd9Sstevel@tonic-gate pushstak(d); 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate oldstak = staktop; 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate else 3947c478bd9Sstevel@tonic-gate { 3957c478bd9Sstevel@tonic-gate pc = c; 3967c478bd9Sstevel@tonic-gate while(d = *pc++) { 3977c478bd9Sstevel@tonic-gate if (staktop >= brkend) 3987c478bd9Sstevel@tonic-gate growstak(staktop); 3997c478bd9Sstevel@tonic-gate pushstak(d); 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate if(!anys(c, ifsnod.namval)) 4027c478bd9Sstevel@tonic-gate oldstak = staktop; 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate d = nextwc(); 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate if (eolchar(d)) 4077c478bd9Sstevel@tonic-gate staktop = oldstak; 4087c478bd9Sstevel@tonic-gate else 4097c478bd9Sstevel@tonic-gate { 4107c478bd9Sstevel@tonic-gate rest = readw(d); 4117c478bd9Sstevel@tonic-gate pc = c; 4127c478bd9Sstevel@tonic-gate while(*pc++ = *rest++); 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate while (n) 4177c478bd9Sstevel@tonic-gate { 418*965005c8Schin assign(n, (unsigned char *)nullstr); 4197c478bd9Sstevel@tonic-gate if (*names) 4207c478bd9Sstevel@tonic-gate n = lookup(*names++); 4217c478bd9Sstevel@tonic-gate else 4227c478bd9Sstevel@tonic-gate n = 0; 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate if (eof) 4267c478bd9Sstevel@tonic-gate rc = 1; 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate if (isastream(0) != 1) 4297c478bd9Sstevel@tonic-gate /* 4307c478bd9Sstevel@tonic-gate * If we are reading on a stream do not attempt to 4317c478bd9Sstevel@tonic-gate * lseek(2) back towards the start because this is 4327c478bd9Sstevel@tonic-gate * logically meaningless, but there is nothing in 4337c478bd9Sstevel@tonic-gate * the standards to pervent the stream implementation 4347c478bd9Sstevel@tonic-gate * from attempting it and breaking our code here 4357c478bd9Sstevel@tonic-gate * 4367c478bd9Sstevel@tonic-gate */ 4377c478bd9Sstevel@tonic-gate lseek(0, (off_t)(f->nxtoff - f->endoff), SEEK_CUR); 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate pop(); 4407c478bd9Sstevel@tonic-gate return(rc); 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate 443*965005c8Schin void 444*965005c8Schin assnum(unsigned char **p, long i) 4457c478bd9Sstevel@tonic-gate { 4467c478bd9Sstevel@tonic-gate int j = ltos(i); 4477c478bd9Sstevel@tonic-gate replace(p, &numbuf[j]); 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate unsigned char * 4517c478bd9Sstevel@tonic-gate make(v) 4527c478bd9Sstevel@tonic-gate unsigned char *v; 4537c478bd9Sstevel@tonic-gate { 454*965005c8Schin unsigned char *p; 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate if (v) 4577c478bd9Sstevel@tonic-gate { 4587c478bd9Sstevel@tonic-gate movstr(v, p = (unsigned char *)alloc(length(v))); 4597c478bd9Sstevel@tonic-gate return(p); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate else 4627c478bd9Sstevel@tonic-gate return(0); 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate struct namnod * 467*965005c8Schin lookup(unsigned char *nam) 4687c478bd9Sstevel@tonic-gate { 469*965005c8Schin struct namnod *nscan = namep; 470*965005c8Schin struct namnod **prev; 4717c478bd9Sstevel@tonic-gate int LR; 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate if (!chkid(nam)) 4747c478bd9Sstevel@tonic-gate failed(nam, notid); 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate while (nscan) 4777c478bd9Sstevel@tonic-gate { 4787c478bd9Sstevel@tonic-gate if ((LR = cf(nam, nscan->namid)) == 0) 4797c478bd9Sstevel@tonic-gate return(nscan); 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate else if (LR < 0) 4827c478bd9Sstevel@tonic-gate prev = &(nscan->namlft); 4837c478bd9Sstevel@tonic-gate else 4847c478bd9Sstevel@tonic-gate prev = &(nscan->namrgt); 4857c478bd9Sstevel@tonic-gate nscan = *prev; 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate /* 4887c478bd9Sstevel@tonic-gate * add name node 4897c478bd9Sstevel@tonic-gate */ 4907c478bd9Sstevel@tonic-gate nscan = (struct namnod *)alloc(sizeof *nscan); 4917c478bd9Sstevel@tonic-gate nscan->namlft = nscan->namrgt = (struct namnod *)NIL; 4927c478bd9Sstevel@tonic-gate nscan->namid = make(nam); 4937c478bd9Sstevel@tonic-gate nscan->namval = 0; 4947c478bd9Sstevel@tonic-gate nscan->namflg = N_DEFAULT; 4957c478bd9Sstevel@tonic-gate nscan->namenv = 0; 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate return(*prev = nscan); 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate BOOL 5017c478bd9Sstevel@tonic-gate chkid(nam) 5027c478bd9Sstevel@tonic-gate unsigned char *nam; 5037c478bd9Sstevel@tonic-gate { 504*965005c8Schin unsigned char *cp = nam; 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate if (!letter(*cp)) 5077c478bd9Sstevel@tonic-gate return(FALSE); 5087c478bd9Sstevel@tonic-gate else 5097c478bd9Sstevel@tonic-gate { 5107c478bd9Sstevel@tonic-gate while (*++cp) 5117c478bd9Sstevel@tonic-gate { 5127c478bd9Sstevel@tonic-gate if (!alphanum(*cp)) 5137c478bd9Sstevel@tonic-gate return(FALSE); 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate return(TRUE); 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate 519*965005c8Schin static void (*namfn)(); 520*965005c8Schin 521*965005c8Schin void 522*965005c8Schin namscan(void (*fn)()) 5237c478bd9Sstevel@tonic-gate { 5247c478bd9Sstevel@tonic-gate namfn = fn; 5257c478bd9Sstevel@tonic-gate namwalk(namep); 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate static void 529*965005c8Schin namwalk(struct namnod *np) 5307c478bd9Sstevel@tonic-gate { 5317c478bd9Sstevel@tonic-gate if (np) 5327c478bd9Sstevel@tonic-gate { 5337c478bd9Sstevel@tonic-gate namwalk(np->namlft); 5347c478bd9Sstevel@tonic-gate (*namfn)(np); 5357c478bd9Sstevel@tonic-gate namwalk(np->namrgt); 5367c478bd9Sstevel@tonic-gate } 5377c478bd9Sstevel@tonic-gate } 5387c478bd9Sstevel@tonic-gate 539*965005c8Schin void 540*965005c8Schin printnam(struct namnod *n) 5417c478bd9Sstevel@tonic-gate { 542*965005c8Schin unsigned char *s; 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate sigchk(); 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate if (n->namflg & N_FUNCTN) 5477c478bd9Sstevel@tonic-gate { 5487c478bd9Sstevel@tonic-gate prs_buff(n->namid); 5497c478bd9Sstevel@tonic-gate prs_buff("(){\n"); 5507c478bd9Sstevel@tonic-gate prf(n->namenv); 5517c478bd9Sstevel@tonic-gate prs_buff("\n}\n"); 5527c478bd9Sstevel@tonic-gate } 5537c478bd9Sstevel@tonic-gate else if (s = n->namval) 5547c478bd9Sstevel@tonic-gate { 5557c478bd9Sstevel@tonic-gate prs_buff(n->namid); 5567c478bd9Sstevel@tonic-gate prc_buff('='); 5577c478bd9Sstevel@tonic-gate prs_buff(s); 5587c478bd9Sstevel@tonic-gate prc_buff(NL); 5597c478bd9Sstevel@tonic-gate } 5607c478bd9Sstevel@tonic-gate } 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate static unsigned char * 563*965005c8Schin staknam(struct namnod *n) 5647c478bd9Sstevel@tonic-gate { 565*965005c8Schin unsigned char *p; 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate p = movstrstak(n->namid, staktop); 5687c478bd9Sstevel@tonic-gate p = movstrstak("=", p); 5697c478bd9Sstevel@tonic-gate p = movstrstak(n->namval, p); 5707c478bd9Sstevel@tonic-gate return(getstak(p + 1 - (unsigned char *)(stakbot))); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate static int namec; 5747c478bd9Sstevel@tonic-gate 575*965005c8Schin void 576*965005c8Schin exname(struct namnod *n) 5777c478bd9Sstevel@tonic-gate { 578*965005c8Schin int flg = n->namflg; 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate if (flg & N_ENVCHG) 5817c478bd9Sstevel@tonic-gate { 5827c478bd9Sstevel@tonic-gate 5837c478bd9Sstevel@tonic-gate if (flg & N_EXPORT) 5847c478bd9Sstevel@tonic-gate { 5857c478bd9Sstevel@tonic-gate free(n->namenv); 5867c478bd9Sstevel@tonic-gate n->namenv = make(n->namval); 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate else 5897c478bd9Sstevel@tonic-gate { 5907c478bd9Sstevel@tonic-gate free(n->namval); 5917c478bd9Sstevel@tonic-gate n->namval = make(n->namenv); 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate if (!(flg & N_FUNCTN)) 5977c478bd9Sstevel@tonic-gate n->namflg = N_DEFAULT; 5987c478bd9Sstevel@tonic-gate 5997c478bd9Sstevel@tonic-gate if (n->namval) 6007c478bd9Sstevel@tonic-gate namec++; 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 604*965005c8Schin void 605*965005c8Schin printro(struct namnod *n) 6067c478bd9Sstevel@tonic-gate { 6077c478bd9Sstevel@tonic-gate if (n->namflg & N_RDONLY) 6087c478bd9Sstevel@tonic-gate { 6097c478bd9Sstevel@tonic-gate prs_buff(readonly); 6107c478bd9Sstevel@tonic-gate prc_buff(SPACE); 6117c478bd9Sstevel@tonic-gate prs_buff(n->namid); 6127c478bd9Sstevel@tonic-gate prc_buff(NL); 6137c478bd9Sstevel@tonic-gate } 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate 616*965005c8Schin void 617*965005c8Schin printexp(struct namnod *n) 6187c478bd9Sstevel@tonic-gate { 6197c478bd9Sstevel@tonic-gate if (n->namflg & N_EXPORT) 6207c478bd9Sstevel@tonic-gate { 6217c478bd9Sstevel@tonic-gate prs_buff(export); 6227c478bd9Sstevel@tonic-gate prc_buff(SPACE); 6237c478bd9Sstevel@tonic-gate prs_buff(n->namid); 6247c478bd9Sstevel@tonic-gate prc_buff(NL); 6257c478bd9Sstevel@tonic-gate } 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 628*965005c8Schin void 629*965005c8Schin setup_env(void) 6307c478bd9Sstevel@tonic-gate { 631*965005c8Schin unsigned char **e = environ; 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate while (*e) 6347c478bd9Sstevel@tonic-gate setname(*e++, N_ENVNAM); 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate static unsigned char **argnam; 6397c478bd9Sstevel@tonic-gate 640*965005c8Schin static void 641*965005c8Schin countnam(struct namnod *n) 6427c478bd9Sstevel@tonic-gate { 6437c478bd9Sstevel@tonic-gate if (n->namval) 6447c478bd9Sstevel@tonic-gate namec++; 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate 647*965005c8Schin static void 648*965005c8Schin pushnam(struct namnod *n) 6497c478bd9Sstevel@tonic-gate { 650*965005c8Schin int flg = n->namflg; 651*965005c8Schin unsigned char *p; 652*965005c8Schin unsigned char *namval; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate if (((flg & N_ENVCHG) && (flg & N_EXPORT)) || (flg & N_FUNCTN)) 6557c478bd9Sstevel@tonic-gate namval = n->namval; 6567c478bd9Sstevel@tonic-gate else { 6577c478bd9Sstevel@tonic-gate /* Discard Local variable in child process */ 6587c478bd9Sstevel@tonic-gate if (!(flg & ~N_ENVCHG)) { 6597c478bd9Sstevel@tonic-gate n->namflg = 0; 6607c478bd9Sstevel@tonic-gate n->namenv = 0; 6617c478bd9Sstevel@tonic-gate if (n->namval) { 6627c478bd9Sstevel@tonic-gate /* Release for re-use */ 6637c478bd9Sstevel@tonic-gate free(n->namval); 6647c478bd9Sstevel@tonic-gate n->namval = (unsigned char *)NIL; 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate namval = n->namenv; 6687c478bd9Sstevel@tonic-gate } 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate if (namval) 6717c478bd9Sstevel@tonic-gate { 6727c478bd9Sstevel@tonic-gate p = movstrstak(n->namid, staktop); 6737c478bd9Sstevel@tonic-gate p = movstrstak("=", p); 6747c478bd9Sstevel@tonic-gate p = movstrstak(namval, p); 6757c478bd9Sstevel@tonic-gate *argnam++ = getstak(p + 1 - (unsigned char *)(stakbot)); 6767c478bd9Sstevel@tonic-gate } 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate 6797c478bd9Sstevel@tonic-gate unsigned char ** 6807c478bd9Sstevel@tonic-gate local_setenv() 6817c478bd9Sstevel@tonic-gate { 682*965005c8Schin unsigned char **er; 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate namec = 0; 6857c478bd9Sstevel@tonic-gate namscan(countnam); 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate argnam = er = (unsigned char **)getstak(namec * BYTESPERWORD + BYTESPERWORD); 6887c478bd9Sstevel@tonic-gate namscan(pushnam); 6897c478bd9Sstevel@tonic-gate *argnam++ = 0; 6907c478bd9Sstevel@tonic-gate return(er); 6917c478bd9Sstevel@tonic-gate } 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate void 6947c478bd9Sstevel@tonic-gate setvars() 6957c478bd9Sstevel@tonic-gate { 6967c478bd9Sstevel@tonic-gate namscan(exname); 6977c478bd9Sstevel@tonic-gate } 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate struct namnod * 7007c478bd9Sstevel@tonic-gate findnam(nam) 701*965005c8Schin unsigned char *nam; 7027c478bd9Sstevel@tonic-gate { 703*965005c8Schin struct namnod *nscan = namep; 7047c478bd9Sstevel@tonic-gate int LR; 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate if (!chkid(nam)) 7077c478bd9Sstevel@tonic-gate return(0); 7087c478bd9Sstevel@tonic-gate while (nscan) 7097c478bd9Sstevel@tonic-gate { 7107c478bd9Sstevel@tonic-gate if ((LR = cf(nam, nscan->namid)) == 0) 7117c478bd9Sstevel@tonic-gate return(nscan); 7127c478bd9Sstevel@tonic-gate else if (LR < 0) 7137c478bd9Sstevel@tonic-gate nscan = nscan->namlft; 7147c478bd9Sstevel@tonic-gate else 7157c478bd9Sstevel@tonic-gate nscan = nscan->namrgt; 7167c478bd9Sstevel@tonic-gate } 7177c478bd9Sstevel@tonic-gate return(0); 7187c478bd9Sstevel@tonic-gate } 7197c478bd9Sstevel@tonic-gate 720*965005c8Schin void 721*965005c8Schin unset_name(unsigned char *name) 7227c478bd9Sstevel@tonic-gate { 723*965005c8Schin struct namnod *n; 724*965005c8Schin unsigned char call_dolocale = 0; 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate if (n = findnam(name)) 7277c478bd9Sstevel@tonic-gate { 7287c478bd9Sstevel@tonic-gate if (n->namflg & N_RDONLY) 7297c478bd9Sstevel@tonic-gate failed(name, wtfailed); 7307c478bd9Sstevel@tonic-gate 7317c478bd9Sstevel@tonic-gate if (n == &pathnod || 7327c478bd9Sstevel@tonic-gate n == &ifsnod || 7337c478bd9Sstevel@tonic-gate n == &ps1nod || 7347c478bd9Sstevel@tonic-gate n == &ps2nod || 7357c478bd9Sstevel@tonic-gate n == &mchknod) 7367c478bd9Sstevel@tonic-gate { 7377c478bd9Sstevel@tonic-gate failed(name, badunset); 7387c478bd9Sstevel@tonic-gate } 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate #ifndef RES 7417c478bd9Sstevel@tonic-gate 7427c478bd9Sstevel@tonic-gate if ((flags & rshflg) && eq(name, "SHELL")) 7437c478bd9Sstevel@tonic-gate failed(name, restricted); 7447c478bd9Sstevel@tonic-gate 7457c478bd9Sstevel@tonic-gate #endif 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate if (n->namflg & N_FUNCTN) 7487c478bd9Sstevel@tonic-gate { 7497c478bd9Sstevel@tonic-gate func_unhash(name); 7507c478bd9Sstevel@tonic-gate freefunc(n); 7517c478bd9Sstevel@tonic-gate } 7527c478bd9Sstevel@tonic-gate else 7537c478bd9Sstevel@tonic-gate { 7547c478bd9Sstevel@tonic-gate call_dolocale++; 7557c478bd9Sstevel@tonic-gate free(n->namval); 7567c478bd9Sstevel@tonic-gate free(n->namenv); 7577c478bd9Sstevel@tonic-gate } 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate n->namval = n->namenv = 0; 7607c478bd9Sstevel@tonic-gate n->namflg = N_DEFAULT; 7617c478bd9Sstevel@tonic-gate 7627c478bd9Sstevel@tonic-gate if (call_dolocale) 7637c478bd9Sstevel@tonic-gate dolocale(name); 7647c478bd9Sstevel@tonic-gate 7657c478bd9Sstevel@tonic-gate if (flags & prompt) 7667c478bd9Sstevel@tonic-gate { 7677c478bd9Sstevel@tonic-gate if (n == &mailpnod) 7687c478bd9Sstevel@tonic-gate setmail(mailnod.namval); 7697c478bd9Sstevel@tonic-gate else if (n == &mailnod && mailpnod.namflg == N_DEFAULT) 7707c478bd9Sstevel@tonic-gate setmail(0); 7717c478bd9Sstevel@tonic-gate } 7727c478bd9Sstevel@tonic-gate } 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate /* 7767c478bd9Sstevel@tonic-gate * The environment variables which affect locale. 7777c478bd9Sstevel@tonic-gate * Note: if all names in this list do not begin with 'L', 7787c478bd9Sstevel@tonic-gate * you MUST modify dolocale(). Also, be sure that the 7797c478bd9Sstevel@tonic-gate * fake_env has the same number of elements as localevar. 7807c478bd9Sstevel@tonic-gate */ 7817c478bd9Sstevel@tonic-gate static char *localevar[] = { 7827c478bd9Sstevel@tonic-gate "LC_ALL", 7837c478bd9Sstevel@tonic-gate "LC_CTYPE", 7847c478bd9Sstevel@tonic-gate "LC_MESSAGES", 7857c478bd9Sstevel@tonic-gate "LANG", 7867c478bd9Sstevel@tonic-gate 0 7877c478bd9Sstevel@tonic-gate }; 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate static char *fake_env[] = { 7907c478bd9Sstevel@tonic-gate 0, 7917c478bd9Sstevel@tonic-gate 0, 7927c478bd9Sstevel@tonic-gate 0, 7937c478bd9Sstevel@tonic-gate 0, 7947c478bd9Sstevel@tonic-gate 0 7957c478bd9Sstevel@tonic-gate }; 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate /* 7987c478bd9Sstevel@tonic-gate * If name is one of several special variables which affect the locale, 7997c478bd9Sstevel@tonic-gate * do a setlocale(). 8007c478bd9Sstevel@tonic-gate */ 8017c478bd9Sstevel@tonic-gate static void 8027c478bd9Sstevel@tonic-gate dolocale(nm) 8037c478bd9Sstevel@tonic-gate char *nm; 8047c478bd9Sstevel@tonic-gate { 8057c478bd9Sstevel@tonic-gate char **real_env; 8067c478bd9Sstevel@tonic-gate struct namnod *n; 8077c478bd9Sstevel@tonic-gate int lv, fe; 8087c478bd9Sstevel@tonic-gate int i; 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate /* 8117c478bd9Sstevel@tonic-gate * Take advantage of fact that names of these vars all start 8127c478bd9Sstevel@tonic-gate * with 'L' to avoid unnecessary work. 8137c478bd9Sstevel@tonic-gate * Do locale processing only if /usr is mounted. 8147c478bd9Sstevel@tonic-gate */ 8157c478bd9Sstevel@tonic-gate if ((*nm != 'L') || !localedir_exists || 8167c478bd9Sstevel@tonic-gate (!(eq(nm, "LC_ALL") || eq(nm, "LC_CTYPE") || 8177c478bd9Sstevel@tonic-gate eq(nm, "LANG") || eq(nm, "LC_MESSAGES")))) 8187c478bd9Sstevel@tonic-gate return; 8197c478bd9Sstevel@tonic-gate 8207c478bd9Sstevel@tonic-gate /* 8217c478bd9Sstevel@tonic-gate * setlocale() has all the smarts built into it, but 8227c478bd9Sstevel@tonic-gate * it works by examining the environment. Unfortunately, 8237c478bd9Sstevel@tonic-gate * when you set an environment variable, the shell does 8247c478bd9Sstevel@tonic-gate * not modify its own environment; it just remembers that the 8257c478bd9Sstevel@tonic-gate * variable needs to be exported to any children. We hack around 8267c478bd9Sstevel@tonic-gate * this by consing up a fake environment for the use of setlocale() 8277c478bd9Sstevel@tonic-gate * and substituting it for the real env before calling setlocale(). 8287c478bd9Sstevel@tonic-gate */ 8297c478bd9Sstevel@tonic-gate 8307c478bd9Sstevel@tonic-gate /* 8317c478bd9Sstevel@tonic-gate * Build the fake environment. 8327c478bd9Sstevel@tonic-gate * Look up the value of each of the special environment 8337c478bd9Sstevel@tonic-gate * variables, and put their value into the fake environment, 8347c478bd9Sstevel@tonic-gate * if they are exported. 8357c478bd9Sstevel@tonic-gate */ 8367c478bd9Sstevel@tonic-gate for (lv = 0, fe = 0; localevar[lv]; lv++) { 8377c478bd9Sstevel@tonic-gate if ((n = findnam(localevar[lv]))) { 838*965005c8Schin char *p, *q; 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate if (!n->namval) 8417c478bd9Sstevel@tonic-gate continue; 8427c478bd9Sstevel@tonic-gate 8437c478bd9Sstevel@tonic-gate fake_env[fe++] = p = alloc(length(localevar[lv]) 8447c478bd9Sstevel@tonic-gate + length(n->namval) + 2); 8457c478bd9Sstevel@tonic-gate /* copy name */ 8467c478bd9Sstevel@tonic-gate q = localevar[lv]; 8477c478bd9Sstevel@tonic-gate while (*q) 8487c478bd9Sstevel@tonic-gate *p++ = *q++; 8497c478bd9Sstevel@tonic-gate 8507c478bd9Sstevel@tonic-gate *p++ = '='; 8517c478bd9Sstevel@tonic-gate 8527c478bd9Sstevel@tonic-gate /* copy value */ 8537c478bd9Sstevel@tonic-gate q = (char*)(n->namval); 8547c478bd9Sstevel@tonic-gate while (*q) 8557c478bd9Sstevel@tonic-gate *p++ = *q++; 8567c478bd9Sstevel@tonic-gate *p++ = '\0'; 8577c478bd9Sstevel@tonic-gate } 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate fake_env[fe] = (char *)0; 8607c478bd9Sstevel@tonic-gate 8617c478bd9Sstevel@tonic-gate /* 8627c478bd9Sstevel@tonic-gate * Switch fake env for real and call setlocale(). 8637c478bd9Sstevel@tonic-gate */ 8647c478bd9Sstevel@tonic-gate real_env = (char **)environ; 8657c478bd9Sstevel@tonic-gate environ = (unsigned char **)fake_env; 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate if (setlocale(LC_ALL, "") == NULL) 8687c478bd9Sstevel@tonic-gate prs("couldn't set locale correctly\n"); 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate /* 8717c478bd9Sstevel@tonic-gate * Switch back and tear down the fake env. 8727c478bd9Sstevel@tonic-gate */ 8737c478bd9Sstevel@tonic-gate environ = (unsigned char **)real_env; 8747c478bd9Sstevel@tonic-gate for (i = 0; i < fe; i++) { 8757c478bd9Sstevel@tonic-gate free(fake_env[i]); 8767c478bd9Sstevel@tonic-gate fake_env[i] = (char *)0; 8777c478bd9Sstevel@tonic-gate } 8787c478bd9Sstevel@tonic-gate } 879