Optimie x86-64 SSE4 memcmp for unaligned data.
[glibc.git] / posix / execvpe.c
blobb4f40d3a7ccf60a4cee6afb158e040890b580250
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
18 02111-1307 USA. */
20 #include <alloca.h>
21 #include <unistd.h>
22 #include <stdarg.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <paths.h>
30 /* The file is accessible but it is not an executable file. Invoke
31 the shell to interpret it as a script. */
32 static void
33 internal_function
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;
39 while (argc > 1)
41 new_argv[argc] = argv[argc - 1];
42 --argc;
47 /* Execute FILE, searching in the `PATH' environment variable if it contains
48 no slashes, with arguments ARGV and environment from ENVP. */
49 int
50 __execvpe (file, argv, envp)
51 const char *file;
52 char *const argv[];
53 char *const envp[];
55 if (*file == '\0')
57 /* We check the simple case first. */
58 __set_errno (ENOENT);
59 return -1;
62 if (strchr (file, '/') != NULL)
64 /* Don't search when it contains a slash. */
65 __execve (file, argv, envp);
67 if (errno == ENOEXEC)
69 /* Count the arguments. */
70 int argc = 0;
71 while (argv[argc++])
73 size_t len = (argc + 1) * sizeof (char *);
74 char **script_argv;
75 void *ptr = NULL;
76 if (__libc_use_alloca (len))
77 script_argv = alloca (len);
78 else
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);
86 free (ptr);
90 else
92 size_t pathlen;
93 size_t alloclen = 0;
94 char *path = getenv ("PATH");
95 if (path == NULL)
97 pathlen = confstr (_CS_PATH, (char *) NULL, 0);
98 alloclen = pathlen + 1;
100 else
101 pathlen = strlen (path);
103 size_t len = strlen (file) + 1;
104 alloclen += pathlen + len + 1;
106 char *name;
107 char *path_malloc = NULL;
108 if (__libc_use_alloca (alloclen))
109 name = alloca (alloclen);
110 else
112 path_malloc = name = malloc (alloclen);
113 if (name == NULL)
114 return -1;
117 if (path == NULL)
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;
123 path[0] = ':';
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. */
130 *--name = '/';
132 char **script_argv = NULL;
133 void *script_argv_malloc = NULL;
134 bool got_eacces = false;
135 char *p = path;
138 char *startp;
140 path = p;
141 p = __strchrnul (path, ':');
143 if (p == path)
144 /* Two adjacent colons, or a colon at the beginning or the end
145 of `PATH' means to search the current directory. */
146 startp = name + 1;
147 else
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. */
158 int argc = 0;
159 while (argv[argc++])
161 size_t arglen = (argc + 1) * sizeof (char *);
162 if (__libc_use_alloca (alloclen + arglen))
163 script_argv = alloca (arglen);
164 else
165 script_argv = script_argv_malloc = malloc (arglen);
166 if (script_argv == NULL)
168 /* A possible EACCES error is not as important as
169 the ENOMEM. */
170 got_eacces = false;
171 break;
173 scripts_argv (startp, argv, argc, script_argv);
176 __execve (script_argv[0], script_argv, envp);
179 switch (errno)
181 case EACCES:
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. */
185 got_eacces = true;
186 case ENOENT:
187 case ESTALE:
188 case ENOTDIR:
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
191 directory. */
192 case ENODEV:
193 case ETIMEDOUT:
194 /* Some strange filesystems like AFS return even
195 stranger error numbers. They cannot reasonably mean
196 anything else so ignore those, too. */
197 break;
199 default:
200 /* Some other error means we found an executable file, but
201 something went wrong executing it; return the error to our
202 caller. */
203 return -1;
206 while (*p++ != '\0');
208 /* We tried every element and none of them worked. */
209 if (got_eacces)
210 /* At least one failure was due to permissions, so report that
211 error. */
212 __set_errno (EACCES);
214 free (script_argv_malloc);
215 free (path_malloc);
218 /* Return the error from the last attempt (probably ENOENT). */
219 return -1;
221 weak_alias (__execvpe, execvpe)