Index: src/OFApplication.h ================================================================== --- src/OFApplication.h +++ src/OFApplication.h @@ -11,11 +11,13 @@ #import "OFObject.h" @class OFString; @class OFArray; +@class OFDictionary; @class OFMutableArray; +@class OFMutableDictionary; #define OF_APPLICATION_DELEGATE(cls) \ int \ main(int argc, char *argv[]) \ { \ @@ -43,16 +45,18 @@ */ @interface OFApplication: OFObject { OFString *programName; OFMutableArray *arguments; + OFMutableDictionary *environment; id delegate; } #ifdef OF_HAVE_PROPERTIES @property (readonly, retain) OFString *programName; @property (readonly, retain) OFArray *arguments; +@property (readonly, retain) OFDictionary *environment; @property (retain) id delegate; #endif /** * \return The only OFApplication instance in the application @@ -67,10 +71,15 @@ /** * \return The arguments passed to the application */ + (OFArray*)arguments; +/** + * \return The environment of the application + */ ++ (OFDictionary*)environment; + /** * Terminates the application. */ + (void)terminate; @@ -100,10 +109,15 @@ /** * \return The arguments passed to the application */ - (OFArray*)arguments; +/** + * \return The environment of the application + */ +- (OFDictionary*)environment; + /** * \return The delegate of the application */ - (id)delegate; Index: src/OFApplication.m ================================================================== --- src/OFApplication.m +++ src/OFApplication.m @@ -12,16 +12,25 @@ #include "config.h" #include #import "OFApplication.h" -#import "OFArray.h" #import "OFString.h" +#import "OFArray.h" +#import "OFDictionary.h" #import "OFAutoreleasePool.h" #import "OFExceptions.h" -OFApplication *app = nil; +#include + +#ifdef __MACH__ +# include +#else + extern char **environ; +#endif + +static OFApplication *app = nil; static void atexit_handler() { id delegate = [app delegate]; @@ -67,10 +76,15 @@ + (OFArray*)arguments { return [app arguments]; } + ++ (OFDictionary*)environment +{ + return [app environment]; +} + (void)terminate { exit(0); } @@ -80,13 +94,45 @@ exit(status); } - init { + OFAutoreleasePool *pool; + char **env; + self = [super init]; + environment = [[OFMutableDictionary alloc] init]; + atexit(atexit_handler); +#ifdef __MACH__ + env = *_NSGetEnviron(); +#else + env = environ; +#endif + + pool = [[OFAutoreleasePool alloc] init]; + for (; *env != NULL; env++) { + OFString *key; + OFString *value; + char *sep; + + if ((sep = strchr(*env, '=')) == NULL) { + fprintf(stderr, "Warning: Invalid environment " + "variable: %s\n", *env); + continue; + } + + key = [OFString stringWithCString: *env + length: sep - *env]; + value = [OFString stringWithCString: sep + 1]; + [environment setObject: value + forKey: key]; + + [pool releaseObjects]; + } + [pool release]; return self; } - (void)setArgumentCount: (int)argc @@ -114,10 +160,15 @@ - (OFArray*)arguments { return [[arguments retain] autorelease]; } + +- (OFDictionary*)environment +{ + return [[environment retain] autorelease]; +} - (id)delegate { return [[delegate retain] autorelease]; } @@ -145,10 +196,11 @@ } - (void)dealloc { [arguments release]; + [environment release]; [delegate release]; [super dealloc]; } @end