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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 /* The file is accessible but it is not an executable file. Invoke
31 the shell to interpret it as a script. */
34 scripts_argv (const char *file, char *const argv[], int argc, char **new_argv)
36 /* Construct an argument list for the shell. */
37 new_argv[0] = (char *) _PATH_BSHELL;
38 new_argv[1] = (char *) file;
41 new_argv[argc] = argv[argc - 1];
47 /* Execute FILE, searching in the `PATH' environment variable if it contains
48 no slashes, with arguments ARGV and environment from ENVP. */
50 __execvpe (file, argv, envp)
57 /* We check the simple case first. */
62 if (strchr (file, '/') != NULL)
64 /* Don't search when it contains a slash. */
65 __execve (file, argv, envp);
69 /* Count the arguments. */
73 size_t len = (argc + 1) * sizeof (char *);
76 if (__libc_use_alloca (len))
77 script_argv = alloca (len);
79 script_argv = ptr = malloc (len);
81 if (script_argv != NULL)
83 scripts_argv (file, argv, argc, script_argv);
84 __execve (script_argv[0], script_argv, envp);
94 char *path = getenv ("PATH");
97 pathlen = confstr (_CS_PATH, (char *) NULL, 0);
98 alloclen = pathlen + 1;
101 pathlen = strlen (path);
103 size_t len = strlen (file) + 1;
104 alloclen += pathlen + len + 1;
107 char *path_malloc = NULL;
108 if (__libc_use_alloca (alloclen))
109 name = alloca (alloclen);
112 path_malloc = name = malloc (alloclen);
119 /* There is no `PATH' in the environment.
120 The default search path is the current directory
121 followed by the path `confstr' returns for `_CS_PATH'. */
122 path = name + pathlen + len + 1;
124 (void) confstr (_CS_PATH, path + 1, pathlen);
127 /* Copy the file name at the top. */
128 name = (char *) memcpy (name + pathlen + 1, file, len);
129 /* And add the slash. */
132 char **script_argv = NULL;
133 void *script_argv_malloc = NULL;
134 bool got_eacces = false;
141 p = __strchrnul (path, ':');
144 /* Two adjacent colons, or a colon at the beginning or the end
145 of `PATH' means to search the current directory. */
148 startp = (char *) memcpy (name - (p - path), path, p - path);
150 /* Try to execute this name. If it works, execve will not return. */
151 __execve (startp, argv, envp);
153 if (errno == ENOEXEC)
155 if (script_argv == NULL)
157 /* Count the arguments. */
161 size_t arglen = (argc + 1) * sizeof (char *);
162 if (__libc_use_alloca (alloclen + arglen))
163 script_argv = alloca (arglen);
165 script_argv = script_argv_malloc = malloc (arglen);
166 if (script_argv == NULL)
168 /* A possible EACCES error is not as important as
173 scripts_argv (startp, argv, argc, script_argv);
176 __execve (script_argv[0], script_argv, envp);
182 /* Record the we got a `Permission denied' error. If we end
183 up finding no executable we can use, we want to diagnose
184 that we did find one but were denied access. */
189 /* Those errors indicate the file is missing or not executable
190 by us, in which case we want to just try the next path
194 /* Some strange filesystems like AFS return even
195 stranger error numbers. They cannot reasonably mean
196 anything else so ignore those, too. */
200 /* Some other error means we found an executable file, but
201 something went wrong executing it; return the error to our
206 while (*p++ != '\0');
208 /* We tried every element and none of them worked. */
210 /* At least one failure was due to permissions, so report that
212 __set_errno (EACCES);
214 free (script_argv_malloc);
218 /* Return the error from the last attempt (probably ENOENT). */
221 weak_alias (__execvpe, execvpe)