Index: src/OFApplication.m ================================================================== --- src/OFApplication.m +++ src/OFApplication.m @@ -31,10 +31,12 @@ #import "autorelease.h" #if defined(__MACH__) && !defined(OF_IOS) # include +#elif defined(_WIN32) +# include #elif !defined(OF_IOS) extern char **environ; #endif static OFApplication *app = nil; @@ -121,23 +123,66 @@ @try { void *pool; #if defined(__MACH__) && !defined(OF_IOS) char **env = *_NSGetEnviron(); +#elif defined(__WIN32) + uint16_t *env; #elif !defined(OF_IOS) char **env = environ; #else char *env; #endif environment = [[OFMutableDictionary alloc] init]; atexit(atexit_handler); -#ifndef OF_IOS +#if defined(_WIN32) + env = GetEnvironmentStringsW(); + + while (*env != 0) { + OFString *tmp, *key, *value; + size_t length, pos; + + pool = objc_autoreleasePoolPush(); + + length = of_string_utf16_length(env); + tmp = [OFString stringWithUTF16String: env + length: length]; + env += length + 1; + + /* + * cmd.exe seems to add some special variables which + * start with a "=", even though variable names are not + * allowed to contain a "=". + */ + if ([tmp hasPrefix: @"="]) { + objc_autoreleasePoolPop(pool); + continue; + } + + pos = [tmp rangeOfString: @"="].location; + if (pos == OF_NOT_FOUND) { + fprintf(stderr, "Warning: Invalid environment " + "variable: %s\n", [tmp UTF8String]); + continue; + } + + key = [tmp substringWithRange: of_range(0, pos)]; + value = [tmp substringWithRange: + of_range(pos + 1, [tmp length] - pos - 1)]; + + [environment setObject: value + forKey: key]; + + objc_autoreleasePoolPop(pool); + } + + FreeEnvironmentStringsW(env); +#elif !defined(OF_IOS) for (; *env != NULL; env++) { - OFString *key; - OFString *value; + OFString *key, *value; char *sep; pool = objc_autoreleasePoolPush(); if ((sep = strchr(*env, '=')) == NULL) { @@ -151,10 +196,11 @@ encoding: OF_STRING_ENCODING_NATIVE length: sep - *env]; value = [OFString stringWithCString: sep + 1 encoding: OF_STRING_ENCODING_NATIVE]; + [environment setObject: value forKey: key]; objc_autoreleasePoolPop(pool); }