hurd: Add i386 fields to TLS structure
[glibc.git] / posix / execvpe.c
blobe067ada17bdd0bd4b9df71efe92c2ab26fa8aec2
1 /* Copyright (C) 1991-2014 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <alloca.h>
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 void
31 internal_function
32 scripts_argv (const char *file, char *const argv[], int argc, char **new_argv)
34 /* Construct an argument list for the shell. */
35 new_argv[0] = (char *) _PATH_BSHELL;
36 new_argv[1] = (char *) file;
37 while (argc > 1)
39 new_argv[argc] = argv[argc - 1];
40 --argc;
45 /* Execute FILE, searching in the `PATH' environment variable if it contains
46 no slashes, with arguments ARGV and environment from ENVP. */
47 int
48 __execvpe (file, argv, envp)
49 const char *file;
50 char *const argv[];
51 char *const envp[];
53 if (*file == '\0')
55 /* We check the simple case first. */
56 __set_errno (ENOENT);
57 return -1;
60 if (strchr (file, '/') != NULL)
62 /* Don't search when it contains a slash. */
63 __execve (file, argv, envp);
65 if (errno == ENOEXEC)
67 /* Count the arguments. */
68 int argc = 0;
69 while (argv[argc++])
71 size_t len = (argc + 1) * sizeof (char *);
72 char **script_argv;
73 void *ptr = NULL;
74 if (__libc_use_alloca (len))
75 script_argv = alloca (len);
76 else
77 script_argv = ptr = malloc (len);
79 if (script_argv != NULL)
81 scripts_argv (file, argv, argc, script_argv);
82 __execve (script_argv[0], script_argv, envp);
84 free (ptr);
88 else
90 size_t pathlen;
91 size_t alloclen = 0;
92 char *path = getenv ("PATH");
93 if (path == NULL)
95 pathlen = confstr (_CS_PATH, (char *) NULL, 0);
96 alloclen = pathlen + 1;
98 else
99 pathlen = strlen (path);
101 size_t len = strlen (file) + 1;
102 alloclen += pathlen + len + 1;
104 char *name;
105 char *path_malloc = NULL;
106 if (__libc_use_alloca (alloclen))
107 name = alloca (alloclen);
108 else
110 path_malloc = name = malloc (alloclen);
111 if (name == NULL)
112 return -1;
115 if (path == NULL)
117 /* There is no `PATH' in the environment.
118 The default search path is the current directory
119 followed by the path `confstr' returns for `_CS_PATH'. */
120 path = name + pathlen + len + 1;
121 path[0] = ':';
122 (void) confstr (_CS_PATH, path + 1, pathlen);
125 /* Copy the file name at the top. */
126 name = (char *) memcpy (name + pathlen + 1, file, len);
127 /* And add the slash. */
128 *--name = '/';
130 char **script_argv = NULL;
131 void *script_argv_malloc = NULL;
132 bool got_eacces = false;
133 char *p = path;
136 char *startp;
138 path = p;
139 p = __strchrnul (path, ':');
141 if (p == path)
142 /* Two adjacent colons, or a colon at the beginning or the end
143 of `PATH' means to search the current directory. */
144 startp = name + 1;
145 else
146 startp = (char *) memcpy (name - (p - path), path, p - path);
148 /* Try to execute this name. If it works, execve will not return. */
149 __execve (startp, argv, envp);
151 if (errno == ENOEXEC)
153 if (script_argv == NULL)
155 /* Count the arguments. */
156 int argc = 0;
157 while (argv[argc++])
159 size_t arglen = (argc + 1) * sizeof (char *);
160 if (__libc_use_alloca (alloclen + arglen))
161 script_argv = alloca (arglen);
162 else
163 script_argv = script_argv_malloc = malloc (arglen);
164 if (script_argv == NULL)
166 /* A possible EACCES error is not as important as
167 the ENOMEM. */
168 got_eacces = false;
169 break;
171 scripts_argv (startp, argv, argc, script_argv);
174 __execve (script_argv[0], script_argv, envp);
177 switch (errno)
179 case EACCES:
180 /* Record the we got a `Permission denied' error. If we end
181 up finding no executable we can use, we want to diagnose
182 that we did find one but were denied access. */
183 got_eacces = true;
184 case ENOENT:
185 case ESTALE:
186 case ENOTDIR:
187 /* Those errors indicate the file is missing or not executable
188 by us, in which case we want to just try the next path
189 directory. */
190 case ENODEV:
191 case ETIMEDOUT:
192 /* Some strange filesystems like AFS return even
193 stranger error numbers. They cannot reasonably mean
194 anything else so ignore those, too. */
195 break;
197 default:
198 /* Some other error means we found an executable file, but
199 something went wrong executing it; return the error to our
200 caller. */
201 return -1;
204 while (*p++ != '\0');
206 /* We tried every element and none of them worked. */
207 if (got_eacces)
208 /* At least one failure was due to permissions, so report that
209 error. */
210 __set_errno (EACCES);
212 free (script_argv_malloc);
213 free (path_malloc);
216 /* Return the error from the last attempt (probably ENOENT). */
217 return -1;
219 weak_alias (__execvpe, execvpe)