1 /* Copyright (C) 1991,92, 1995-99, 2002, 2004, 2005, 2007, 2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
29 /* The file is accessible but it is not an executable file. Invoke
30 the shell to interpret it as a script. */
33 scripts_argv (const char *file, char *const argv[], int argc, char **new_argv)
35 /* Construct an argument list for the shell. */
36 new_argv[0] = (char *) _PATH_BSHELL;
37 new_argv[1] = (char *) file;
40 new_argv[argc] = argv[argc - 1];
46 /* Execute FILE, searching in the `PATH' environment variable if it contains
47 no slashes, with arguments ARGV and environment from ENVP. */
49 __execvpe (file, argv, envp)
56 /* We check the simple case first. */
61 if (strchr (file, '/') != NULL)
63 /* Don't search when it contains a slash. */
64 __execve (file, argv, envp);
68 /* Count the arguments. */
72 size_t len = (argc + 1) * sizeof (char *);
75 if (__libc_use_alloca (len))
76 script_argv = alloca (len);
78 script_argv = ptr = malloc (len);
80 if (script_argv != NULL)
82 scripts_argv (file, argv, argc, script_argv);
83 __execve (script_argv[0], script_argv, envp);
93 char *path = getenv ("PATH");
96 pathlen = confstr (_CS_PATH, (char *) NULL, 0);
97 alloclen = pathlen + 1;
100 pathlen = strlen (path);
102 size_t len = strlen (file) + 1;
103 alloclen += pathlen + len + 1;
106 char *path_malloc = NULL;
107 if (__libc_use_alloca (alloclen))
108 name = alloca (alloclen);
111 path_malloc = name = malloc (alloclen);
118 /* There is no `PATH' in the environment.
119 The default search path is the current directory
120 followed by the path `confstr' returns for `_CS_PATH'. */
121 path = name + pathlen + len + 1;
123 (void) confstr (_CS_PATH, path + 1, pathlen);
126 /* Copy the file name at the top. */
127 name = (char *) memcpy (name + pathlen + 1, file, len);
128 /* And add the slash. */
131 char **script_argv = NULL;
132 void *script_argv_malloc = NULL;
133 bool got_eacces = false;
140 p = __strchrnul (path, ':');
143 /* Two adjacent colons, or a colon at the beginning or the end
144 of `PATH' means to search the current directory. */
147 startp = (char *) memcpy (name - (p - path), path, p - path);
149 /* Try to execute this name. If it works, execve will not return. */
150 __execve (startp, argv, envp);
152 if (errno == ENOEXEC)
154 if (script_argv == NULL)
156 /* Count the arguments. */
160 size_t arglen = (argc + 1) * sizeof (char *);
161 if (__libc_use_alloca (alloclen + arglen))
162 script_argv = alloca (arglen);
164 script_argv = script_argv_malloc = malloc (arglen);
165 if (script_argv == NULL)
167 /* A possible EACCES error is not as important as
172 scripts_argv (startp, argv, argc, script_argv);
175 __execve (script_argv[0], script_argv, envp);
181 /* Record the we got a `Permission denied' error. If we end
182 up finding no executable we can use, we want to diagnose
183 that we did find one but were denied access. */
188 /* Those errors indicate the file is missing or not executable
189 by us, in which case we want to just try the next path
193 /* Some strange filesystems like AFS return even
194 stranger error numbers. They cannot reasonably mean
195 anything else so ignore those, too. */
199 /* Some other error means we found an executable file, but
200 something went wrong executing it; return the error to our
205 while (*p++ != '\0');
207 /* We tried every element and none of them worked. */
209 /* At least one failure was due to permissions, so report that
211 __set_errno (EACCES);
213 free (script_argv_malloc);
217 /* Return the error from the last attempt (probably ENOENT). */
220 weak_alias (__execvpe, execvpe)