1 /* RCS $Id: environ.c,v 1.1.1.1 2000-09-22 15:33:27 hr Exp $
4 -- Set up and free for environ
7 -- This file contains routines that will fill in and dispose of the
8 -- list of environmental variables in the environ global variable.
11 -- Dennis Vadura, dvadura@dmake.wticorp.com
15 -- http://dmake.wticorp.com/
18 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
20 -- This program is NOT free software; you can redistribute it and/or
21 -- modify it under the terms of the Software License Agreement Provided
22 -- in the file <distribution-root>/readme/license.txt.
25 -- Use cvs log to obtain detailed change logs.
30 /* The char used to replace the equal signs in environmental variable names. */
31 const char kEqualReplace
= '_';
33 /* Maximum size of a "name=value" environmental string, including the ending '\0'.
34 Larger environmental variables will be clipped before dmake sees them.
35 (Caution: When I tested the program, the Mac or dmake trashed memory
36 when environmental variables of >4K were read in. I looked around a bit
37 and couldn't find out the exact cause, so I simply made this variable.
38 The memory trashing may be related to the value for MAXLINELENGTH.) */
39 const int kMaxEnvLen
= 1024;
42 /* The list of environmental variables in the form "name=value".
43 (Once make_env() has been called.) */
44 char **environ
= NULL
;
46 /* Characters replaced during make_env() */
50 struct ReplaceChar
*fpNext
;
51 }; /* struct ReplaceChar */
52 struct ReplaceChar
*gpReplaceList
= NULL
;
55 void AddReplace (char *pReplacePos
);
60 * Set up the environmental variables in a format used by
61 * the environ global variable.
63 * environ has already been set to main's envp argument when
64 * this suboroutine is called. We assume that envp is a copy
65 * MPW makes for this process' use alone, so we can modify it
80 environ
= MALLOC (1, char *);
91 while (*(ppMacEnv
++) != NULL
) {
96 if ((environ
= MALLOC (numenv
, char *)) == NULL
) {
101 for (ppCurEnv
= environ
; (numenv
-- > 0) && (*ppMacEnv
!= NULL
); ++ppCurEnv
, ++ppMacEnv
) {
103 len
= strlen (pMacPos
) + 1;
104 len
+= strlen (pMacPos
+ len
) + 1;
106 if (len
> MAXLEN
) len
= MAXLEN
;
107 if ((*ppCurEnv
= MALLOC (len
, char)) == NULL
) {
112 for (pCurPos
= *ppCurEnv
; ((pCurPos
- *ppCurEnv
) < MAXLEN
- 1); ++pCurPos
, ++pMacPos
) {
113 if (*pMacPos
== '=') {
114 *pCurPos
= gEqualReplace
;
116 } else if (*pMacPos
== '\0') {
137 /* Get rid of any equal signs in any environmental name, and put
138 equal signs between the names and their values */
139 for (ppCurEnv
= environ
; *ppCurEnv
!= NULL
; ++ppCurEnv
) {
142 for (pCurPos
= *ppCurEnv
;
143 ((pCurPos
- *ppCurEnv
< kMaxEnvLen
- 1) &&
144 ((*pCurPos
!= '\0') || firstnil
));
146 if (*pCurPos
== '=') {
147 AddReplace (pCurPos
);
148 *pCurPos
= kEqualReplace
;
150 } else if (*pCurPos
== '\0') {
151 AddReplace (pCurPos
);
154 } /* if ... else if */
157 /* If the environtmental variable was too large ... */
158 if (*pCurPos
!= '\0') {
159 AddReplace (pCurPos
);
162 AddReplace (--pCurPos
);
171 } /* PUBLIC void make_env () */
175 * The character at pReplacePos is about to be replaced. Remember the
176 * old value so we can restore it when we're done.
178 void AddReplace (char *pReplacePos
) {
179 struct ReplaceChar
*pReplaceChar
;
181 if ((pReplaceChar
= MALLOC (1, struct ReplaceChar
)) == NULL
) {
184 pReplaceChar
->fpPos
= pReplacePos
;
185 pReplaceChar
->fC
= *pReplacePos
;
186 pReplaceChar
->fpNext
= gpReplaceList
;
187 gpReplaceList
= pReplaceChar
;
188 } /* void AddReplace () */
192 * Restore the old environmental variables to the way they looked before
193 * the make_env() call, on the unlikely chance that something else will look
194 * at our copy of the environmental variables during the program execution.
200 struct ReplaceChar
*pReplaceChar
;
202 while (gpReplaceList
!= NULL
) {
203 pReplaceChar
= gpReplaceList
;
204 gpReplaceList
= pReplaceChar
->fpNext
;
206 *(pReplaceChar
->fpPos
) = pReplaceChar
->fC
;
222 /* Restore the environment list to what it was before we
224 for (ppCurEnv
= environ
; *ppCurEnv
!= NULL
; ++ppCurEnv
) {
225 for (pCurPos
= *ppCurEnv
; *pCurPos
!= '='; ++pCurPos
)
231 } /* PUBLIC void free_env () */