17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright (c) 1994, by Sun Microsytems, Inc.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate/*
297c478bdstevel@tonic-gate * interfaces to find an executable (from libc code)
307c478bdstevel@tonic-gate */
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate/* Copyright (c) 1988 AT&T */
337c478bdstevel@tonic-gate/* All Rights Reserved   */
347c478bdstevel@tonic-gate
357c478bdstevel@tonic-gate
367c478bdstevel@tonic-gate#include <unistd.h>
377c478bdstevel@tonic-gate#include <stdlib.h>
387c478bdstevel@tonic-gate#include <string.h>
397c478bdstevel@tonic-gate#include <errno.h>
407c478bdstevel@tonic-gate#include <limits.h>
417c478bdstevel@tonic-gate#include <sys/types.h>
427c478bdstevel@tonic-gate#include <sys/stat.h>
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate#include "prb_proc_int.h"
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gatestatic const char *exec_cat(const char *s1, const char *s2, char *si);
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gateprb_status_t
497c478bdstevel@tonic-gatefind_executable(const char *name, char *ret_path)
507c478bdstevel@tonic-gate{
517c478bdstevel@tonic-gate	const char	 *pathstr;
527c478bdstevel@tonic-gate	char		fname[PATH_MAX + 2];
537c478bdstevel@tonic-gate	const char	 *cp;
547c478bdstevel@tonic-gate	struct stat	 stat_buf;
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gate	if (*name == '\0') {
577c478bdstevel@tonic-gate		return (prb_status_map(ENOENT));
587c478bdstevel@tonic-gate	}
597c478bdstevel@tonic-gate	if ((pathstr = getenv("PATH")) == NULL) {
607c478bdstevel@tonic-gate		if (geteuid() == 0 || getuid() == 0)
617c478bdstevel@tonic-gate			pathstr = "/usr/sbin:/usr/bin";
627c478bdstevel@tonic-gate		else
637c478bdstevel@tonic-gate			pathstr = "/usr/bin:";
647c478bdstevel@tonic-gate	}
657c478bdstevel@tonic-gate	cp = strchr(name, '/') ? (const char *) "" : pathstr;
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate	do {
687c478bdstevel@tonic-gate		cp = exec_cat(cp, name, fname);
697c478bdstevel@tonic-gate		if (stat(fname, &stat_buf) != -1) {
707c478bdstevel@tonic-gate			/* successful find of the file */
717c478bdstevel@tonic-gate			(void) strncpy(ret_path, fname, PATH_MAX + 2);
727c478bdstevel@tonic-gate			return (PRB_STATUS_OK);
737c478bdstevel@tonic-gate		}
747c478bdstevel@tonic-gate	} while (cp);
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate	return (prb_status_map(ENOENT));
777c478bdstevel@tonic-gate}
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gatestatic const char *
827c478bdstevel@tonic-gateexec_cat(const char *s1, const char *s2, char *si)
837c478bdstevel@tonic-gate{
847c478bdstevel@tonic-gate	char		   *s;
857c478bdstevel@tonic-gate	/* number of characters in s2 */
867c478bdstevel@tonic-gate	int			 cnt = PATH_MAX + 1;
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gate	s = si;
897c478bdstevel@tonic-gate	while (*s1 && *s1 != ':') {
907c478bdstevel@tonic-gate		if (cnt > 0) {
917c478bdstevel@tonic-gate			*s++ = *s1++;
927c478bdstevel@tonic-gate			cnt--;
937c478bdstevel@tonic-gate		} else
947c478bdstevel@tonic-gate			s1++;
957c478bdstevel@tonic-gate	}
967c478bdstevel@tonic-gate	if (si != s && cnt > 0) {
977c478bdstevel@tonic-gate		*s++ = '/';
987c478bdstevel@tonic-gate		cnt--;
997c478bdstevel@tonic-gate	}
1007c478bdstevel@tonic-gate	while (*s2 && cnt > 0) {
1017c478bdstevel@tonic-gate		*s++ = *s2++;
1027c478bdstevel@tonic-gate		cnt--;
1037c478bdstevel@tonic-gate	}
1047c478bdstevel@tonic-gate	*s = '\0';
1057c478bdstevel@tonic-gate	return (*s1 ? ++s1 : 0);
1067c478bdstevel@tonic-gate}
107