home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / LOADPROG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  3.8 KB  |  124 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - loadprog.c
  3.  *
  4.  * function(s)
  5.  *      _LoadProg -- Load and Execute a program
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #include <_process.h>
  19. #include <process.h>
  20. #include <stdlib.h>
  21. #include <_dir.h>
  22. #include <errno.h>
  23. #include <string.h>
  24.  
  25.  
  26. /*-----------------------------------------------------------------------*
  27.  
  28. Name        _LoadProg -- Load and Execute a program
  29.  
  30. Usage        #include <_process.h>
  31.         int _LoadProg(int (* Func)(char *, char *, char *),
  32.                   char *pathP,
  33.                   char *argP[],
  34.                   char *envV[],
  35.                   int UsePath)
  36.  
  37. Prototype in    _process.h
  38.  
  39. Description    _Loadprog loads  and runs another  program, known as  child
  40.         process.
  41.  
  42.         The child process overlays the    current program if _exec is
  43.         passed    as  Func  argument,  otherwise    the  calling parent
  44.         process regains  control after program execution  if _spawn
  45.         is passed as Func argument.
  46.  
  47.         *pathP    is  the  file  name  of  the  called child process.
  48.         _LoadProg searches  for pathname using the  standard MS-DOS
  49.         search algorithm:
  50.  
  51.             . no  extension or no period - search  for exact file
  52.               name;  if  not  successful,  add .EXE and search
  53.               again
  54.  
  55.             . extension given - search only for exact file name
  56.  
  57.             . period  given - search only for file name with no
  58.               extension
  59.  
  60.         UsePath, if  true, specifies that the  function will search
  61.         for  the child    in those  directories specified  by the DOS
  62.         PATH  environment   variable.  If  false,   _LoadProg  only
  63.         searches the root and current working directory.
  64.  
  65.         At   execution    time,    the  child   process  receives    two
  66.         arguments:
  67.             - a command string built from argP
  68.             - an environment built from envV.
  69.  
  70. Return value    A successful _exec does not return, and a successful _spawn
  71.         returns  the exit  code of   the child    process. On  error,
  72.         _LoadProg  returns  -1,  and  errno  is  set  to one of the
  73.         following:
  74.             E2BIG    Argument list too long
  75.             EACCES    Permission denied
  76.             EMFILE    Too many open files
  77.             ENOENT    Path or file name not found
  78.             ENOEXEC Exec format error
  79.             ENOMEM    Not enough core
  80.  
  81. *------------------------------------------------------------------------*/
  82. int near _LoadProg(int cdecl near (*Func)(char *, char *, char *), char *pathP,
  83.             char *argP[], char *envV[], int UsePath)
  84. {
  85.         char    *cmdP, *envP;
  86.     void    *envSave;
  87.     int    rc;
  88.  
  89.     /*    First, Check if the program exists    */
  90.     if ((pathP = __searchpath(pathP, UsePath | _PROGRAM)) == NULL)
  91.     {
  92.         errno = ENOENT;
  93.         return -1;
  94.     }
  95.  
  96.     /* Then, concatenate arguments to make the DOS command line */
  97.     if ((cmdP = __DOScmd(argP)) == NULL)
  98.     {
  99.         errno = ENOMEM;
  100.         return -1;
  101.     }
  102.  
  103.     /*    Then, concatenate environment strings        */
  104.     if (envV == NULL)
  105.         envV = environ;
  106.     if ((envP = __DOSenv(envV, pathP, &envSave)) == NULL)
  107.     {
  108.         errno = ENOMEM;
  109.         free(cmdP);
  110.         return -1;
  111.     }
  112.  
  113.     /*    Flush all byte streams        */
  114.     (*_exitbuf)();
  115.  
  116.     /*    Now, call the low level _exec/_spawn function    */
  117.     rc = (*Func)(pathP, cmdP, envP);
  118.  
  119.     /*    Release all working buffers    */
  120.     free(envSave);
  121.     free(cmdP);
  122.     return (rc);
  123. }
  124.