execve() Execute Program Using Argument Array, Environment
#include <process.h>
int execve(pathname,argv,envp);
char *pathname; Path name of file to be executed
char *argv[]; Array of pointers to arguments
char *envp[]; Array of pointers to environment
settings
execve() loads and executes a new child process. The child process is
placed in the memory currently occupied by the calling process. There
must be sufficient memory to load and execute the child process.
'pathname' specifies the file name of the child process. If
'pathname' has a file name extension, then only that file is searched
for. If 'pathname' ends with a period (.), then 'pathname' without an
extension is searched for. If 'pathname' has no extension and does
not end with a period, then execve() searches for 'pathname' and, if
it is not found, appends ".EXE" and searches again.
'argv' points to an array of pointers; the pointers in turn point to
the strings that are passed to the child process as command line
parameters. The 'argv' array is terminated by a NULL pointer at
'argv[n+1]'. 'argv[0]' must not be NULL, and is usually set to
'pathname'.
The combined length of all the strings forming the argument list
passed to the child process must not exceed 128 bytes. This includes
"n" (for 0-n arguments) and space characters (required to separate the
arguments), but does not include the null ('\0') terminating
character.
'envp' points to an array of pointers that in turn point to strings
that define an environment variable. These strings usually have the
form: "ENVVAR=value" where "ENVVAR" is the name of the environment
variable and "value' is the string value to set it to. The 'envp'
array is terminated by a NULL pointer. If 'envp' is NULL, then the
child process acquires the environment of the calling process.
Returns: If execve() is successful, it does not return to the
calling process. (See the spawn...() routines for a
similar function that can return to the calling process).
If an error occurs, execve() returns -1 to the calling
process. On error, 'errno' (defined in <stdlib.h>) is set
to one of the following values (defined in <errno.h>):
E2BIG Argument list or environment list too big.
(List > 128 bytes, or environment > 32k)
EACCES Locking or sharing violation on file.
(MS-DOS 3.0 and later)
EMFILE Too many files open.
ENOENT File or path not found.
ENOEXEC File not executable.
ENOMEM Not enough memory.
Notes: Any file open when an exec call is made remains open in
the child process. This includes 'stdin','stdout',
'stderr', 'stdaux', and 'stdprn'.
execve() does not preserve the translation modes of open
files. Use setmode() in the child process to set the
desired translation modes.
'environ' (defined in <stdlib.h>) points to a list of
environment settings for the current process.
See the spawn...() routines for similar though more
flexible functions that can return to the calling program.
Caution: The file pointers to open buffered files are not always
preserved correctly. The information in the buffer may be
lost.
Signal settings are not preserved. They are reset to the
default in the child process.
-------------------------------- Example ---------------------------------
The following statements transfer execution to the child process
"child.exe" and pass it the three arguments "child", "arg1", and
"arg2". The PATH environment variable is set to "C:\\TEST" for the
child process:
#include <process.h> /* for 'execve' */
#include <stdio.h> /* for 'printf' and 'NULL' */
#include <stdlib.h> /* for 'errno' */
#include <errno.h> /* for 'ENOENT' and 'ENOMEM' */
char *args[] = {"child", "arg1", "arg2", NULL};
char *env[] = {"PATH=C:\\TEST", NULL};
main()
{
execve("child.exe", args, env);
/* only get here on an exec error */
if (errno == ENOENT)
printf("child.exe not found in current directory\n");
else if (errno == ENOMEM)
printf("not enough memory to execute child.exe\n");
else
printf("error #%d trying to exec child.exe\n", errno);
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster