Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - exec execv execlp execvp http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   exec execv execlp execvp

   Usage
   #include <process.h>
   #include <errno.h> /* for error checking only */
   int execl(char *path,char *arg0, arg1,...,argn, NULL);
   int execv(char *path,char *argv[]);
   int execlp(char *path,char *arg0, arg1,...,argn, NULL);
   int execvp(char *path,char *argv[]);

   Description
   exec  functions  load and run other programs, or  child  processes.  A
   successful  exec call replaces the parent process in memory  with  the
   child  process.  Sufficient  memory must be available  for  the  child
   process.  exec  overwrites the current program in memory  with  a  new
   program and begins execution.

   path specifies the file name of the child process to execute.
   execl  or  execv return a file not found error if  the  child  process
   program is not found.

   execlp  or  execvp  will  search the DOS PATH  for  the  file  when  a
   directory  is  not stated in path and the file is not in  the  current
   directory.

   Arguments  passed to a subprocess are made up of character strings  in
   the  exec  call. These form the argument list for the  child  process.
   Their combined length (except arg0) must not exceed 127 characters.
   execl, execlp

   (lower case L) The argument pointers are passed as separate  arguments
   and   is  commonly  used  when  the  number  of  arguments  is   known
   beforehand. Under DOS 3.0 and higher path is available as arg[0] to
   the  child  process.  In earlier versions arg[0]  is  ignored,  though
   included for compatibility.
   execv, execvp

   The  argument  pointers  are passed as an array  of  pointers,  making
   variable numbers of arguments convenient. The argv argument used in
   the  execv  and  execp is an array of  character  pointers.  The  last
   pointer in argv must be NULL to indicate the end of the array.

   Example
   #include <process.h>
   #include <errno.h>
   #include <stdio.h>
   main(int argc,char *argv[])
   {
   char *args[4];
   int result;
   args[0] = "arg0";
   args[1] = "arg1";
   args[2] = "arg2";
   args[3] = NULL;
        switch (argv[1][0])
        {
             case '1': execl("myprog.exe","arg0","arg1","arg2",NULL);
                       break;
             case '2': execlp("myprog.exe","arg0","arg1","arg2",NULL);
                       break;
             case '3': execv("myprog.exe",args);
                       break;
             case '4': execvp("myprog.exe",args);
                       break;
             default : printf("Enter a number between 1 and 4 on"
                              "the command line\n");
                       exit(1);
        }
        printf("Process myprog.exe not exec.\n");
   }

   Return Value
   The  exec... functions do not return if the call was successful. If  a
   return  occurs, then the return value is -1, indicating an error.  The
   external  integer errno will be set to a value indicating  the  error.
   The values are declared in errno.h and are defined as follows:

   E2BIG     The argument list is more than 127 bytes.
   ENOENT    The file name was not found.


See Also: abort exit spawn system

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson