| 1 |
/* getdomainname emulation for systems that doesn't have it. |
| 2 |
|
| 3 |
Copyright (C) 2003, 2006, 2008, 2010-2026 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This program is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation, either version 3 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Written by Simon Josefsson. */ |
| 19 |
|
| 20 |
#include <config.h> |
| 21 |
|
| 22 |
/* Specification. */ |
| 23 |
#include <unistd.h> |
| 24 |
|
| 25 |
#include <limits.h> |
| 26 |
#include <string.h> |
| 27 |
#include <errno.h> |
| 28 |
|
| 29 |
#if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H /* Solaris */ |
| 30 |
# include <sys/systeminfo.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
/* Return the NIS domain name of the machine. |
| 34 |
WARNING! The NIS domain name is unrelated to the fully qualified host name |
| 35 |
of the machine. It is also unrelated to email addresses. |
| 36 |
WARNING! The NIS domain name is usually the empty string or "(none)" when |
| 37 |
not using NIS. |
| 38 |
|
| 39 |
Put up to LEN bytes of the NIS domain name into NAME. |
| 40 |
Null terminate it if the name is shorter than LEN. |
| 41 |
If the NIS domain name is longer than LEN, set errno = EINVAL and return -1. |
| 42 |
Return 0 if successful, otherwise set errno and return -1. */ |
| 43 |
int |
| 44 |
getdomainname (char *name, size_t len) |
| 45 |
#undef getdomainname |
| 46 |
{ |
| 47 |
#if HAVE_GETDOMAINNAME /* Mac OS X, FreeBSD, AIX */ |
| 48 |
extern int getdomainname (char *, int); |
| 49 |
|
| 50 |
if (len > INT_MAX) |
| 51 |
len = INT_MAX; |
| 52 |
return getdomainname (name, (int) len); |
| 53 |
#elif HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H && defined SI_SRPC_DOMAIN |
| 54 |
/* Solaris */ |
| 55 |
/* The third argument is a 'long', but the return value must fit in an |
| 56 |
'int', therefore it's better to avoid arguments > INT_MAX. */ |
| 57 |
int ret = sysinfo (SI_SRPC_DOMAIN, name, len > INT_MAX ? INT_MAX : len); |
| 58 |
if (ret < 0) |
| 59 |
/* errno is set here. */ |
| 60 |
return -1; |
| 61 |
if (ret > len) |
| 62 |
{ |
| 63 |
errno = EINVAL; |
| 64 |
return -1; |
| 65 |
} |
| 66 |
return 0; |
| 67 |
#else /* HP-UX, Cygwin, mingw */ |
| 68 |
const char *result = ""; /* Hardcode your domain name if you want. */ |
| 69 |
size_t result_len = strlen (result); |
| 70 |
|
| 71 |
if (result_len > len) |
| 72 |
{ |
| 73 |
errno = EINVAL; |
| 74 |
return -1; |
| 75 |
} |
| 76 |
memcpy (name, result, result_len); |
| 77 |
if (result_len < len) |
| 78 |
name[result_len] = '\0'; |
| 79 |
return 0; |
| 80 |
#endif |
| 81 |
} |