@@ -1,7 +1,7 @@ /* - * Copyright (c) 2008-2021 Jonathan Schleifer + * Copyright (c) 2008-2022 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 @@ -64,15 +64,21 @@ # define RESOLV_CONF_PATH @"AmiTCP:db/resolv.conf" #else # define HOSTS_PATH @"/etc/hosts" # define RESOLV_CONF_PATH @"/etc/resolv.conf" #endif + +#ifndef HOST_NAME_MAX +# define HOST_NAME_MAX 255 +#endif #ifndef OF_WII static OFString * domainFromHostname(OFString *hostname) { + OFString *ret; + if (hostname == nil) return nil; @try { OFSocketAddressParseIP(hostname, 0); @@ -79,30 +85,32 @@ /* * If we are still here, the host name is a valid IP address. * We can't use that as local domain. */ - return nil; + ret = nil; } @catch (OFInvalidFormatException *e) { /* Not an IP address -> we can use it if it contains a dot. */ size_t pos = [hostname rangeOfString: @"."].location; - if (pos == OFNotFound) - return nil; - - return [hostname substringFromIndex: pos + 1]; + if (pos != OFNotFound) + ret = [hostname substringFromIndex: pos + 1]; + else + ret = nil; } + + return ret; } #endif #if !defined(OF_WII) && !defined(OF_MORPHOS) static OFString * obtainHostname(void) { - char hostname[256]; + char hostname[HOST_NAME_MAX + 1]; - if (gethostname(hostname, 256) != 0) + if (gethostname(hostname, HOST_NAME_MAX + 1) != 0) return nil; return [OFString stringWithCString: hostname encoding: [OFLocale encoding]]; }