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 539e7390aSna195498 * Common Development and Distribution License (the "License"). 639e7390aSna195498 * 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*d6b882a9SNobutomo Nakano * Copyright 2009 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 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * UNIX shell 327c478bd9Sstevel@tonic-gate */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include "hash.h" 357c478bd9Sstevel@tonic-gate #include "defs.h" 367c478bd9Sstevel@tonic-gate #include <sys/types.h> 377c478bd9Sstevel@tonic-gate #include <sys/stat.h> 387c478bd9Sstevel@tonic-gate #include <errno.h> 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #define EXECUTE 01 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate static unsigned char cost; 437c478bd9Sstevel@tonic-gate static int dotpath; 447c478bd9Sstevel@tonic-gate static int multrel; 457c478bd9Sstevel@tonic-gate static struct entry relcmd; 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate static int argpath(); 48965005c8Schin static void pr_path(unsigned char *, int); 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate short 517c478bd9Sstevel@tonic-gate pathlook(com, flg, arg) 527c478bd9Sstevel@tonic-gate unsigned char *com; 537c478bd9Sstevel@tonic-gate int flg; 54965005c8Schin struct argnod *arg; 557c478bd9Sstevel@tonic-gate { 56965005c8Schin unsigned char *name = com; 57965005c8Schin ENTRY *h; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate ENTRY hentry; 607c478bd9Sstevel@tonic-gate int count = 0; 617c478bd9Sstevel@tonic-gate int i; 627c478bd9Sstevel@tonic-gate int pathset = 0; 637c478bd9Sstevel@tonic-gate int oldpath = 0; 647c478bd9Sstevel@tonic-gate struct namnod *n; 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate hentry.data = 0; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate if (any('/', name)) 717c478bd9Sstevel@tonic-gate return(COMMAND); 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate h = hfind(name); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate if (h) 777c478bd9Sstevel@tonic-gate { 787c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 797c478bd9Sstevel@tonic-gate { 807c478bd9Sstevel@tonic-gate if (flg) 817c478bd9Sstevel@tonic-gate h->hits++; 827c478bd9Sstevel@tonic-gate return(h->data); 837c478bd9Sstevel@tonic-gate } 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate if (arg && (pathset = argpath(arg))) 867c478bd9Sstevel@tonic-gate return(PATH_COMMAND); 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate if ((h->data & DOT_COMMAND) == DOT_COMMAND) 897c478bd9Sstevel@tonic-gate { 907c478bd9Sstevel@tonic-gate if (multrel == 0 && hashdata(h->data) > dotpath) 917c478bd9Sstevel@tonic-gate oldpath = hashdata(h->data); 927c478bd9Sstevel@tonic-gate else 937c478bd9Sstevel@tonic-gate oldpath = dotpath; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate h->data = 0; 967c478bd9Sstevel@tonic-gate goto pathsrch; 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate if (h->data & (COMMAND | REL_COMMAND)) 1007c478bd9Sstevel@tonic-gate { 1017c478bd9Sstevel@tonic-gate if (flg) 1027c478bd9Sstevel@tonic-gate h->hits++; 1037c478bd9Sstevel@tonic-gate return(h->data); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate h->data = 0; 1077c478bd9Sstevel@tonic-gate h->cost = 0; 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate if (i = syslook(name, commands, no_commands)) 1117c478bd9Sstevel@tonic-gate { 1127c478bd9Sstevel@tonic-gate hentry.data = (BUILTIN | i); 1137c478bd9Sstevel@tonic-gate count = 1; 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate else 1167c478bd9Sstevel@tonic-gate { 1177c478bd9Sstevel@tonic-gate if (arg && (pathset = argpath(arg))) 1187c478bd9Sstevel@tonic-gate return(PATH_COMMAND); 1197c478bd9Sstevel@tonic-gate pathsrch: 1207c478bd9Sstevel@tonic-gate count = findpath(name, oldpath); 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate if (count > 0) 1247c478bd9Sstevel@tonic-gate { 1257c478bd9Sstevel@tonic-gate if (h == 0) 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate hentry.cost = 0; 1287c478bd9Sstevel@tonic-gate hentry.key = make(name); 1297c478bd9Sstevel@tonic-gate h = henter(hentry); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate if (h->data == 0) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate if (count < dotpath) 1357c478bd9Sstevel@tonic-gate h->data = COMMAND | count; 1367c478bd9Sstevel@tonic-gate else 1377c478bd9Sstevel@tonic-gate { 1387c478bd9Sstevel@tonic-gate h->data = REL_COMMAND | count; 1397c478bd9Sstevel@tonic-gate h->next = relcmd.next; 1407c478bd9Sstevel@tonic-gate relcmd.next = h; 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate h->hits = flg; 1467c478bd9Sstevel@tonic-gate h->cost += cost; 1477c478bd9Sstevel@tonic-gate return(h->data); 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate else 1507c478bd9Sstevel@tonic-gate { 1517c478bd9Sstevel@tonic-gate return(-count); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate static void 1577c478bd9Sstevel@tonic-gate zapentry(h) 1587c478bd9Sstevel@tonic-gate ENTRY *h; 1597c478bd9Sstevel@tonic-gate { 1607c478bd9Sstevel@tonic-gate h->data &= HASHZAP; 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate void 1647c478bd9Sstevel@tonic-gate zaphash() 1657c478bd9Sstevel@tonic-gate { 1667c478bd9Sstevel@tonic-gate hscan(zapentry); 1677c478bd9Sstevel@tonic-gate relcmd.next = 0; 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate void 1717c478bd9Sstevel@tonic-gate zapcd() 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate ENTRY *ptr = relcmd.next; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate while (ptr) 1767c478bd9Sstevel@tonic-gate { 1777c478bd9Sstevel@tonic-gate ptr->data |= CDMARK; 1787c478bd9Sstevel@tonic-gate ptr = ptr->next; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate relcmd.next = 0; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate static void 1857c478bd9Sstevel@tonic-gate hashout(h) 1867c478bd9Sstevel@tonic-gate ENTRY *h; 1877c478bd9Sstevel@tonic-gate { 1887c478bd9Sstevel@tonic-gate sigchk(); 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate if (hashtype(h->data) == NOTFOUND) 1917c478bd9Sstevel@tonic-gate return; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 1947c478bd9Sstevel@tonic-gate return; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate prn_buff(h->hits); 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate if (h->data & REL_COMMAND) 1997c478bd9Sstevel@tonic-gate prc_buff('*'); 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate prc_buff(TAB); 2037c478bd9Sstevel@tonic-gate prn_buff(h->cost); 2047c478bd9Sstevel@tonic-gate prc_buff(TAB); 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate pr_path(h->key, hashdata(h->data)); 2077c478bd9Sstevel@tonic-gate prc_buff(NL); 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate void 2117c478bd9Sstevel@tonic-gate hashpr() 2127c478bd9Sstevel@tonic-gate { 21339e7390aSna195498 prs_buff(_gettext("hits cost command\n")); 2147c478bd9Sstevel@tonic-gate hscan(hashout); 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 217965005c8Schin void 218965005c8Schin set_dotpath(void) 2197c478bd9Sstevel@tonic-gate { 220965005c8Schin unsigned char *path; 221965005c8Schin int cnt = 1; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate dotpath = 10000; 2247c478bd9Sstevel@tonic-gate path = getpath(""); 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate while (path && *path) 2277c478bd9Sstevel@tonic-gate { 2287c478bd9Sstevel@tonic-gate if (*path == '/') 2297c478bd9Sstevel@tonic-gate cnt++; 2307c478bd9Sstevel@tonic-gate else 2317c478bd9Sstevel@tonic-gate { 2327c478bd9Sstevel@tonic-gate if (dotpath == 10000) 2337c478bd9Sstevel@tonic-gate dotpath = cnt; 2347c478bd9Sstevel@tonic-gate else 2357c478bd9Sstevel@tonic-gate { 2367c478bd9Sstevel@tonic-gate multrel = 1; 2377c478bd9Sstevel@tonic-gate return; 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate path = nextpath(path); 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate multrel = 0; 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate 247965005c8Schin void 248965005c8Schin hash_func(unsigned char *name) 2497c478bd9Sstevel@tonic-gate { 2507c478bd9Sstevel@tonic-gate ENTRY *h; 2517c478bd9Sstevel@tonic-gate ENTRY hentry; 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate h = hfind(name); 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate if (h) 2567c478bd9Sstevel@tonic-gate h->data = FUNCTION; 2577c478bd9Sstevel@tonic-gate else 2587c478bd9Sstevel@tonic-gate { 2597c478bd9Sstevel@tonic-gate hentry.data = FUNCTION; 2607c478bd9Sstevel@tonic-gate hentry.key = make(name); 2617c478bd9Sstevel@tonic-gate hentry.cost = 0; 2627c478bd9Sstevel@tonic-gate hentry.hits = 0; 2637c478bd9Sstevel@tonic-gate henter(hentry); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 267965005c8Schin void 268965005c8Schin func_unhash(unsigned char *name) 2697c478bd9Sstevel@tonic-gate { 2707c478bd9Sstevel@tonic-gate ENTRY *h; 2717c478bd9Sstevel@tonic-gate int i; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate h = hfind(name); 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate if (h && (h->data & FUNCTION)) { 2767c478bd9Sstevel@tonic-gate if(i = syslook(name, commands, no_commands)) 2777c478bd9Sstevel@tonic-gate h->data = (BUILTIN|i); 2787c478bd9Sstevel@tonic-gate else 2797c478bd9Sstevel@tonic-gate h->data = NOTFOUND; 2807c478bd9Sstevel@tonic-gate } 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate short 2857c478bd9Sstevel@tonic-gate hash_cmd(name) 2867c478bd9Sstevel@tonic-gate unsigned char *name; 2877c478bd9Sstevel@tonic-gate { 2887c478bd9Sstevel@tonic-gate ENTRY *h; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate if (any('/', name)) 2917c478bd9Sstevel@tonic-gate return(COMMAND); 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate h = hfind(name); 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate if (h) 2967c478bd9Sstevel@tonic-gate { 2977c478bd9Sstevel@tonic-gate if (h->data & (BUILTIN | FUNCTION)) 2987c478bd9Sstevel@tonic-gate return(h->data); 2997c478bd9Sstevel@tonic-gate else if ((h->data & REL_COMMAND) == REL_COMMAND) 3007c478bd9Sstevel@tonic-gate { /* unlink h from relative command list */ 3017c478bd9Sstevel@tonic-gate ENTRY *ptr = &relcmd; 3027c478bd9Sstevel@tonic-gate while(ptr-> next != h) 3037c478bd9Sstevel@tonic-gate ptr = ptr->next; 3047c478bd9Sstevel@tonic-gate ptr->next = h->next; 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate zapentry(h); 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate return(pathlook(name, 0, 0)); 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate /* 3147c478bd9Sstevel@tonic-gate * Return 0 if found, 1 if not. 3157c478bd9Sstevel@tonic-gate */ 316965005c8Schin int 317965005c8Schin what_is_path(unsigned char *name) 3187c478bd9Sstevel@tonic-gate { 319965005c8Schin ENTRY *h; 3207c478bd9Sstevel@tonic-gate int cnt; 3217c478bd9Sstevel@tonic-gate short hashval; 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate h = hfind(name); 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate prs_buff(name); 3267c478bd9Sstevel@tonic-gate if (h) 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate hashval = hashdata(h->data); 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate switch (hashtype(h->data)) 3317c478bd9Sstevel@tonic-gate { 3327c478bd9Sstevel@tonic-gate case BUILTIN: 33339e7390aSna195498 prs_buff(_gettext(" is a shell builtin\n")); 3347c478bd9Sstevel@tonic-gate return (0); 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate case FUNCTION: 3377c478bd9Sstevel@tonic-gate { 3387c478bd9Sstevel@tonic-gate struct namnod *n = lookup(name); 339*d6b882a9SNobutomo Nakano struct fndnod *f = fndptr(n->namenv); 3407c478bd9Sstevel@tonic-gate 34139e7390aSna195498 prs_buff(_gettext(" is a function\n")); 3427c478bd9Sstevel@tonic-gate prs_buff(name); 3437c478bd9Sstevel@tonic-gate prs_buff("(){\n"); 344*d6b882a9SNobutomo Nakano if (f != NULL) 345*d6b882a9SNobutomo Nakano prf(f->fndval); 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 { 35939e7390aSna195498 prs_buff(_gettext(" not" 36039e7390aSna195498 " found\n")); 3617c478bd9Sstevel@tonic-gate return (1); 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate else 3647c478bd9Sstevel@tonic-gate hashval = hashdata(hash); 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate case COMMAND: 36939e7390aSna195498 prs_buff(_gettext(" is hashed (")); 3707c478bd9Sstevel@tonic-gate pr_path(name, hashval); 3717c478bd9Sstevel@tonic-gate prs_buff(")\n"); 3727c478bd9Sstevel@tonic-gate return (0); 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate if (syslook(name, commands, no_commands)) 3777c478bd9Sstevel@tonic-gate { 37839e7390aSna195498 prs_buff(_gettext(" is a shell builtin\n")); 3797c478bd9Sstevel@tonic-gate return (0); 3807c478bd9Sstevel@tonic-gate } 3817c478bd9Sstevel@tonic-gate 3827c478bd9Sstevel@tonic-gate if ((cnt = findpath(name, 0)) > 0) 3837c478bd9Sstevel@tonic-gate { 38439e7390aSna195498 prs_buff(_gettext(" is ")); 3857c478bd9Sstevel@tonic-gate pr_path(name, cnt); 3867c478bd9Sstevel@tonic-gate prc_buff(NL); 3877c478bd9Sstevel@tonic-gate return (0); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate else 3907c478bd9Sstevel@tonic-gate { 39139e7390aSna195498 prs_buff(_gettext(" not found\n")); 3927c478bd9Sstevel@tonic-gate return (1); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate } 3957c478bd9Sstevel@tonic-gate 396965005c8Schin int 397965005c8Schin findpath(unsigned char *name, int oldpath) 3987c478bd9Sstevel@tonic-gate { 399965005c8Schin unsigned char *path; 400965005c8Schin int count = 1; 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate unsigned char *p; 4037c478bd9Sstevel@tonic-gate int ok = 1; 4047c478bd9Sstevel@tonic-gate int e_code = 1; 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate cost = 0; 4077c478bd9Sstevel@tonic-gate path = getpath(name); 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate if (oldpath) 4107c478bd9Sstevel@tonic-gate { 4117c478bd9Sstevel@tonic-gate count = dotpath; 4127c478bd9Sstevel@tonic-gate while (--count) 4137c478bd9Sstevel@tonic-gate path = nextpath(path); 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate if (oldpath > dotpath) 4167c478bd9Sstevel@tonic-gate { 4177c478bd9Sstevel@tonic-gate catpath(path, name); 4187c478bd9Sstevel@tonic-gate p = curstak(); 4197c478bd9Sstevel@tonic-gate cost = 1; 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate if ((ok = chk_access(p, S_IEXEC, 1)) == 0) 4227c478bd9Sstevel@tonic-gate return(dotpath); 4237c478bd9Sstevel@tonic-gate else 4247c478bd9Sstevel@tonic-gate return(oldpath); 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate else 4277c478bd9Sstevel@tonic-gate count = dotpath; 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate while (path) 4317c478bd9Sstevel@tonic-gate { 4327c478bd9Sstevel@tonic-gate path = catpath(path, name); 4337c478bd9Sstevel@tonic-gate cost++; 4347c478bd9Sstevel@tonic-gate p = curstak(); 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate if ((ok = chk_access(p, S_IEXEC, 1)) == 0) 4377c478bd9Sstevel@tonic-gate break; 4387c478bd9Sstevel@tonic-gate else 4397c478bd9Sstevel@tonic-gate e_code = max(e_code, ok); 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate count++; 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate return(ok ? -e_code : count); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate /* 4487c478bd9Sstevel@tonic-gate * Determine if file given by name is accessible with permissions 4497c478bd9Sstevel@tonic-gate * given by mode. 4507c478bd9Sstevel@tonic-gate * Regflag argument non-zero means not to consider 4517c478bd9Sstevel@tonic-gate * a non-regular file as executable. 4527c478bd9Sstevel@tonic-gate */ 4537c478bd9Sstevel@tonic-gate 454965005c8Schin int 455965005c8Schin chk_access(unsigned char *name, mode_t mode, int regflag) 4567c478bd9Sstevel@tonic-gate { 4577c478bd9Sstevel@tonic-gate static int flag; 4587c478bd9Sstevel@tonic-gate static uid_t euid; 4597c478bd9Sstevel@tonic-gate struct stat statb; 4607c478bd9Sstevel@tonic-gate mode_t ftype; 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate if(flag == 0) { 4637c478bd9Sstevel@tonic-gate euid = geteuid(); 4647c478bd9Sstevel@tonic-gate flag = 1; 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate ftype = statb.st_mode & S_IFMT; 4677c478bd9Sstevel@tonic-gate if (stat((char *)name, &statb) == 0) { 4687c478bd9Sstevel@tonic-gate ftype = statb.st_mode & S_IFMT; 4697c478bd9Sstevel@tonic-gate if(mode == S_IEXEC && regflag && ftype != S_IFREG) 4707c478bd9Sstevel@tonic-gate return(2); 4717c478bd9Sstevel@tonic-gate if(access((char *)name, 010|(mode>>6)) == 0) { 4727c478bd9Sstevel@tonic-gate if(euid == 0) { 4737c478bd9Sstevel@tonic-gate if (ftype != S_IFREG || mode != S_IEXEC) 4747c478bd9Sstevel@tonic-gate return(0); 4757c478bd9Sstevel@tonic-gate /* root can execute file as long as it has execute 4767c478bd9Sstevel@tonic-gate permission for someone */ 4777c478bd9Sstevel@tonic-gate if (statb.st_mode & (S_IEXEC|(S_IEXEC>>3)|(S_IEXEC>>6))) 4787c478bd9Sstevel@tonic-gate return(0); 4797c478bd9Sstevel@tonic-gate return(3); 4807c478bd9Sstevel@tonic-gate } 4817c478bd9Sstevel@tonic-gate return(0); 4827c478bd9Sstevel@tonic-gate } 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate return(errno == EACCES ? 3 : 1); 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate 487965005c8Schin static void 488965005c8Schin pr_path(unsigned char *name, int count) 4897c478bd9Sstevel@tonic-gate { 490965005c8Schin unsigned char *path; 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate path = getpath(name); 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate while (--count && path) 4957c478bd9Sstevel@tonic-gate path = nextpath(path, name); 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate catpath(path, name); 4987c478bd9Sstevel@tonic-gate prs_buff(curstak()); 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate 502965005c8Schin static int 503965005c8Schin argpath(struct argnod *arg) 5047c478bd9Sstevel@tonic-gate { 505965005c8Schin unsigned char *s; 506965005c8Schin unsigned char *start; 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate while (arg) 5097c478bd9Sstevel@tonic-gate { 5107c478bd9Sstevel@tonic-gate s = arg->argval; 5117c478bd9Sstevel@tonic-gate start = s; 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate if (letter(*s)) 5147c478bd9Sstevel@tonic-gate { 5157c478bd9Sstevel@tonic-gate while (alphanum(*s)) 5167c478bd9Sstevel@tonic-gate s++; 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate if (*s == '=') 5197c478bd9Sstevel@tonic-gate { 5207c478bd9Sstevel@tonic-gate *s = 0; 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate if (eq(start, pathname)) 5237c478bd9Sstevel@tonic-gate { 5247c478bd9Sstevel@tonic-gate *s = '='; 5257c478bd9Sstevel@tonic-gate return(1); 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate else 5287c478bd9Sstevel@tonic-gate *s = '='; 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate } 5317c478bd9Sstevel@tonic-gate arg = arg->argnxt; 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate 5347c478bd9Sstevel@tonic-gate return(0); 5357c478bd9Sstevel@tonic-gate } 536