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 5*39e7390aSna195498 * Common Development and Distribution License (the "License"). 6*39e7390aSna195498 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21965005c8Schin 22965005c8Schin /* 23*39e7390aSna195498 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24965005c8Schin * Use is subject to license terms. 25965005c8Schin */ 26965005c8Schin 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 30965005c8Schin #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * UNIX shell 337c478bd9Sstevel@tonic-gate */ 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include "hash.h" 367c478bd9Sstevel@tonic-gate #include "defs.h" 377c478bd9Sstevel@tonic-gate #include <sys/types.h> 387c478bd9Sstevel@tonic-gate #include <sys/stat.h> 397c478bd9Sstevel@tonic-gate #include <errno.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #define EXECUTE 01 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate static unsigned char cost; 447c478bd9Sstevel@tonic-gate static int dotpath; 457c478bd9Sstevel@tonic-gate static int multrel; 467c478bd9Sstevel@tonic-gate static struct entry relcmd; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate static int argpath(); 49965005c8Schin static void pr_path(unsigned char *, int); 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate short 527c478bd9Sstevel@tonic-gate pathlook(com, flg, arg) 537c478bd9Sstevel@tonic-gate unsigned char *com; 547c478bd9Sstevel@tonic-gate int flg; 55965005c8Schin struct argnod *arg; 567c478bd9Sstevel@tonic-gate { 57965005c8Schin unsigned char *name = com; 58965005c8Schin ENTRY *h; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate ENTRY hentry; 617c478bd9Sstevel@tonic-gate int count = 0; 627c478bd9Sstevel@tonic-gate int i; 637c478bd9Sstevel@tonic-gate int pathset = 0; 647c478bd9Sstevel@tonic-gate int oldpath = 0; 657c478bd9Sstevel@tonic-gate struct namnod *n; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate hentry.data = 0; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate if (any('/', name)) 727c478bd9Sstevel@tonic-gate return(COMMAND); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate h = hfind(name); 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate if (h) 787c478bd9Sstevel@tonic-gate { 797c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 807c478bd9Sstevel@tonic-gate { 817c478bd9Sstevel@tonic-gate if (flg) 827c478bd9Sstevel@tonic-gate h->hits++; 837c478bd9Sstevel@tonic-gate return(h->data); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate if (arg && (pathset = argpath(arg))) 877c478bd9Sstevel@tonic-gate return(PATH_COMMAND); 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate if ((h->data & DOT_COMMAND) == DOT_COMMAND) 907c478bd9Sstevel@tonic-gate { 917c478bd9Sstevel@tonic-gate if (multrel == 0 && hashdata(h->data) > dotpath) 927c478bd9Sstevel@tonic-gate oldpath = hashdata(h->data); 937c478bd9Sstevel@tonic-gate else 947c478bd9Sstevel@tonic-gate oldpath = dotpath; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate h->data = 0; 977c478bd9Sstevel@tonic-gate goto pathsrch; 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate if (h->data & (COMMAND | REL_COMMAND)) 1017c478bd9Sstevel@tonic-gate { 1027c478bd9Sstevel@tonic-gate if (flg) 1037c478bd9Sstevel@tonic-gate h->hits++; 1047c478bd9Sstevel@tonic-gate return(h->data); 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate h->data = 0; 1087c478bd9Sstevel@tonic-gate h->cost = 0; 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate if (i = syslook(name, commands, no_commands)) 1127c478bd9Sstevel@tonic-gate { 1137c478bd9Sstevel@tonic-gate hentry.data = (BUILTIN | i); 1147c478bd9Sstevel@tonic-gate count = 1; 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate else 1177c478bd9Sstevel@tonic-gate { 1187c478bd9Sstevel@tonic-gate if (arg && (pathset = argpath(arg))) 1197c478bd9Sstevel@tonic-gate return(PATH_COMMAND); 1207c478bd9Sstevel@tonic-gate pathsrch: 1217c478bd9Sstevel@tonic-gate count = findpath(name, oldpath); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate if (count > 0) 1257c478bd9Sstevel@tonic-gate { 1267c478bd9Sstevel@tonic-gate if (h == 0) 1277c478bd9Sstevel@tonic-gate { 1287c478bd9Sstevel@tonic-gate hentry.cost = 0; 1297c478bd9Sstevel@tonic-gate hentry.key = make(name); 1307c478bd9Sstevel@tonic-gate h = henter(hentry); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate if (h->data == 0) 1347c478bd9Sstevel@tonic-gate { 1357c478bd9Sstevel@tonic-gate if (count < dotpath) 1367c478bd9Sstevel@tonic-gate h->data = COMMAND | count; 1377c478bd9Sstevel@tonic-gate else 1387c478bd9Sstevel@tonic-gate { 1397c478bd9Sstevel@tonic-gate h->data = REL_COMMAND | count; 1407c478bd9Sstevel@tonic-gate h->next = relcmd.next; 1417c478bd9Sstevel@tonic-gate relcmd.next = h; 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate h->hits = flg; 1477c478bd9Sstevel@tonic-gate h->cost += cost; 1487c478bd9Sstevel@tonic-gate return(h->data); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate else 1517c478bd9Sstevel@tonic-gate { 1527c478bd9Sstevel@tonic-gate return(-count); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate static void 1587c478bd9Sstevel@tonic-gate zapentry(h) 1597c478bd9Sstevel@tonic-gate ENTRY *h; 1607c478bd9Sstevel@tonic-gate { 1617c478bd9Sstevel@tonic-gate h->data &= HASHZAP; 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate void 1657c478bd9Sstevel@tonic-gate zaphash() 1667c478bd9Sstevel@tonic-gate { 1677c478bd9Sstevel@tonic-gate hscan(zapentry); 1687c478bd9Sstevel@tonic-gate relcmd.next = 0; 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate void 1727c478bd9Sstevel@tonic-gate zapcd() 1737c478bd9Sstevel@tonic-gate { 1747c478bd9Sstevel@tonic-gate ENTRY *ptr = relcmd.next; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate while (ptr) 1777c478bd9Sstevel@tonic-gate { 1787c478bd9Sstevel@tonic-gate ptr->data |= CDMARK; 1797c478bd9Sstevel@tonic-gate ptr = ptr->next; 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate relcmd.next = 0; 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate static void 1867c478bd9Sstevel@tonic-gate hashout(h) 1877c478bd9Sstevel@tonic-gate ENTRY *h; 1887c478bd9Sstevel@tonic-gate { 1897c478bd9Sstevel@tonic-gate sigchk(); 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate if (hashtype(h->data) == NOTFOUND) 1927c478bd9Sstevel@tonic-gate return; 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 1957c478bd9Sstevel@tonic-gate return; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate prn_buff(h->hits); 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate if (h->data & REL_COMMAND) 2007c478bd9Sstevel@tonic-gate prc_buff('*'); 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate prc_buff(TAB); 2047c478bd9Sstevel@tonic-gate prn_buff(h->cost); 2057c478bd9Sstevel@tonic-gate prc_buff(TAB); 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate pr_path(h->key, hashdata(h->data)); 2087c478bd9Sstevel@tonic-gate prc_buff(NL); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate void 2127c478bd9Sstevel@tonic-gate hashpr() 2137c478bd9Sstevel@tonic-gate { 214*39e7390aSna195498 prs_buff(_gettext("hits cost command\n")); 2157c478bd9Sstevel@tonic-gate hscan(hashout); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate 218965005c8Schin void 219965005c8Schin set_dotpath(void) 2207c478bd9Sstevel@tonic-gate { 221965005c8Schin unsigned char *path; 222965005c8Schin int cnt = 1; 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate dotpath = 10000; 2257c478bd9Sstevel@tonic-gate path = getpath(""); 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate while (path && *path) 2287c478bd9Sstevel@tonic-gate { 2297c478bd9Sstevel@tonic-gate if (*path == '/') 2307c478bd9Sstevel@tonic-gate cnt++; 2317c478bd9Sstevel@tonic-gate else 2327c478bd9Sstevel@tonic-gate { 2337c478bd9Sstevel@tonic-gate if (dotpath == 10000) 2347c478bd9Sstevel@tonic-gate dotpath = cnt; 2357c478bd9Sstevel@tonic-gate else 2367c478bd9Sstevel@tonic-gate { 2377c478bd9Sstevel@tonic-gate multrel = 1; 2387c478bd9Sstevel@tonic-gate return; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate path = nextpath(path); 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate multrel = 0; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 248965005c8Schin void 249965005c8Schin hash_func(unsigned char *name) 2507c478bd9Sstevel@tonic-gate { 2517c478bd9Sstevel@tonic-gate ENTRY *h; 2527c478bd9Sstevel@tonic-gate ENTRY hentry; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate h = hfind(name); 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if (h) 2577c478bd9Sstevel@tonic-gate h->data = FUNCTION; 2587c478bd9Sstevel@tonic-gate else 2597c478bd9Sstevel@tonic-gate { 2607c478bd9Sstevel@tonic-gate hentry.data = FUNCTION; 2617c478bd9Sstevel@tonic-gate hentry.key = make(name); 2627c478bd9Sstevel@tonic-gate hentry.cost = 0; 2637c478bd9Sstevel@tonic-gate hentry.hits = 0; 2647c478bd9Sstevel@tonic-gate henter(hentry); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate 268965005c8Schin void 269965005c8Schin func_unhash(unsigned char *name) 2707c478bd9Sstevel@tonic-gate { 2717c478bd9Sstevel@tonic-gate ENTRY *h; 2727c478bd9Sstevel@tonic-gate int i; 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate h = hfind(name); 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate if (h && (h->data & FUNCTION)) { 2777c478bd9Sstevel@tonic-gate if(i = syslook(name, commands, no_commands)) 2787c478bd9Sstevel@tonic-gate h->data = (BUILTIN|i); 2797c478bd9Sstevel@tonic-gate else 2807c478bd9Sstevel@tonic-gate h->data = NOTFOUND; 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate short 2867c478bd9Sstevel@tonic-gate hash_cmd(name) 2877c478bd9Sstevel@tonic-gate unsigned char *name; 2887c478bd9Sstevel@tonic-gate { 2897c478bd9Sstevel@tonic-gate ENTRY *h; 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate if (any('/', name)) 2927c478bd9Sstevel@tonic-gate return(COMMAND); 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate h = hfind(name); 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate if (h) 2977c478bd9Sstevel@tonic-gate { 2987c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 2997c478bd9Sstevel@tonic-gate return(h->data); 3007c478bd9Sstevel@tonic-gate else if ((h->data & REL_COMMAND) == REL_COMMAND) 3017c478bd9Sstevel@tonic-gate { /* unlink h from relative command list */ 3027c478bd9Sstevel@tonic-gate ENTRY *ptr = &relcmd; 3037c478bd9Sstevel@tonic-gate while(ptr-> next != h) 3047c478bd9Sstevel@tonic-gate ptr = ptr->next; 3057c478bd9Sstevel@tonic-gate ptr->next = h->next; 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate zapentry(h); 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate return(pathlook(name, 0, 0)); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate /* 3157c478bd9Sstevel@tonic-gate * Return 0 if found, 1 if not. 3167c478bd9Sstevel@tonic-gate */ 317965005c8Schin int 318965005c8Schin what_is_path(unsigned char *name) 3197c478bd9Sstevel@tonic-gate { 320965005c8Schin ENTRY *h; 3217c478bd9Sstevel@tonic-gate int cnt; 3227c478bd9Sstevel@tonic-gate short hashval; 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate h = hfind(name); 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate prs_buff(name); 3277c478bd9Sstevel@tonic-gate if (h) 3287c478bd9Sstevel@tonic-gate { 3297c478bd9Sstevel@tonic-gate hashval = hashdata(h->data); 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate switch (hashtype(h->data)) 3327c478bd9Sstevel@tonic-gate { 3337c478bd9Sstevel@tonic-gate case BUILTIN: 334*39e7390aSna195498 prs_buff(_gettext(" is a shell builtin\n")); 3357c478bd9Sstevel@tonic-gate return (0); 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate case FUNCTION: 3387c478bd9Sstevel@tonic-gate { 3397c478bd9Sstevel@tonic-gate struct namnod *n = lookup(name); 3407c478bd9Sstevel@tonic-gate 341*39e7390aSna195498 prs_buff(_gettext(" is a function\n")); 3427c478bd9Sstevel@tonic-gate prs_buff(name); 3437c478bd9Sstevel@tonic-gate prs_buff("(){\n"); 3447c478bd9Sstevel@tonic-gate prf(n->namenv); 3457c478bd9Sstevel@tonic-gate prs_buff("\n}\n"); 3467c478bd9Sstevel@tonic-gate return (0); 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate case REL_COMMAND: 3507c478bd9Sstevel@tonic-gate { 3517c478bd9Sstevel@tonic-gate short hash; 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate if ((h->data & DOT_COMMAND) == DOT_COMMAND) 3547c478bd9Sstevel@tonic-gate { 3557c478bd9Sstevel@tonic-gate hash = pathlook(name, 0, 0); 3567c478bd9Sstevel@tonic-gate if (hashtype(hash) == NOTFOUND) 3577c478bd9Sstevel@tonic-gate { 358*39e7390aSna195498 prs_buff(_gettext(" not" 359*39e7390aSna195498 " 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: 368*39e7390aSna195498 prs_buff(_gettext(" 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 { 377*39e7390aSna195498 prs_buff(_gettext(" 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 { 383*39e7390aSna195498 prs_buff(_gettext(" 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 { 390*39e7390aSna195498 prs_buff(_gettext(" not found\n")); 3917c478bd9Sstevel@tonic-gate return (1); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate 395965005c8Schin int 396965005c8Schin findpath(unsigned char *name, int oldpath) 3977c478bd9Sstevel@tonic-gate { 398965005c8Schin unsigned char *path; 399965005c8Schin 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 453965005c8Schin int 454965005c8Schin 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 486965005c8Schin static void 487965005c8Schin pr_path(unsigned char *name, int count) 4887c478bd9Sstevel@tonic-gate { 489965005c8Schin 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 501965005c8Schin static int 502965005c8Schin argpath(struct argnod *arg) 5037c478bd9Sstevel@tonic-gate { 504965005c8Schin unsigned char *s; 505965005c8Schin 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