Sat Feb 10 04:18:48 1996 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[platform/upstream/glibc.git] / posix / execvp.c
1 /* Copyright (C) 1991, 1992, 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB.  If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA.  */
18
19 #include <unistd.h>
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <paths.h>
25
26 /* Execute FILE, searching in the `PATH' environment variable if it contains
27    no slashes, with arguments ARGV and environment from `environ'.  */
28 int
29 execvp (file, argv)
30      const char *file;
31      char *const argv[];
32 {
33   void execute (const char *file, char *const argv[])
34     {
35       execv (file, argv);
36
37       if (errno == ENOEXEC)
38         {
39           /* The file is accessible but it is not an executable file.
40              Invoke the shell to interpret it as a script.  */
41
42           int argc;
43           char **new_argv;
44
45           /* Count the arguments.  */
46           for (argc = 0; argv[argc++];);
47
48           /* Construct an argument list for the shell.  */
49           new_argv = __alloca ((argc + 1) * sizeof (char *));
50           for (new_argv[0] = _PATH_BSHELL; argc > 0; --argc)
51             new_argv[argc] = argv[argc - 1];
52
53           /* Execute the shell.  */
54           execv (new_argv[0], new_argv);
55         }
56     }
57
58   if (strchr (file, '/') != NULL)
59     /* Don't search when it contains a slash.  */
60     execute (file, argv);
61   else
62     {
63       char *path, *p, *name;
64       size_t len;
65
66       path = getenv ("PATH");
67       if (path == NULL)
68         {
69           /* There is no `PATH' in the environment.
70              The default search path is the current directory
71              followed by the path `confstr' returns for `_CS_PATH'.  */
72           len = confstr (_CS_PATH, (char *) NULL, 0);
73           path = (char *) __alloca (1 + len);
74           path[0] = ':';
75           (void) confstr (_CS_PATH, path + 1, len);
76         }
77
78       len = strlen (file) + 1;
79       name = __alloca (strlen (path) + len);
80       p = path;
81       do
82         {
83           path = p;
84           p = strchr (path, ':');
85           if (p == NULL)
86             p = strchr (path, '\0');
87
88           if (p == path)
89             /* Two adjacent colons, or a colon at the beginning or the end
90                of `PATH' means to search the current directory.  */
91             (void) memcpy (name, file, len);
92           else
93             {
94               /* Construct the pathname to try.  */
95               (void) memcpy (name, path, p - path);
96               name[p - path] = '/';
97               (void) memcpy (&name[(p - path) + 1], file, len);
98             }
99
100           /* Try to execute this name.  If it works, execv will not return.  */
101           execute (name, argv);
102
103           switch (errno)
104             {
105             case ENOENT:
106             case EACCES:
107               /* Those errors indicate the file is missing or not executable
108                  by us, in which case we want to just try the next path
109                  directory.  */
110               break;
111
112             default:
113               /* Some other error means we found an executable file, but
114                  something went wrong executing it; return the error to our
115                  caller.  */
116               return -1;
117             }
118         }
119       while (*p++ != '\0');
120     }
121
122   /* We tried every element and none of them worked.
123      Return the error from the last attempt (probably ENOENT).  */
124   return -1;
125 }