Index: src/OFApplication.m ================================================================== --- src/OFApplication.m +++ src/OFApplication.m @@ -229,11 +229,11 @@ if (env != NULL) { for (; *env != NULL; env++) { OFString *key, *value; char *sep; const of_string_encoding_t encoding = - OF_STRING_ENCODING_NATIVE; + [OFString nativeOSEncoding]; pool = objc_autoreleasePoolPush(); if ((sep = strchr(*env, '=')) == NULL) { fprintf(stderr, "Warning: Invalid " @@ -326,23 +326,24 @@ { #ifndef _WIN32 void *pool = objc_autoreleasePoolPush(); OFMutableArray *arguments; int i; + of_string_encoding_t encoding; _argc = argc; _argv = argv; - _programName = [[OFString alloc] - initWithCString: (*argv)[0] - encoding: OF_STRING_ENCODING_NATIVE]; + encoding = [OFString nativeOSEncoding]; + + _programName = [[OFString alloc] initWithCString: (*argv)[0] + encoding: encoding]; arguments = [[OFMutableArray alloc] init]; for (i = 1; i < *argc; i++) - [arguments addObject: - [OFString stringWithCString: (*argv)[i] - encoding: OF_STRING_ENCODING_NATIVE]]; + [arguments addObject: [OFString stringWithCString: (*argv)[i] + encoding: encoding]]; [arguments makeImmutable]; _arguments = arguments; objc_autoreleasePoolPop(pool); Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -110,11 +110,11 @@ of_stat(OFString *path, of_stat_t *buffer) { #ifdef _WIN32 return _wstat([path UTF16String], buffer); #else - return stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], + return stat([path cStringWithEncoding: [OFString nativeOSEncoding]], buffer); #endif } int @@ -121,14 +121,14 @@ of_lstat(OFString *path, of_stat_t *buffer) { #if defined(_WIN32) return _wstat([path UTF16String], buffer); #elif defined(HAVE_LSTAT) - return lstat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], + return lstat([path cStringWithEncoding: [OFString nativeOSEncoding]], buffer); #else - return stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], + return stat([path cStringWithEncoding: [OFString nativeOSEncoding]], buffer); #endif } static int @@ -204,11 +204,11 @@ #endif @try { #ifndef _WIN32 ret = [OFString stringWithCString: buffer - encoding: OF_STRING_ENCODING_NATIVE]; + encoding: [OFString nativeOSEncoding]]; #else ret = [OFString stringWithUTF16String: buffer]; #endif } @finally { free(buffer); @@ -271,11 +271,11 @@ { if (path == nil) @throw [OFInvalidArgumentException exception]; #ifndef _WIN32 - if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], + if (mkdir([path cStringWithEncoding: [OFString nativeOSEncoding]], DIR_MODE)) #else if (_wmkdir([path UTF16String])) #endif @throw [OFCreateDirectoryFailedException @@ -326,22 +326,23 @@ } + (OFArray*)contentsOfDirectoryAtPath: (OFString*)path { OFMutableArray *files; + of_string_encoding_t encoding; if (path == nil) @throw [OFInvalidArgumentException exception]; files = [OFMutableArray array]; + encoding = [OFString nativeOSEncoding]; #ifndef _WIN32 DIR *dir; struct dirent *dirent; - if ((dir = opendir([path cStringWithEncoding: - OF_STRING_ENCODING_NATIVE])) == NULL) + if ((dir = opendir([path cStringWithEncoding: encoding])) == NULL) @throw [OFOpenFileFailedException exceptionWithPath: path mode: @"r"]; @try { while ((dirent = readdir(dir)) != NULL) { @@ -350,13 +351,12 @@ if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..")) continue; - file = [OFString - stringWithCString: dirent->d_name - encoding: OF_STRING_ENCODING_NATIVE]; + file = [OFString stringWithCString: dirent->d_name + encoding: encoding]; [files addObject: file]; objc_autoreleasePoolPop(pool); } } @finally { @@ -404,11 +404,11 @@ { if (path == nil) @throw [OFInvalidArgumentException exception]; #ifndef _WIN32 - if (chdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) + if (chdir([path cStringWithEncoding: [OFString nativeOSEncoding]])) #else if (_wchdir([path UTF16String])) #endif @throw [OFChangeCurrentDirectoryPathFailedException exceptionWithPath: path]; @@ -452,11 +452,11 @@ { if (path == nil) @throw [OFInvalidArgumentException exception]; # ifndef _WIN32 - if (chmod([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], + if (chmod([path cStringWithEncoding: [OFString nativeOSEncoding]], permissions)) # else if (_wchmod([path UTF16String], permissions)) # endif @throw [OFChangePermissionsFailedException @@ -470,13 +470,16 @@ owner: (OFString*)owner group: (OFString*)group { uid_t uid = -1; gid_t gid = -1; + of_string_encoding_t encoding; if (path == nil || (owner == nil && group == nil)) @throw [OFInvalidArgumentException exception]; + + encoding = [OFString nativeOSEncoding]; # ifdef OF_HAVE_THREADS if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; @@ -483,12 +486,12 @@ @try { # endif if (owner != nil) { struct passwd *passwd; - if ((passwd = getpwnam([owner cStringWithEncoding: - OF_STRING_ENCODING_NATIVE])) == NULL) + if ((passwd = getpwnam([owner + cStringWithEncoding: encoding])) == NULL) @throw [OFChangeOwnerFailedException exceptionWithPath: path owner: owner group: group]; @@ -496,12 +499,12 @@ } if (group != nil) { struct group *group_; - if ((group_ = getgrnam([group cStringWithEncoding: - OF_STRING_ENCODING_NATIVE])) == NULL) + if ((group_ = getgrnam([group + cStringWithEncoding: encoding])) == NULL) @throw [OFChangeOwnerFailedException exceptionWithPath: path owner: owner group: group]; @@ -512,12 +515,11 @@ if (!of_mutex_unlock(&mutex)) @throw [OFUnlockFailedException exception]; } # endif - if (chown([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], - uid, gid)) + if (chown([path cStringWithEncoding: encoding], uid, gid)) @throw [OFChangeOwnerFailedException exceptionWithPath: path owner: owner group: group]; } #endif @@ -642,10 +644,11 @@ + (void)moveItemAtPath: (OFString*)source toPath: (OFString*)destination { void *pool; of_stat_t s; + of_string_encoding_t encoding; if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); @@ -655,13 +658,15 @@ @throw [OFCopyItemFailedException exceptionWithSourcePath: source destinationPath: destination]; } + encoding = [OFString nativeOSEncoding]; + #ifndef _WIN32 - if (rename([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE], - [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) { + if (rename([source cStringWithEncoding: encoding], + [destination cStringWithEncoding: encoding])) { #else if (_wrename([source UTF16String], [destination UTF16String])) { #endif if (errno != EXDEV) @throw [OFMoveItemFailedException @@ -725,11 +730,11 @@ objc_autoreleasePoolPop(pool2); } } #ifndef _WIN32 - if (remove([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) + if (remove([path cStringWithEncoding: [OFString nativeOSEncoding]])) #else if (_wremove([path UTF16String])) #endif @throw [OFRemoveItemFailedException exceptionWithPath: path]; @@ -739,18 +744,20 @@ #ifdef OF_HAVE_LINK + (void)linkItemAtPath: (OFString*)source toPath: (OFString*)destination { void *pool; + of_string_encoding_t encoding; if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); + encoding = [OFString nativeOSEncoding]; - if (link([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE], - [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0) + if (link([source cStringWithEncoding: encoding], + [destination cStringWithEncoding: encoding]) != 0) @throw [OFLinkFailedException exceptionWithSourcePath: source destinationPath: destination]; objc_autoreleasePoolPop(pool); @@ -760,18 +767,20 @@ #ifdef OF_HAVE_SYMLINK + (void)createSymbolicLinkAtPath: (OFString*)destination withDestinationPath: (OFString*)source { void *pool; + of_string_encoding_t encoding; if (source == nil || destination == nil) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); + encoding = [OFString nativeOSEncoding]; - if (symlink([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE], - [destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0) + if (symlink([source cStringWithEncoding: encoding], + [destination cStringWithEncoding: encoding]) != 0) @throw [OFCreateSymbolicLinkFailedException exceptionWithSourcePath: source destinationPath: destination]; objc_autoreleasePoolPop(pool); @@ -779,23 +788,25 @@ + (OFString*)destinationOfSymbolicLinkAtPath: (OFString*)path { char destination[PATH_MAX]; ssize_t length; + of_string_encoding_t encoding; if (path == nil) @throw [OFInvalidArgumentException exception]; - length = readlink([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], + encoding = [OFString nativeOSEncoding]; + length = readlink([path cStringWithEncoding: encoding], destination, PATH_MAX); if (length < 0) @throw [OFOpenFileFailedException exceptionWithPath: path mode: @"r"]; return [OFString stringWithCString: destination - encoding: OF_STRING_ENCODING_NATIVE + encoding: encoding length: length]; } #endif - init @@ -814,11 +825,11 @@ if ((flags = parseMode([mode UTF8String])) == -1) @throw [OFInvalidArgumentException exception]; #ifndef _WIN32 if ((_fd = open([path cStringWithEncoding: - OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1) + [OFString nativeOSEncoding]], flags, DEFAULT_MODE)) == -1) #else if ((_fd = _wopen([path UTF16String], flags, DEFAULT_MODE)) == -1) #endif @throw [OFOpenFileFailedException Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -102,19 +102,19 @@ { OFString *description = [exception description]; OFArray *backtrace = nil; fprintf(stderr, "\nRuntime error: Unhandled exception:\n%s\n", - [description cStringWithEncoding: OF_STRING_ENCODING_NATIVE]); + [description cStringWithEncoding: [OFString nativeOSEncoding]]); if ([exception respondsToSelector: @selector(backtrace)]) backtrace = [exception backtrace]; if (backtrace != nil) { OFString *s = [backtrace componentsJoinedByString: @"\n "]; fprintf(stderr, "\nBacktrace:\n %s\n\n", - [s cStringWithEncoding: OF_STRING_ENCODING_NATIVE]); + [s cStringWithEncoding: [OFString nativeOSEncoding]]); } abort(); } #endif @@ -203,11 +203,11 @@ const char* _NSPrintForDebugger(id object) { return [[object description] - cStringWithEncoding: OF_STRING_ENCODING_NATIVE]; + cStringWithEncoding: [OFString nativeOSEncoding]]; } /* References for static linking */ void _references_to_categories_of_OFObject(void) { Index: src/OFPlugin.m ================================================================== --- src/OFPlugin.m +++ src/OFPlugin.m @@ -45,11 +45,11 @@ path = [path stringByAppendingString: @PLUGIN_SUFFIX]; #ifndef _WIN32 if ((handle = dlopen([path cStringWithEncoding: - OF_STRING_ENCODING_NATIVE], RTLD_LAZY)) == NULL) + [OFString nativeOSEncoding]], RTLD_LAZY)) == NULL) #else if ((handle = LoadLibraryW([path UTF16String])) == NULL) #endif @throw [OFInitializationFailedException exceptionWithClass: self]; Index: src/OFProcess.m ================================================================== --- src/OFProcess.m +++ src/OFProcess.m @@ -138,21 +138,23 @@ switch ((_pid = fork())) { case 0:; OFString **objects = [arguments objects]; size_t i, count = [arguments count]; char **argv; + of_string_encoding_t encoding; argv = [self allocMemoryWithSize: sizeof(char*) count: count + 2]; - argv[0] = (char*)[programName cStringWithEncoding: - OF_STRING_ENCODING_NATIVE]; + encoding = [OFString nativeOSEncoding]; + + argv[0] = (char*)[programName + cStringWithEncoding: encoding]; for (i = 0; i < count; i++) argv[i + 1] = (char*)[objects[i] - cStringWithEncoding: - OF_STRING_ENCODING_NATIVE]; + cStringWithEncoding: encoding]; argv[i + 1] = NULL; if (environment != nil) { #ifdef __MACH__ @@ -166,12 +168,11 @@ close(_readPipe[0]); close(_writePipe[1]); dup2(_writePipe[0], 0); dup2(_readPipe[1], 1); - execvp([program cStringWithEncoding: - OF_STRING_ENCODING_NATIVE], argv); + execvp([program cStringWithEncoding: encoding], argv); @throw [OFInitializationFailedException exceptionWithClass: [self class]]; case -1: @throw [OFInitializationFailedException @@ -302,13 +303,16 @@ - (char**)OF_environmentForDictionary: (OFDictionary*)environment { OFEnumerator *keyEnumerator, *objectEnumerator; char **envp; size_t i, count; + of_string_encoding_t encoding; if (environment == nil) return NULL; + + encoding = [OFString nativeOSEncoding]; count = [environment count]; envp = [self allocMemoryWithSize: sizeof(char*) count: count + 1]; @@ -321,22 +325,19 @@ size_t keyLen, objectLen; key = [keyEnumerator nextObject]; object = [objectEnumerator nextObject]; - keyLen = [key cStringLengthWithEncoding: - OF_STRING_ENCODING_NATIVE]; - objectLen = [object cStringLengthWithEncoding: - OF_STRING_ENCODING_NATIVE]; + keyLen = [key cStringLengthWithEncoding: encoding]; + objectLen = [object cStringLengthWithEncoding: encoding]; envp[i] = [self allocMemoryWithSize: keyLen + objectLen + 2]; - memcpy(envp[i], [key cStringWithEncoding: - OF_STRING_ENCODING_NATIVE], keyLen); + memcpy(envp[i], [key cStringWithEncoding: encoding], keyLen); envp[i][keyLen] = '='; - memcpy(envp[i] + keyLen + 1, [object cStringWithEncoding: - OF_STRING_ENCODING_NATIVE], objectLen); + memcpy(envp[i] + keyLen + 1, + [object cStringWithEncoding: encoding], objectLen); envp[i][keyLen + objectLen + 1] = '\0'; } envp[i] = NULL; Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -65,13 +65,10 @@ enum { OF_STRING_SEARCH_BACKWARDS = 1, OF_STRING_SKIP_EMPTY = 2 }; -/* FIXME */ -#define OF_STRING_ENCODING_NATIVE OF_STRING_ENCODING_UTF_8 - #ifdef OF_HAVE_BLOCKS /*! * @brief A block for enumerating the lines of a string. * * @param line The current line @@ -325,10 +322,20 @@ * @param components An array of components for the path * @return A new autoreleased OFString */ + (OFString*)pathWithComponents: (OFArray*)components; +/*! + * @brief Returns the string encoding native to the operating system. + * + * This is useful to encode strings correctly for passing them to operating + * system calls. + * + * @return The string encoding native to the operating system + */ ++ (of_string_encoding_t)nativeOSEncoding; + /*! * @brief Initializes an already allocated OFString from a UTF-8 encoded C * string. * * @param UTF8String A UTF-8 encoded C string to initialize the OFString with Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -647,10 +647,16 @@ objc_autoreleasePoolPop(pool); return ret; } + ++ (of_string_encoding_t)nativeOSEncoding +{ + /* FIXME */ + return OF_STRING_ENCODING_UTF_8; +} - init { if (object_getClass(self) == [OFString class]) { @try { @@ -806,12 +812,12 @@ struct stat st; @try { OFFile *file; - if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], - &st) == -1) + if (stat([path cStringWithEncoding: [OFString + nativeOSEncoding]], &st) == -1) @throw [OFOpenFileFailedException exceptionWithPath: path mode: @"rb"]; if (st.st_size > SIZE_MAX) Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -363,12 +363,11 @@ hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICSERV; snprintf(portCString, 7, "%" PRIu16, port); - if (getaddrinfo([host cStringWithEncoding: OF_STRING_ENCODING_NATIVE], - portCString, &hints, &res0)) + if (getaddrinfo([host UTF8String], portCString, &hints, &res0)) @throw [OFAddressTranslationFailedException exceptionWithHost: host socket: self]; for (res = res0; res != NULL; res = res->ai_next) { @@ -397,12 +396,12 @@ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = OF_BSWAP16_IF_LE(port); - if ((addr.sin_addr.s_addr = inet_addr([host cStringWithEncoding: - OF_STRING_ENCODING_NATIVE])) != (in_addr_t)(-1)) { + if ((addr.sin_addr.s_addr = inet_addr([host UTF8String])) != + (in_addr_t)(-1)) { if ((_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { @throw [OFConnectionFailedException exceptionWithHost: host port: port @@ -429,12 +428,11 @@ # ifdef OF_HAVE_THREADS addrlist = [[OFDataArray alloc] initWithItemSize: sizeof(char**)]; [mutex lock]; # endif - if ((he = gethostbyname([host cStringWithEncoding: - OF_STRING_ENCODING_NATIVE])) == NULL) { + if ((he = gethostbyname([host UTF8String])) == NULL) { # ifdef OF_HAVE_THREADS [addrlist release]; [mutex unlock]; # endif @throw [OFAddressTranslationFailedException @@ -574,12 +572,11 @@ hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_NUMERICSERV | AI_PASSIVE; snprintf(portCString, 7, "%" PRIu16, port); - if (getaddrinfo([host cStringWithEncoding: OF_STRING_ENCODING_NATIVE], - portCString, &hints, &res)) + if (getaddrinfo([host UTF8String], portCString, &hints, &res)) @throw [OFAddressTranslationFailedException exceptionWithHost: host socket: self]; if ((_socket = socket(res->ai_family, SOCK_STREAM, @@ -605,20 +602,19 @@ #else memset(&addr, 0, sizeof(addr)); addr.in.sin_family = AF_INET; addr.in.sin_port = OF_BSWAP16_IF_LE(port); - if ((addr.in.sin_addr.s_addr = inet_addr([host cStringWithEncoding: - OF_STRING_ENCODING_NATIVE])) == (in_addr_t)(-1)) { + if ((addr.in.sin_addr.s_addr = inet_addr([host UTF8String])) == + (in_addr_t)(-1)) { # ifdef OF_HAVE_THREADS [mutex lock]; @try { # endif struct hostent *he; - if ((he = gethostbyname([host cStringWithEncoding: - OF_STRING_ENCODING_NATIVE])) == NULL) + if ((he = gethostbyname([host UTF8String])) == NULL) @throw [OFAddressTranslationFailedException exceptionWithHost: host socket: self]; if (he->h_addrtype != AF_INET || @@ -760,12 +756,11 @@ if (getnameinfo((struct sockaddr*)_sockAddr, _sockAddrLen, host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV)) @throw [OFAddressTranslationFailedException exceptionWithSocket: self]; - return [OFString stringWithCString: host - encoding: OF_STRING_ENCODING_NATIVE]; + return [OFString stringWithUTF8String: host]; } @finally { [self freeMemory: host]; } #else # ifdef OF_HAVE_THREADS @@ -777,12 +772,11 @@ if (host == NULL) @throw [OFAddressTranslationFailedException exceptionWithSocket: self]; - return [OFString stringWithCString: host - encoding: OF_STRING_ENCODING_NATIVE]; + return [OFString stringWithUTF8String: host]; # ifdef OF_HAVE_THREADS } @finally { [mutex unlock]; } # endif