Debian Codegraph @

pTk/mTk/tclGeneric/tclEnv.c

raw symbols SCIP
1 /*
2 * tclEnv.c --
3 *
4 * Tcl support for environment variables, including a setenv
5 * procedure. This file contains the generic portion of the
6 * environment module. It is primarily responsible for keeping
7 * the "env" arrays in sync with the system environment variables.
8 *
9 * Copyright (c) 1991-1994 The Regents of the University of California.
10 * Copyright (c) 1994-1998 Sun Microsystems, Inc.
11 *
12 * See the file "license.terms" for information on usage and redistribution
13 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 *
15 * RCS: @(#) $Id: tclEnv.c,v 1.20.2.1 2003/05/14 17:17:46 hobbs Exp $
16 */
17
18 #include "tclInt.h"
19 #include "tclPort.h"
20
21 TCL_DECLARE_MUTEX(envMutex) /* To serialize access to environ */
22
23 static int cacheSize = 0; /* Number of env strings in environCache. */
24 static char **environCache = NULL;
25 /* Array containing all of the environment
26 * strings that Tcl has allocated. */
27
28 #ifndef USE_PUTENV
29 static int environSize = 0; /* Non-zero means that the environ array was
30 * malloced and has this many total entries
31 * allocated to it (not all may be in use at
32 * once). Zero means that the environment
33 * array is in its original static state. */
34 #endif
35
36 /*
37 * For MacOS X
38 */
39 #if defined(__APPLE__) && defined(__DYNAMIC__)
40 #include <crt_externs.h>
41 char **environ = NULL;
42 #endif
43
44 /*
45 * Declarations for local procedures defined in this file:
46 */
47
48 static char * EnvTraceProc _ANSI_ARGS_((ClientData clientData,
49 Tcl_Interp *interp, CONST char *name1,
50 CONST char *name2, int flags));
51 static void ReplaceString _ANSI_ARGS_((CONST char *oldStr,
52 char *newStr));
53 void TclSetEnv _ANSI_ARGS_((CONST char *name,
54 CONST char *value));
55 void TclUnsetEnv _ANSI_ARGS_((CONST char *name));
56
57 #if defined (__CYGWIN__) && defined(__WIN32__)
58 static void TclCygwinPutenv _ANSI_ARGS_((CONST char *string));
59 #endif
60
61 /*
62 *----------------------------------------------------------------------
63 *
64 * TclSetupEnv --
65 *
66 * This procedure is invoked for an interpreter to make environment
67 * variables accessible from that interpreter via the "env"
68 * associative array.
69 *
70 * Results:
71 * None.
72 *
73 * Side effects:
74 * The interpreter is added to a list of interpreters managed
75 * by us, so that its view of envariables can be kept consistent
76 * with the view in other interpreters. If this is the first
77 * call to TclSetupEnv, then additional initialization happens,
78 * such as copying the environment to dynamically-allocated space
79 * for ease of management.
80 *
81 *----------------------------------------------------------------------
82 */
83
84 void
85 TclSetupEnv(interp)
86 Tcl_Interp *interp; /* Interpreter whose "env" array is to be
87 * managed. */
88 {
89 Tcl_DString envString;
90 char *p1, *p2;
91 int i;
92
93 /*
94 * For MacOS X
95 */
96 #if defined(__APPLE__) && defined(__DYNAMIC__)
97 environ = *_NSGetEnviron();
98 #endif
99
100 /*
101 * Synchronize the values in the environ array with the contents
102 * of the Tcl "env" variable. To do this:
103 * 1) Remove the trace that fires when the "env" var is unset.
104 * 2) Unset the "env" variable.
105 * 3) If there are no environ variables, create an empty "env"
106 * array. Otherwise populate the array with current values.
107 * 4) Add a trace that synchronizes the "env" array.
108 */
109
110 Tcl_UntraceVar2(interp, "env", (char *) NULL,
111 TCL_GLOBAL_ONLY | TCL_TRACE_WRITES | TCL_TRACE_UNSETS |
112 TCL_TRACE_READS | TCL_TRACE_ARRAY, EnvTraceProc,
113 (ClientData) NULL);
114
115 Tcl_UnsetVar2(interp, "env", (char *) NULL, TCL_GLOBAL_ONLY);
116
117 if (environ[0] == NULL) {
118 Tcl_Obj *varNamePtr;
119
120 varNamePtr = Tcl_NewStringObj("env", -1);
121 Tcl_IncrRefCount(varNamePtr);
122 TclArraySet(interp, varNamePtr, NULL);
123 Tcl_DecrRefCount(varNamePtr);
124 } else {
125 Tcl_MutexLock(&envMutex);
126 for (i = 0; environ[i] != NULL; i++) {
127 p1 = Tcl_ExternalToUtfDString(NULL, environ[i], -1, &envString);
128 p2 = strchr(p1, '=');
129 if (p2 == NULL) {
130 /*
131 * This condition seem to happen occasionally under some
132 * versions of Solaris; ignore the entry.
133 */
134
135 continue;
136 }
137 p2++;
138 p2[-1] = '\0';
139 Tcl_SetVar2(interp, "env", p1, p2, TCL_GLOBAL_ONLY);
140 Tcl_DStringFree(&envString);
141 }
142 Tcl_MutexUnlock(&envMutex);
143 }
144
145 Tcl_TraceVar2(interp, "env", (char *) NULL,
146 TCL_GLOBAL_ONLY | TCL_TRACE_WRITES | TCL_TRACE_UNSETS |
147 TCL_TRACE_READS | TCL_TRACE_ARRAY, EnvTraceProc,
148 (ClientData) NULL);
149 }
150
151 /*
152 *----------------------------------------------------------------------
153 *
154 * TclSetEnv --
155 *
156 * Set an environment variable, replacing an existing value
157 * or creating a new variable if there doesn't exist a variable
158 * by the given name. This procedure is intended to be a
159 * stand-in for the UNIX "setenv" procedure so that applications
160 * using that procedure will interface properly to Tcl. To make
161 * it a stand-in, the Makefile must define "TclSetEnv" to "setenv".
162 *
163 * Results:
164 * None.
165 *
166 * Side effects:
167 * The environ array gets updated.
168 *
169 *----------------------------------------------------------------------
170 */
171
172 void
173 TclSetEnv(name, value)
174 CONST char *name; /* Name of variable whose value is to be
175 * set (UTF-8). */
176 CONST char *value; /* New value for variable (UTF-8). */
177 {
178 Tcl_DString envString;
179 int index, length, nameLength;
180 char *p, *oldValue;
181 CONST char *p2;
182
183 /*
184 * Figure out where the entry is going to go. If the name doesn't
185 * already exist, enlarge the array if necessary to make room. If the
186 * name exists, free its old entry.
187 */
188
189 Tcl_MutexLock(&envMutex);
190 index = TclpFindVariable(name, &length);
191
192 if (index == -1) {
193 #ifndef USE_PUTENV
194 if ((length + 2) > environSize) {
195 char **newEnviron;
196
197 newEnviron = (char **) ckalloc((unsigned)
198 ((length + 5) * sizeof(char *)));
199 memcpy((VOID *) newEnviron, (VOID *) environ,
200 length*sizeof(char *));
201 if (environSize != 0) {
202 ckfree((char *) environ);
203 }
204 environ = newEnviron;
205 environSize = length + 5;
206 #if defined(__APPLE__) && defined(__DYNAMIC__)
207 {
208 char ***e = _NSGetEnviron();
209 *e = environ;
210 }
211 #endif
212 }
213 index = length;
214 environ[index + 1] = NULL;
215 #endif
216 oldValue = NULL;
217 nameLength = strlen(name);
218 } else {
219 CONST char *env;
220
221 /*
222 * Compare the new value to the existing value. If they're
223 * the same then quit immediately (e.g. don't rewrite the
224 * value or propagate it to other interpreters). Otherwise,
225 * when there are N interpreters there will be N! propagations
226 * of the same value among the interpreters.
227 */
228
229 env = Tcl_ExternalToUtfDString(NULL, environ[index], -1, &envString);
230 if (strcmp(value, (env + length + 1)) == 0) {
231 Tcl_DStringFree(&envString);
232 Tcl_MutexUnlock(&envMutex);
233 return;
234 }
235 Tcl_DStringFree(&envString);
236
237 oldValue = environ[index];
238 nameLength = length;
239 }
240
241
242 /*
243 * Create a new entry. Build a complete UTF string that contains
244 * a "name=value" pattern. Then convert the string to the native
245 * encoding, and set the environ array value.
246 */
247
248 p = (char *) ckalloc((unsigned) (nameLength + strlen(value) + 2));
249 strcpy(p, name);
250 p[nameLength] = '=';
251 strcpy(p+nameLength+1, value);
252 p2 = Tcl_UtfToExternalDString(NULL, p, -1, &envString);
253
254 /*
255 * Copy the native string to heap memory.
256 */
257
258 p = (char *) ckrealloc(p, (unsigned) (strlen(p2) + 1));
259 strcpy(p, p2);
260 Tcl_DStringFree(&envString);
261
262 #ifdef USE_PUTENV
263 /*
264 * Update the system environment.
265 */
266
267 putenv(p);
268 index = TclpFindVariable(name, &length);
269 #else
270 environ[index] = p;
271 #endif
272
273 /*
274 * Watch out for versions of putenv that copy the string (e.g. VC++).
275 * In this case we need to free the string immediately. Otherwise
276 * update the string in the cache.
277 */
278
279 if ((index != -1) && (environ[index] == p)) {
280 ReplaceString(oldValue, p);
281 #ifdef HAVE_PUTENV_THAT_COPIES
282 } else {
283 /* This putenv() copies instead of taking ownership */
284 ckfree(p);
285 #endif
286 }
287
288 Tcl_MutexUnlock(&envMutex);
289
290 if (!strcmp(name, "HOME")) {
291 /*
292 * If the user's home directory has changed, we must invalidate
293 * the filesystem cache, because '~' expansions will now be
294 * incorrect.
295 */
296 Tcl_FSMountsChanged(NULL);
297 }
298 }
299
300 /*
301 *----------------------------------------------------------------------
302 *
303 * Tcl_PutEnv --
304 *
305 * Set an environment variable. Similar to setenv except that
306 * the information is passed in a single string of the form
307 * NAME=value, rather than as separate name strings. This procedure
308 * is intended to be a stand-in for the UNIX "putenv" procedure
309 * so that applications using that procedure will interface
310 * properly to Tcl. To make it a stand-in, the Makefile will
311 * define "Tcl_PutEnv" to "putenv".
312 *
313 * Results:
314 * None.
315 *
316 * Side effects:
317 * The environ array gets updated, as do all of the interpreters
318 * that we manage.
319 *
320 *----------------------------------------------------------------------
321 */
322
323 int
324 Tcl_PutEnv(string)
325 CONST char *string; /* Info about environment variable in the
326 * form NAME=value. (native) */
327 {
328 Tcl_DString nameString;
329 CONST char *name;
330 char *value;
331
332 if (string == NULL) {
333 return 0;
334 }
335
336 /*
337 * First convert the native string to UTF. Then separate the
338 * string into name and value parts, and call TclSetEnv to do
339 * all of the real work.
340 */
341
342 name = Tcl_ExternalToUtfDString(NULL, string, -1, &nameString);
343 value = strchr(name, '=');
344
345 if ((value != NULL) && (value != name)) {
346 value[0] = '\0';
347 TclSetEnv(name, value+1);
348 }
349
350 Tcl_DStringFree(&nameString);
351 return 0;
352 }
353
354 /*
355 *----------------------------------------------------------------------
356 *
357 * TclUnsetEnv --
358 *
359 * Remove an environment variable, updating the "env" arrays
360 * in all interpreters managed by us. This function is intended
361 * to replace the UNIX "unsetenv" function (but to do this the
362 * Makefile must be modified to redefine "TclUnsetEnv" to
363 * "unsetenv".
364 *
365 * Results:
366 * None.
367 *
368 * Side effects:
369 * Interpreters are updated, as is environ.
370 *
371 *----------------------------------------------------------------------
372 */
373
374 void
375 TclUnsetEnv(name)
376 CONST char *name; /* Name of variable to remove (UTF-8). */
377 {
378 char *oldValue;
379 int length;
380 int index;
381 #ifdef USE_PUTENV
382 Tcl_DString envString;
383 char *string;
384 #else
385 char **envPtr;
386 #endif
387
388 Tcl_MutexLock(&envMutex);
389 index = TclpFindVariable(name, &length);
390
391 /*
392 * First make sure that the environment variable exists to avoid
393 * doing needless work and to avoid recursion on the unset.
394 */
395
396 if (index == -1) {
397 Tcl_MutexUnlock(&envMutex);
398 return;
399 }
400 /*
401 * Remember the old value so we can free it if Tcl created the string.
402 */
403
404 oldValue = environ[index];
405
406 /*
407 * Update the system environment. This must be done before we
408 * update the interpreters or we will recurse.
409 */
410
411 #ifdef USE_PUTENV
412 string = ckalloc((unsigned int) length+2);
413 memcpy((VOID *) string, (VOID *) name, (size_t) length);
414 string[length] = '=';
415 string[length+1] = '\0';
416
417 Tcl_UtfToExternalDString(NULL, string, -1, &envString);
418 string = ckrealloc(string, (unsigned) (Tcl_DStringLength(&envString)+1));
419 strcpy(string, Tcl_DStringValue(&envString));
420 Tcl_DStringFree(&envString);
421
422 putenv(string);
423
424 /*
425 * Watch out for versions of putenv that copy the string (e.g. VC++).
426 * In this case we need to free the string immediately. Otherwise
427 * update the string in the cache.
428 */
429
430 if (environ[index] == string) {
431 ReplaceString(oldValue, string);
432 #ifdef HAVE_PUTENV_THAT_COPIES
433 } else {
434 /* This putenv() copies instead of taking ownership */
435 ckfree(string);
436 #endif
437 }
438 #else
439 for (envPtr = environ+index+1; ; envPtr++) {
440 envPtr[-1] = *envPtr;
441 if (*envPtr == NULL) {
442 break;
443 }
444 }
445 ReplaceString(oldValue, NULL);
446 #endif
447
448 Tcl_MutexUnlock(&envMutex);
449 }
450
451 /*
452 *---------------------------------------------------------------------------
453 *
454 * TclGetEnv --
455 *
456 * Retrieve the value of an environment variable.
457 *
458 * Results:
459 * The result is a pointer to a string specifying the value of the
460 * environment variable, or NULL if that environment variable does
461 * not exist. Storage for the result string is allocated in valuePtr;
462 * the caller must call Tcl_DStringFree() when the result is no
463 * longer needed.
464 *
465 * Side effects:
466 * None.
467 *
468 *----------------------------------------------------------------------
469 */
470
471 CONST char *
472 TclGetEnv(name, valuePtr)
473 CONST char *name; /* Name of environment variable to find
474 * (UTF-8). */
475 Tcl_DString *valuePtr; /* Uninitialized or free DString in which
476 * the value of the environment variable is
477 * stored. */
478 {
479 int length, index;
480 CONST char *result;
481
482 Tcl_MutexLock(&envMutex);
483 index = TclpFindVariable(name, &length);
484 result = NULL;
485 if (index != -1) {
486 Tcl_DString envStr;
487
488 result = Tcl_ExternalToUtfDString(NULL, environ[index], -1, &envStr);
489 result += length;
490 if (*result == '=') {
491 result++;
492 Tcl_DStringInit(valuePtr);
493 Tcl_DStringAppend(valuePtr, result, -1);
494 result = Tcl_DStringValue(valuePtr);
495 } else {
496 result = NULL;
497 }
498 Tcl_DStringFree(&envStr);
499 }
500 Tcl_MutexUnlock(&envMutex);
501 return result;
502 }
503
504 /*
505 *----------------------------------------------------------------------
506 *
507 * EnvTraceProc --
508 *
509 * This procedure is invoked whenever an environment variable
510 * is read, modified or deleted. It propagates the change to the global
511 * "environ" array.
512 *
513 * Results:
514 * Always returns NULL to indicate success.
515 *
516 * Side effects:
517 * Environment variable changes get propagated. If the whole
518 * "env" array is deleted, then we stop managing things for
519 * this interpreter (usually this happens because the whole
520 * interpreter is being deleted).
521 *
522 *----------------------------------------------------------------------
523 */
524
525 /* ARGSUSED */
526 static char *
527 EnvTraceProc(clientData, interp, name1, name2, flags)
528 ClientData clientData; /* Not used. */
529 Tcl_Interp *interp; /* Interpreter whose "env" variable is
530 * being modified. */
531 CONST char *name1; /* Better be "env". */
532 CONST char *name2; /* Name of variable being modified, or NULL
533 * if whole array is being deleted (UTF-8). */
534 int flags; /* Indicates what's happening. */
535 {
536 /*
537 * For array traces, let TclSetupEnv do all the work.
538 */
539
540 if (flags & TCL_TRACE_ARRAY) {
541 TclSetupEnv(interp);
542 return NULL;
543 }
544
545 /*
546 * If name2 is NULL, then return and do nothing.
547 */
548
549 if (name2 == NULL) {
550 return NULL;
551 }
552
553 /*
554 * If a value is being set, call TclSetEnv to do all of the work.
555 */
556
557 if (flags & TCL_TRACE_WRITES) {
558 CONST char *value;
559
560 value = Tcl_GetVar2(interp, "env", name2, TCL_GLOBAL_ONLY);
561 TclSetEnv(name2, value);
562 }
563
564 /*
565 * If a value is being read, call TclGetEnv to do all of the work.
566 */
567
568 if (flags & TCL_TRACE_READS) {
569 Tcl_DString valueString;
570 CONST char *value;
571
572 value = TclGetEnv(name2, &valueString);
573 if (value == NULL) {
574 return "no such variable";
575 }
576 Tcl_SetVar2(interp, name1, name2, value, 0);
577 Tcl_DStringFree(&valueString);
578 }
579
580 /*
581 * For unset traces, let TclUnsetEnv do all the work.
582 */
583
584 if (flags & TCL_TRACE_UNSETS) {
585 TclUnsetEnv(name2);
586 }
587 return NULL;
588 }
589
590 /*
591 *----------------------------------------------------------------------
592 *
593 * ReplaceString --
594 *
595 * Replace one string with another in the environment variable
596 * cache. The cache keeps track of all of the environment
597 * variables that Tcl has modified so they can be freed later.
598 *
599 * Results:
600 * None.
601 *
602 * Side effects:
603 * May free the old string.
604 *
605 *----------------------------------------------------------------------
606 */
607
608 static void
609 ReplaceString(oldStr, newStr)
610 CONST char *oldStr; /* Old environment string. */
611 char *newStr; /* New environment string. */
612 {
613 int i;
614 char **newCache;
615
616 /*
617 * Check to see if the old value was allocated by Tcl. If so,
618 * it needs to be deallocated to avoid memory leaks. Note that this
619 * algorithm is O(n), not O(1). This will result in n-squared behavior
620 * if lots of environment changes are being made.
621 */
622
623 for (i = 0; i < cacheSize; i++) {
624 if ((environCache[i] == oldStr) || (environCache[i] == NULL)) {
625 break;
626 }
627 }
628 if (i < cacheSize) {
629 /*
630 * Replace or delete the old value.
631 */
632
633 if (environCache[i]) {
634 ckfree(environCache[i]);
635 }
636
637 if (newStr) {
638 environCache[i] = newStr;
639 } else {
640 for (; i < cacheSize-1; i++) {
641 environCache[i] = environCache[i+1];
642 }
643 environCache[cacheSize-1] = NULL;
644 }
645 } else {
646 int allocatedSize = (cacheSize + 5) * sizeof(char *);
647
648 /*
649 * We need to grow the cache in order to hold the new string.
650 */
651
652 newCache = (char **) ckalloc((unsigned) allocatedSize);
653 (VOID *) memset(newCache, (int) 0, (size_t) allocatedSize);
654
655 if (environCache) {
656 memcpy((VOID *) newCache, (VOID *) environCache,
657 (size_t) (cacheSize * sizeof(char*)));
658 ckfree((char *) environCache);
659 }
660 environCache = newCache;
661 environCache[cacheSize] = newStr;
662 environCache[cacheSize+1] = NULL;
663 cacheSize += 5;
664 }
665 }
666
667 /*
668 *----------------------------------------------------------------------
669 *
670 * TclFinalizeEnvironment --
671 *
672 * This function releases any storage allocated by this module
673 * that isn't still in use by the global environment. Any
674 * strings that are still in the environment will be leaked.
675 *
676 * Results:
677 * None.
678 *
679 * Side effects:
680 * May deallocate storage.
681 *
682 *----------------------------------------------------------------------
683 */
684
685 void
686 TclFinalizeEnvironment()
687 {
688 /*
689 * For now we just deallocate the cache array and none of the environment
690 * strings. This may leak more memory that strictly necessary, since some
691 * of the strings may no longer be in the environment. However,
692 * determining which ones are ok to delete is n-squared, and is pretty
693 * unlikely, so we don't bother.
694 */
695
696 if (environCache) {
697 ckfree((char *) environCache);
698 environCache = NULL;
699 cacheSize = 0;
700 #ifndef USE_PUTENV
701 environSize = 0;
702 #endif
703 }
704 }
705
706 #if defined(__CYGWIN__) && defined(__WIN32__)
707
708 #include <windows.h>
709
710 /*
711 * When using cygwin, when an environment variable changes, we need to synch
712 * with both the cygwin environment (in case the application C code calls
713 * fork) and the Windows environment (in case the application TCL code calls
714 * exec, which calls the Windows CreateProcess function).
715 */
716
717 static void
718 TclCygwinPutenv(str)
719 const char *str;
720 {
721 char *name, *value;
722
723 /* Get the name and value, so that we can change the environment
724 variable for Windows. */
725 name = (char *) alloca (strlen (str) + 1);
726 strcpy (name, str);
727 for (value = name; *value != '=' && *value != '\0'; ++value)
728 ;
729 if (*value == '\0') {
730 /* Can't happen. */
731 return;
732 }
733 *value = '\0';
734 ++value;
735 if (*value == '\0') {
736 value = NULL;
737 }
738
739 /* Set the cygwin environment variable. */
740 #undef putenv
741 if (value == NULL) {
742 unsetenv (name);
743 } else {
744 putenv(str);
745 }
746
747 /*
748 * Before changing the environment variable in Windows, if this is PATH,
749 * we need to convert the value back to a Windows style path.
750 *
751 * FIXME: The calling program may know it is running under windows, and
752 * may have set the path to a Windows path, or, worse, appended or
753 * prepended a Windows path to PATH.
754 */
755 if (strcmp (name, "PATH") != 0) {
756 /* If this is Path, eliminate any PATH variable, to prevent any
757 confusion. */
758 if (strcmp (name, "Path") == 0) {
759 SetEnvironmentVariable ("PATH", (char *) NULL);
760 unsetenv ("PATH");
761 }
762
763 SetEnvironmentVariable (name, value);
764 } else {
765 char *buf;
766
767 /* Eliminate any Path variable, to prevent any confusion. */
768 SetEnvironmentVariable ("Path", (char *) NULL);
769 unsetenv ("Path");
770
771 if (value == NULL) {
772 buf = NULL;
773 } else {
774 int size;
775
776 size = cygwin_posix_to_win32_path_list_buf_size (value);
777 buf = (char *) alloca (size + 1);
778 cygwin_posix_to_win32_path_list (value, buf);
779 }
780
781 SetEnvironmentVariable (name, buf);
782 }
783 }
784
785 #endif /* __CYGWIN__ && __WIN32__ */