Moved to sysdeps/generic.
[glibc.git] / posix / execvp.c
blobd8dce7aeb7a53fe9c3ecfa966e388baef68ac8c2
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 <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <paths.h>
27 /* The file is accessible but it is not an executable file. Invoke
28 the shell to interpret it as a script. */
29 static char **
30 internal_function
31 allocate_scripts_argv (const char *file, char *const argv[])
33 /* Count the arguments. */
34 int argc = 0;
35 while (argv[argc++])
38 /* Construct an argument list for the shell. */
39 char **new_argv = (char **) malloc ((argc + 1) * sizeof (char *));
40 if (new_argv != NULL)
42 new_argv[0] = (char *) _PATH_BSHELL;
43 new_argv[1] = (char *) file;
44 while (argc > 1)
46 new_argv[argc] = argv[argc - 1];
47 --argc;
51 return new_argv;
55 /* Execute FILE, searching in the `PATH' environment variable if it contains
56 no slashes, with arguments ARGV and environment from `environ'. */
57 int
58 execvp (file, argv)
59 const char *file;
60 char *const argv[];
62 if (*file == '\0')
64 /* We check the simple case first. */
65 __set_errno (ENOENT);
66 return -1;
69 char **script_argv = NULL;
71 if (strchr (file, '/') != NULL)
73 /* Don't search when it contains a slash. */
74 __execve (file, argv, __environ);
76 if (errno == ENOEXEC)
78 script_argv = allocate_scripts_argv (file, argv);
79 if (script_argv != NULL)
81 __execve (script_argv[0], script_argv, __environ);
83 free (script_argv);
87 else
89 char *path = getenv ("PATH");
90 bool path_malloc = false;
91 if (path == NULL)
93 /* There is no `PATH' in the environment.
94 The default search path is the current directory
95 followed by the path `confstr' returns for `_CS_PATH'. */
96 size_t len = confstr (_CS_PATH, (char *) NULL, 0);
97 path = (char *) malloc (1 + len);
98 if (path == NULL)
99 return -1;
100 path[0] = ':';
101 (void) confstr (_CS_PATH, path + 1, len);
102 path_malloc = true;
105 size_t len = strlen (file) + 1;
106 size_t pathlen = strlen (path);
107 char *name = malloc (pathlen + len + 1);
108 if (name == NULL)
110 if (path_malloc)
111 free (path);
112 return -1;
114 /* Copy the file name at the top. */
115 name = (char *) memcpy (name + pathlen + 1, file, len);
116 /* And add the slash. */
117 *--name = '/';
119 bool got_eacces = false;
120 char *p = path;
123 char *startp;
125 path = p;
126 p = __strchrnul (path, ':');
128 if (p == path)
129 /* Two adjacent colons, or a colon at the beginning or the end
130 of `PATH' means to search the current directory. */
131 startp = name + 1;
132 else
133 startp = (char *) memcpy (name - (p - path), path, p - path);
135 /* Try to execute this name. If it works, execv will not return. */
136 __execve (startp, argv, __environ);
138 if (errno == ENOEXEC)
140 if (script_argv == NULL)
142 script_argv = allocate_scripts_argv (file, argv);
143 if (script_argv == NULL)
145 /* A possible EACCES error is not as important as
146 the ENOMEM. */
147 got_eacces = false;
148 break;
152 __execve (script_argv[0], script_argv, __environ);
155 switch (errno)
157 case EACCES:
158 /* Record the we got a `Permission denied' error. If we end
159 up finding no executable we can use, we want to diagnose
160 that we did find one but were denied access. */
161 got_eacces = true;
162 case ENOENT:
163 case ESTALE:
164 case ENOTDIR:
165 /* Those errors indicate the file is missing or not executable
166 by us, in which case we want to just try the next path
167 directory. */
168 case ENODEV:
169 case ETIMEDOUT:
170 /* Some strange filesystems like AFS return even
171 stranger error numbers. They cannot reasonably mean
172 anything else so ignore those, too. */
173 break;
175 default:
176 /* Some other error means we found an executable file, but
177 something went wrong executing it; return the error to our
178 caller. */
179 return -1;
182 while (*p++ != '\0');
184 /* We tried every element and none of them worked. */
185 if (got_eacces)
186 /* At least one failure was due to permissions, so report that
187 error. */
188 __set_errno (EACCES);
190 free (script_argv);
191 free (name);
192 if (path_malloc)
193 free (path);
196 /* Return the error from the last attempt (probably ENOENT). */
197 return -1;
199 libc_hidden_def (execvp)