Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>spawnle() execute program using argument list and environment</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 spawnle()               Execute Program Using Argument List and Environment

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

 int        spawnle(modeflag,pathname,arg0,arg1,...,argn,NULL,envp);
 int        modeflag;                    Execution mode for parent process
 char       *pathname;                   Path name of file to be executed
 char       *arg0,*arg1,...,*argn;       List of pointers to argument
 char       *envp[];                     Array of pointers to environment
                                         settings

    spawnle() loads and executes a new child process.  'modeflag'
    determines what action the parent process will take after the new
    process is loaded; it should be set to one of the following values
    (defined in <process.h>):

         P_WAIT     Wait for child process to finish.

       P_NOWAIT     Execute parent process concurrently with child
                    process (valid in OS/2 protected mode only).

      P_NOWAITO     Execute parent process concurrently with child
                    process and ignore wait() and cwait() calls against
                    child process (valid in OS/2 protected mode only).

      P_OVERLAY     Overlay child process in memory occupied by parent
                    process.  This is equivalent to using the execle()
                    function.

    '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 spawnle() searches for 'pathname' and, if
    it is not found, it first appends ".COM" and searches again, followed
    by ".EXE" if still not found.

     'arg0', 'arg1',...,'argn' are passed to the child process as
    command-line parameters.  A NULL pointer must follow 'argn' to
    terminate the list of arguments. 'arg0' must not be NULL, and is
    usually set to 'pathname'.

    The maximum length of the combined arguments passed to the child
    process must not exceed 128 bytes.  This includes "n" (for 0 to n
    arguments) 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 environment variables.  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:    When the mode is P_NOWAIT or P_NOWAITO, the spawn
                functions return the process ID. To obtain the exit code
                for a process spawned with P_NOWAIT, call wait() or
                cwait() and specify the process ID. No exit code can be
                obtained for P_NOWAITO.

                When the mode is P_WAIT, the spawn functions return the
                exit status of the child process.  The exit status is
                zero if the process terminated normally and the child
                process did not call the exit routine with a nonzero
                argument. The child process can set the exit status to a
                nonzero value by calling exit() with the desired return
                value.

                If not set by the child process, a positive exit status
                indicates an error by an abort() or an interrupt.

                On error, -1 is returned and 'errno' is set to one of the
                following:

                    E2BIG       Argument list or environment list too big.
                                  (List > 128 bytes, or environment > 32k)
                    EINVAL      Invalid 'modeflag' argument
                    ENOENT      File or path not found.
                    ENOEXEC     File not executable.
                    ENOMEM      Not enough memory.

                A child process spawned with 'modeflag' set to P_OVERLAY
                does not return to the calling process.

      Notes:    Any file open when a spawn call is made remains open in
                the child process.  This includes 'stdin','stdout',
                'stderr', 'stdaux', and 'stdprn'.

                The translation modes of open files are not preserved in
                a call to spawnle().  setmode() can be used in the child
                process to set the desired translation modes.

                Signal settings are reset to the default in the child
                process; they are not preserved.

 Portability:   OS/2 or MS-DOS 3.0 and higher only.

   -------------------------------- 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.  The exit status of the child is printed upon return.

           #include <process.h>    /* for 'spawnle' and 'P_WAIT' */
           #include <stdio.h>      /* for 'printf' and 'NULL' */
           #include <stdlib.h>     /* for 'errno' */
           #include <errno.h>      /* for 'ENOENT' and 'ENOMEM' */

           char *env[] = {"PATH=C:\\TEST", NULL};

           main()
           {
               int result;

               result = spawnle(P_WAIT,"child.exe","child",
                                "arg1","arg2",NULL, env);
               if (result != -1)
                   printf("exit status of child.exe = %d\n", result);
               else {
                   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 spawn child.exe\n",
                               errno);
               }
           }


See Also: spawn...() exec...()

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