merge the formfield patch from ooo-build
[ooovba.git] / dmake / mac / environ.c
blobe338564f0ad8d8f8212ea742a92ec235731edd7f
1 /* RCS $Id: environ.c,v 1.1.1.1 2000-09-22 15:33:27 hr Exp $
2 --
3 -- SYNOPSIS
4 -- Set up and free for environ
5 --
6 -- DESCRIPTION
7 -- This file contains routines that will fill in and dispose of the
8 -- list of environmental variables in the environ global variable.
9 --
10 -- AUTHOR
11 -- Dennis Vadura, dvadura@dmake.wticorp.com
14 -- WWW
15 -- http://dmake.wticorp.com/
17 -- COPYRIGHT
18 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
19 --
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.
24 -- LOG
25 -- Use cvs log to obtain detailed change logs.
28 #include "extern.h"
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() */
47 struct ReplaceChar {
48 char *fpPos;
49 char fC;
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
66 * below.
68 PUBLIC void
69 make_env()
71 char **ppCurEnv;
72 char *pCurPos;
73 #if 0
74 char **ppMacEnv;
75 char *pMacPos;
77 if (!gMECalled) {
78 gMECalled = TRUE;
80 environ = MALLOC (1, char *);
81 *environ = NULL;
82 #endif
83 #if 0
85 int numenv;
86 int len;
87 int firstnil;
89 numenv = 1;
90 ppMacEnv = environ;
91 while (*(ppMacEnv++) != NULL) {
92 ++numenv;
93 } /* while */
95 ppMacEnv = environ;
96 if ((environ = MALLOC (numenv, char *)) == NULL) {
97 No_ram ();
98 } /* if */
100 numenv = 80;
101 for (ppCurEnv = environ; (numenv-- > 0) && (*ppMacEnv != NULL); ++ppCurEnv, ++ppMacEnv) {
102 pMacPos = *ppMacEnv;
103 len = strlen (pMacPos) + 1;
104 len += strlen (pMacPos + len) + 1;
105 #define MAXLEN 4098
106 if (len > MAXLEN) len = MAXLEN;
107 if ((*ppCurEnv = MALLOC (len, char)) == NULL) {
108 No_ram ();
109 } /* if */
111 firstnil = TRUE;
112 for (pCurPos = *ppCurEnv; ((pCurPos - *ppCurEnv) < MAXLEN - 1); ++pCurPos, ++pMacPos) {
113 if (*pMacPos == '=') {
114 *pCurPos = gEqualReplace;
116 } else if (*pMacPos == '\0') {
117 if (firstnil) {
118 *pCurPos = '=';
119 firstnil = FALSE;
120 } else {
121 *pCurPos = *pMacPos;
122 break;
123 } /* if ... else */
125 } else {
126 *pCurPos = *pMacPos;
127 } /* if ... elses */
128 } /* for */
129 firstnil = FALSE;
130 } /* for */
131 *ppCurEnv = NULL;
133 #endif
135 int firstnil;
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) {
141 firstnil = TRUE;
142 for (pCurPos = *ppCurEnv;
143 ((pCurPos - *ppCurEnv < kMaxEnvLen - 1) &&
144 ((*pCurPos != '\0') || firstnil));
145 ++pCurPos) {
146 if (*pCurPos == '=') {
147 AddReplace (pCurPos);
148 *pCurPos = kEqualReplace;
150 } else if (*pCurPos == '\0') {
151 AddReplace (pCurPos);
152 *pCurPos = '=';
153 firstnil = FALSE;
154 } /* if ... else if */
155 } /* for */
157 /* If the environtmental variable was too large ... */
158 if (*pCurPos != '\0') {
159 AddReplace (pCurPos);
160 *pCurPos = '\0';
161 if (firstnil) {
162 AddReplace (--pCurPos);
163 *pCurPos = '=';
164 } /* if */
165 } /* if */
166 } /* for */
168 #if 0
169 } /* if */
170 #endif
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) {
182 No_ram ();
183 } /* if */
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.
197 PUBLIC void
198 free_env()
200 struct ReplaceChar *pReplaceChar;
202 while (gpReplaceList != NULL) {
203 pReplaceChar = gpReplaceList;
204 gpReplaceList = pReplaceChar->fpNext;
206 *(pReplaceChar->fpPos) = pReplaceChar->fC;
208 FREE (pReplaceChar);
209 } /* while */
211 #if 0
212 char **ppCurEnv;
213 char *pCurPos;
215 if (!gFECalled) {
216 gFECalled = TRUE;
218 //FREE (environ);
219 environ = NULL;
220 #endif
221 #if 0
222 /* Restore the environment list to what it was before we
223 read it in. */
224 for (ppCurEnv = environ; *ppCurEnv != NULL; ++ppCurEnv) {
225 for (pCurPos = *ppCurEnv; *pCurPos != '='; ++pCurPos)
227 *pCurPos = '\0';
228 } /* for */
229 } /* if */
230 #endif
231 } /* PUBLIC void free_env () */