1 /* Copyright (C) 1991-2015 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/>. */
28 /* The file is accessible but it is not an executable file. Invoke
29 the shell to interpret it as a script. */
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;
39 new_argv[argc] = argv[argc - 1];
45 /* Execute FILE, searching in the `PATH' environment variable if it contains
46 no slashes, with arguments ARGV and environment from ENVP. */
48 __execvpe (file, argv, envp)
55 /* We check the simple case first. */
60 if (strchr (file, '/') != NULL)
62 /* Don't search when it contains a slash. */
63 __execve (file, argv, envp);
67 /* Count the arguments. */
71 size_t len = (argc + 1) * sizeof (char *);
74 if (__libc_use_alloca (len))
75 script_argv = alloca (len);
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);
92 char *path = getenv ("PATH");
95 pathlen = confstr (_CS_PATH, (char *) NULL, 0);
96 alloclen = pathlen + 1;
99 pathlen = strlen (path);
101 size_t len = strlen (file) + 1;
102 alloclen += pathlen + len + 1;
105 char *path_malloc = NULL;
106 if (__libc_use_alloca (alloclen))
107 name = alloca (alloclen);
110 path_malloc = name = malloc (alloclen);
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;
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. */
130 char **script_argv = NULL;
131 void *script_argv_malloc = NULL;
132 bool got_eacces = false;
139 p = __strchrnul (path, ':');
142 /* Two adjacent colons, or a colon at the beginning or the end
143 of `PATH' means to search the current directory. */
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. */
159 size_t arglen = (argc + 1) * sizeof (char *);
160 if (__libc_use_alloca (alloclen + arglen))
161 script_argv = alloca (arglen);
163 script_argv = script_argv_malloc = malloc (arglen);
164 if (script_argv == NULL)
166 /* A possible EACCES error is not as important as
171 scripts_argv (startp, argv, argc, script_argv);
174 __execve (script_argv[0], script_argv, envp);
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. */
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
192 /* Some strange filesystems like AFS return even
193 stranger error numbers. They cannot reasonably mean
194 anything else so ignore those, too. */
198 /* Some other error means we found an executable file, but
199 something went wrong executing it; return the error to our
204 while (*p++ != '\0');
206 /* We tried every element and none of them worked. */
208 /* At least one failure was due to permissions, so report that
210 __set_errno (EACCES);
212 free (script_argv_malloc);
216 /* Return the error from the last attempt (probably ENOENT). */
219 weak_alias (__execvpe, execvpe)