1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate /* Portions Copyright(c) 1988, Sun Microsystems, Inc. */ 27*7c478bd9Sstevel@tonic-gate /* All Rights Reserved. */ 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.10.5.1 */ 30*7c478bd9Sstevel@tonic-gate /* 31*7c478bd9Sstevel@tonic-gate * UNIX shell 32*7c478bd9Sstevel@tonic-gate */ 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #include "hash.h" 35*7c478bd9Sstevel@tonic-gate #include "defs.h" 36*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 38*7c478bd9Sstevel@tonic-gate #include <errno.h> 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #define EXECUTE 01 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate static unsigned char cost; 43*7c478bd9Sstevel@tonic-gate static int dotpath; 44*7c478bd9Sstevel@tonic-gate static int multrel; 45*7c478bd9Sstevel@tonic-gate static struct entry relcmd; 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate static int argpath(); 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate short 50*7c478bd9Sstevel@tonic-gate pathlook(com, flg, arg) 51*7c478bd9Sstevel@tonic-gate unsigned char *com; 52*7c478bd9Sstevel@tonic-gate int flg; 53*7c478bd9Sstevel@tonic-gate register struct argnod *arg; 54*7c478bd9Sstevel@tonic-gate { 55*7c478bd9Sstevel@tonic-gate register unsigned char *name = com; 56*7c478bd9Sstevel@tonic-gate register ENTRY *h; 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate ENTRY hentry; 59*7c478bd9Sstevel@tonic-gate int count = 0; 60*7c478bd9Sstevel@tonic-gate int i; 61*7c478bd9Sstevel@tonic-gate int pathset = 0; 62*7c478bd9Sstevel@tonic-gate int oldpath = 0; 63*7c478bd9Sstevel@tonic-gate struct namnod *n; 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate hentry.data = 0; 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate if (any('/', name)) 70*7c478bd9Sstevel@tonic-gate return(COMMAND); 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate h = hfind(name); 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate if (h) 76*7c478bd9Sstevel@tonic-gate { 77*7c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 78*7c478bd9Sstevel@tonic-gate { 79*7c478bd9Sstevel@tonic-gate if (flg) 80*7c478bd9Sstevel@tonic-gate h->hits++; 81*7c478bd9Sstevel@tonic-gate return(h->data); 82*7c478bd9Sstevel@tonic-gate } 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate if (arg && (pathset = argpath(arg))) 85*7c478bd9Sstevel@tonic-gate return(PATH_COMMAND); 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate if ((h->data & DOT_COMMAND) == DOT_COMMAND) 88*7c478bd9Sstevel@tonic-gate { 89*7c478bd9Sstevel@tonic-gate if (multrel == 0 && hashdata(h->data) > dotpath) 90*7c478bd9Sstevel@tonic-gate oldpath = hashdata(h->data); 91*7c478bd9Sstevel@tonic-gate else 92*7c478bd9Sstevel@tonic-gate oldpath = dotpath; 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate h->data = 0; 95*7c478bd9Sstevel@tonic-gate goto pathsrch; 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate if (h->data & (COMMAND | REL_COMMAND)) 99*7c478bd9Sstevel@tonic-gate { 100*7c478bd9Sstevel@tonic-gate if (flg) 101*7c478bd9Sstevel@tonic-gate h->hits++; 102*7c478bd9Sstevel@tonic-gate return(h->data); 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate h->data = 0; 106*7c478bd9Sstevel@tonic-gate h->cost = 0; 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate if (i = syslook(name, commands, no_commands)) 110*7c478bd9Sstevel@tonic-gate { 111*7c478bd9Sstevel@tonic-gate hentry.data = (BUILTIN | i); 112*7c478bd9Sstevel@tonic-gate count = 1; 113*7c478bd9Sstevel@tonic-gate } 114*7c478bd9Sstevel@tonic-gate else 115*7c478bd9Sstevel@tonic-gate { 116*7c478bd9Sstevel@tonic-gate if (arg && (pathset = argpath(arg))) 117*7c478bd9Sstevel@tonic-gate return(PATH_COMMAND); 118*7c478bd9Sstevel@tonic-gate pathsrch: 119*7c478bd9Sstevel@tonic-gate count = findpath(name, oldpath); 120*7c478bd9Sstevel@tonic-gate } 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate if (count > 0) 123*7c478bd9Sstevel@tonic-gate { 124*7c478bd9Sstevel@tonic-gate if (h == 0) 125*7c478bd9Sstevel@tonic-gate { 126*7c478bd9Sstevel@tonic-gate hentry.cost = 0; 127*7c478bd9Sstevel@tonic-gate hentry.key = make(name); 128*7c478bd9Sstevel@tonic-gate h = henter(hentry); 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate if (h->data == 0) 132*7c478bd9Sstevel@tonic-gate { 133*7c478bd9Sstevel@tonic-gate if (count < dotpath) 134*7c478bd9Sstevel@tonic-gate h->data = COMMAND | count; 135*7c478bd9Sstevel@tonic-gate else 136*7c478bd9Sstevel@tonic-gate { 137*7c478bd9Sstevel@tonic-gate h->data = REL_COMMAND | count; 138*7c478bd9Sstevel@tonic-gate h->next = relcmd.next; 139*7c478bd9Sstevel@tonic-gate relcmd.next = h; 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate h->hits = flg; 145*7c478bd9Sstevel@tonic-gate h->cost += cost; 146*7c478bd9Sstevel@tonic-gate return(h->data); 147*7c478bd9Sstevel@tonic-gate } 148*7c478bd9Sstevel@tonic-gate else 149*7c478bd9Sstevel@tonic-gate { 150*7c478bd9Sstevel@tonic-gate return(-count); 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate static void 156*7c478bd9Sstevel@tonic-gate zapentry(h) 157*7c478bd9Sstevel@tonic-gate ENTRY *h; 158*7c478bd9Sstevel@tonic-gate { 159*7c478bd9Sstevel@tonic-gate h->data &= HASHZAP; 160*7c478bd9Sstevel@tonic-gate } 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate void 163*7c478bd9Sstevel@tonic-gate zaphash() 164*7c478bd9Sstevel@tonic-gate { 165*7c478bd9Sstevel@tonic-gate hscan(zapentry); 166*7c478bd9Sstevel@tonic-gate relcmd.next = 0; 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate void 170*7c478bd9Sstevel@tonic-gate zapcd() 171*7c478bd9Sstevel@tonic-gate { 172*7c478bd9Sstevel@tonic-gate ENTRY *ptr = relcmd.next; 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate while (ptr) 175*7c478bd9Sstevel@tonic-gate { 176*7c478bd9Sstevel@tonic-gate ptr->data |= CDMARK; 177*7c478bd9Sstevel@tonic-gate ptr = ptr->next; 178*7c478bd9Sstevel@tonic-gate } 179*7c478bd9Sstevel@tonic-gate relcmd.next = 0; 180*7c478bd9Sstevel@tonic-gate } 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate 183*7c478bd9Sstevel@tonic-gate static void 184*7c478bd9Sstevel@tonic-gate hashout(h) 185*7c478bd9Sstevel@tonic-gate ENTRY *h; 186*7c478bd9Sstevel@tonic-gate { 187*7c478bd9Sstevel@tonic-gate sigchk(); 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate if (hashtype(h->data) == NOTFOUND) 190*7c478bd9Sstevel@tonic-gate return; 191*7c478bd9Sstevel@tonic-gate 192*7c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 193*7c478bd9Sstevel@tonic-gate return; 194*7c478bd9Sstevel@tonic-gate 195*7c478bd9Sstevel@tonic-gate prn_buff(h->hits); 196*7c478bd9Sstevel@tonic-gate 197*7c478bd9Sstevel@tonic-gate if (h->data & REL_COMMAND) 198*7c478bd9Sstevel@tonic-gate prc_buff('*'); 199*7c478bd9Sstevel@tonic-gate 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate prc_buff(TAB); 202*7c478bd9Sstevel@tonic-gate prn_buff(h->cost); 203*7c478bd9Sstevel@tonic-gate prc_buff(TAB); 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate pr_path(h->key, hashdata(h->data)); 206*7c478bd9Sstevel@tonic-gate prc_buff(NL); 207*7c478bd9Sstevel@tonic-gate } 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate void 210*7c478bd9Sstevel@tonic-gate hashpr() 211*7c478bd9Sstevel@tonic-gate { 212*7c478bd9Sstevel@tonic-gate prs_buff("hits cost command\n"); 213*7c478bd9Sstevel@tonic-gate hscan(hashout); 214*7c478bd9Sstevel@tonic-gate } 215*7c478bd9Sstevel@tonic-gate 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate set_dotpath() 218*7c478bd9Sstevel@tonic-gate { 219*7c478bd9Sstevel@tonic-gate register unsigned char *path; 220*7c478bd9Sstevel@tonic-gate register int cnt = 1; 221*7c478bd9Sstevel@tonic-gate 222*7c478bd9Sstevel@tonic-gate dotpath = 10000; 223*7c478bd9Sstevel@tonic-gate path = getpath(""); 224*7c478bd9Sstevel@tonic-gate 225*7c478bd9Sstevel@tonic-gate while (path && *path) 226*7c478bd9Sstevel@tonic-gate { 227*7c478bd9Sstevel@tonic-gate if (*path == '/') 228*7c478bd9Sstevel@tonic-gate cnt++; 229*7c478bd9Sstevel@tonic-gate else 230*7c478bd9Sstevel@tonic-gate { 231*7c478bd9Sstevel@tonic-gate if (dotpath == 10000) 232*7c478bd9Sstevel@tonic-gate dotpath = cnt; 233*7c478bd9Sstevel@tonic-gate else 234*7c478bd9Sstevel@tonic-gate { 235*7c478bd9Sstevel@tonic-gate multrel = 1; 236*7c478bd9Sstevel@tonic-gate return; 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate } 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate path = nextpath(path); 241*7c478bd9Sstevel@tonic-gate } 242*7c478bd9Sstevel@tonic-gate 243*7c478bd9Sstevel@tonic-gate multrel = 0; 244*7c478bd9Sstevel@tonic-gate } 245*7c478bd9Sstevel@tonic-gate 246*7c478bd9Sstevel@tonic-gate 247*7c478bd9Sstevel@tonic-gate hash_func(name) 248*7c478bd9Sstevel@tonic-gate unsigned char *name; 249*7c478bd9Sstevel@tonic-gate { 250*7c478bd9Sstevel@tonic-gate ENTRY *h; 251*7c478bd9Sstevel@tonic-gate ENTRY hentry; 252*7c478bd9Sstevel@tonic-gate 253*7c478bd9Sstevel@tonic-gate h = hfind(name); 254*7c478bd9Sstevel@tonic-gate 255*7c478bd9Sstevel@tonic-gate if (h) 256*7c478bd9Sstevel@tonic-gate h->data = FUNCTION; 257*7c478bd9Sstevel@tonic-gate else 258*7c478bd9Sstevel@tonic-gate { 259*7c478bd9Sstevel@tonic-gate hentry.data = FUNCTION; 260*7c478bd9Sstevel@tonic-gate hentry.key = make(name); 261*7c478bd9Sstevel@tonic-gate hentry.cost = 0; 262*7c478bd9Sstevel@tonic-gate hentry.hits = 0; 263*7c478bd9Sstevel@tonic-gate henter(hentry); 264*7c478bd9Sstevel@tonic-gate } 265*7c478bd9Sstevel@tonic-gate } 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate func_unhash(name) 268*7c478bd9Sstevel@tonic-gate unsigned char *name; 269*7c478bd9Sstevel@tonic-gate { 270*7c478bd9Sstevel@tonic-gate ENTRY *h; 271*7c478bd9Sstevel@tonic-gate int i; 272*7c478bd9Sstevel@tonic-gate 273*7c478bd9Sstevel@tonic-gate h = hfind(name); 274*7c478bd9Sstevel@tonic-gate 275*7c478bd9Sstevel@tonic-gate if (h && (h->data & FUNCTION)) { 276*7c478bd9Sstevel@tonic-gate if(i = syslook(name, commands, no_commands)) 277*7c478bd9Sstevel@tonic-gate h->data = (BUILTIN|i); 278*7c478bd9Sstevel@tonic-gate else 279*7c478bd9Sstevel@tonic-gate h->data = NOTFOUND; 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate short 285*7c478bd9Sstevel@tonic-gate hash_cmd(name) 286*7c478bd9Sstevel@tonic-gate unsigned char *name; 287*7c478bd9Sstevel@tonic-gate { 288*7c478bd9Sstevel@tonic-gate ENTRY *h; 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate if (any('/', name)) 291*7c478bd9Sstevel@tonic-gate return(COMMAND); 292*7c478bd9Sstevel@tonic-gate 293*7c478bd9Sstevel@tonic-gate h = hfind(name); 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate if (h) 296*7c478bd9Sstevel@tonic-gate { 297*7c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 298*7c478bd9Sstevel@tonic-gate return(h->data); 299*7c478bd9Sstevel@tonic-gate else if ((h->data & REL_COMMAND) == REL_COMMAND) 300*7c478bd9Sstevel@tonic-gate { /* unlink h from relative command list */ 301*7c478bd9Sstevel@tonic-gate ENTRY *ptr = &relcmd; 302*7c478bd9Sstevel@tonic-gate while(ptr-> next != h) 303*7c478bd9Sstevel@tonic-gate ptr = ptr->next; 304*7c478bd9Sstevel@tonic-gate ptr->next = h->next; 305*7c478bd9Sstevel@tonic-gate } 306*7c478bd9Sstevel@tonic-gate zapentry(h); 307*7c478bd9Sstevel@tonic-gate } 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate return(pathlook(name, 0, 0)); 310*7c478bd9Sstevel@tonic-gate } 311*7c478bd9Sstevel@tonic-gate 312*7c478bd9Sstevel@tonic-gate 313*7c478bd9Sstevel@tonic-gate /* 314*7c478bd9Sstevel@tonic-gate * Return 0 if found, 1 if not. 315*7c478bd9Sstevel@tonic-gate */ 316*7c478bd9Sstevel@tonic-gate what_is_path(name) 317*7c478bd9Sstevel@tonic-gate register unsigned char *name; 318*7c478bd9Sstevel@tonic-gate { 319*7c478bd9Sstevel@tonic-gate register ENTRY *h; 320*7c478bd9Sstevel@tonic-gate int cnt; 321*7c478bd9Sstevel@tonic-gate short hashval; 322*7c478bd9Sstevel@tonic-gate 323*7c478bd9Sstevel@tonic-gate h = hfind(name); 324*7c478bd9Sstevel@tonic-gate 325*7c478bd9Sstevel@tonic-gate prs_buff(name); 326*7c478bd9Sstevel@tonic-gate if (h) 327*7c478bd9Sstevel@tonic-gate { 328*7c478bd9Sstevel@tonic-gate hashval = hashdata(h->data); 329*7c478bd9Sstevel@tonic-gate 330*7c478bd9Sstevel@tonic-gate switch (hashtype(h->data)) 331*7c478bd9Sstevel@tonic-gate { 332*7c478bd9Sstevel@tonic-gate case BUILTIN: 333*7c478bd9Sstevel@tonic-gate prs_buff(" is a shell builtin\n"); 334*7c478bd9Sstevel@tonic-gate return (0); 335*7c478bd9Sstevel@tonic-gate 336*7c478bd9Sstevel@tonic-gate case FUNCTION: 337*7c478bd9Sstevel@tonic-gate { 338*7c478bd9Sstevel@tonic-gate struct namnod *n = lookup(name); 339*7c478bd9Sstevel@tonic-gate 340*7c478bd9Sstevel@tonic-gate prs_buff(" is a function\n"); 341*7c478bd9Sstevel@tonic-gate prs_buff(name); 342*7c478bd9Sstevel@tonic-gate prs_buff("(){\n"); 343*7c478bd9Sstevel@tonic-gate prf(n->namenv); 344*7c478bd9Sstevel@tonic-gate prs_buff("\n}\n"); 345*7c478bd9Sstevel@tonic-gate return (0); 346*7c478bd9Sstevel@tonic-gate } 347*7c478bd9Sstevel@tonic-gate 348*7c478bd9Sstevel@tonic-gate case REL_COMMAND: 349*7c478bd9Sstevel@tonic-gate { 350*7c478bd9Sstevel@tonic-gate short hash; 351*7c478bd9Sstevel@tonic-gate 352*7c478bd9Sstevel@tonic-gate if ((h->data & DOT_COMMAND) == DOT_COMMAND) 353*7c478bd9Sstevel@tonic-gate { 354*7c478bd9Sstevel@tonic-gate hash = pathlook(name, 0, 0); 355*7c478bd9Sstevel@tonic-gate if (hashtype(hash) == NOTFOUND) 356*7c478bd9Sstevel@tonic-gate { 357*7c478bd9Sstevel@tonic-gate prs_buff(" not found\n"); 358*7c478bd9Sstevel@tonic-gate return (1); 359*7c478bd9Sstevel@tonic-gate } 360*7c478bd9Sstevel@tonic-gate else 361*7c478bd9Sstevel@tonic-gate hashval = hashdata(hash); 362*7c478bd9Sstevel@tonic-gate } 363*7c478bd9Sstevel@tonic-gate } 364*7c478bd9Sstevel@tonic-gate 365*7c478bd9Sstevel@tonic-gate case COMMAND: 366*7c478bd9Sstevel@tonic-gate prs_buff(" is hashed ("); 367*7c478bd9Sstevel@tonic-gate pr_path(name, hashval); 368*7c478bd9Sstevel@tonic-gate prs_buff(")\n"); 369*7c478bd9Sstevel@tonic-gate return (0); 370*7c478bd9Sstevel@tonic-gate } 371*7c478bd9Sstevel@tonic-gate } 372*7c478bd9Sstevel@tonic-gate 373*7c478bd9Sstevel@tonic-gate if (syslook(name, commands, no_commands)) 374*7c478bd9Sstevel@tonic-gate { 375*7c478bd9Sstevel@tonic-gate prs_buff(" is a shell builtin\n"); 376*7c478bd9Sstevel@tonic-gate return (0); 377*7c478bd9Sstevel@tonic-gate } 378*7c478bd9Sstevel@tonic-gate 379*7c478bd9Sstevel@tonic-gate if ((cnt = findpath(name, 0)) > 0) 380*7c478bd9Sstevel@tonic-gate { 381*7c478bd9Sstevel@tonic-gate prs_buff(" is "); 382*7c478bd9Sstevel@tonic-gate pr_path(name, cnt); 383*7c478bd9Sstevel@tonic-gate prc_buff(NL); 384*7c478bd9Sstevel@tonic-gate return (0); 385*7c478bd9Sstevel@tonic-gate } 386*7c478bd9Sstevel@tonic-gate else 387*7c478bd9Sstevel@tonic-gate { 388*7c478bd9Sstevel@tonic-gate prs_buff(" not found\n"); 389*7c478bd9Sstevel@tonic-gate return (1); 390*7c478bd9Sstevel@tonic-gate } 391*7c478bd9Sstevel@tonic-gate } 392*7c478bd9Sstevel@tonic-gate 393*7c478bd9Sstevel@tonic-gate 394*7c478bd9Sstevel@tonic-gate findpath(name, oldpath) 395*7c478bd9Sstevel@tonic-gate register unsigned char *name; 396*7c478bd9Sstevel@tonic-gate int oldpath; 397*7c478bd9Sstevel@tonic-gate { 398*7c478bd9Sstevel@tonic-gate register unsigned char *path; 399*7c478bd9Sstevel@tonic-gate register int count = 1; 400*7c478bd9Sstevel@tonic-gate 401*7c478bd9Sstevel@tonic-gate unsigned char *p; 402*7c478bd9Sstevel@tonic-gate int ok = 1; 403*7c478bd9Sstevel@tonic-gate int e_code = 1; 404*7c478bd9Sstevel@tonic-gate 405*7c478bd9Sstevel@tonic-gate cost = 0; 406*7c478bd9Sstevel@tonic-gate path = getpath(name); 407*7c478bd9Sstevel@tonic-gate 408*7c478bd9Sstevel@tonic-gate if (oldpath) 409*7c478bd9Sstevel@tonic-gate { 410*7c478bd9Sstevel@tonic-gate count = dotpath; 411*7c478bd9Sstevel@tonic-gate while (--count) 412*7c478bd9Sstevel@tonic-gate path = nextpath(path); 413*7c478bd9Sstevel@tonic-gate 414*7c478bd9Sstevel@tonic-gate if (oldpath > dotpath) 415*7c478bd9Sstevel@tonic-gate { 416*7c478bd9Sstevel@tonic-gate catpath(path, name); 417*7c478bd9Sstevel@tonic-gate p = curstak(); 418*7c478bd9Sstevel@tonic-gate cost = 1; 419*7c478bd9Sstevel@tonic-gate 420*7c478bd9Sstevel@tonic-gate if ((ok = chk_access(p, S_IEXEC, 1)) == 0) 421*7c478bd9Sstevel@tonic-gate return(dotpath); 422*7c478bd9Sstevel@tonic-gate else 423*7c478bd9Sstevel@tonic-gate return(oldpath); 424*7c478bd9Sstevel@tonic-gate } 425*7c478bd9Sstevel@tonic-gate else 426*7c478bd9Sstevel@tonic-gate count = dotpath; 427*7c478bd9Sstevel@tonic-gate } 428*7c478bd9Sstevel@tonic-gate 429*7c478bd9Sstevel@tonic-gate while (path) 430*7c478bd9Sstevel@tonic-gate { 431*7c478bd9Sstevel@tonic-gate path = catpath(path, name); 432*7c478bd9Sstevel@tonic-gate cost++; 433*7c478bd9Sstevel@tonic-gate p = curstak(); 434*7c478bd9Sstevel@tonic-gate 435*7c478bd9Sstevel@tonic-gate if ((ok = chk_access(p, S_IEXEC, 1)) == 0) 436*7c478bd9Sstevel@tonic-gate break; 437*7c478bd9Sstevel@tonic-gate else 438*7c478bd9Sstevel@tonic-gate e_code = max(e_code, ok); 439*7c478bd9Sstevel@tonic-gate 440*7c478bd9Sstevel@tonic-gate count++; 441*7c478bd9Sstevel@tonic-gate } 442*7c478bd9Sstevel@tonic-gate 443*7c478bd9Sstevel@tonic-gate return(ok ? -e_code : count); 444*7c478bd9Sstevel@tonic-gate } 445*7c478bd9Sstevel@tonic-gate 446*7c478bd9Sstevel@tonic-gate /* 447*7c478bd9Sstevel@tonic-gate * Determine if file given by name is accessible with permissions 448*7c478bd9Sstevel@tonic-gate * given by mode. 449*7c478bd9Sstevel@tonic-gate * Regflag argument non-zero means not to consider 450*7c478bd9Sstevel@tonic-gate * a non-regular file as executable. 451*7c478bd9Sstevel@tonic-gate */ 452*7c478bd9Sstevel@tonic-gate 453*7c478bd9Sstevel@tonic-gate chk_access(name, mode, regflag) 454*7c478bd9Sstevel@tonic-gate register unsigned char *name; 455*7c478bd9Sstevel@tonic-gate mode_t mode; 456*7c478bd9Sstevel@tonic-gate int regflag; 457*7c478bd9Sstevel@tonic-gate { 458*7c478bd9Sstevel@tonic-gate static int flag; 459*7c478bd9Sstevel@tonic-gate static uid_t euid; 460*7c478bd9Sstevel@tonic-gate struct stat statb; 461*7c478bd9Sstevel@tonic-gate mode_t ftype; 462*7c478bd9Sstevel@tonic-gate 463*7c478bd9Sstevel@tonic-gate if(flag == 0) { 464*7c478bd9Sstevel@tonic-gate euid = geteuid(); 465*7c478bd9Sstevel@tonic-gate flag = 1; 466*7c478bd9Sstevel@tonic-gate } 467*7c478bd9Sstevel@tonic-gate ftype = statb.st_mode & S_IFMT; 468*7c478bd9Sstevel@tonic-gate if (stat((char *)name, &statb) == 0) { 469*7c478bd9Sstevel@tonic-gate ftype = statb.st_mode & S_IFMT; 470*7c478bd9Sstevel@tonic-gate if(mode == S_IEXEC && regflag && ftype != S_IFREG) 471*7c478bd9Sstevel@tonic-gate return(2); 472*7c478bd9Sstevel@tonic-gate if(access((char *)name, 010|(mode>>6)) == 0) { 473*7c478bd9Sstevel@tonic-gate if(euid == 0) { 474*7c478bd9Sstevel@tonic-gate if (ftype != S_IFREG || mode != S_IEXEC) 475*7c478bd9Sstevel@tonic-gate return(0); 476*7c478bd9Sstevel@tonic-gate /* root can execute file as long as it has execute 477*7c478bd9Sstevel@tonic-gate permission for someone */ 478*7c478bd9Sstevel@tonic-gate if (statb.st_mode & (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6))) 479*7c478bd9Sstevel@tonic-gate return(0); 480*7c478bd9Sstevel@tonic-gate return(3); 481*7c478bd9Sstevel@tonic-gate } 482*7c478bd9Sstevel@tonic-gate return(0); 483*7c478bd9Sstevel@tonic-gate } 484*7c478bd9Sstevel@tonic-gate } 485*7c478bd9Sstevel@tonic-gate return(errno == EACCES ? 3 : 1); 486*7c478bd9Sstevel@tonic-gate } 487*7c478bd9Sstevel@tonic-gate 488*7c478bd9Sstevel@tonic-gate 489*7c478bd9Sstevel@tonic-gate pr_path(name, count) 490*7c478bd9Sstevel@tonic-gate register unsigned char *name; 491*7c478bd9Sstevel@tonic-gate int count; 492*7c478bd9Sstevel@tonic-gate { 493*7c478bd9Sstevel@tonic-gate register unsigned char *path; 494*7c478bd9Sstevel@tonic-gate 495*7c478bd9Sstevel@tonic-gate path = getpath(name); 496*7c478bd9Sstevel@tonic-gate 497*7c478bd9Sstevel@tonic-gate while (--count && path) 498*7c478bd9Sstevel@tonic-gate path = nextpath(path, name); 499*7c478bd9Sstevel@tonic-gate 500*7c478bd9Sstevel@tonic-gate catpath(path, name); 501*7c478bd9Sstevel@tonic-gate prs_buff(curstak()); 502*7c478bd9Sstevel@tonic-gate } 503*7c478bd9Sstevel@tonic-gate 504*7c478bd9Sstevel@tonic-gate 505*7c478bd9Sstevel@tonic-gate static 506*7c478bd9Sstevel@tonic-gate argpath(arg) 507*7c478bd9Sstevel@tonic-gate register struct argnod *arg; 508*7c478bd9Sstevel@tonic-gate { 509*7c478bd9Sstevel@tonic-gate register unsigned char *s; 510*7c478bd9Sstevel@tonic-gate register unsigned char *start; 511*7c478bd9Sstevel@tonic-gate 512*7c478bd9Sstevel@tonic-gate while (arg) 513*7c478bd9Sstevel@tonic-gate { 514*7c478bd9Sstevel@tonic-gate s = arg->argval; 515*7c478bd9Sstevel@tonic-gate start = s; 516*7c478bd9Sstevel@tonic-gate 517*7c478bd9Sstevel@tonic-gate if (letter(*s)) 518*7c478bd9Sstevel@tonic-gate { 519*7c478bd9Sstevel@tonic-gate while (alphanum(*s)) 520*7c478bd9Sstevel@tonic-gate s++; 521*7c478bd9Sstevel@tonic-gate 522*7c478bd9Sstevel@tonic-gate if (*s == '=') 523*7c478bd9Sstevel@tonic-gate { 524*7c478bd9Sstevel@tonic-gate *s = 0; 525*7c478bd9Sstevel@tonic-gate 526*7c478bd9Sstevel@tonic-gate if (eq(start, pathname)) 527*7c478bd9Sstevel@tonic-gate { 528*7c478bd9Sstevel@tonic-gate *s = '='; 529*7c478bd9Sstevel@tonic-gate return(1); 530*7c478bd9Sstevel@tonic-gate } 531*7c478bd9Sstevel@tonic-gate else 532*7c478bd9Sstevel@tonic-gate *s = '='; 533*7c478bd9Sstevel@tonic-gate } 534*7c478bd9Sstevel@tonic-gate } 535*7c478bd9Sstevel@tonic-gate arg = arg->argnxt; 536*7c478bd9Sstevel@tonic-gate } 537*7c478bd9Sstevel@tonic-gate 538*7c478bd9Sstevel@tonic-gate return(0); 539*7c478bd9Sstevel@tonic-gate } 540