Index: src/OFFileManager.m ================================================================== --- src/OFFileManager.m +++ src/OFFileManager.m @@ -33,10 +33,11 @@ #import "OFURL.h" #import "OFURLHandler.h" #import "OFChangeCurrentDirectoryPathFailedException.h" #import "OFCopyItemFailedException.h" +#import "OFGetCurrentDirectoryPathFailedException.h" #import "OFInvalidArgumentException.h" #import "OFMoveItemFailedException.h" #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" #import "OFRemoveItemFailedException.h" @@ -157,22 +158,18 @@ } return [OFString stringWithCString: buffer encoding: [OFLocalization encoding]]; #else - OFString *ret; - char *buffer = getcwd(NULL, 0); - - @try { - ret = [OFString - stringWithCString: buffer - encoding: [OFLocalization encoding]]; - } @finally { - free(buffer); - } - - return ret; + char buffer[PATH_MAX]; + + if ((getcwd(buffer, PATH_MAX)) == NULL) + @throw [OFGetCurrentDirectoryPathFailedException + exceptionWithErrNo: errno]; + + return [OFString stringWithCString: buffer + encoding: [OFLocalization encoding]]; #endif } - (OFURL *)currentDirectoryURL { Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -138,10 +138,13 @@ #endif #import "OFCopyItemFailedException.h" #import "OFCreateDirectoryFailedException.h" #import "OFCreateSymbolicLinkFailedException.h" #import "OFEnumerationMutationException.h" +#ifdef OF_HAVE_FILES +# import "OFGetCurrentDirectoryPathFailedException.h" +#endif #import "OFGetOptionFailedException.h" #import "OFHashAlreadyCalculatedException.h" #ifdef OF_HAVE_SOCKETS # import "OFHTTPRequestFailedException.h" #endif Index: src/exceptions/Makefile ================================================================== --- src/exceptions/Makefile +++ src/exceptions/Makefile @@ -45,13 +45,15 @@ OFUnknownXMLEntityException.m \ OFUnlockFailedException.m \ OFUnsupportedProtocolException.m \ OFUnsupportedVersionException.m \ OFWriteFailedException.m \ + ${USE_SRCS_FILES} \ ${USE_SRCS_SOCKETS} \ ${USE_SRCS_THREADS} \ ${USE_SRCS_PLUGINS} +SRCS_FILES = OFGetCurrentDirectoryPathFailedException.m SRCS_SOCKETS = OFAcceptFailedException.m \ OFAddressTranslationFailedException.m \ OFAlreadyConnectedException.m \ OFBindFailedException.m \ OFConnectionFailedException.m \ ADDED src/exceptions/OFGetCurrentDirectoryPathFailedException.h Index: src/exceptions/OFGetCurrentDirectoryPathFailedException.h ================================================================== --- src/exceptions/OFGetCurrentDirectoryPathFailedException.h +++ src/exceptions/OFGetCurrentDirectoryPathFailedException.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFException.h" + +OF_ASSUME_NONNULL_BEGIN + +/*! + * @class OFGetCurrentDirectoryPathFailedException \ + * OFGetCurrentDirectoryPathFailedException.h \ + * ObjFW/OFGetCurrentDirectoryPathFailedException.h + * + * @brief An exception indicating that getting the current directory path + * failed. + */ +@interface OFGetCurrentDirectoryPathFailedException: OFException +{ + int _errNo; +} + +/*! + * @brief The errno of the error that occurred. + */ +@property (readonly, nonatomic) int errNo; + ++ (instancetype)exception OF_UNAVAILABLE; + +/*! + * @brief Creates a new, autoreleased get current directory path failed + * exception. + * + * @param errNo The errno of the error that occurred + * @return A new, autoreleased get current directory failed exception + */ ++ (instancetype)exceptionWithErrNo: (int)errNo; + +- (instancetype)init OF_UNAVAILABLE; + +/*! + * @brief Initializes an already allocated get current directory path failed + * exception. + * + * @param errNo The errno of the error that occurred + * @return An initialized get current directory path failed exception + */ +- (instancetype)initWithErrNo: (int)errNo OF_DESIGNATED_INITIALIZER; +@end + +OF_ASSUME_NONNULL_END ADDED src/exceptions/OFGetCurrentDirectoryPathFailedException.m Index: src/exceptions/OFGetCurrentDirectoryPathFailedException.m ================================================================== --- src/exceptions/OFGetCurrentDirectoryPathFailedException.m +++ src/exceptions/OFGetCurrentDirectoryPathFailedException.m @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFGetCurrentDirectoryPathFailedException.h" +#import "OFString.h" + +@implementation OFGetCurrentDirectoryPathFailedException +@synthesize errNo = _errNo; + ++ (instancetype)exception +{ + OF_UNRECOGNIZED_SELECTOR +} + ++ (instancetype)exceptionWithErrNo: (int)errNo +{ + return [[[self alloc] initWithErrNo: errNo] autorelease]; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (instancetype)initWithErrNo: (int)errNo +{ + self = [super init]; + + _errNo = errNo; + + return self; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: + @"Getting the current directory path failed: %@", + of_strerror(_errNo)]; +} +@end Index: tests/OFURLTests.m ================================================================== --- tests/OFURLTests.m +++ tests/OFURLTests.m @@ -103,11 +103,11 @@ OFInvalidFormatException, [OFURL URLWithString: @"#`" relativeToURL: u1]) #ifdef OF_HAVE_FILES - TEST(@"+[fileURLWithPath:isDirectory:]", + TEST(@"+[fileURLWithPath:]", [[[OFURL fileURLWithPath: @"testfile.txt"] fileSystemRepresentation] isEqual: [[[OFFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent: @"testfile.txt"]]) #endif