X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/1f2f436a38f7ae2d39a943ad2898d8fed4ed2e58..6dccf0e0b5e80b7b6176e8d332e646175431bb3d:/gen/FreeBSD/gethostname.c?ds=sidebyside diff --git a/gen/FreeBSD/gethostname.c b/gen/FreeBSD/gethostname.c index 65af716..031e2c8 100644 --- a/gen/FreeBSD/gethostname.c +++ b/gen/FreeBSD/gethostname.c @@ -33,8 +33,10 @@ static char sccsid[] = "@(#)gethostname.c 8.1 (Berkeley) 6/4/93"; #include __FBSDID("$FreeBSD: src/lib/libc/gen/gethostname.c,v 1.8 2007/01/09 00:27:54 imp Exp $"); +#include #include #include +#include #include #include @@ -48,10 +50,22 @@ gethostname(name, namelen) mib[0] = CTL_KERN; mib[1] = KERN_HOSTNAME; + if (namelen < MAXHOSTNAMELEN + 1) { + char local_buf[MAXHOSTNAMELEN + 1]; + size_t local_len = sizeof(local_buf); + if (sysctl(mib, 2, local_buf, &local_len, NULL, 0) == -1) { + if (errno == ENOMEM) + errno = ENAMETOOLONG; + return (-1); + } + strncpy(name, local_buf, namelen); + name[namelen - 1] = 0; + } else { if (sysctl(mib, 2, name, &namelen, NULL, 0) == -1) { if (errno == ENOMEM) errno = ENAMETOOLONG; return (-1); + } } return (0); }