BSD 4_4 release
[unix-history] / usr / src / lib / libc / gen / exec.c
index 5fce2be..2f7eadd 100644 (file)
@@ -1,12 +1,38 @@
 /*-
 /*-
- * Copyright (c) 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
  */
 
 #if defined(LIBC_SCCS) && !defined(lint)
-static char sccsid[] = "@(#)exec.c     5.9 (Berkeley) %G%";
+static char sccsid[] = "@(#)exec.c     8.1 (Berkeley) 6/4/93";
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
@@ -32,15 +58,19 @@ buildargv(ap, arg, envpp)
        const char *arg;
        char ***envpp;
 {
        const char *arg;
        char ***envpp;
 {
-       register size_t max, off;
-       register char **argv = NULL;
-
-       for (off = max = 0;; ++off) {
-               if (off >= max) {
-                       max += 50;      /* Starts out at 0. */
-                       max *= 2;       /* Ramp up fast. */
-                       if (!(argv = realloc(argv, max * sizeof(char *))))
-                               return(NULL);
+       static int memsize;
+       static char **argv;
+       register int off;
+
+       argv = NULL;
+       for (off = 0;; ++off) {
+               if (off >= memsize) {
+                       memsize += 50;  /* Starts out at 0. */
+                       memsize *= 2;   /* Ramp up fast. */
+                       if (!(argv = realloc(argv, memsize * sizeof(char *)))) {
+                               memsize = 0;
+                               return (NULL);
+                       }
                        if (off == 0) {
                                argv[0] = (char *)arg;
                                off = 1;
                        if (off == 0) {
                                argv[0] = (char *)arg;
                                off = 1;
@@ -52,7 +82,7 @@ buildargv(ap, arg, envpp)
        /* Get environment pointer if user supposed to provide one. */
        if (envpp)
                *envpp = va_arg(ap, char **);
        /* Get environment pointer if user supposed to provide one. */
        if (envpp)
                *envpp = va_arg(ap, char **);
-       return(argv);
+       return (argv);
 }
 
 int
 }
 
 int
@@ -74,13 +104,13 @@ execl(name, arg, va_alist)
 #else
        va_start(ap);
 #endif
 #else
        va_start(ap);
 #endif
-       if (argv = buildargv(ap, arg, (char ***)NULL))
+       if (argv = buildargv(ap, arg, NULL))
                (void)execve(name, argv, environ);
        va_end(ap);
        sverrno = errno;
        free(argv);
        errno = sverrno;
                (void)execve(name, argv, environ);
        va_end(ap);
        sverrno = errno;
        free(argv);
        errno = sverrno;
-       return(-1);
+       return (-1);
 }
 
 int
 }
 
 int
@@ -108,7 +138,7 @@ execle(name, arg, va_alist)
        sverrno = errno;
        free(argv);
        errno = sverrno;
        sverrno = errno;
        free(argv);
        errno = sverrno;
-       return(-1);
+       return (-1);
 }
 
 int
 }
 
 int
@@ -130,13 +160,13 @@ execlp(name, arg, va_alist)
 #else
        va_start(ap);
 #endif
 #else
        va_start(ap);
 #endif
-       if (argv = buildargv(ap, arg, (char ***)NULL))
+       if (argv = buildargv(ap, arg, NULL))
                (void)execvp(name, argv);
        va_end(ap);
        sverrno = errno;
        free(argv);
        errno = sverrno;
                (void)execvp(name, argv);
        va_end(ap);
        sverrno = errno;
        free(argv);
        errno = sverrno;
-       return(-1);
+       return (-1);
 }
 
 int
 }
 
 int
@@ -145,7 +175,7 @@ execv(name, argv)
        char * const *argv;
 {
        (void)execve(name, argv, environ);
        char * const *argv;
 {
        (void)execve(name, argv, environ);
-       return(-1);
+       return (-1);
 }
 
 int
 }
 
 int
@@ -153,7 +183,9 @@ execvp(name, argv)
        const char *name;
        char * const *argv;
 {
        const char *name;
        char * const *argv;
 {
-       register int lp, ln;
+       static int memsize;
+       static char **memp;
+       register int cnt, lp, ln;
        register char *p;
        int eacces, etxtbsy;
        char *bp, *cur, *path, buf[MAXPATHLEN];
        register char *p;
        int eacces, etxtbsy;
        char *bp, *cur, *path, buf[MAXPATHLEN];
@@ -207,20 +239,20 @@ retry:            (void)execve(bp, argv, environ);
                        break;
                case ENOENT:
                        break;
                        break;
                case ENOENT:
                        break;
-               case ENOEXEC: {
-                       register size_t cnt;
-                       register char **ap;
-
-                       for (cnt = 0, ap = (char **)argv; *ap; ++ap, ++cnt);
-                       if (ap = malloc((cnt + 2) * sizeof(char *))) {
-                               bcopy(argv + 1, ap + 2, cnt * sizeof(char *));
-                               ap[0] = "sh";
-                               ap[1] = bp;
-                               (void)execve(_PATH_BSHELL, ap, environ);
-                               free(ap);
+               case ENOEXEC:
+                       for (cnt = 0; argv[cnt]; ++cnt);
+                       if ((cnt + 2) * sizeof(char *) > memsize) {
+                               memsize = (cnt + 2) * sizeof(char *);
+                               if ((memp = realloc(memp, memsize)) == NULL) {
+                                       memsize = 0;
+                                       goto done;
+                               }
                        }
                        }
+                       memp[0] = "sh";
+                       memp[1] = bp;
+                       bcopy(argv + 1, memp + 2, cnt * sizeof(char *));
+                       (void)execve(_PATH_BSHELL, memp, environ);
                        goto done;
                        goto done;
-               }
                case ETXTBSY:
                        if (etxtbsy < 3)
                                (void)sleep(++etxtbsy);
                case ETXTBSY:
                        if (etxtbsy < 3)
                                (void)sleep(++etxtbsy);
@@ -235,5 +267,5 @@ retry:              (void)execve(bp, argv, environ);
                errno = ENOENT;
 done:  if (path)
                free(path);
                errno = ENOENT;
 done:  if (path)
                free(path);
-       return(-1);
+       return (-1);
 }
 }