home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / LOADPROG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.3 KB  |  123 lines

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