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

  1. /*------------------------------------------------------------------------
  2.  * filename - dosenv.c
  3.  *
  4.  * function(s)
  5.  *        __DOSenv - Prepare Spawn/Exec environment
  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 <stdlib.h>
  19. #include <string.h>
  20.  
  21. /*-----------------------------------------------------------------------*
  22.  
  23. Name            DOSenv -- Prepare Spawn/Exec environment
  24.  
  25. Usage           char * pascal __DOSenv(char **envV,
  26.                                        char *pathP,
  27.                                        void **envSave);
  28.  
  29. Prototype in    _process.h
  30.  
  31. Description     This function allocates  a buffer and fill it  with all the
  32.                 environment  strings one  after  the  others. If  the pathP
  33.                 variable is nonzero, then it is  appended to the end of the
  34.                 end of  the environment assumed it  is for a spawn  or exec
  35.                 purpose.
  36.  
  37.  
  38. Return value    DOSenv  returns a  pointer  to  the environment  buffer if
  39.                 successful, and NULL on error.
  40.  
  41. -------------------------------------------------------------------------*/
  42. char    * pascal near __DOSenv(char **envV, char *pathP, void **envSave)
  43. {
  44.         register char   **envW;
  45.         register unsigned       envS;
  46.         char    *bufP;
  47.  
  48. /*      Compute the environment size including  the NULL string at the
  49.         end of the environment. (Environment size < 32 Kbytes)
  50. */
  51.         envS = 1;
  52.         if ((envW = envV) != NULL)
  53.                 for (envS = 0; *envW && **envW; envS += strlen(*envW++) + 1)
  54.                         ;
  55.         envS++;
  56.         if (pathP)
  57.                 envS += 2 + strlen(pathP) + 1;
  58.         if (envS >= 0x2000)
  59.                 return (NULL);
  60.  
  61. /*      Allocate a buffer
  62. */
  63.         if ((bufP = malloc(envS + 15)) != NULL) {
  64.  
  65. /*              The environment MUST be paragraph aligned
  66. */
  67.                 *envSave = bufP;
  68.                 bufP += 15;
  69.                 (*((unsigned *)&(bufP))) &= 0xFFF0;
  70.  
  71. /*              Concatenate all environment strings
  72. */
  73.                 if ((envW = envV) != NULL && *envW != NULL)
  74.                         while (*envW && **envW)
  75.                 {
  76.                                 bufP = _stpcpy(bufP, *envW++);
  77.                                 *bufP++ = '\0';
  78.                 }
  79.         else
  80.             *bufP++ = '\0';
  81.                 *bufP++ = '\0';
  82.  
  83. /*              Append program name to the environment
  84. */
  85.                 if (pathP) {
  86.                         *((short *)bufP)++ = 1;
  87.                         bufP = _stpcpy(bufP, pathP);
  88.                         *bufP++ = '\0';
  89.                 }
  90.                 return bufP - envS;
  91.         }
  92.         else
  93.                 return NULL;
  94. }
  95.