xref: /titanic_52/usr/src/lib/libbc/libc/gen/common/execvp.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*5d54f3d8Smuffin  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*      Copyright (c) 1984 AT&T */
287c478bd9Sstevel@tonic-gate /*        All Rights Reserved   */
297c478bd9Sstevel@tonic-gate 
30*5d54f3d8Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  *	execlp(name, arg,...,0)	(like execl, but does path search)
357c478bd9Sstevel@tonic-gate  *	execvp(name, argv)	(like execv, but does path search)
367c478bd9Sstevel@tonic-gate  */
37*5d54f3d8Smuffin #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
39*5d54f3d8Smuffin #include <stdarg.h>
40*5d54f3d8Smuffin #include <string.h>
41*5d54f3d8Smuffin #include <stdlib.h>
42*5d54f3d8Smuffin #include <unistd.h>
437c478bd9Sstevel@tonic-gate 
44*5d54f3d8Smuffin static char *execat(char *, char *, char *);
457c478bd9Sstevel@tonic-gate static char *shell = "/bin/sh";
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate int
48*5d54f3d8Smuffin execlp(char *name, ...)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	va_list	args;
51*5d54f3d8Smuffin 	int	r;
527c478bd9Sstevel@tonic-gate 
53*5d54f3d8Smuffin 	va_start(args, name);
54*5d54f3d8Smuffin 	r = execvp(name, (char **)args);
55*5d54f3d8Smuffin 	va_end(args);
56*5d54f3d8Smuffin 
57*5d54f3d8Smuffin 	return (r);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate int
61*5d54f3d8Smuffin execvp(char *name, char **argv)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	char	*pathstr;
647c478bd9Sstevel@tonic-gate 	char	fname[MAXPATHLEN];
657c478bd9Sstevel@tonic-gate 	char	*newargs[256];
667c478bd9Sstevel@tonic-gate 	int	i;
67*5d54f3d8Smuffin 	char	*cp;
68*5d54f3d8Smuffin 	unsigned etxtbsy = 1;
69*5d54f3d8Smuffin 	int	eacces = 0;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if ((pathstr = getenv("PATH")) == NULL)
727c478bd9Sstevel@tonic-gate 		pathstr = ":/usr/ucb:/bin:/usr/bin";
737c478bd9Sstevel@tonic-gate 	cp = strchr(name, '/') ? "": pathstr;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	do {
767c478bd9Sstevel@tonic-gate 		cp = execat(cp, name, fname);
777c478bd9Sstevel@tonic-gate 	retry:
787c478bd9Sstevel@tonic-gate 		(void) execv(fname, argv);
797c478bd9Sstevel@tonic-gate 		switch (errno) {
807c478bd9Sstevel@tonic-gate 		case ENOEXEC:
817c478bd9Sstevel@tonic-gate 			newargs[0] = "sh";
827c478bd9Sstevel@tonic-gate 			newargs[1] = fname;
83*5d54f3d8Smuffin 			for (i = 1; (newargs[i+1] = argv[i]) != NULL; ++i) {
847c478bd9Sstevel@tonic-gate 				if (i >= 254) {
857c478bd9Sstevel@tonic-gate 					errno = E2BIG;
867c478bd9Sstevel@tonic-gate 					return(-1);
877c478bd9Sstevel@tonic-gate 				}
887c478bd9Sstevel@tonic-gate 			}
897c478bd9Sstevel@tonic-gate 			(void) execv(shell, newargs);
907c478bd9Sstevel@tonic-gate 			return (-1);
917c478bd9Sstevel@tonic-gate 		case ETXTBSY:
927c478bd9Sstevel@tonic-gate 			if (++etxtbsy > 5)
937c478bd9Sstevel@tonic-gate 				return (-1);
947c478bd9Sstevel@tonic-gate 			(void) sleep(etxtbsy);
957c478bd9Sstevel@tonic-gate 			goto retry;
967c478bd9Sstevel@tonic-gate 		case EACCES:
977c478bd9Sstevel@tonic-gate 			++eacces;
987c478bd9Sstevel@tonic-gate 			break;
997c478bd9Sstevel@tonic-gate 		case ENOMEM:
1007c478bd9Sstevel@tonic-gate 		case E2BIG:
1017c478bd9Sstevel@tonic-gate 		case EFAULT:
1027c478bd9Sstevel@tonic-gate 			return (-1);
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 	} while (cp);
1057c478bd9Sstevel@tonic-gate 	if (eacces)
1067c478bd9Sstevel@tonic-gate 		errno = EACCES;
1077c478bd9Sstevel@tonic-gate 	return (-1);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static char *
111*5d54f3d8Smuffin execat(char *s1, char *s2, char *si)
1127c478bd9Sstevel@tonic-gate {
113*5d54f3d8Smuffin 	char	*s;
1147c478bd9Sstevel@tonic-gate 	char	*end;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	s = si;
1177c478bd9Sstevel@tonic-gate 	end = s + MAXPATHLEN;
1187c478bd9Sstevel@tonic-gate 	while (*s1 && *s1 != ':' && s < end)
1197c478bd9Sstevel@tonic-gate 		*s++ = *s1++;
1207c478bd9Sstevel@tonic-gate 	if (si != s && s < end)
1217c478bd9Sstevel@tonic-gate 		*s++ = '/';
1227c478bd9Sstevel@tonic-gate 	while (*s2 && s < end)
1237c478bd9Sstevel@tonic-gate 		*s++ = *s2++;
1247c478bd9Sstevel@tonic-gate 	*s = '\0';
1257c478bd9Sstevel@tonic-gate 	return (*s1 ? ++s1: 0);
1267c478bd9Sstevel@tonic-gate }
127