xref: /titanic_52/usr/src/lib/libtnfctl/prb_findexec.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1994, by Sun Microsytems, Inc.
24*7c478bd9Sstevel@tonic-gate  */
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate  * interfaces to find an executable (from libc code)
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
33*7c478bd9Sstevel@tonic-gate /* All Rights Reserved   */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <unistd.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate #include <limits.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include "prb_proc_int.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static const char *exec_cat(const char *s1, const char *s2, char *si);
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate prb_status_t
49*7c478bd9Sstevel@tonic-gate find_executable(const char *name, char *ret_path)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	const char	 *pathstr;
52*7c478bd9Sstevel@tonic-gate 	char		fname[PATH_MAX + 2];
53*7c478bd9Sstevel@tonic-gate 	const char	 *cp;
54*7c478bd9Sstevel@tonic-gate 	struct stat	 stat_buf;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	if (*name == '\0') {
57*7c478bd9Sstevel@tonic-gate 		return (prb_status_map(ENOENT));
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 	if ((pathstr = getenv("PATH")) == NULL) {
60*7c478bd9Sstevel@tonic-gate 		if (geteuid() == 0 || getuid() == 0)
61*7c478bd9Sstevel@tonic-gate 			pathstr = "/usr/sbin:/usr/bin";
62*7c478bd9Sstevel@tonic-gate 		else
63*7c478bd9Sstevel@tonic-gate 			pathstr = "/usr/bin:";
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 	cp = strchr(name, '/') ? (const char *) "" : pathstr;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	do {
68*7c478bd9Sstevel@tonic-gate 		cp = exec_cat(cp, name, fname);
69*7c478bd9Sstevel@tonic-gate 		if (stat(fname, &stat_buf) != -1) {
70*7c478bd9Sstevel@tonic-gate 			/* successful find of the file */
71*7c478bd9Sstevel@tonic-gate 			(void) strncpy(ret_path, fname, PATH_MAX + 2);
72*7c478bd9Sstevel@tonic-gate 			return (PRB_STATUS_OK);
73*7c478bd9Sstevel@tonic-gate 		}
74*7c478bd9Sstevel@tonic-gate 	} while (cp);
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	return (prb_status_map(ENOENT));
77*7c478bd9Sstevel@tonic-gate }
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate static const char *
82*7c478bd9Sstevel@tonic-gate exec_cat(const char *s1, const char *s2, char *si)
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	char		   *s;
85*7c478bd9Sstevel@tonic-gate 	/* number of characters in s2 */
86*7c478bd9Sstevel@tonic-gate 	int			 cnt = PATH_MAX + 1;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	s = si;
89*7c478bd9Sstevel@tonic-gate 	while (*s1 && *s1 != ':') {
90*7c478bd9Sstevel@tonic-gate 		if (cnt > 0) {
91*7c478bd9Sstevel@tonic-gate 			*s++ = *s1++;
92*7c478bd9Sstevel@tonic-gate 			cnt--;
93*7c478bd9Sstevel@tonic-gate 		} else
94*7c478bd9Sstevel@tonic-gate 			s1++;
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 	if (si != s && cnt > 0) {
97*7c478bd9Sstevel@tonic-gate 		*s++ = '/';
98*7c478bd9Sstevel@tonic-gate 		cnt--;
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 	while (*s2 && cnt > 0) {
101*7c478bd9Sstevel@tonic-gate 		*s++ = *s2++;
102*7c478bd9Sstevel@tonic-gate 		cnt--;
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 	*s = '\0';
105*7c478bd9Sstevel@tonic-gate 	return (*s1 ? ++s1 : 0);
106*7c478bd9Sstevel@tonic-gate }
107