diff lib/getdomainname.c @ 13911:e6744cae0421

getdomainname: Use the system function when possible. * lib/unistd.in.h: Include <netdb.h>, for getdomainname's declaration. (getdomainname): Replace if needed. Provide the declaration if it is missing. Don't use _GL_CXXALIAS_SYS_CAST. * lib/getdomainname.c: Include <limits.h> and <sys/systeminfo.h>. (getdomainname): When the system has getdomainname, call the system function. When sysinfo (SI_SRPC_DOMAIN, ...) is possible, use that. * m4/getdomainname.m4 (gl_FUNC_GETDOMAINNAME): Require gl_HEADER_SYS_SOCKET and gl_HEADER_NETDB. Test whether the function is found in libnsl. Look for the declaration also in <netdb.h>. Replace the function if its second argument is of type 'int' or if it is found in libnsl. (gl_PREREQ_GETDOMAINNAME): Define HAVE_GETDOMAINNAME. Check for <sys/systeminfo.h> and sysinfo(). * modules/getdomainname (Depends-on): Add netdb, sys_socket. * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize HAVE_DECL_GETDOMAINNAME and REPLACE_GETDOMAINNAME instead of HAVE_GETDOMAINNAME. * modules/unistd (Makefile.am): Substitute HAVE_DECL_GETDOMAINNAME and REPLACE_GETDOMAINNAME instead of HAVE_GETDOMAINNAME. * doc/glibc-functions/getdomainname.texi: Document the problems with the getdomainname declaration.
author Bruno Haible <bruno@clisp.org>
date Tue, 30 Nov 2010 21:27:21 +0100
parents c2cbabec01dd
children 97fc9a21a8fb
line wrap: on
line diff
--- a/lib/getdomainname.c
+++ b/lib/getdomainname.c
@@ -1,6 +1,6 @@
 /* getdomainname emulation for systems that doesn't have it.
 
-   Copyright (C) 2003, 2006, 2008, 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2006, 2008, 2010 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -22,9 +22,14 @@
 /* Specification. */
 #include <unistd.h>
 
+#include <limits.h>
 #include <string.h>
 #include <errno.h>
 
+#if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H /* IRIX, OSF/1, Solaris */
+# include <sys/systeminfo.h>
+#endif
+
 /* Return the NIS domain name of the machine.
    WARNING! The NIS domain name is unrelated to the fully qualified host name
             of the machine.  It is also unrelated to email addresses.
@@ -37,7 +42,31 @@
    Return 0 if successful, otherwise set errno and return -1.  */
 int
 getdomainname (char *name, size_t len)
+#undef getdomainname
 {
+#if HAVE_GETDOMAINNAME                 /* MacOS X, FreeBSD, AIX, IRIX, OSF/1 */
+  extern int getdomainname (char *, int);
+
+  if (len > INT_MAX)
+    len = INT_MAX;
+  return getdomainname (name, (int) len);
+#elif HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H && defined SI_SRPC_DOMAIN
+                                       /* Solaris */
+  int ret;
+
+  /* The third argument is a 'long', but the return value must fit in an
+     'int', therefore it's better to avoid arguments > INT_MAX.  */
+  ret = sysinfo (SI_SRPC_DOMAIN, name, len > INT_MAX ? INT_MAX : len);
+  if (ret < 0)
+    /* errno is set here.  */
+    return -1;
+  if (ret > len)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  return 0;
+#else                                  /* HP-UX, Cygwin, mingw */
   const char *result = "";      /* Hardcode your domain name if you want.  */
   size_t result_len = strlen (result);
 
@@ -50,4 +79,5 @@
   if (result_len < len)
     name[result_len] = '\0';
   return 0;
+#endif
 }