diff --git a/ChangeLog b/ChangeLog index e5a08f7e7..ffa0e6cc9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2005-11-15 Richard Frith-Macdonald + + * Tools/gdnc.m: + * Source/GSPrivate.h: + * Source/NSException.m: + * Source/NSObject.m: + * Source/NSPathUtilities.m: + * Source/NSProcessInfo.m: + * Source/NSString.m: + * Source/NSUserDefaults.m: + Be consistent and access environment variables only via the + NSProcessInfo class ...so a program which uses a category to + override it can effectively change the environment safely. + 2005-11-14 Richard Frith-Macdonald * Source/win32/GSFileHandleWin32.m: Move mingw32 implementation of diff --git a/Source/GSPrivate.h b/Source/GSPrivate.h index 101873ba0..4d546f7b5 100644 --- a/Source/GSPrivate.h +++ b/Source/GSPrivate.h @@ -182,11 +182,6 @@ NSDictionary *GSUserDefaultsDictionaryRepresentation(void); */ BOOL GSUserDefaultsFlag(GSUserDefaultFlagType type); -/** - * Get a flag from an environment variable - return def if not defined. - */ -BOOL GSEnvironmentFlag(const char *name, BOOL def); - /** diff --git a/Source/NSException.m b/Source/NSException.m index 6d83c734f..f1436b0f7 100644 --- a/Source/NSException.m +++ b/Source/NSException.m @@ -31,6 +31,7 @@ #include "Foundation/NSCoder.h" #include "Foundation/NSThread.h" #include "Foundation/NSDictionary.h" +#include "Foundation/NSProcessInfo.h" #include /** @@ -88,7 +89,8 @@ static void _terminate() #else shouldAbort = NO; // exit() by default. #endif - shouldAbort = GSEnvironmentFlag("CRASH_ON_ABORT", shouldAbort); + shouldAbort = [[[[NSProcessInfo processInfo] environment] + objectForKey: @"CRASH_ON_ABORT"] boolValue]; if (shouldAbort == YES) { abort(); diff --git a/Source/NSObject.m b/Source/NSObject.m index 0f3c822e2..92bcfa41d 100644 --- a/Source/NSObject.m +++ b/Source/NSObject.m @@ -954,8 +954,6 @@ GSDescriptionForClassMethod(pcl self, SEL aSel) zombieMap = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks, NSNonOwnedPointerMapValueCallBacks, 0); zombieClass = [NSZombie class]; - NSZombieEnabled = GSEnvironmentFlag("NSZombieEnabled", NO); - NSDeallocateZombies = GSEnvironmentFlag("NSDeallocateZombies", NO); autorelease_class = [NSAutoreleasePool class]; autorelease_sel = @selector(addObject:); @@ -973,6 +971,11 @@ GSDescriptionForClassMethod(pcl self, SEL aSel) selector: @selector(_becomeMultiThreaded:) name: NSWillBecomeMultiThreadedNotification object: nil]; + + NSZombieEnabled = [[[[NSProcessInfo processInfo] environment] + objectForKey: @"NSZombieEnabled"] boolValue]; + NSDeallocateZombies = [[[[NSProcessInfo processInfo] environment] + objectForKey: @"NSDeallocateZombies"] boolValue]; } return; } diff --git a/Source/NSPathUtilities.m b/Source/NSPathUtilities.m index bbf5a515a..b6800c55b 100644 --- a/Source/NSPathUtilities.m +++ b/Source/NSPathUtilities.m @@ -872,21 +872,31 @@ NSUserName(void) #if defined(__WIN32__) if (theUserName == nil) { - const unichar *loginName = 0; - /* The GetUserName function returns the current user name */ - unichar buf[1024]; - DWORD n = 1024; - - if (GetEnvironmentVariableW(L"LOGNAME", buf, 1024) != 0 && buf[0] != '\0') - loginName = buf; - else if (GetUserNameW(buf, &n) != 0 && buf[0] != '\0') - loginName = buf; - if (loginName) - theUserName = [[NSString alloc] initWithCharacters: loginName - length: wcslen(loginName)]; + /* Use the LOGNAME environment variable if set. */ + theUserName = [[[NSProcessInfo processInfo] environment] + objectForKey: @"LOGNAME"]; + if ([theUserName length] > 0) + { + RETAIN(theUserName); + } else - [NSException raise: NSInternalInconsistencyException - format: @"Unable to determine current user name"]; + { + /* The GetUserName function returns the current user name */ + unichar buf[1024]; + DWORD n = 1024; + + if (GetUserNameW(buf, &n) != 0 && buf[0] != '\0') + { + theUserName = [[NSString alloc] initWithCharacters: buf + length: wcslen(buf)]; + } + else + { + theUserName = nil; + [NSException raise: NSInternalInconsistencyException + format: @"Unable to determine current user name"]; + } + } } #else /* Set olduid to some invalid uid that we could never start off running diff --git a/Source/NSProcessInfo.m b/Source/NSProcessInfo.m index 53e5fbb34..1992f1c3d 100644 --- a/Source/NSProcessInfo.m +++ b/Source/NSProcessInfo.m @@ -1178,34 +1178,6 @@ BOOL GSDebugSet(NSString *level) } -BOOL -GSEnvironmentFlag(const char *name, BOOL def) -{ - const char *c = getenv(name); - BOOL a = def; - - if (c != 0) - { - a = NO; - if ((c[0] == 'y' || c[0] == 'Y') && (c[1] == 'e' || c[1] == 'E') - && (c[2] == 's' || c[2] == 'S') && c[3] == 0) - { - a = YES; - } - else if ((c[0] == 't' || c[0] == 'T') && (c[1] == 'r' || c[1] == 'R') - && (c[2] == 'u' || c[2] == 'U') && (c[3] == 'e' || c[3] == 'E') - && c[4] == 0) - { - a = YES; - } - else if (isdigit(c[0]) && c[0] != '0') - { - a = YES; - } - } - return a; -} - /** * Used by NSException uncaught exception handler - must not call any * methods/functions which might cause a recursive exception. diff --git a/Source/NSString.m b/Source/NSString.m index d6b893835..0cd908d6e 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -57,6 +57,7 @@ #include "Foundation/NSFileManager.h" #include "Foundation/NSPortCoder.h" #include "Foundation/NSPathUtilities.h" +#include "Foundation/NSProcessInfo.h" #include "Foundation/NSRange.h" #include "Foundation/NSException.h" #include "Foundation/NSData.h" @@ -427,6 +428,8 @@ handle_printf_atsign (FILE *stream, if (self == [NSString class] && beenHere == NO) { + NSString *setting; + beenHere = YES; cMemberSel = @selector(characterIsMember:); caiSel = @selector(characterAtIndex:); @@ -435,17 +438,6 @@ handle_printf_atsign (FILE *stream, _DefaultStringEncoding = GetDefEncoding(); _ByteEncodingOk = GSIsByteEncoding(_DefaultStringEncoding); - if (getenv("GNUSTEP_PATH_HANDLING") != 0) - { - if (strcmp("unix", getenv("GNUSTEP_PATH_HANDLING")) == 0) - { - pathHandling = PH_UNIX; - } - else if (strcmp("windows", getenv("GNUSTEP_PATH_HANDLING")) == 0) - { - pathHandling = PH_WINDOWS; - } - } NSStringClass = self; [self setVersion: 1]; @@ -475,6 +467,20 @@ handle_printf_atsign (FILE *stream, [NSException raise: NSGenericException format: @"register printf handling of %%@ failed"]; #endif /* HAVE_REGISTER_PRINTF_FUNCTION */ + + setting = [[[NSProcessInfo processInfo] environment] + objectForKey: @"GNUSTEP_PATH_HANDLING"]; + if (setting != nil) + { + if ([setting isEqualToString: @"unix"] == YES) + { + pathHandling = PH_UNIX; + } + else if ([setting isEqualToString: @"windows"] == YES) + { + pathHandling = PH_WINDOWS; + } + } } } diff --git a/Source/NSUserDefaults.m b/Source/NSUserDefaults.m index 391877dca..2e06cda0e 100644 --- a/Source/NSUserDefaults.m +++ b/Source/NSUserDefaults.m @@ -627,27 +627,36 @@ static BOOL setSharedDefaults = NO; /* Flag to prevent infinite recursion */ #endif if (currLang == nil) { - const char *env_list; NSString *env; - env_list = getenv("LANGUAGES"); - if (env_list != 0) + env = [[[NSProcessInfo processInfo] environment] + objectForKey: @"LANGUAGES"]; + if (env != nil) { - env = [NSStringClass stringWithCString: env_list]; currLang = [env componentsSeparatedByString: @";"]; } } if (currLang != nil) { - if ([currLang containsObject: @""] == YES) - { - NSMutableArray *a = [currLang mutableCopy]; + NSMutableArray *a = [currLang mutableCopy]; + unsigned c = [a count]; - [a removeObject: @""]; - currLang = (NSArray*)AUTORELEASE(a); + while (c-- > 0) + { + NSString *s = [[a objectAtIndex: c] stringByTrimmingSpaces]; + + if ([s length] == 0) + { + [a removeObjectAtIndex: c]; + } + else + { + [a replaceObjectAtIndex: c withObject: s]; + } } - [userLanguages addObjectsFromArray: currLang]; + [userLanguages addObjectsFromArray: a]; + RELEASE(a); } /* Check if "English" is included. We do this to make sure all the diff --git a/Tools/gdnc.m b/Tools/gdnc.m index 517d27229..cf7e989a6 100644 --- a/Tools/gdnc.m +++ b/Tools/gdnc.m @@ -104,7 +104,7 @@ ihandler(int sig) { static BOOL beenHere = NO; BOOL action; - const char *e; + NSString *e; /* * Deal with recursive call of handler. @@ -128,17 +128,11 @@ ihandler(int sig) #else action = NO; // exit() by default. #endif - e = getenv("CRASH_ON_ABORT"); - if (e != 0) + e = [[[NSProcessInfo processInfo] environment] objectForKey: + @"CRASH_ON_ABORT"]; + if (e != nil) { - if (strcasecmp(e, "yes") == 0 || strcasecmp(e, "true") == 0) - action = YES; - else if (strcasecmp(e, "no") == 0 || strcasecmp(e, "false") == 0) - action = NO; - else if (isdigit(*e) && *e != '0') - action = YES; - else - action = NO; + action = [e boolValue]; } if (action == YES)