os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/mac/tclMacEnv.c
First public contribution.
4 * Implements the "environment" on a Macintosh.
6 * Copyright (c) 1995-1996 Sun Microsystems, Inc.
8 * See the file "license.terms" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 * RCS: @(#) $Id: tclMacEnv.c,v 1.2 1998/09/14 18:40:04 stanton Exp $
16 #include <TextUtils.h>
17 #include <Resources.h>
22 #include "tclMacInt.h"
25 #define kMaxEnvStringSize 255
26 #define kMaxEnvVarSize 100
27 #define kLoginnameTag "LOGIN="
28 #define kUsernameTag "USER="
29 #define kDefaultDirTag "HOME="
32 * The following specifies a text file where additional environment variables
33 * can be set. The file must reside in the preferences folder. If the file
34 * doesn't exist NO error will occur. Commet out the difinition if you do
35 * NOT want to use an environment variables file.
37 #define kPrefsFile "Tcl Environment Variables"
40 * The following specifies the Name of a 'STR#' resource in the application
41 * where additional environment variables may be set. If the resource doesn't
42 * exist no errors will occur. Commet it out if you don't want it.
44 #define REZ_ENV "\pTcl Environment Variables"
47 char **environ = NULL;
50 * Declarations for local procedures defined in this file:
52 static char ** RezRCVariables _ANSI_ARGS_((void));
53 static char ** FileRCVariables _ANSI_ARGS_((void));
54 static char ** PathVariables _ANSI_ARGS_((void));
55 static char ** SystemVariables _ANSI_ARGS_((void));
56 static char * MakeFolderEnvVar _ANSI_ARGS_((char * prefixTag,
58 static char * GetUserName _ANSI_ARGS_((void));
61 *----------------------------------------------------------------------
65 * Creates environment variables from the applications resource fork.
66 * The function looks for the 'STR#' resource with the name defined
67 * in the #define REZ_ENV. If the define is not defined this code
68 * will not be included. If the resource doesn't exist or no strings
69 * reside in the resource nothing will happen.
72 * ptr to value on success, NULL if error.
75 * Memory is allocated and returned to the caller.
77 *----------------------------------------------------------------------
84 Handle envStrs = NULL;
88 envStrs = GetNamedResource('STR#', REZ_ENV);
89 if (envStrs == NULL) return NULL;
90 numStrs = *((short *) (*envStrs));
92 rezEnv = (char **) ckalloc((numStrs + 1) * sizeof(char *));
94 if (envStrs != NULL) {
97 short theID, index = 1;
101 GetResInfo(envStrs, &theID, &theType, theName);
103 GetIndString(theName, theID, index++);
104 if (theName[0] == '\0') break;
105 string = (char *) ckalloc(theName[0] + 2);
106 strncpy(string, (char *) theName + 1, theName[0]);
107 string[theName[0]] = '\0';
108 rezEnv[i++] = string;
110 ReleaseResource(envStrs);
121 *----------------------------------------------------------------------
125 * Creates environment variables from a file in the system preferences
126 * folder. The function looks for a file in the preferences folder
127 * a name defined in the #define kPrefsFile. If the define is not
128 * defined this code will not be included. If the resource doesn't exist or
129 * no strings reside in the resource nothing will happen.
132 * ptr to value on success, NULL if error.
135 * Memory is allocated and returned to the caller.
137 *----------------------------------------------------------------------
144 char *prefsFolder = NULL;
145 char *tempPtr = NULL;
146 char **fileEnv = NULL;
147 FILE *thePrefsFile = NULL;
151 Handle theString = NULL;
154 Tcl_DString lineRead;
156 err = FSpFindFolder(kOnSystemDisk, kPreferencesFolderType,
157 kDontCreateFolder, &prefDir);
161 err = FSpPathFromLocation(&prefDir, &size, &theString);
165 (void) Munger(theString, size, NULL, 0, kPrefsFile, strlen(kPrefsFile));
168 chan = Tcl_OpenFileChannel(NULL, *theString, "r", 0);
170 DisposeHandle(theString);
176 * We found a env file. Let start parsing it.
178 fileEnv = (char **) ckalloc((kMaxEnvVarSize + 1) * sizeof(char *));
181 Tcl_DStringInit(&lineRead);
182 while (Tcl_Gets(chan, &lineRead) != -1) {
184 * First strip off new line char
186 if (lineRead.string[lineRead.length-1] == '\n') {
187 lineRead.string[lineRead.length-1] = '\0';
189 if (lineRead.string[0] == '\0' || lineRead.string[0] == '#') {
191 * skip empty lines or commented lines
193 Tcl_DStringSetLength(&lineRead, 0);
197 tempPtr = (char *) ckalloc(lineRead.length + 1);
198 strcpy(tempPtr, lineRead.string);
199 fileEnv[i++] = tempPtr;
200 Tcl_DStringSetLength(&lineRead, 0);
204 Tcl_Close(NULL, chan);
205 Tcl_DStringFree(&lineRead);
212 *----------------------------------------------------------------------
214 * MakeFolderEnvVar --
216 * This function creates "environment" variable by taking a prefix and
217 * appending a folder path to a directory. The directory is specified
218 * by a integer value acceptable by the FindFolder function.
221 * The function returns an *allocated* string. If the folder doesn't
222 * exist the return string is still allocated and just contains the
226 * Memory is allocated and returned to the caller.
228 *----------------------------------------------------------------------
233 char * prefixTag, /* Prefix added before result. */
234 long whichFolder) /* Constant for FSpFindFolder. */
236 char * thePath = NULL;
237 char * result = NULL;
238 OSErr theErr = noErr;
239 Handle theString = NULL;
243 Tcl_DString tagPathStr;
245 Tcl_DStringInit(&pathStr);
246 theErr = FSpFindFolder(kOnSystemDisk, whichFolder,
247 kDontCreateFolder, &theFolder);
248 if (theErr == noErr) {
249 theErr = FSpPathFromLocation(&theFolder, &size, &theString);
252 tclPlatform = TCL_PLATFORM_MAC;
253 Tcl_DStringAppend(&pathStr, *theString, -1);
255 DisposeHandle(theString);
257 Tcl_DStringInit(&tagPathStr);
258 Tcl_DStringAppend(&tagPathStr, prefixTag, strlen(prefixTag));
259 Tcl_DStringAppend(&tagPathStr, pathStr.string, pathStr.length);
260 Tcl_DStringFree(&pathStr);
263 * Make sure the path ends with a ':'
265 if (tagPathStr.string[tagPathStr.length - 1] != ':') {
266 Tcl_DStringAppend(&tagPathStr, ":", 1);
270 * Don't free tagPathStr - rather make sure it's allocated
271 * and return it as the result.
273 if (tagPathStr.string == tagPathStr.staticSpace) {
274 result = (char *) ckalloc(tagPathStr.length + 1);
275 strcpy(result, tagPathStr.string);
277 result = tagPathStr.string;
280 result = (char *) ckalloc(strlen(prefixTag) + 1);
281 strcpy(result, prefixTag);
288 *----------------------------------------------------------------------
292 * Creates environment variables from the system call FSpFindFolder.
293 * The function generates environment variables for many of the
294 * commonly used paths on the Macintosh.
297 * ptr to value on success, NULL if error.
300 * Memory is allocated and returned to the caller.
302 *----------------------------------------------------------------------
310 char *thePath = NULL;
312 sysEnv = (char **) ckalloc((12) * sizeof(char *));
314 sysEnv[i++] = MakeFolderEnvVar("PREF_FOLDER=", kPreferencesFolderType);
315 sysEnv[i++] = MakeFolderEnvVar("SYS_FOLDER=", kSystemFolderType);
316 sysEnv[i++] = MakeFolderEnvVar("TEMP=", kTemporaryFolderType);
317 sysEnv[i++] = MakeFolderEnvVar("APPLE_M_FOLDER=", kAppleMenuFolderType);
318 sysEnv[i++] = MakeFolderEnvVar("CP_FOLDER=", kControlPanelFolderType);
319 sysEnv[i++] = MakeFolderEnvVar("DESK_FOLDER=", kDesktopFolderType);
320 sysEnv[i++] = MakeFolderEnvVar("EXT_FOLDER=", kExtensionFolderType);
321 sysEnv[i++] = MakeFolderEnvVar("PRINT_MON_FOLDER=",
322 kPrintMonitorDocsFolderType);
323 sysEnv[i++] = MakeFolderEnvVar("SHARED_TRASH_FOLDER=",
324 kWhereToEmptyTrashFolderType);
325 sysEnv[i++] = MakeFolderEnvVar("TRASH_FOLDER=", kTrashFolderType);
326 sysEnv[i++] = MakeFolderEnvVar("START_UP_FOLDER=", kStartupFolderType);
333 *----------------------------------------------------------------------
337 * Creates environment variables from various Mac system calls.
340 * ptr to value on success, NULL if error.
343 * Memory is allocated and returned to the caller.
345 *----------------------------------------------------------------------
353 char * thePath = NULL;
354 Handle theString = NULL;
358 sysEnv = (char **) ckalloc((4) * sizeof(char *));
361 * Get user name from chooser. It will be assigned to both
362 * the USER and LOGIN environment variables.
364 thePath = GetUserName();
365 if (thePath != NULL) {
366 sysEnv[i] = (char *) ckalloc(strlen(kLoginnameTag) + strlen(thePath) + 1);
367 strcpy(sysEnv[i], kLoginnameTag);
368 strcpy(sysEnv[i]+strlen(kLoginnameTag), thePath);
370 sysEnv[i] = (char *) ckalloc(strlen(kUsernameTag) + strlen(thePath) + 1);
371 strcpy(sysEnv[i], kUsernameTag);
372 strcpy(sysEnv[i]+strlen(kUsernameTag), thePath);
377 * Get 'home' directory
379 #ifdef kDefaultDirTag
380 FSpGetDefaultDir(¤tDir);
381 FSpPathFromLocation(¤tDir, &size, &theString);
383 sysEnv[i] = (char *) ckalloc(strlen(kDefaultDirTag) + size + 4);
384 strcpy(sysEnv[i], kDefaultDirTag);
385 strncpy(sysEnv[i]+strlen(kDefaultDirTag) , *theString, size);
386 if (sysEnv[i][strlen(kDefaultDirTag) + size - 1] != ':') {
387 sysEnv[i][strlen(kDefaultDirTag) + size] = ':';
388 sysEnv[i][strlen(kDefaultDirTag) + size + 1] = '\0';
390 sysEnv[i][strlen(kDefaultDirTag) + size] = '\0';
393 DisposeHandle(theString);
402 *----------------------------------------------------------------------
406 * This function allocates and populates the global "environ"
407 * variable. Entries are in traditional Unix format but variables
408 * are, hopefully, a bit more relevant for the Macintosh.
411 * The number of elements in the newly created environ array.
414 * Memory is allocated and pointed too by the environ variable.
416 *----------------------------------------------------------------------
422 char ** sysEnv = NULL;
423 char ** pathEnv = NULL;
424 char ** fileEnv = NULL;
425 char ** rezEnv = NULL;
429 sysEnv = SystemVariables();
430 if (sysEnv != NULL) {
431 for (i = 0; sysEnv[i] != NULL; count++, i++) {
436 pathEnv = PathVariables();
437 if (pathEnv != NULL) {
438 for (i = 0; pathEnv[i] != NULL; count++, i++) {
444 fileEnv = FileRCVariables();
445 if (fileEnv != NULL) {
446 for (i = 0; fileEnv[i] != NULL; count++, i++) {
453 rezEnv = RezRCVariables();
454 if (rezEnv != NULL) {
455 for (i = 0; rezEnv[i] != NULL; count++, i++) {
462 * Create environ variable
464 environ = (char **) ckalloc((count + 1) * sizeof(char *));
467 if (sysEnv != NULL) {
468 for (i = 0; sysEnv[i] != NULL;)
469 environ[j++] = sysEnv[i++];
470 ckfree((char *) sysEnv);
473 if (pathEnv != NULL) {
474 for (i = 0; pathEnv[i] != NULL;)
475 environ[j++] = pathEnv[i++];
476 ckfree((char *) pathEnv);
480 if (fileEnv != NULL) {
481 for (i = 0; fileEnv[i] != NULL;)
482 environ[j++] = fileEnv[i++];
483 ckfree((char *) fileEnv);
488 if (rezEnv != NULL) {
489 for (i = 0; rezEnv[i] != NULL;)
490 environ[j++] = rezEnv[i++];
491 ckfree((char *) rezEnv);
500 *----------------------------------------------------------------------
504 * Get the user login name.
507 * ptr to static string, NULL if error.
512 *----------------------------------------------------------------------
522 refnum = CurResFile();
524 h = GetResource('STR ', -16096);
531 strncpy(buf, (*h)+1, **h);
535 return(buf[0] ? buf : NULL);