1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
#ifndef WNT
#include <Standard_NullObject.hxx>
#include <Standard_ConstructionError.hxx>
#include <Standard_Failure.hxx>
#include <Standard_Mutex.hxx>
#include <OSD_Environment.ixx>
#include <OSD_WhoAmI.hxx>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
static const OSD_WhoAmI Iam = OSD_WEnvironment;
// ----------------------------------------------------------------------
//
// Updated by : JPT Dec,7 1992
// What : OSD_Environment::OSD_Environment
// (const TCollection_AsciiString& Name,
// const TCollection_AsciiString& Value)
// Value could contain the character $ (Ex setenv a = $c)
//
// ----------------------------------------------------------------------
// Create object
OSD_Environment::OSD_Environment()
{
}
// ----------------------------------------------------------------------
// Constructor
// ----------------------------------------------------------------------
OSD_Environment::OSD_Environment(const TCollection_AsciiString& Name)
{
if (!Name.IsAscii() || Name.Search("$") != -1 )
Standard_ConstructionError::Raise("OSD_Environment::OSD_Environment: bad argument");
myName = Name;
}
// ----------------------------------------------------------------------
// Create an environment variable and initialize it
// ----------------------------------------------------------------------
OSD_Environment::OSD_Environment(const TCollection_AsciiString& Name,
const TCollection_AsciiString& Value)
{
if (!Name.IsAscii() || !Value.IsAscii() ||
// JPT : Dec-7-1992 Name.Search("$") != -1 || Value.Search("$") != -1)
Name.Search("$") != -1 )
Standard_ConstructionError::Raise("OSD_Environment::OSD_Environment: bad argument");
myName = Name;
myValue = Value;
}
// ----------------------------------------------------------------------
// Returns the name of the symbol
// ----------------------------------------------------------------------
TCollection_AsciiString OSD_Environment::Name () const
{
return myName;
}
// ----------------------------------------------------------------------
// Set new value for environment variable
// ----------------------------------------------------------------------
void OSD_Environment::SetName (const TCollection_AsciiString& Name)
{
myError.Reset();
if (!Name.IsAscii() || Name.Search("$") != -1 )
Standard_ConstructionError::Raise("OSD_Environment::SetName: bad argument");
myName = Name;
}
// ----------------------------------------------------------------------
// Change value
// ----------------------------------------------------------------------
void OSD_Environment::SetValue (const TCollection_AsciiString& Value)
{
if (!Value.IsAscii() || Value.Search("$") != -1)
Standard_ConstructionError::Raise("OSD_Environment::Change: bad argument");
myValue = Value;
}
// ----------------------------------------------------------------------
// Get environment variable physically
// ----------------------------------------------------------------------
TCollection_AsciiString OSD_Environment::Value()
{
char *result = getenv(myName.ToCString());
if (result == NULL) myValue.Clear();
else myValue = result;
return myValue;
}
// ----------------------------------------------------------------------
// Sets physically the environment variable
// ----------------------------------------------------------------------
void OSD_Environment::Build ()
{
// Static buffer to hold definitions of new variables for the environment.
// Note that they need to be static since putenv does not make a copy
// of the string, but just adds its pointer to the environment.
static char **buffer = 0 ; // JPT:
static int Ibuffer = 0 ; // Tout ca pour putenv,getenv
// Use mutex to avoid concurrent access to the buffer
static Standard_Mutex theMutex;
Standard_Mutex::Sentry aSentry ( theMutex );
// check if such variable has already been created in the buffer
int index = -1, len = myName.Length();
for ( int i=0; i < Ibuffer; i++ ) {
if ( ! strncmp ( buffer[i], myName.ToCString(), len ) && buffer[i][len] == '=' ) {
index = i;
break;
}
}
// and either add a new entry, or remember the old entry for a while
char *old_value = 0;
if ( index >=0 ) {
old_value = buffer[index];
}
else {
// Allocation memoire. Surtout tout la heap!
index = Ibuffer++;
buffer = (char **) realloc ( buffer, Ibuffer * sizeof(char*) );
}
// create a new entry in the buffer and add it to environment
buffer[index] = (char *) malloc ( len + myValue.Length() + 2 );
sprintf(buffer[index], "%s=%s", myName.ToCString(), myValue.ToCString());
putenv(buffer[index]);
// then (and only then!) free old entry, if existed
if ( old_value )
free ( old_value );
// check the result
char *result = getenv(myName.ToCString());
if (result == NULL)
myError.SetValue(errno, Iam, "Set Environment");
}
// ----------------------------------------------------------------------
// Remove physically the environment variable
// ----------------------------------------------------------------------
void OSD_Environment::Remove ()
{
myValue.Clear();
Build();
}
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
void OSD_Environment::Reset()
{
myError.Reset();
}
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
Standard_Boolean OSD_Environment::Failed() const
{
return myError.Failed();
}
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
void OSD_Environment::Perror()
{
myError.Perror();
}
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
Standard_Integer OSD_Environment::Error() const
{
return myError.Error();
}
#else
//------------------------------------------------------------------------
//------------------- WNT Sources of OSD_Environment --------------------
//------------------------------------------------------------------------
#define STRICT
#include <OSD_Environment.hxx>
#include <OSD_WNT.hxx>
#include <windows.h>
#include <stdio.h>
static void __fastcall _set_error ( OSD_Error&, DWORD );
OSD_Environment :: OSD_Environment () {
} // end constructor ( 1 )
OSD_Environment :: OSD_Environment ( const TCollection_AsciiString& Name ) {
myName = Name;
} // end constructor ( 2 )
OSD_Environment :: OSD_Environment (
const TCollection_AsciiString& Name,
const TCollection_AsciiString& Value
) {
myName = Name;
myValue = Value;
} // end constructor ( 3 )
void OSD_Environment :: SetValue ( const TCollection_AsciiString& Value ) {
myValue = Value;
} // end OSD_Environment :: SetValue
TCollection_AsciiString OSD_Environment :: Value () {
Standard_PCharacter pBuff = NULL;
DWORD dwSize = 0;
char* envVal = NULL;
myValue.Clear ();
SetLastError ( ERROR_SUCCESS );
dwSize = GetEnvironmentVariable ( myName.ToCString (), pBuff, dwSize );
if ( ( dwSize == 0 && GetLastError () != ERROR_SUCCESS ) ||
( envVal = getenv ( myName.ToCString () ) ) == NULL
)
_set_error ( myError, ERROR_ENVVAR_NOT_FOUND );
else if ( envVal != NULL )
myValue = envVal;
else {
++dwSize;
pBuff = new Standard_Character[ dwSize ];
GetEnvironmentVariable ( (char *)myName.ToCString (), pBuff, dwSize );
myValue = pBuff;
delete [] pBuff;
Reset ();
} // end else
return myValue;
} // end OSD_Environment :: Value
void OSD_Environment :: SetName ( const TCollection_AsciiString& name ) {
myName = name;
} // end OSD_Environment :: SetName
TCollection_AsciiString OSD_Environment :: Name () const {
return myName;
} // end OSD_Environment :: Name
void OSD_Environment :: Build () {
TCollection_AsciiString str;
str = myName + TEXT( "=" ) + myValue;
putenv ( str.ToCString () );
} // end OSD_Environment :: Build
void OSD_Environment :: Remove () {
TCollection_AsciiString str;
str = myName + TEXT( "=" );
putenv ( str.ToCString () );
} // end OSD_Environment :: Remove
Standard_Boolean OSD_Environment :: Failed () const {
return myError.Failed ();
} // end OSD_Environment :: Failed
void OSD_Environment :: Reset () {
myError.Reset ();
} // end OSD_Environment :: Reset
void OSD_Environment :: Perror () {
if ( ErrorPrefix () )
( *ErrorStream () ) << TEXT( '\'' ) << myName.ToCString () << TEXT( "' - " );
myError.Perror ();
} // end OSD_Environment :: Perror
Standard_Integer OSD_Environment :: Error () const {
return myError.Error ();
} // end OSD_Environment :: Error
static void __fastcall _set_error ( OSD_Error& err, DWORD code ) {
DWORD errCode;
Standard_Character buffer[ 2048 ];
errCode = code ? code : GetLastError ();
if ( !FormatMessage (
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
0, errCode, MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ),
buffer, 2048, NULL
)
) {
sprintf ( buffer, "error code %d", (Standard_Integer)errCode );
SetLastError ( errCode );
} // end if
err.SetValue ( errCode, OSD_WEnvironment, buffer );
} // end _set_error
#endif
|