10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*722Smuffin * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
30*722Smuffin #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*LINTLIBRARY*/
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * execlp(name, arg,...,0) (like execl, but does path search)
350Sstevel@tonic-gate * execvp(name, argv) (like execv, but does path search)
360Sstevel@tonic-gate */
37*722Smuffin #include <errno.h>
380Sstevel@tonic-gate #include <sys/param.h>
39*722Smuffin #include <stdarg.h>
40*722Smuffin #include <string.h>
41*722Smuffin #include <stdlib.h>
42*722Smuffin #include <unistd.h>
430Sstevel@tonic-gate
44*722Smuffin static char *execat(char *, char *, char *);
450Sstevel@tonic-gate static char *shell = "/bin/sh";
460Sstevel@tonic-gate
470Sstevel@tonic-gate int
execlp(char * name,...)48*722Smuffin execlp(char *name, ...)
490Sstevel@tonic-gate {
50*722Smuffin va_list args;
51*722Smuffin int r;
520Sstevel@tonic-gate
53*722Smuffin va_start(args, name);
54*722Smuffin r = execvp(name, (char **)args);
55*722Smuffin va_end(args);
56*722Smuffin
57*722Smuffin return (r);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate
600Sstevel@tonic-gate int
execvp(char * name,char ** argv)61*722Smuffin execvp(char *name, char **argv)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate char *pathstr;
640Sstevel@tonic-gate char fname[MAXPATHLEN];
650Sstevel@tonic-gate char *newargs[256];
660Sstevel@tonic-gate int i;
67*722Smuffin char *cp;
68*722Smuffin unsigned etxtbsy = 1;
69*722Smuffin int eacces = 0;
700Sstevel@tonic-gate
71*722Smuffin if ((pathstr = getenv("PATH")) == NULL)
720Sstevel@tonic-gate pathstr = ":/usr/ucb:/bin:/usr/bin";
73*722Smuffin cp = strchr(name, '/') ? "": pathstr;
740Sstevel@tonic-gate
750Sstevel@tonic-gate do {
760Sstevel@tonic-gate cp = execat(cp, name, fname);
770Sstevel@tonic-gate retry:
780Sstevel@tonic-gate (void) execv(fname, argv);
79*722Smuffin switch (errno) {
800Sstevel@tonic-gate case ENOEXEC:
810Sstevel@tonic-gate newargs[0] = "sh";
820Sstevel@tonic-gate newargs[1] = fname;
83*722Smuffin for (i = 1; (newargs[i+1] = argv[i]) != NULL; ++i) {
84*722Smuffin if (i >= 254) {
850Sstevel@tonic-gate errno = E2BIG;
860Sstevel@tonic-gate return(-1);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate }
890Sstevel@tonic-gate (void) execv(shell, newargs);
90*722Smuffin return (-1);
910Sstevel@tonic-gate case ETXTBSY:
92*722Smuffin if (++etxtbsy > 5)
93*722Smuffin return (-1);
940Sstevel@tonic-gate (void) sleep(etxtbsy);
950Sstevel@tonic-gate goto retry;
960Sstevel@tonic-gate case EACCES:
970Sstevel@tonic-gate ++eacces;
980Sstevel@tonic-gate break;
990Sstevel@tonic-gate case ENOMEM:
1000Sstevel@tonic-gate case E2BIG:
1010Sstevel@tonic-gate case EFAULT:
102*722Smuffin return (-1);
1030Sstevel@tonic-gate }
104*722Smuffin } while (cp);
105*722Smuffin if (eacces)
1060Sstevel@tonic-gate errno = EACCES;
107*722Smuffin return (-1);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate static char *
execat(char * s1,char * s2,char * si)111*722Smuffin execat(char *s1, char *s2, char *si)
1120Sstevel@tonic-gate {
113*722Smuffin char *s;
1140Sstevel@tonic-gate char *end;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate s = si;
1170Sstevel@tonic-gate end = s + MAXPATHLEN;
118*722Smuffin while (*s1 && *s1 != ':' && s < end)
1190Sstevel@tonic-gate *s++ = *s1++;
120*722Smuffin if (si != s && s < end)
1210Sstevel@tonic-gate *s++ = '/';
122*722Smuffin while (*s2 && s < end)
1230Sstevel@tonic-gate *s++ = *s2++;
1240Sstevel@tonic-gate *s = '\0';
125*722Smuffin return (*s1 ? ++s1: 0);
1260Sstevel@tonic-gate }
127