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

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