home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dmake40.zip / mac / environ.c < prev    next >
C/C++ Source or Header  |  1994-10-23  |  7KB  |  241 lines

  1. /* RCS      -- $Header: /u5/dvadura/src/public/dmake/src/mac/RCS/environ.c,v 1.1 1994/10/06 17:42:58 dvadura Exp $
  2. -- SYNOPSIS -- Set up and free for environ
  3. --
  4. -- DESCRIPTION
  5. --  This file contains routines that will fill in and dispose of the
  6. --  list of environmental variables in the environ global variable.
  7. --
  8. -- AUTHOR
  9. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  10. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  11. --
  12. --
  13. -- COPYRIGHT
  14. --      Copyright (c) 1992,1994 by Dennis Vadura.  All rights reserved.
  15. -- 
  16. --      This program is free software; you can redistribute it and/or
  17. --      modify it under the terms of the GNU General Public License
  18. --      (version 1), as published by the Free Software Foundation, and
  19. --      found in the file 'LICENSE' included with this distribution.
  20. -- 
  21. --      This program is distributed in the hope that it will be useful,
  22. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  23. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. --      GNU General Public License for more details.
  25. -- 
  26. --      You should have received a copy of the GNU General Public License
  27. --      along with this program;  if not, write to the Free Software
  28. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. --
  30. -- LOG
  31. --     $Log: environ.c,v $
  32.  * Revision 1.1  1994/10/06  17:42:58  dvadura
  33.  * dmake Release Version 4.0, Initial revision
  34.  *
  35. */
  36.  
  37. #include "extern.h"
  38.  
  39. /* The char used to replace the equal signs in environmental variable names. */
  40. const char kEqualReplace = '_';
  41.  
  42. /* Maximum size of a "name=value" environmental string, including the ending '\0'.
  43.    Larger environmental variables will be clipped before dmake sees them.
  44.    (Caution: When I tested the program, the Mac or dmake trashed memory
  45.     when environmental variables of >4K were read in.  I looked around a bit
  46.     and couldn't find out the exact cause, so I simply made this variable.
  47.     The memory trashing may be related to the value for MAXLINELENGTH.) */
  48. const int kMaxEnvLen = 1024;
  49.  
  50.  
  51. /* The list of environmental variables in the form "name=value".
  52.    (Once make_env() has been called.) */
  53. char **environ = NULL;
  54.  
  55. /* Characters replaced during make_env() */
  56. struct ReplaceChar {
  57.     char *fpPos;
  58.     char fC;
  59.     struct ReplaceChar *fpNext;
  60. }; /* struct ReplaceChar */
  61. struct ReplaceChar *gpReplaceList = NULL;
  62.  
  63.  
  64. void AddReplace (char *pReplacePos);
  65.  
  66.  
  67.  
  68. /*
  69.  * Set up the environmental variables in a format used by
  70.  * the environ global variable.
  71.  *
  72.  * environ has already been set to main's envp argument when
  73.  * this suboroutine is called.  We assume that envp is a copy
  74.  * MPW makes for this process' use alone, so we can modify it
  75.  * below.
  76.  */
  77. PUBLIC void
  78. make_env() 
  79. {
  80.     char **ppCurEnv;
  81.     char *pCurPos;
  82. #if 0
  83.     char **ppMacEnv;
  84.     char *pMacPos;
  85.  
  86.     if (!gMECalled) {
  87.         gMECalled = TRUE;
  88.  
  89. environ = MALLOC (1, char *);
  90. *environ = NULL;
  91. #endif
  92. #if 0
  93. {
  94.     int numenv;
  95.     int len;
  96.     int firstnil;
  97.  
  98.     numenv = 1;
  99.     ppMacEnv = environ;
  100.     while (*(ppMacEnv++) != NULL) {
  101.         ++numenv;
  102.     } /* while */
  103.  
  104.     ppMacEnv = environ;
  105.     if ((environ = MALLOC (numenv, char *)) == NULL) {
  106.         No_ram ();
  107.     } /* if */
  108.  
  109. numenv = 80;
  110.     for (ppCurEnv = environ; (numenv-- > 0) && (*ppMacEnv != NULL); ++ppCurEnv, ++ppMacEnv) {
  111.         pMacPos = *ppMacEnv;
  112.         len = strlen (pMacPos) + 1;
  113.         len += strlen (pMacPos + len) + 1;
  114. #define MAXLEN 4098
  115. if (len > MAXLEN) len = MAXLEN;
  116.         if ((*ppCurEnv = MALLOC (len, char)) == NULL) {
  117.             No_ram ();
  118.         } /* if */
  119.  
  120.         firstnil = TRUE;
  121.         for (pCurPos = *ppCurEnv; ((pCurPos - *ppCurEnv) < MAXLEN - 1); ++pCurPos, ++pMacPos) {
  122.             if (*pMacPos == '=') {
  123.                 *pCurPos = gEqualReplace;
  124.  
  125.             } else if (*pMacPos == '\0') {
  126.                 if (firstnil) {
  127.                     *pCurPos = '=';
  128.                     firstnil = FALSE;
  129.                 } else {
  130.                     *pCurPos = *pMacPos;
  131.                     break;
  132.                 } /* if ... else */
  133.  
  134.             } else {
  135.                 *pCurPos = *pMacPos;
  136.             } /* if ... elses */
  137.         } /* for */
  138. firstnil = FALSE;
  139.     } /* for */
  140.     *ppCurEnv = NULL;
  141. }
  142. #endif
  143. {
  144.         int firstnil;
  145.  
  146.         /* Get rid of any equal signs in any environmental name, and put
  147.            equal signs between the names and their values */
  148.         for (ppCurEnv = environ; *ppCurEnv != NULL; ++ppCurEnv) {
  149.  
  150.             firstnil = TRUE;
  151.             for (pCurPos = *ppCurEnv;
  152.                  ((pCurPos - *ppCurEnv < kMaxEnvLen - 1) &&
  153.                   ((*pCurPos != '\0') || !firstnil));
  154.                  ++pCurPos) {
  155.                 if (*pCurPos == '=') {
  156.                     AddReplace (pCurPos);
  157.                     *pCurPos = kEqualReplace;
  158.  
  159.                 } else if (*pCurPos == '\0') {
  160.                     AddReplace (pCurPos);
  161.                     *pCurPos = '=';
  162.                     firstnil = FALSE;
  163.                 } /* if ... else if */
  164.             } /* for */
  165.  
  166.             /* If the environtmental variable was too large ... */
  167.             if (*pCurPos != '\0') {
  168.                 AddReplace (pCurPos);
  169.                 *pCurPos = '\0';
  170.                 if (firstnil) {
  171.                     AddReplace (--pCurPos);
  172.                     *pCurPos = '=';
  173.                 } /* if */
  174.             } /* if */
  175.         } /* for */
  176. }
  177. #if 0
  178.     } /* if */
  179. #endif
  180. } /* PUBLIC void make_env () */
  181.  
  182.  
  183. /*
  184.  * The character at pReplacePos is about to be replaced.  Remember the
  185.  * old value so we can restore it when we're done.
  186.  */
  187. void AddReplace (char *pReplacePos) {
  188.     struct ReplaceChar *pReplaceChar;
  189.  
  190.     if ((pReplaceChar = MALLOC (1, struct ReplaceChar)) == NULL) {
  191.         No_ram ();
  192.     } /* if */
  193.     pReplaceChar->fpPos = pReplacePos;
  194.     pReplaceChar->fC = *pReplacePos;
  195.     pReplaceChar->fpNext = gpReplaceList;
  196.     gpReplaceList = pReplaceChar;
  197. } /* void AddReplace () */
  198.  
  199.  
  200. /*
  201.  * Restore the old environmental variables to the way they looked before
  202.  * the make_env() call, on the unlikely chance that something else will look
  203.  * at our copy of the environmental variables during the program execution.
  204.  * 
  205.  */
  206. PUBLIC void
  207. free_env()
  208. {
  209.     struct ReplaceChar *pReplaceChar;
  210.  
  211.     while (gpReplaceList != NULL) {
  212.         pReplaceChar = gpReplaceList;
  213.         gpReplaceList = pReplaceChar->fpNext;
  214.  
  215.         *(pReplaceChar->fpPos) = pReplaceChar->fC;
  216.  
  217.         FREE (pReplaceChar);
  218.     } /* while */
  219.  
  220. #if 0
  221.     char **ppCurEnv;
  222.     char *pCurPos;
  223.  
  224.     if (!gFECalled) {
  225.         gFECalled = TRUE;
  226.  
  227. //FREE (environ);
  228. environ = NULL;
  229. #endif
  230. #if 0
  231.         /* Restore the environment list to what it was before we
  232.            read it in. */
  233.         for (ppCurEnv = environ; *ppCurEnv != NULL; ++ppCurEnv) {
  234.             for (pCurPos = *ppCurEnv; *pCurPos != '='; ++pCurPos)
  235.                 ;
  236.             *pCurPos = '\0';
  237.         } /* for */
  238.     } /* if */
  239. #endif
  240. } /* PUBLIC void free_env () */
  241.