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
12 extern char** __environ
;
15 /* Execute PATH with all arguments after PATH until
16 a NULL pointer and environment from `environ'. */
18 execl (const char *path
, const char *arg
, ...)
20 size_t argv_max
= 1024;
21 const char **argv
= alloca (argv_max
* sizeof (const char *));
29 while (argv
[i
++] != NULL
)
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 *));
45 #ifndef _STACK_GROWS_DOWN
46 if ((char *) argv
+ i
== (char *) nptr
)
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 *);
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. */
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
;
84 while (argv
[i
++] != NULL
)
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 *));
100 #ifndef _STACK_GROWS_DOWN
101 if ((char *) argv
+ i
== (char *) nptr
)
102 /* Stack grows up. */
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 *);
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 *));
140 va_start (args
, arg
);
142 while (argv
[i
++] != NULL
)
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 *));
158 #ifndef _STACK_GROWS_DOWN
159 if ((char *) argv
+ i
== (char *) nptr
)
160 /* Stack grows up. */
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 *);
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. */
186 script_execute (const char *file
, char *const argv
[])
188 /* Count the arguments. */
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
;
200 new_argv
[argc
] = argv
[argc
- 1];
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'. */
219 /* We check the simple case first. */
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
);
235 char *path
, *p
, *name
;
239 path
= getenv ("PATH");
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
);
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. */
266 p
= strchrnul (path
, ':');
268 p
= strchr(path
, ':');
269 if (NULL
== p
) p
= path
+ strlen(path
) + 1;
273 /* Two adjacent colons, or a colon at the beginning or the end
274 of `PATH' means to search the current directory. */
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
);
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. */
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
301 /* Some other error means we found an executable file, but
302 something went wrong executing it; return the error to our
307 while (*p
++ != '\0');
309 /* We tried every element and none of them worked. */
311 /* At least one failure was due to permissions, so report that
316 /* Return the error from the last attempt (probably ENOENT). */