home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 1.ddi / WINLBSRC.ZIP / SETENVRN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  2.6 KB  |  78 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - setenvrn.c
  3.  *
  4.  * function(s)
  5.  *     _setenviron (init proc)
  6.  *
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <dos.h>
  20. #include <mem.h>
  21.  
  22. #pragma warn -use
  23.  
  24. extern int _envLng;             /* environment length           */
  25. extern int _envseg;             /* environment segment          */
  26. extern int _envSize;    /* # of strings in environment * sizeof(char *) */
  27.  
  28. extern unsigned _WinAllocFlag;
  29.  
  30. #define EXTRA   4               /* # of extra envp slots to include*/
  31.  
  32. char **environ = NULL;
  33.  
  34. /*----------------------------------------------------------------------*
  35.  
  36. Name    _setenviron - initializes the global variable environ
  37.  
  38. Usage   Is executed when any reference to environ causes this module
  39.         to be linked in.
  40.  
  41. Desc.   Allocates and initializes the global variable environ.
  42.  
  43. Notes   Must be executed after _setenv init proc so that _envLng, _envseg,
  44.         and _envSize are already setup.
  45.  
  46. *-----------------------------------------------------------------------*/
  47. static void _setenviron(void)
  48. {
  49.         int     i;
  50.         char    *evBlock;
  51.     unsigned Old_WinAllocFlag = _WinAllocFlag;
  52.  
  53.         /*
  54.           Get block to hold strings, copy the strings to the local block.
  55.           Get a block to hold the envp array pointers.  We'll get a
  56.           block large enough to hold 4 more pointers than there currently
  57.           are.  This way minor additions can be made to the environment
  58.           without having to realloc the pointer array each time.
  59.           Fill in array of pointers with the addresses of the strings.
  60.           Make _envSize reflect how many slots there are now.
  61.         */
  62.  
  63.     _WinAllocFlag |= 0x2000;
  64.         if ((evBlock = malloc(_envLng)) == NULL)
  65.                 abort();
  66.         movedata(_envseg, 0, FP_SEG((char far *)evBlock),
  67.              FP_OFF((char far *)evBlock), _envLng);
  68.         if ((environ = (char **)calloc((_envSize/sizeof(char *))+EXTRA,
  69.         sizeof(char *))) == NULL)
  70.                 abort();
  71.         for (i=0; i<(_envSize/sizeof(char*)); i++, evBlock+=(strlen(evBlock)+1))
  72.                 environ[i] = evBlock;
  73.         _envSize += EXTRA*sizeof(char*);
  74.     _WinAllocFlag = Old_WinAllocFlag;
  75. }
  76.  
  77. #pragma startup _setenviron 9  /* Must be executed after _setenv (8) */
  78.