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 */ 22*965005c8Schin 23*965005c8Schin /* 24*965005c8Schin * Copyright 1990 Sun Microsystems, Inc. All rights reserved. 25*965005c8Schin * Use is subject to license terms. 26*965005c8Schin */ 27*965005c8Schin 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 297c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate 31*965005c8Schin #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 "hash.h" 377c478bd9Sstevel@tonic-gate #include "defs.h" 387c478bd9Sstevel@tonic-gate #include <sys/types.h> 397c478bd9Sstevel@tonic-gate #include <sys/stat.h> 407c478bd9Sstevel@tonic-gate #include <errno.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #define EXECUTE 01 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate static unsigned char cost; 457c478bd9Sstevel@tonic-gate static int dotpath; 467c478bd9Sstevel@tonic-gate static int multrel; 477c478bd9Sstevel@tonic-gate static struct entry relcmd; 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate static int argpath(); 50*965005c8Schin static void pr_path(unsigned char *, int); 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate short 537c478bd9Sstevel@tonic-gate pathlook(com, flg, arg) 547c478bd9Sstevel@tonic-gate unsigned char *com; 557c478bd9Sstevel@tonic-gate int flg; 56*965005c8Schin struct argnod *arg; 577c478bd9Sstevel@tonic-gate { 58*965005c8Schin unsigned char *name = com; 59*965005c8Schin ENTRY *h; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate ENTRY hentry; 627c478bd9Sstevel@tonic-gate int count = 0; 637c478bd9Sstevel@tonic-gate int i; 647c478bd9Sstevel@tonic-gate int pathset = 0; 657c478bd9Sstevel@tonic-gate int oldpath = 0; 667c478bd9Sstevel@tonic-gate struct namnod *n; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate hentry.data = 0; 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate if (any('/', name)) 737c478bd9Sstevel@tonic-gate return(COMMAND); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate h = hfind(name); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate if (h) 797c478bd9Sstevel@tonic-gate { 807c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 817c478bd9Sstevel@tonic-gate { 827c478bd9Sstevel@tonic-gate if (flg) 837c478bd9Sstevel@tonic-gate h->hits++; 847c478bd9Sstevel@tonic-gate return(h->data); 857c478bd9Sstevel@tonic-gate } 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate if (arg && (pathset = argpath(arg))) 887c478bd9Sstevel@tonic-gate return(PATH_COMMAND); 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate if ((h->data & DOT_COMMAND) == DOT_COMMAND) 917c478bd9Sstevel@tonic-gate { 927c478bd9Sstevel@tonic-gate if (multrel == 0 && hashdata(h->data) > dotpath) 937c478bd9Sstevel@tonic-gate oldpath = hashdata(h->data); 947c478bd9Sstevel@tonic-gate else 957c478bd9Sstevel@tonic-gate oldpath = dotpath; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate h->data = 0; 987c478bd9Sstevel@tonic-gate goto pathsrch; 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if (h->data & (COMMAND | REL_COMMAND)) 1027c478bd9Sstevel@tonic-gate { 1037c478bd9Sstevel@tonic-gate if (flg) 1047c478bd9Sstevel@tonic-gate h->hits++; 1057c478bd9Sstevel@tonic-gate return(h->data); 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate h->data = 0; 1097c478bd9Sstevel@tonic-gate h->cost = 0; 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate if (i = syslook(name, commands, no_commands)) 1137c478bd9Sstevel@tonic-gate { 1147c478bd9Sstevel@tonic-gate hentry.data = (BUILTIN | i); 1157c478bd9Sstevel@tonic-gate count = 1; 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate else 1187c478bd9Sstevel@tonic-gate { 1197c478bd9Sstevel@tonic-gate if (arg && (pathset = argpath(arg))) 1207c478bd9Sstevel@tonic-gate return(PATH_COMMAND); 1217c478bd9Sstevel@tonic-gate pathsrch: 1227c478bd9Sstevel@tonic-gate count = findpath(name, oldpath); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate if (count > 0) 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate if (h == 0) 1287c478bd9Sstevel@tonic-gate { 1297c478bd9Sstevel@tonic-gate hentry.cost = 0; 1307c478bd9Sstevel@tonic-gate hentry.key = make(name); 1317c478bd9Sstevel@tonic-gate h = henter(hentry); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate if (h->data == 0) 1357c478bd9Sstevel@tonic-gate { 1367c478bd9Sstevel@tonic-gate if (count < dotpath) 1377c478bd9Sstevel@tonic-gate h->data = COMMAND | count; 1387c478bd9Sstevel@tonic-gate else 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate h->data = REL_COMMAND | count; 1417c478bd9Sstevel@tonic-gate h->next = relcmd.next; 1427c478bd9Sstevel@tonic-gate relcmd.next = h; 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate h->hits = flg; 1487c478bd9Sstevel@tonic-gate h->cost += cost; 1497c478bd9Sstevel@tonic-gate return(h->data); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate else 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate return(-count); 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate static void 1597c478bd9Sstevel@tonic-gate zapentry(h) 1607c478bd9Sstevel@tonic-gate ENTRY *h; 1617c478bd9Sstevel@tonic-gate { 1627c478bd9Sstevel@tonic-gate h->data &= HASHZAP; 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate void 1667c478bd9Sstevel@tonic-gate zaphash() 1677c478bd9Sstevel@tonic-gate { 1687c478bd9Sstevel@tonic-gate hscan(zapentry); 1697c478bd9Sstevel@tonic-gate relcmd.next = 0; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate void 1737c478bd9Sstevel@tonic-gate zapcd() 1747c478bd9Sstevel@tonic-gate { 1757c478bd9Sstevel@tonic-gate ENTRY *ptr = relcmd.next; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate while (ptr) 1787c478bd9Sstevel@tonic-gate { 1797c478bd9Sstevel@tonic-gate ptr->data |= CDMARK; 1807c478bd9Sstevel@tonic-gate ptr = ptr->next; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate relcmd.next = 0; 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate static void 1877c478bd9Sstevel@tonic-gate hashout(h) 1887c478bd9Sstevel@tonic-gate ENTRY *h; 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate sigchk(); 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate if (hashtype(h->data) == NOTFOUND) 1937c478bd9Sstevel@tonic-gate return; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 1967c478bd9Sstevel@tonic-gate return; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate prn_buff(h->hits); 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate if (h->data & REL_COMMAND) 2017c478bd9Sstevel@tonic-gate prc_buff('*'); 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate prc_buff(TAB); 2057c478bd9Sstevel@tonic-gate prn_buff(h->cost); 2067c478bd9Sstevel@tonic-gate prc_buff(TAB); 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate pr_path(h->key, hashdata(h->data)); 2097c478bd9Sstevel@tonic-gate prc_buff(NL); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate void 2137c478bd9Sstevel@tonic-gate hashpr() 2147c478bd9Sstevel@tonic-gate { 2157c478bd9Sstevel@tonic-gate prs_buff("hits cost command\n"); 2167c478bd9Sstevel@tonic-gate hscan(hashout); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 219*965005c8Schin void 220*965005c8Schin set_dotpath(void) 2217c478bd9Sstevel@tonic-gate { 222*965005c8Schin unsigned char *path; 223*965005c8Schin int cnt = 1; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate dotpath = 10000; 2267c478bd9Sstevel@tonic-gate path = getpath(""); 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate while (path && *path) 2297c478bd9Sstevel@tonic-gate { 2307c478bd9Sstevel@tonic-gate if (*path == '/') 2317c478bd9Sstevel@tonic-gate cnt++; 2327c478bd9Sstevel@tonic-gate else 2337c478bd9Sstevel@tonic-gate { 2347c478bd9Sstevel@tonic-gate if (dotpath == 10000) 2357c478bd9Sstevel@tonic-gate dotpath = cnt; 2367c478bd9Sstevel@tonic-gate else 2377c478bd9Sstevel@tonic-gate { 2387c478bd9Sstevel@tonic-gate multrel = 1; 2397c478bd9Sstevel@tonic-gate return; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate path = nextpath(path); 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate multrel = 0; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 249*965005c8Schin void 250*965005c8Schin hash_func(unsigned char *name) 2517c478bd9Sstevel@tonic-gate { 2527c478bd9Sstevel@tonic-gate ENTRY *h; 2537c478bd9Sstevel@tonic-gate ENTRY hentry; 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate h = hfind(name); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate if (h) 2587c478bd9Sstevel@tonic-gate h->data = FUNCTION; 2597c478bd9Sstevel@tonic-gate else 2607c478bd9Sstevel@tonic-gate { 2617c478bd9Sstevel@tonic-gate hentry.data = FUNCTION; 2627c478bd9Sstevel@tonic-gate hentry.key = make(name); 2637c478bd9Sstevel@tonic-gate hentry.cost = 0; 2647c478bd9Sstevel@tonic-gate hentry.hits = 0; 2657c478bd9Sstevel@tonic-gate henter(hentry); 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 269*965005c8Schin void 270*965005c8Schin func_unhash(unsigned char *name) 2717c478bd9Sstevel@tonic-gate { 2727c478bd9Sstevel@tonic-gate ENTRY *h; 2737c478bd9Sstevel@tonic-gate int i; 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate h = hfind(name); 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate if (h && (h->data & FUNCTION)) { 2787c478bd9Sstevel@tonic-gate if(i = syslook(name, commands, no_commands)) 2797c478bd9Sstevel@tonic-gate h->data = (BUILTIN|i); 2807c478bd9Sstevel@tonic-gate else 2817c478bd9Sstevel@tonic-gate h->data = NOTFOUND; 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate short 2877c478bd9Sstevel@tonic-gate hash_cmd(name) 2887c478bd9Sstevel@tonic-gate unsigned char *name; 2897c478bd9Sstevel@tonic-gate { 2907c478bd9Sstevel@tonic-gate ENTRY *h; 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate if (any('/', name)) 2937c478bd9Sstevel@tonic-gate return(COMMAND); 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate h = hfind(name); 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate if (h) 2987c478bd9Sstevel@tonic-gate { 2997c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 3007c478bd9Sstevel@tonic-gate return(h->data); 3017c478bd9Sstevel@tonic-gate else if ((h->data & REL_COMMAND) == REL_COMMAND) 3027c478bd9Sstevel@tonic-gate { /* unlink h from relative command list */ 3037c478bd9Sstevel@tonic-gate ENTRY *ptr = &relcmd; 3047c478bd9Sstevel@tonic-gate while(ptr-> next != h) 3057c478bd9Sstevel@tonic-gate ptr = ptr->next; 3067c478bd9Sstevel@tonic-gate ptr->next = h->next; 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate zapentry(h); 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate return(pathlook(name, 0, 0)); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate /* 3167c478bd9Sstevel@tonic-gate * Return 0 if found, 1 if not. 3177c478bd9Sstevel@tonic-gate */ 318*965005c8Schin int 319*965005c8Schin what_is_path(unsigned char *name) 3207c478bd9Sstevel@tonic-gate { 321*965005c8Schin ENTRY *h; 3227c478bd9Sstevel@tonic-gate int cnt; 3237c478bd9Sstevel@tonic-gate short hashval; 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate h = hfind(name); 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate prs_buff(name); 3287c478bd9Sstevel@tonic-gate if (h) 3297c478bd9Sstevel@tonic-gate { 3307c478bd9Sstevel@tonic-gate hashval = hashdata(h->data); 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate switch (hashtype(h->data)) 3337c478bd9Sstevel@tonic-gate { 3347c478bd9Sstevel@tonic-gate case BUILTIN: 3357c478bd9Sstevel@tonic-gate prs_buff(" is a shell builtin\n"); 3367c478bd9Sstevel@tonic-gate return (0); 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate case FUNCTION: 3397c478bd9Sstevel@tonic-gate { 3407c478bd9Sstevel@tonic-gate struct namnod *n = lookup(name); 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate prs_buff(" is a function\n"); 3437c478bd9Sstevel@tonic-gate prs_buff(name); 3447c478bd9Sstevel@tonic-gate prs_buff("(){\n"); 3457c478bd9Sstevel@tonic-gate prf(n->namenv); 3467c478bd9Sstevel@tonic-gate prs_buff("\n}\n"); 3477c478bd9Sstevel@tonic-gate return (0); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate case REL_COMMAND: 3517c478bd9Sstevel@tonic-gate { 3527c478bd9Sstevel@tonic-gate short hash; 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate if ((h->data & DOT_COMMAND) == DOT_COMMAND) 3557c478bd9Sstevel@tonic-gate { 3567c478bd9Sstevel@tonic-gate hash = pathlook(name, 0, 0); 3577c478bd9Sstevel@tonic-gate if (hashtype(hash) == NOTFOUND) 3587c478bd9Sstevel@tonic-gate { 3597c478bd9Sstevel@tonic-gate prs_buff(" not found\n"); 3607c478bd9Sstevel@tonic-gate return (1); 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate else 3637c478bd9Sstevel@tonic-gate hashval = hashdata(hash); 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate case COMMAND: 3687c478bd9Sstevel@tonic-gate prs_buff(" is hashed ("); 3697c478bd9Sstevel@tonic-gate pr_path(name, hashval); 3707c478bd9Sstevel@tonic-gate prs_buff(")\n"); 3717c478bd9Sstevel@tonic-gate return (0); 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate if (syslook(name, commands, no_commands)) 3767c478bd9Sstevel@tonic-gate { 3777c478bd9Sstevel@tonic-gate prs_buff(" is a shell builtin\n"); 3787c478bd9Sstevel@tonic-gate return (0); 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate if ((cnt = findpath(name, 0)) > 0) 3827c478bd9Sstevel@tonic-gate { 3837c478bd9Sstevel@tonic-gate prs_buff(" is "); 3847c478bd9Sstevel@tonic-gate pr_path(name, cnt); 3857c478bd9Sstevel@tonic-gate prc_buff(NL); 3867c478bd9Sstevel@tonic-gate return (0); 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate else 3897c478bd9Sstevel@tonic-gate { 3907c478bd9Sstevel@tonic-gate prs_buff(" not found\n"); 3917c478bd9Sstevel@tonic-gate return (1); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 395*965005c8Schin int 396*965005c8Schin findpath(unsigned char *name, int oldpath) 3977c478bd9Sstevel@tonic-gate { 398*965005c8Schin unsigned char *path; 399*965005c8Schin int count = 1; 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate unsigned char *p; 4027c478bd9Sstevel@tonic-gate int ok = 1; 4037c478bd9Sstevel@tonic-gate int e_code = 1; 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate cost = 0; 4067c478bd9Sstevel@tonic-gate path = getpath(name); 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate if (oldpath) 4097c478bd9Sstevel@tonic-gate { 4107c478bd9Sstevel@tonic-gate count = dotpath; 4117c478bd9Sstevel@tonic-gate while (--count) 4127c478bd9Sstevel@tonic-gate path = nextpath(path); 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate if (oldpath > dotpath) 4157c478bd9Sstevel@tonic-gate { 4167c478bd9Sstevel@tonic-gate catpath(path, name); 4177c478bd9Sstevel@tonic-gate p = curstak(); 4187c478bd9Sstevel@tonic-gate cost = 1; 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate if ((ok = chk_access(p, S_IEXEC, 1)) == 0) 4217c478bd9Sstevel@tonic-gate return(dotpath); 4227c478bd9Sstevel@tonic-gate else 4237c478bd9Sstevel@tonic-gate return(oldpath); 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate else 4267c478bd9Sstevel@tonic-gate count = dotpath; 4277c478bd9Sstevel@tonic-gate } 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate while (path) 4307c478bd9Sstevel@tonic-gate { 4317c478bd9Sstevel@tonic-gate path = catpath(path, name); 4327c478bd9Sstevel@tonic-gate cost++; 4337c478bd9Sstevel@tonic-gate p = curstak(); 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate if ((ok = chk_access(p, S_IEXEC, 1)) == 0) 4367c478bd9Sstevel@tonic-gate break; 4377c478bd9Sstevel@tonic-gate else 4387c478bd9Sstevel@tonic-gate e_code = max(e_code, ok); 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate count++; 4417c478bd9Sstevel@tonic-gate } 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate return(ok ? -e_code : count); 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate /* 4477c478bd9Sstevel@tonic-gate * Determine if file given by name is accessible with permissions 4487c478bd9Sstevel@tonic-gate * given by mode. 4497c478bd9Sstevel@tonic-gate * Regflag argument non-zero means not to consider 4507c478bd9Sstevel@tonic-gate * a non-regular file as executable. 4517c478bd9Sstevel@tonic-gate */ 4527c478bd9Sstevel@tonic-gate 453*965005c8Schin int 454*965005c8Schin chk_access(unsigned char *name, mode_t mode, int regflag) 4557c478bd9Sstevel@tonic-gate { 4567c478bd9Sstevel@tonic-gate static int flag; 4577c478bd9Sstevel@tonic-gate static uid_t euid; 4587c478bd9Sstevel@tonic-gate struct stat statb; 4597c478bd9Sstevel@tonic-gate mode_t ftype; 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate if(flag == 0) { 4627c478bd9Sstevel@tonic-gate euid = geteuid(); 4637c478bd9Sstevel@tonic-gate flag = 1; 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate ftype = statb.st_mode & S_IFMT; 4667c478bd9Sstevel@tonic-gate if (stat((char *)name, &statb) == 0) { 4677c478bd9Sstevel@tonic-gate ftype = statb.st_mode & S_IFMT; 4687c478bd9Sstevel@tonic-gate if(mode == S_IEXEC && regflag && ftype != S_IFREG) 4697c478bd9Sstevel@tonic-gate return(2); 4707c478bd9Sstevel@tonic-gate if(access((char *)name, 010|(mode>>6)) == 0) { 4717c478bd9Sstevel@tonic-gate if(euid == 0) { 4727c478bd9Sstevel@tonic-gate if (ftype != S_IFREG || mode != S_IEXEC) 4737c478bd9Sstevel@tonic-gate return(0); 4747c478bd9Sstevel@tonic-gate /* root can execute file as long as it has execute 4757c478bd9Sstevel@tonic-gate permission for someone */ 4767c478bd9Sstevel@tonic-gate if (statb.st_mode & (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6))) 4777c478bd9Sstevel@tonic-gate return(0); 4787c478bd9Sstevel@tonic-gate return(3); 4797c478bd9Sstevel@tonic-gate } 4807c478bd9Sstevel@tonic-gate return(0); 4817c478bd9Sstevel@tonic-gate } 4827c478bd9Sstevel@tonic-gate } 4837c478bd9Sstevel@tonic-gate return(errno == EACCES ? 3 : 1); 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 486*965005c8Schin static void 487*965005c8Schin pr_path(unsigned char *name, int count) 4887c478bd9Sstevel@tonic-gate { 489*965005c8Schin unsigned char *path; 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate path = getpath(name); 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate while (--count && path) 4947c478bd9Sstevel@tonic-gate path = nextpath(path, name); 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate catpath(path, name); 4977c478bd9Sstevel@tonic-gate prs_buff(curstak()); 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate 501*965005c8Schin static int 502*965005c8Schin argpath(struct argnod *arg) 5037c478bd9Sstevel@tonic-gate { 504*965005c8Schin unsigned char *s; 505*965005c8Schin unsigned char *start; 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate while (arg) 5087c478bd9Sstevel@tonic-gate { 5097c478bd9Sstevel@tonic-gate s = arg->argval; 5107c478bd9Sstevel@tonic-gate start = s; 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate if (letter(*s)) 5137c478bd9Sstevel@tonic-gate { 5147c478bd9Sstevel@tonic-gate while (alphanum(*s)) 5157c478bd9Sstevel@tonic-gate s++; 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate if (*s == '=') 5187c478bd9Sstevel@tonic-gate { 5197c478bd9Sstevel@tonic-gate *s = 0; 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate if (eq(start, pathname)) 5227c478bd9Sstevel@tonic-gate { 5237c478bd9Sstevel@tonic-gate *s = '='; 5247c478bd9Sstevel@tonic-gate return(1); 5257c478bd9Sstevel@tonic-gate } 5267c478bd9Sstevel@tonic-gate else 5277c478bd9Sstevel@tonic-gate *s = '='; 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate arg = arg->argnxt; 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate return(0); 5347c478bd9Sstevel@tonic-gate } 535