6429 SMB domain join doesn't work with libreSSL
[unleashed.git] / usr / src / lib / libtnfctl / prb_findexec.c
blob17feab3ce8a7cda01629f44d71ced6797bd4fab2
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright (c) 1994, by Sun Microsytems, Inc.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * interfaces to find an executable (from libc code)
32 /* Copyright (c) 1988 AT&T */
33 /* All Rights Reserved */
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <limits.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
44 #include "prb_proc_int.h"
46 static const char *exec_cat(const char *s1, const char *s2, char *si);
48 prb_status_t
49 find_executable(const char *name, char *ret_path)
51 const char *pathstr;
52 char fname[PATH_MAX + 2];
53 const char *cp;
54 struct stat stat_buf;
56 if (*name == '\0') {
57 return (prb_status_map(ENOENT));
59 if ((pathstr = getenv("PATH")) == NULL) {
60 if (geteuid() == 0 || getuid() == 0)
61 pathstr = "/usr/sbin:/usr/bin";
62 else
63 pathstr = "/usr/bin:";
65 cp = strchr(name, '/') ? (const char *) "" : pathstr;
67 do {
68 cp = exec_cat(cp, name, fname);
69 if (stat(fname, &stat_buf) != -1) {
70 /* successful find of the file */
71 (void) strncpy(ret_path, fname, PATH_MAX + 2);
72 return (PRB_STATUS_OK);
74 } while (cp);
76 return (prb_status_map(ENOENT));
81 static const char *
82 exec_cat(const char *s1, const char *s2, char *si)
84 char *s;
85 /* number of characters in s2 */
86 int cnt = PATH_MAX + 1;
88 s = si;
89 while (*s1 && *s1 != ':') {
90 if (cnt > 0) {
91 *s++ = *s1++;
92 cnt--;
93 } else
94 s1++;
96 if (si != s && cnt > 0) {
97 *s++ = '/';
98 cnt--;
100 while (*s2 && cnt > 0) {
101 *s++ = *s2++;
102 cnt--;
104 *s = '\0';
105 return (*s1 ? ++s1 : 0);