Borland C++ reference

execl, execle, execlp, execlpe, execv, execve, execvp, execvpe

partly corrected

Loads and runs other programs.

Defined in header <process.h>

Syntax

#include <process.h>
int execl(char  *path, char *argO *argl, ..., *argn, NULL);
int execle(char  *path, char *argO, *argl, ..., *argn, NULL,  char  **env);

int execlp(char  *path, char  *argO*argl,  ..., *argn, NULL);
int execlpe(char  *path, char  *argO, *argl, ..., *argn, NULL,  char **env);

int execv(char  *path, char  *argvl]);
int execve(char   *path, char *argv[], char **env);

int execvp(char   *path, char *argvl]);
int execvpe(char   *path, char *argv[], char **env);

Portability

DOSUNIXWindowsANSI CC++ only
■

Remarks

The functions in the exec... family load and run (execute) other programs, known as child processes. When an exec... call succeeds, the child process overlays the parent process. There must be sufficient memory available for loading and executing the child process.

path is the file name of the called child process. The exec... functions search for path using the standard DOS search algorithm:

□ If no explicit extension is given, the functions search for the file as given. If the file is not found, they add .COM and search again. If that search is not successful, they add .EXE and search one last time. □ If an explicit extension or a period is given, the functions search for the file exactly as given.

The suffixes /, v, p, and e added to the exec... "family name" specify that the named function operate with certain capabilities.

p The function searches for the file in those directories specified by the DOS PATH environment variable (without the p suffix, the function searches only the current working directory). If the path parameter does not contain an explicit directory, the function searches first the current directory, then the directories set with the DOS PATH environment variable.

/ The argument pointers {argO, argl,..., argn) are passed as separate arguments. Typically, the / suffix is used when you know in advance the number of arguments to be passed.

V The argument pointers (argv[0] ..., arg[n]) are passed as an array of pointers. Typically, the v suffix is used when a variable number of arguments is to be passed.

e The argument env can be passed to the child process, letting you alter the environment for the child process. Without the e suffix, child processes inherit the environment of the parent process.

Each function in the exec... family must have one of the two argument-specifying suffixes (either / or i;). The path search and environment inheritance suffixes (p and e) are optional.

For example,

B execl is an exec... function that takes separate arguments, searches only the root or current directory for the child, and passes on the parent's environment to the child. ■ execvpe is an exec... function that takes an array of argument pointers, incorporates PATH in its search for the child process, and accepts the env argument for altering the child's environment.

The exec... functions must pass at least one argument to the child process (argO or argv[0]); this argument is, by convention, a copy of path. (Using a different value for this 0th argument won't produce an error.)

Under DOS 3.x, path is available for the child process; under earlier versions of DOS, the child process cannot use the passed value of the 0th argument (argO or argv[0]).

When the / suffix is used, argO usually points to path, and argl,..., argn point to character strings that form the new list of arguments. A mandatory null following argn marks the end of the list.

When the e suffix is used, you pass a list of new environment settings through the argument env. This environment argument is an array of character pointers. Each element points to a null-terminated character string of the form

envvar = value where envvar is the name of an environment variable, and value is the string value to which envvar is set. The last element in env is null. When env is null, the child inherits the parents' environment settings.

The combined length of argO + argl + ... + argn (or of argv[0] + argv[l] + .. + argn[n]), including space characters that separate the arguments, must be less than 128 bytes. Null terminators are not counted.

When an exec... function call is made, any open files remain open in the child process.

Return value

If successful, the exec... functions do not return. On error, the exec... functions return -1, and the global variable errno is set to one of the following:

E2BIGArg list too long
EACCESPermission denied
EMFILEToo many open files
ENOENTPath or file name not found
ENOEXECExec format error
ENOMEMNot enough core

See also

Example

#include <stdio.h>
#include <stdlib.h>  ■

int main{int argc, char  *argv[])
{
   int i;
   printf ("Child running ...\n");
   printf ("%s\n",getenv("PATH"));
   for{i =0;  i < argc;  i++)
   printf ("argv[%d]: %s\n", i, argv[i]
   return 0;

#include <stdio.h>
#include <errno.h>
#include <dos.h>
int main(int argc, char  *argv[])
{
   int loop;
   printf ("%s running...\n\n", argv[0]);
   if (argc == 1)  {    /* check for only one command-line parameter  */
      printf ("%s calling itself again...\n", argv[0]);
      execl (argv[0], argv[0], "ONE", "TWO", "THREE", NULL);
      perror("EXEC:") ;
      exit(l);
   }
   printf("%s called with  arguments: \n", argv[0]);
   for (loop = 1; loop <=  argc; loop++)
   puts(argv[loop] );   /* display all command-line parameters  */
   return 0;

#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <dos.h>
int main(int argc, char  *argv[], char *env[])
{
   int loop;
   char *new_env[] =  { "TESTING", NULL };
   printf("%s running. ..\n\n", argv[0]);
   if (argc == 1)  {    /* check for only one command-line parameter  */
      printf("%s calling  itself again...\n", argv[0]);
      execle (argv[0], argv[0], "ONE", "TWO", "THREE", NULL, new_env) ;
      perror("EXEC:") ;
      exitd);
   }
   printf("%s called with  arguments:\n", argv[0]);
   for  (loop = 1; loop <= argc; loop++)
   puts (argv[loop]);   /* display all comimand-line parameters */

   /* display the  first environment parameter */
   printf ("value of env[0]: %s\n",env[0]);
   return
}

#include <process.h>
#include <stdio.h>
#include <errno.h>
int main(int argc,  char *argv[], char **envp)
{
   int i;
   printf ("Command line arguments:\n");
   for  (i=0; i < argc; ++i)
      printf ("[%2d] %s\n", i, argv[i]);
   printf ("About to exec child with argl arg2 ...\n");
   execipe ("CHILD.EXE", "CHILD.EXE", "argl", "arg2", NULL, envp)
   perror("exec error");
   exit(l);
   return

iinclude <process.h>
#include <stdio.h>
#include <errno.h>

int main(int argc,  char *argv[], char **envp)
{
   int i;
   printf ("Command line arguments:\n");
   for  (i=0; i < argc; ++i)
      printf ("[%2d] %s\n", i, argv[i]);
   printf ("About to exec child with argl arg2 ...\n");
   execipe ("CHILD.EXE", "CHILD.EXE", "argl", "arg2", NULL, envp)
   perror("exec  error");
   return 1;
)

#include <process.h>
#include  <stdio.hS
#include <errno.h>
void main (int argc, char *argv[])
(
   int i;
   printf ("Command line arguments:\n");
   for  (i=0; i<argc; i++)
      printf ("[%2d] : %s\n", i, argv[i]);
   printf ("About to exec child with argl arg2 ...\n"
   execv ("CHILD.EXE", argv);
   perror("exec error");
   exit(l);

#include <process.h>
#include <stdio.h>
#include <errno.h>
void main (int argc, char *argv[], char **envp)
{
   int i;
   printf ("Command line arguments:\n");
   for  (i=0; i<argc; ++i)
      printf ("[%2d] : %s\n", i, argv[i]);
   printf ("About to exec child with argl arg2 ...\n"
   execve ("CHILD.EXE", argv, envp);
   perror("exec error");
   exit(l);

#include <process.h>
#include <stdio.h>
#include <errno.h>
void main{int argc,  char *argv[])
{
   int i;
   printf ("Command line arguments:\n");
   for  (i=0; i<argc; ++i)
      printf ("[%2d] : %s\n", i, argv[i]);
   printf ("About to exec child with argl arg2 ...\n"
   execvp ("CHILD.EXE", argv);
   perror("exec error");
   exit(l) ;
}

#include <process.h>
#include <stdio.h>                        *
#include <errno.h>
void main(int argc,  char *argv[], char **envp)
{
   int i;
   printf ("Command line arguments:\n");
   for  (i=0; i<argc; ++i)
      printf ("[%2d] : %s\n", i, argv[i]);
   printf ("About to exec child with argl arg2 ...\n"
   execvpe ("CHILD.EXE", argv, envp);
   perror("exec error");
   exit(l) ;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.