1 /* getdomainname emulation for systems that doesn't have it. 2 3 Copyright (C) 2003, 2006, 2008, 2010-2021 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 /* IRIX, OSF/1, 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, IRIX, OSF/1 */ 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 int ret; 56 57 /* The third argument is a 'long', but the return value must fit in an 58 'int', therefore it's better to avoid arguments > INT_MAX. */ 59 ret = sysinfo (SI_SRPC_DOMAIN, name, len > INT_MAX ? INT_MAX : len); 60 if (ret < 0) 61 /* errno is set here. */ 62 return -1; 63 if (ret > len) 64 { 65 errno = EINVAL; 66 return -1; 67 } 68 return 0; 69 #else /* HP-UX, Cygwin, mingw */ 70 const char *result = ""; /* Hardcode your domain name if you want. */ 71 size_t result_len = strlen (result); 72 73 if (result_len > len) 74 { 75 errno = EINVAL; 76 return -1; 77 } 78 memcpy (name, result, result_len); 79 if (result_len < len) 80 name[result_len] = '\0'; 81 return 0; 82 #endif 83 }