Dash:
[t2.git] / misc / tools-source / fl_wrapper_execl.c
blob12b6c268ba1f93065c762e4ecd5a4c74cdc1319e
1 /*
2 * Copyright (C) 1991, 92, 94, 97, 98, 99 Free Software Foundation, Inc.
3 * This file is part of the GNU C Library.
5 * --- NO-T2-COPYRIGHT-NOTE ---
7 * glibc-2.2.5/posix/execl.c
8 */
10 #ifndef __GLIBC__
11 /* e.g. musl */
12 extern char** __environ;
13 #endif
15 /* Execute PATH with all arguments after PATH until
16 a NULL pointer and environment from `environ'. */
17 int
18 execl (const char *path, const char *arg, ...)
20 size_t argv_max = 1024;
21 const char **argv = alloca (argv_max * sizeof (const char *));
22 unsigned int i;
23 va_list args;
25 argv[0] = arg;
27 va_start (args, arg);
28 i = 0;
29 while (argv[i++] != NULL)
31 if (i == argv_max)
33 const char **nptr = alloca ((argv_max *= 2) * sizeof (const char *));
35 #ifndef _STACK_GROWS_UP
36 if ((char *) nptr + argv_max == (char *) argv)
38 /* Stack grows down. */
39 argv = (const char **) memcpy (nptr, argv,
40 i * sizeof (const char *));
41 argv_max += i;
43 else
44 #endif
45 #ifndef _STACK_GROWS_DOWN
46 if ((char *) argv + i == (char *) nptr)
47 /* Stack grows up. */
48 argv_max += i;
49 else
50 #endif
51 /* We have a hole in the stack. */
52 argv = (const char **) memcpy (nptr, argv,
53 i * sizeof (const char *));
56 argv[i] = va_arg (args, const char *);
58 va_end (args);
60 return execve (path, (char *const *) argv, __environ);
64 * Copyright (C) 1991, 92, 94, 97, 98, 99 Free Software Foundation, Inc.
65 * This file is part of the GNU C Library.
67 * glibc-2.2.5/posix/execle.c
70 /* Execute PATH with all arguments after PATH until a NULL pointer,
71 and the argument after that for environment. */
72 int
73 execle (const char *path, const char *arg, ...)
75 size_t argv_max = 1024;
76 const char **argv = alloca (argv_max * sizeof (const char *));
77 const char *const *envp;
78 unsigned int i;
79 va_list args;
80 argv[0] = arg;
82 va_start (args, arg);
83 i = 0;
84 while (argv[i++] != NULL)
86 if (i == argv_max)
88 const char **nptr = alloca ((argv_max *= 2) * sizeof (const char *));
90 #ifndef _STACK_GROWS_UP
91 if ((char *) nptr + argv_max == (char *) argv)
93 /* Stack grows down. */
94 argv = (const char **) memcpy (nptr, argv,
95 i * sizeof (const char *));
96 argv_max += i;
98 else
99 #endif
100 #ifndef _STACK_GROWS_DOWN
101 if ((char *) argv + i == (char *) nptr)
102 /* Stack grows up. */
103 argv_max += i;
104 else
105 #endif
106 /* We have a hole in the stack. */
107 argv = (const char **) memcpy (nptr, argv,
108 i * sizeof (const char *));
111 argv[i] = va_arg (args, const char *);
114 envp = va_arg (args, const char *const *);
115 va_end (args);
117 return execve (path, (char *const *) argv, (char *const *) envp);
121 * Copyright (C) 1991, 92, 94, 97, 98, 99 Free Software Foundation, Inc.
122 * This file is part of the GNU C Library.
124 * glibc-2.2.5/posix/execlp.c
127 /* Execute FILE, searching in the `PATH' environment variable if
128 it contains no slashes, with all arguments after FILE until a
129 NULL pointer and environment from `environ'. */
131 execlp (const char *file, const char *arg, ...)
133 size_t argv_max = 1024;
134 const char **argv = alloca (argv_max * sizeof (const char *));
135 unsigned int i;
136 va_list args;
138 argv[0] = arg;
140 va_start (args, arg);
141 i = 0;
142 while (argv[i++] != NULL)
144 if (i == argv_max)
146 const char **nptr = alloca ((argv_max *= 2) * sizeof (const char *));
148 #ifndef _STACK_GROWS_UP
149 if ((char *) nptr + argv_max == (char *) argv)
151 /* Stack grows down. */
152 argv = (const char **) memcpy (nptr, argv,
153 i * sizeof (const char *));
154 argv_max += i;
156 else
157 #endif
158 #ifndef _STACK_GROWS_DOWN
159 if ((char *) argv + i == (char *) nptr)
160 /* Stack grows up. */
161 argv_max += i;
162 else
163 #endif
164 /* We have a hole in the stack. */
165 argv = (const char **) memcpy (nptr, argv,
166 i * sizeof (const char *));
169 argv[i] = va_arg (args, const char *);
171 va_end (args);
173 return execvp (file, (char *const *) argv);
177 * Copyright (C) 1991, 92, 94, 97, 98, 99 Free Software Foundation, Inc.
178 * This file is part of the GNU C Library.
180 * glibc-2.2.5/posix/execvp.c
183 /* The file is accessible but it is not an executable file. Invoke
184 the shell to interpret it as a script. */
185 static void
186 script_execute (const char *file, char *const argv[])
188 /* Count the arguments. */
189 int argc = 0;
190 while (argv[argc++])
193 /* Construct an argument list for the shell. */
195 char *new_argv[argc + 1];
196 new_argv[0] = (char *) "/bin/sh";
197 new_argv[1] = (char *) file;
198 while (argc > 1)
200 new_argv[argc] = argv[argc - 1];
201 --argc;
204 /* Execute the shell. */
205 execve (new_argv[0], new_argv, __environ);
210 /* Execute FILE, searching in the `PATH' environment variable if it contains
211 no slashes, with arguments ARGV and environment from `environ'. */
213 execvp (file, argv)
214 const char *file;
215 char *const argv[];
217 if (*file == '\0')
219 /* We check the simple case first. */
220 errno = ENOENT;
221 return -1;
224 if (strchr (file, '/') != NULL)
226 /* Don't search when it contains a slash. */
227 execve (file, argv, __environ);
229 if (errno == ENOEXEC)
230 script_execute (file, argv);
232 else
234 int got_eacces = 0;
235 char *path, *p, *name;
236 size_t len;
237 size_t pathlen;
239 path = getenv ("PATH");
240 if (path == NULL)
242 /* There is no `PATH' in the environment.
243 The default search path is the current directory
244 followed by the path `confstr' returns for `_CS_PATH'. */
245 len = confstr (_CS_PATH, (char *) NULL, 0);
246 path = (char *) alloca (1 + len);
247 path[0] = ':';
248 (void) confstr (_CS_PATH, path + 1, len);
251 len = strlen (file) + 1;
252 pathlen = strlen (path);
253 name = alloca (pathlen + len + 1);
254 /* Copy the file name at the top. */
255 name = (char *) memcpy (name + pathlen + 1, file, len);
256 /* And add the slash. */
257 *--name = '/';
259 p = path;
262 char *startp;
264 path = p;
265 #ifndef __dietlibc__
266 p = strchrnul (path, ':');
267 #else
268 p = strchr(path, ':');
269 if (NULL == p) p = path + strlen(path) + 1;
270 #endif
272 if (p == path)
273 /* Two adjacent colons, or a colon at the beginning or the end
274 of `PATH' means to search the current directory. */
275 startp = name + 1;
276 else
277 startp = (char *) memcpy (name - (p - path), path, p - path);
279 /* Try to execute this name. If it works, execv will not return. */
280 execve (startp, argv, __environ);
282 if (errno == ENOEXEC)
283 script_execute (startp, argv);
285 switch (errno)
287 case EACCES:
288 /* Record the we got a `Permission denied' error. If we end
289 up finding no executable we can use, we want to diagnose
290 that we did find one but were denied access. */
291 got_eacces = 1;
292 case ENOENT:
293 case ESTALE:
294 case ENOTDIR:
295 /* Those errors indicate the file is missing or not executable
296 by us, in which case we want to just try the next path
297 directory. */
298 break;
300 default:
301 /* Some other error means we found an executable file, but
302 something went wrong executing it; return the error to our
303 caller. */
304 return -1;
307 while (*p++ != '\0');
309 /* We tried every element and none of them worked. */
310 if (got_eacces)
311 /* At least one failure was due to permissions, so report that
312 error. */
313 errno = EACCES;
316 /* Return the error from the last attempt (probably ENOENT). */
317 return -1;