2004-11-18 Alexandre Oliva <aoliva@redhat.com>
[glibc.git] / posix / execvp.c
blob9ccfd7fc22ca934d95d4dbc57ad4ea34ffc54864
1 /* Copyright (C) 1991,92,1995-99,2002,2004,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <unistd.h>
20 #include <stdarg.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <paths.h>
28 /* The file is accessible but it is not an executable file. Invoke
29 the shell to interpret it as a script. */
30 static char **
31 internal_function
32 allocate_scripts_argv (const char *file, char *const argv[])
34 /* Count the arguments. */
35 int argc = 0;
36 while (argv[argc++])
39 /* Construct an argument list for the shell. */
40 char **new_argv = (char **) malloc ((argc + 1) * sizeof (char *));
41 if (new_argv != NULL)
43 new_argv[0] = (char *) _PATH_BSHELL;
44 new_argv[1] = (char *) file;
45 while (argc > 1)
47 new_argv[argc] = argv[argc - 1];
48 --argc;
52 return new_argv;
56 /* Execute FILE, searching in the `PATH' environment variable if it contains
57 no slashes, with arguments ARGV and environment from `environ'. */
58 int
59 execvp (file, argv)
60 const char *file;
61 char *const argv[];
63 if (*file == '\0')
65 /* We check the simple case first. */
66 __set_errno (ENOENT);
67 return -1;
70 char **script_argv = NULL;
72 if (strchr (file, '/') != NULL)
74 /* Don't search when it contains a slash. */
75 __execve (file, argv, __environ);
77 if (errno == ENOEXEC)
79 script_argv = allocate_scripts_argv (file, argv);
80 if (script_argv != NULL)
82 __execve (script_argv[0], script_argv, __environ);
84 free (script_argv);
88 else
90 char *path = getenv ("PATH");
91 bool path_malloc = false;
92 if (path == NULL)
94 /* There is no `PATH' in the environment.
95 The default search path is the current directory
96 followed by the path `confstr' returns for `_CS_PATH'. */
97 size_t len = confstr (_CS_PATH, (char *) NULL, 0);
98 path = (char *) malloc (1 + len);
99 if (path == NULL)
100 return -1;
101 path[0] = ':';
102 (void) confstr (_CS_PATH, path + 1, len);
103 path_malloc = true;
106 size_t len = strlen (file) + 1;
107 size_t pathlen = strlen (path);
108 char *name = malloc (pathlen + len + 1);
109 if (name == NULL)
111 if (path_malloc)
112 free (path);
113 return -1;
115 /* Copy the file name at the top. */
116 name = (char *) memcpy (name + pathlen + 1, file, len);
117 /* And add the slash. */
118 *--name = '/';
120 bool got_eacces = false;
121 char *p = path;
124 char *startp;
126 path = p;
127 p = __strchrnul (path, ':');
129 if (p == path)
130 /* Two adjacent colons, or a colon at the beginning or the end
131 of `PATH' means to search the current directory. */
132 startp = name + 1;
133 else
134 startp = (char *) memcpy (name - (p - path), path, p - path);
136 /* Try to execute this name. If it works, execv will not return. */
137 __execve (startp, argv, __environ);
139 if (errno == ENOEXEC)
141 if (script_argv == NULL)
143 script_argv = allocate_scripts_argv (file, argv);
144 if (script_argv == NULL)
146 /* A possible EACCES error is not as important as
147 the ENOMEM. */
148 got_eacces = false;
149 break;
153 __execve (script_argv[0], script_argv, __environ);
156 switch (errno)
158 case EACCES:
159 /* Record the we got a `Permission denied' error. If we end
160 up finding no executable we can use, we want to diagnose
161 that we did find one but were denied access. */
162 got_eacces = true;
163 case ENOENT:
164 case ESTALE:
165 case ENOTDIR:
166 /* Those errors indicate the file is missing or not executable
167 by us, in which case we want to just try the next path
168 directory. */
169 case ENODEV:
170 case ETIMEDOUT:
171 /* Some strange filesystems like AFS return even
172 stranger error numbers. They cannot reasonably mean
173 anything else so ignore those, too. */
174 break;
176 default:
177 /* Some other error means we found an executable file, but
178 something went wrong executing it; return the error to our
179 caller. */
180 return -1;
183 while (*p++ != '\0');
185 /* We tried every element and none of them worked. */
186 if (got_eacces)
187 /* At least one failure was due to permissions, so report that
188 error. */
189 __set_errno (EACCES);
191 free (script_argv);
192 free (name);
193 if (path_malloc)
194 free (path);
197 /* Return the error from the last attempt (probably ENOENT). */
198 return -1;
200 libc_hidden_def (execvp)