sl@0
|
1 |
/*
|
sl@0
|
2 |
* tclMacEnv.c --
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* Implements the "environment" on a Macintosh.
|
sl@0
|
5 |
*
|
sl@0
|
6 |
* Copyright (c) 1995-1996 Sun Microsystems, Inc.
|
sl@0
|
7 |
*
|
sl@0
|
8 |
* See the file "license.terms" for information on usage and redistribution
|
sl@0
|
9 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
sl@0
|
10 |
*
|
sl@0
|
11 |
* RCS: @(#) $Id: tclMacEnv.c,v 1.2 1998/09/14 18:40:04 stanton Exp $
|
sl@0
|
12 |
*/
|
sl@0
|
13 |
|
sl@0
|
14 |
#include <Gestalt.h>
|
sl@0
|
15 |
#include <Folders.h>
|
sl@0
|
16 |
#include <TextUtils.h>
|
sl@0
|
17 |
#include <Resources.h>
|
sl@0
|
18 |
#include <string.h>
|
sl@0
|
19 |
|
sl@0
|
20 |
#include "tcl.h"
|
sl@0
|
21 |
#include "tclInt.h"
|
sl@0
|
22 |
#include "tclMacInt.h"
|
sl@0
|
23 |
#include "tclPort.h"
|
sl@0
|
24 |
|
sl@0
|
25 |
#define kMaxEnvStringSize 255
|
sl@0
|
26 |
#define kMaxEnvVarSize 100
|
sl@0
|
27 |
#define kLoginnameTag "LOGIN="
|
sl@0
|
28 |
#define kUsernameTag "USER="
|
sl@0
|
29 |
#define kDefaultDirTag "HOME="
|
sl@0
|
30 |
|
sl@0
|
31 |
/*
|
sl@0
|
32 |
* The following specifies a text file where additional environment variables
|
sl@0
|
33 |
* can be set. The file must reside in the preferences folder. If the file
|
sl@0
|
34 |
* doesn't exist NO error will occur. Commet out the difinition if you do
|
sl@0
|
35 |
* NOT want to use an environment variables file.
|
sl@0
|
36 |
*/
|
sl@0
|
37 |
#define kPrefsFile "Tcl Environment Variables"
|
sl@0
|
38 |
|
sl@0
|
39 |
/*
|
sl@0
|
40 |
* The following specifies the Name of a 'STR#' resource in the application
|
sl@0
|
41 |
* where additional environment variables may be set. If the resource doesn't
|
sl@0
|
42 |
* exist no errors will occur. Commet it out if you don't want it.
|
sl@0
|
43 |
*/
|
sl@0
|
44 |
#define REZ_ENV "\pTcl Environment Variables"
|
sl@0
|
45 |
|
sl@0
|
46 |
/* Globals */
|
sl@0
|
47 |
char **environ = NULL;
|
sl@0
|
48 |
|
sl@0
|
49 |
/*
|
sl@0
|
50 |
* Declarations for local procedures defined in this file:
|
sl@0
|
51 |
*/
|
sl@0
|
52 |
static char ** RezRCVariables _ANSI_ARGS_((void));
|
sl@0
|
53 |
static char ** FileRCVariables _ANSI_ARGS_((void));
|
sl@0
|
54 |
static char ** PathVariables _ANSI_ARGS_((void));
|
sl@0
|
55 |
static char ** SystemVariables _ANSI_ARGS_((void));
|
sl@0
|
56 |
static char * MakeFolderEnvVar _ANSI_ARGS_((char * prefixTag,
|
sl@0
|
57 |
long whichFolder));
|
sl@0
|
58 |
static char * GetUserName _ANSI_ARGS_((void));
|
sl@0
|
59 |
|
sl@0
|
60 |
/*
|
sl@0
|
61 |
*----------------------------------------------------------------------
|
sl@0
|
62 |
*
|
sl@0
|
63 |
* RezRCVariables --
|
sl@0
|
64 |
*
|
sl@0
|
65 |
* Creates environment variables from the applications resource fork.
|
sl@0
|
66 |
* The function looks for the 'STR#' resource with the name defined
|
sl@0
|
67 |
* in the #define REZ_ENV. If the define is not defined this code
|
sl@0
|
68 |
* will not be included. If the resource doesn't exist or no strings
|
sl@0
|
69 |
* reside in the resource nothing will happen.
|
sl@0
|
70 |
*
|
sl@0
|
71 |
* Results:
|
sl@0
|
72 |
* ptr to value on success, NULL if error.
|
sl@0
|
73 |
*
|
sl@0
|
74 |
* Side effects:
|
sl@0
|
75 |
* Memory is allocated and returned to the caller.
|
sl@0
|
76 |
*
|
sl@0
|
77 |
*----------------------------------------------------------------------
|
sl@0
|
78 |
*/
|
sl@0
|
79 |
|
sl@0
|
80 |
#ifdef REZ_ENV
|
sl@0
|
81 |
static char **
|
sl@0
|
82 |
RezRCVariables()
|
sl@0
|
83 |
{
|
sl@0
|
84 |
Handle envStrs = NULL;
|
sl@0
|
85 |
char** rezEnv = NULL;
|
sl@0
|
86 |
short int numStrs;
|
sl@0
|
87 |
|
sl@0
|
88 |
envStrs = GetNamedResource('STR#', REZ_ENV);
|
sl@0
|
89 |
if (envStrs == NULL) return NULL;
|
sl@0
|
90 |
numStrs = *((short *) (*envStrs));
|
sl@0
|
91 |
|
sl@0
|
92 |
rezEnv = (char **) ckalloc((numStrs + 1) * sizeof(char *));
|
sl@0
|
93 |
|
sl@0
|
94 |
if (envStrs != NULL) {
|
sl@0
|
95 |
ResType theType;
|
sl@0
|
96 |
Str255 theName;
|
sl@0
|
97 |
short theID, index = 1;
|
sl@0
|
98 |
int i = 0;
|
sl@0
|
99 |
char* string;
|
sl@0
|
100 |
|
sl@0
|
101 |
GetResInfo(envStrs, &theID, &theType, theName);
|
sl@0
|
102 |
for(;;) {
|
sl@0
|
103 |
GetIndString(theName, theID, index++);
|
sl@0
|
104 |
if (theName[0] == '\0') break;
|
sl@0
|
105 |
string = (char *) ckalloc(theName[0] + 2);
|
sl@0
|
106 |
strncpy(string, (char *) theName + 1, theName[0]);
|
sl@0
|
107 |
string[theName[0]] = '\0';
|
sl@0
|
108 |
rezEnv[i++] = string;
|
sl@0
|
109 |
}
|
sl@0
|
110 |
ReleaseResource(envStrs);
|
sl@0
|
111 |
|
sl@0
|
112 |
rezEnv[i] = NULL;
|
sl@0
|
113 |
return rezEnv;
|
sl@0
|
114 |
}
|
sl@0
|
115 |
|
sl@0
|
116 |
return NULL;
|
sl@0
|
117 |
}
|
sl@0
|
118 |
#endif
|
sl@0
|
119 |
|
sl@0
|
120 |
/*
|
sl@0
|
121 |
*----------------------------------------------------------------------
|
sl@0
|
122 |
*
|
sl@0
|
123 |
* FileRCVariables --
|
sl@0
|
124 |
*
|
sl@0
|
125 |
* Creates environment variables from a file in the system preferences
|
sl@0
|
126 |
* folder. The function looks for a file in the preferences folder
|
sl@0
|
127 |
* a name defined in the #define kPrefsFile. If the define is not
|
sl@0
|
128 |
* defined this code will not be included. If the resource doesn't exist or
|
sl@0
|
129 |
* no strings reside in the resource nothing will happen.
|
sl@0
|
130 |
*
|
sl@0
|
131 |
* Results:
|
sl@0
|
132 |
* ptr to value on success, NULL if error.
|
sl@0
|
133 |
*
|
sl@0
|
134 |
* Side effects:
|
sl@0
|
135 |
* Memory is allocated and returned to the caller.
|
sl@0
|
136 |
*
|
sl@0
|
137 |
*----------------------------------------------------------------------
|
sl@0
|
138 |
*/
|
sl@0
|
139 |
|
sl@0
|
140 |
#ifdef kPrefsFile
|
sl@0
|
141 |
static char **
|
sl@0
|
142 |
FileRCVariables()
|
sl@0
|
143 |
{
|
sl@0
|
144 |
char *prefsFolder = NULL;
|
sl@0
|
145 |
char *tempPtr = NULL;
|
sl@0
|
146 |
char **fileEnv = NULL;
|
sl@0
|
147 |
FILE *thePrefsFile = NULL;
|
sl@0
|
148 |
int i;
|
sl@0
|
149 |
FSSpec prefDir;
|
sl@0
|
150 |
OSErr err;
|
sl@0
|
151 |
Handle theString = NULL;
|
sl@0
|
152 |
Tcl_Channel chan;
|
sl@0
|
153 |
int size;
|
sl@0
|
154 |
Tcl_DString lineRead;
|
sl@0
|
155 |
|
sl@0
|
156 |
err = FSpFindFolder(kOnSystemDisk, kPreferencesFolderType,
|
sl@0
|
157 |
kDontCreateFolder, &prefDir);
|
sl@0
|
158 |
if (err != noErr) {
|
sl@0
|
159 |
return NULL;
|
sl@0
|
160 |
}
|
sl@0
|
161 |
err = FSpPathFromLocation(&prefDir, &size, &theString);
|
sl@0
|
162 |
if (err != noErr) {
|
sl@0
|
163 |
return NULL;
|
sl@0
|
164 |
}
|
sl@0
|
165 |
(void) Munger(theString, size, NULL, 0, kPrefsFile, strlen(kPrefsFile));
|
sl@0
|
166 |
|
sl@0
|
167 |
HLock(theString);
|
sl@0
|
168 |
chan = Tcl_OpenFileChannel(NULL, *theString, "r", 0);
|
sl@0
|
169 |
HUnlock(theString);
|
sl@0
|
170 |
DisposeHandle(theString);
|
sl@0
|
171 |
if (chan == NULL) {
|
sl@0
|
172 |
return NULL;
|
sl@0
|
173 |
}
|
sl@0
|
174 |
|
sl@0
|
175 |
/*
|
sl@0
|
176 |
* We found a env file. Let start parsing it.
|
sl@0
|
177 |
*/
|
sl@0
|
178 |
fileEnv = (char **) ckalloc((kMaxEnvVarSize + 1) * sizeof(char *));
|
sl@0
|
179 |
|
sl@0
|
180 |
i = 0;
|
sl@0
|
181 |
Tcl_DStringInit(&lineRead);
|
sl@0
|
182 |
while (Tcl_Gets(chan, &lineRead) != -1) {
|
sl@0
|
183 |
/*
|
sl@0
|
184 |
* First strip off new line char
|
sl@0
|
185 |
*/
|
sl@0
|
186 |
if (lineRead.string[lineRead.length-1] == '\n') {
|
sl@0
|
187 |
lineRead.string[lineRead.length-1] = '\0';
|
sl@0
|
188 |
}
|
sl@0
|
189 |
if (lineRead.string[0] == '\0' || lineRead.string[0] == '#') {
|
sl@0
|
190 |
/*
|
sl@0
|
191 |
* skip empty lines or commented lines
|
sl@0
|
192 |
*/
|
sl@0
|
193 |
Tcl_DStringSetLength(&lineRead, 0);
|
sl@0
|
194 |
continue;
|
sl@0
|
195 |
}
|
sl@0
|
196 |
|
sl@0
|
197 |
tempPtr = (char *) ckalloc(lineRead.length + 1);
|
sl@0
|
198 |
strcpy(tempPtr, lineRead.string);
|
sl@0
|
199 |
fileEnv[i++] = tempPtr;
|
sl@0
|
200 |
Tcl_DStringSetLength(&lineRead, 0);
|
sl@0
|
201 |
}
|
sl@0
|
202 |
|
sl@0
|
203 |
fileEnv[i] = NULL;
|
sl@0
|
204 |
Tcl_Close(NULL, chan);
|
sl@0
|
205 |
Tcl_DStringFree(&lineRead);
|
sl@0
|
206 |
|
sl@0
|
207 |
return fileEnv;
|
sl@0
|
208 |
}
|
sl@0
|
209 |
#endif
|
sl@0
|
210 |
|
sl@0
|
211 |
/*
|
sl@0
|
212 |
*----------------------------------------------------------------------
|
sl@0
|
213 |
*
|
sl@0
|
214 |
* MakeFolderEnvVar --
|
sl@0
|
215 |
*
|
sl@0
|
216 |
* This function creates "environment" variable by taking a prefix and
|
sl@0
|
217 |
* appending a folder path to a directory. The directory is specified
|
sl@0
|
218 |
* by a integer value acceptable by the FindFolder function.
|
sl@0
|
219 |
*
|
sl@0
|
220 |
* Results:
|
sl@0
|
221 |
* The function returns an *allocated* string. If the folder doesn't
|
sl@0
|
222 |
* exist the return string is still allocated and just contains the
|
sl@0
|
223 |
* given prefix.
|
sl@0
|
224 |
*
|
sl@0
|
225 |
* Side effects:
|
sl@0
|
226 |
* Memory is allocated and returned to the caller.
|
sl@0
|
227 |
*
|
sl@0
|
228 |
*----------------------------------------------------------------------
|
sl@0
|
229 |
*/
|
sl@0
|
230 |
|
sl@0
|
231 |
static char *
|
sl@0
|
232 |
MakeFolderEnvVar(
|
sl@0
|
233 |
char * prefixTag, /* Prefix added before result. */
|
sl@0
|
234 |
long whichFolder) /* Constant for FSpFindFolder. */
|
sl@0
|
235 |
{
|
sl@0
|
236 |
char * thePath = NULL;
|
sl@0
|
237 |
char * result = NULL;
|
sl@0
|
238 |
OSErr theErr = noErr;
|
sl@0
|
239 |
Handle theString = NULL;
|
sl@0
|
240 |
FSSpec theFolder;
|
sl@0
|
241 |
int size;
|
sl@0
|
242 |
Tcl_DString pathStr;
|
sl@0
|
243 |
Tcl_DString tagPathStr;
|
sl@0
|
244 |
|
sl@0
|
245 |
Tcl_DStringInit(&pathStr);
|
sl@0
|
246 |
theErr = FSpFindFolder(kOnSystemDisk, whichFolder,
|
sl@0
|
247 |
kDontCreateFolder, &theFolder);
|
sl@0
|
248 |
if (theErr == noErr) {
|
sl@0
|
249 |
theErr = FSpPathFromLocation(&theFolder, &size, &theString);
|
sl@0
|
250 |
|
sl@0
|
251 |
HLock(theString);
|
sl@0
|
252 |
tclPlatform = TCL_PLATFORM_MAC;
|
sl@0
|
253 |
Tcl_DStringAppend(&pathStr, *theString, -1);
|
sl@0
|
254 |
HUnlock(theString);
|
sl@0
|
255 |
DisposeHandle(theString);
|
sl@0
|
256 |
|
sl@0
|
257 |
Tcl_DStringInit(&tagPathStr);
|
sl@0
|
258 |
Tcl_DStringAppend(&tagPathStr, prefixTag, strlen(prefixTag));
|
sl@0
|
259 |
Tcl_DStringAppend(&tagPathStr, pathStr.string, pathStr.length);
|
sl@0
|
260 |
Tcl_DStringFree(&pathStr);
|
sl@0
|
261 |
|
sl@0
|
262 |
/*
|
sl@0
|
263 |
* Make sure the path ends with a ':'
|
sl@0
|
264 |
*/
|
sl@0
|
265 |
if (tagPathStr.string[tagPathStr.length - 1] != ':') {
|
sl@0
|
266 |
Tcl_DStringAppend(&tagPathStr, ":", 1);
|
sl@0
|
267 |
}
|
sl@0
|
268 |
|
sl@0
|
269 |
/*
|
sl@0
|
270 |
* Don't free tagPathStr - rather make sure it's allocated
|
sl@0
|
271 |
* and return it as the result.
|
sl@0
|
272 |
*/
|
sl@0
|
273 |
if (tagPathStr.string == tagPathStr.staticSpace) {
|
sl@0
|
274 |
result = (char *) ckalloc(tagPathStr.length + 1);
|
sl@0
|
275 |
strcpy(result, tagPathStr.string);
|
sl@0
|
276 |
} else {
|
sl@0
|
277 |
result = tagPathStr.string;
|
sl@0
|
278 |
}
|
sl@0
|
279 |
} else {
|
sl@0
|
280 |
result = (char *) ckalloc(strlen(prefixTag) + 1);
|
sl@0
|
281 |
strcpy(result, prefixTag);
|
sl@0
|
282 |
}
|
sl@0
|
283 |
|
sl@0
|
284 |
return result;
|
sl@0
|
285 |
}
|
sl@0
|
286 |
|
sl@0
|
287 |
/*
|
sl@0
|
288 |
*----------------------------------------------------------------------
|
sl@0
|
289 |
*
|
sl@0
|
290 |
* PathVariables --
|
sl@0
|
291 |
*
|
sl@0
|
292 |
* Creates environment variables from the system call FSpFindFolder.
|
sl@0
|
293 |
* The function generates environment variables for many of the
|
sl@0
|
294 |
* commonly used paths on the Macintosh.
|
sl@0
|
295 |
*
|
sl@0
|
296 |
* Results:
|
sl@0
|
297 |
* ptr to value on success, NULL if error.
|
sl@0
|
298 |
*
|
sl@0
|
299 |
* Side effects:
|
sl@0
|
300 |
* Memory is allocated and returned to the caller.
|
sl@0
|
301 |
*
|
sl@0
|
302 |
*----------------------------------------------------------------------
|
sl@0
|
303 |
*/
|
sl@0
|
304 |
|
sl@0
|
305 |
static char **
|
sl@0
|
306 |
PathVariables()
|
sl@0
|
307 |
{
|
sl@0
|
308 |
int i = 0;
|
sl@0
|
309 |
char **sysEnv;
|
sl@0
|
310 |
char *thePath = NULL;
|
sl@0
|
311 |
|
sl@0
|
312 |
sysEnv = (char **) ckalloc((12) * sizeof(char *));
|
sl@0
|
313 |
|
sl@0
|
314 |
sysEnv[i++] = MakeFolderEnvVar("PREF_FOLDER=", kPreferencesFolderType);
|
sl@0
|
315 |
sysEnv[i++] = MakeFolderEnvVar("SYS_FOLDER=", kSystemFolderType);
|
sl@0
|
316 |
sysEnv[i++] = MakeFolderEnvVar("TEMP=", kTemporaryFolderType);
|
sl@0
|
317 |
sysEnv[i++] = MakeFolderEnvVar("APPLE_M_FOLDER=", kAppleMenuFolderType);
|
sl@0
|
318 |
sysEnv[i++] = MakeFolderEnvVar("CP_FOLDER=", kControlPanelFolderType);
|
sl@0
|
319 |
sysEnv[i++] = MakeFolderEnvVar("DESK_FOLDER=", kDesktopFolderType);
|
sl@0
|
320 |
sysEnv[i++] = MakeFolderEnvVar("EXT_FOLDER=", kExtensionFolderType);
|
sl@0
|
321 |
sysEnv[i++] = MakeFolderEnvVar("PRINT_MON_FOLDER=",
|
sl@0
|
322 |
kPrintMonitorDocsFolderType);
|
sl@0
|
323 |
sysEnv[i++] = MakeFolderEnvVar("SHARED_TRASH_FOLDER=",
|
sl@0
|
324 |
kWhereToEmptyTrashFolderType);
|
sl@0
|
325 |
sysEnv[i++] = MakeFolderEnvVar("TRASH_FOLDER=", kTrashFolderType);
|
sl@0
|
326 |
sysEnv[i++] = MakeFolderEnvVar("START_UP_FOLDER=", kStartupFolderType);
|
sl@0
|
327 |
sysEnv[i++] = NULL;
|
sl@0
|
328 |
|
sl@0
|
329 |
return sysEnv;
|
sl@0
|
330 |
}
|
sl@0
|
331 |
|
sl@0
|
332 |
/*
|
sl@0
|
333 |
*----------------------------------------------------------------------
|
sl@0
|
334 |
*
|
sl@0
|
335 |
* SystemVariables --
|
sl@0
|
336 |
*
|
sl@0
|
337 |
* Creates environment variables from various Mac system calls.
|
sl@0
|
338 |
*
|
sl@0
|
339 |
* Results:
|
sl@0
|
340 |
* ptr to value on success, NULL if error.
|
sl@0
|
341 |
*
|
sl@0
|
342 |
* Side effects:
|
sl@0
|
343 |
* Memory is allocated and returned to the caller.
|
sl@0
|
344 |
*
|
sl@0
|
345 |
*----------------------------------------------------------------------
|
sl@0
|
346 |
*/
|
sl@0
|
347 |
|
sl@0
|
348 |
static char **
|
sl@0
|
349 |
SystemVariables()
|
sl@0
|
350 |
{
|
sl@0
|
351 |
int i = 0;
|
sl@0
|
352 |
char ** sysEnv;
|
sl@0
|
353 |
char * thePath = NULL;
|
sl@0
|
354 |
Handle theString = NULL;
|
sl@0
|
355 |
FSSpec currentDir;
|
sl@0
|
356 |
int size;
|
sl@0
|
357 |
|
sl@0
|
358 |
sysEnv = (char **) ckalloc((4) * sizeof(char *));
|
sl@0
|
359 |
|
sl@0
|
360 |
/*
|
sl@0
|
361 |
* Get user name from chooser. It will be assigned to both
|
sl@0
|
362 |
* the USER and LOGIN environment variables.
|
sl@0
|
363 |
*/
|
sl@0
|
364 |
thePath = GetUserName();
|
sl@0
|
365 |
if (thePath != NULL) {
|
sl@0
|
366 |
sysEnv[i] = (char *) ckalloc(strlen(kLoginnameTag) + strlen(thePath) + 1);
|
sl@0
|
367 |
strcpy(sysEnv[i], kLoginnameTag);
|
sl@0
|
368 |
strcpy(sysEnv[i]+strlen(kLoginnameTag), thePath);
|
sl@0
|
369 |
i++;
|
sl@0
|
370 |
sysEnv[i] = (char *) ckalloc(strlen(kUsernameTag) + strlen(thePath) + 1);
|
sl@0
|
371 |
strcpy(sysEnv[i], kUsernameTag);
|
sl@0
|
372 |
strcpy(sysEnv[i]+strlen(kUsernameTag), thePath);
|
sl@0
|
373 |
i++;
|
sl@0
|
374 |
}
|
sl@0
|
375 |
|
sl@0
|
376 |
/*
|
sl@0
|
377 |
* Get 'home' directory
|
sl@0
|
378 |
*/
|
sl@0
|
379 |
#ifdef kDefaultDirTag
|
sl@0
|
380 |
FSpGetDefaultDir(¤tDir);
|
sl@0
|
381 |
FSpPathFromLocation(¤tDir, &size, &theString);
|
sl@0
|
382 |
HLock(theString);
|
sl@0
|
383 |
sysEnv[i] = (char *) ckalloc(strlen(kDefaultDirTag) + size + 4);
|
sl@0
|
384 |
strcpy(sysEnv[i], kDefaultDirTag);
|
sl@0
|
385 |
strncpy(sysEnv[i]+strlen(kDefaultDirTag) , *theString, size);
|
sl@0
|
386 |
if (sysEnv[i][strlen(kDefaultDirTag) + size - 1] != ':') {
|
sl@0
|
387 |
sysEnv[i][strlen(kDefaultDirTag) + size] = ':';
|
sl@0
|
388 |
sysEnv[i][strlen(kDefaultDirTag) + size + 1] = '\0';
|
sl@0
|
389 |
} else {
|
sl@0
|
390 |
sysEnv[i][strlen(kDefaultDirTag) + size] = '\0';
|
sl@0
|
391 |
}
|
sl@0
|
392 |
HUnlock(theString);
|
sl@0
|
393 |
DisposeHandle(theString);
|
sl@0
|
394 |
i++;
|
sl@0
|
395 |
#endif
|
sl@0
|
396 |
|
sl@0
|
397 |
sysEnv[i++] = NULL;
|
sl@0
|
398 |
return sysEnv;
|
sl@0
|
399 |
}
|
sl@0
|
400 |
|
sl@0
|
401 |
/*
|
sl@0
|
402 |
*----------------------------------------------------------------------
|
sl@0
|
403 |
*
|
sl@0
|
404 |
* TclMacCreateEnv --
|
sl@0
|
405 |
*
|
sl@0
|
406 |
* This function allocates and populates the global "environ"
|
sl@0
|
407 |
* variable. Entries are in traditional Unix format but variables
|
sl@0
|
408 |
* are, hopefully, a bit more relevant for the Macintosh.
|
sl@0
|
409 |
*
|
sl@0
|
410 |
* Results:
|
sl@0
|
411 |
* The number of elements in the newly created environ array.
|
sl@0
|
412 |
*
|
sl@0
|
413 |
* Side effects:
|
sl@0
|
414 |
* Memory is allocated and pointed too by the environ variable.
|
sl@0
|
415 |
*
|
sl@0
|
416 |
*----------------------------------------------------------------------
|
sl@0
|
417 |
*/
|
sl@0
|
418 |
|
sl@0
|
419 |
int
|
sl@0
|
420 |
TclMacCreateEnv()
|
sl@0
|
421 |
{
|
sl@0
|
422 |
char ** sysEnv = NULL;
|
sl@0
|
423 |
char ** pathEnv = NULL;
|
sl@0
|
424 |
char ** fileEnv = NULL;
|
sl@0
|
425 |
char ** rezEnv = NULL;
|
sl@0
|
426 |
int count = 0;
|
sl@0
|
427 |
int i, j;
|
sl@0
|
428 |
|
sl@0
|
429 |
sysEnv = SystemVariables();
|
sl@0
|
430 |
if (sysEnv != NULL) {
|
sl@0
|
431 |
for (i = 0; sysEnv[i] != NULL; count++, i++) {
|
sl@0
|
432 |
/* Empty Loop */
|
sl@0
|
433 |
}
|
sl@0
|
434 |
}
|
sl@0
|
435 |
|
sl@0
|
436 |
pathEnv = PathVariables();
|
sl@0
|
437 |
if (pathEnv != NULL) {
|
sl@0
|
438 |
for (i = 0; pathEnv[i] != NULL; count++, i++) {
|
sl@0
|
439 |
/* Empty Loop */
|
sl@0
|
440 |
}
|
sl@0
|
441 |
}
|
sl@0
|
442 |
|
sl@0
|
443 |
#ifdef kPrefsFile
|
sl@0
|
444 |
fileEnv = FileRCVariables();
|
sl@0
|
445 |
if (fileEnv != NULL) {
|
sl@0
|
446 |
for (i = 0; fileEnv[i] != NULL; count++, i++) {
|
sl@0
|
447 |
/* Empty Loop */
|
sl@0
|
448 |
}
|
sl@0
|
449 |
}
|
sl@0
|
450 |
#endif
|
sl@0
|
451 |
|
sl@0
|
452 |
#ifdef REZ_ENV
|
sl@0
|
453 |
rezEnv = RezRCVariables();
|
sl@0
|
454 |
if (rezEnv != NULL) {
|
sl@0
|
455 |
for (i = 0; rezEnv[i] != NULL; count++, i++) {
|
sl@0
|
456 |
/* Empty Loop */
|
sl@0
|
457 |
}
|
sl@0
|
458 |
}
|
sl@0
|
459 |
#endif
|
sl@0
|
460 |
|
sl@0
|
461 |
/*
|
sl@0
|
462 |
* Create environ variable
|
sl@0
|
463 |
*/
|
sl@0
|
464 |
environ = (char **) ckalloc((count + 1) * sizeof(char *));
|
sl@0
|
465 |
j = 0;
|
sl@0
|
466 |
|
sl@0
|
467 |
if (sysEnv != NULL) {
|
sl@0
|
468 |
for (i = 0; sysEnv[i] != NULL;)
|
sl@0
|
469 |
environ[j++] = sysEnv[i++];
|
sl@0
|
470 |
ckfree((char *) sysEnv);
|
sl@0
|
471 |
}
|
sl@0
|
472 |
|
sl@0
|
473 |
if (pathEnv != NULL) {
|
sl@0
|
474 |
for (i = 0; pathEnv[i] != NULL;)
|
sl@0
|
475 |
environ[j++] = pathEnv[i++];
|
sl@0
|
476 |
ckfree((char *) pathEnv);
|
sl@0
|
477 |
}
|
sl@0
|
478 |
|
sl@0
|
479 |
#ifdef kPrefsFile
|
sl@0
|
480 |
if (fileEnv != NULL) {
|
sl@0
|
481 |
for (i = 0; fileEnv[i] != NULL;)
|
sl@0
|
482 |
environ[j++] = fileEnv[i++];
|
sl@0
|
483 |
ckfree((char *) fileEnv);
|
sl@0
|
484 |
}
|
sl@0
|
485 |
#endif
|
sl@0
|
486 |
|
sl@0
|
487 |
#ifdef REZ_ENV
|
sl@0
|
488 |
if (rezEnv != NULL) {
|
sl@0
|
489 |
for (i = 0; rezEnv[i] != NULL;)
|
sl@0
|
490 |
environ[j++] = rezEnv[i++];
|
sl@0
|
491 |
ckfree((char *) rezEnv);
|
sl@0
|
492 |
}
|
sl@0
|
493 |
#endif
|
sl@0
|
494 |
|
sl@0
|
495 |
environ[j] = NULL;
|
sl@0
|
496 |
return j;
|
sl@0
|
497 |
}
|
sl@0
|
498 |
|
sl@0
|
499 |
/*
|
sl@0
|
500 |
*----------------------------------------------------------------------
|
sl@0
|
501 |
*
|
sl@0
|
502 |
* GetUserName --
|
sl@0
|
503 |
*
|
sl@0
|
504 |
* Get the user login name.
|
sl@0
|
505 |
*
|
sl@0
|
506 |
* Results:
|
sl@0
|
507 |
* ptr to static string, NULL if error.
|
sl@0
|
508 |
*
|
sl@0
|
509 |
* Side effects:
|
sl@0
|
510 |
* None.
|
sl@0
|
511 |
*
|
sl@0
|
512 |
*----------------------------------------------------------------------
|
sl@0
|
513 |
*/
|
sl@0
|
514 |
|
sl@0
|
515 |
static char *
|
sl@0
|
516 |
GetUserName()
|
sl@0
|
517 |
{
|
sl@0
|
518 |
static char buf[33];
|
sl@0
|
519 |
short refnum;
|
sl@0
|
520 |
Handle h;
|
sl@0
|
521 |
|
sl@0
|
522 |
refnum = CurResFile();
|
sl@0
|
523 |
UseResFile(0);
|
sl@0
|
524 |
h = GetResource('STR ', -16096);
|
sl@0
|
525 |
UseResFile(refnum);
|
sl@0
|
526 |
if (h == NULL) {
|
sl@0
|
527 |
return NULL;
|
sl@0
|
528 |
}
|
sl@0
|
529 |
|
sl@0
|
530 |
HLock(h);
|
sl@0
|
531 |
strncpy(buf, (*h)+1, **h);
|
sl@0
|
532 |
buf[**h] = '\0';
|
sl@0
|
533 |
HUnlock(h);
|
sl@0
|
534 |
ReleaseResource(h);
|
sl@0
|
535 |
return(buf[0] ? buf : NULL);
|
sl@0
|
536 |
}
|