Define sethostname on platforms that do not provide the declaration. Provide a function for platforms that lack it. The general handling of the provided function is to simply return -1 and set errno to ENOSYS. A handler is provided for Minix.
Signed-off-by: Ben Walton <bwal...@artsci.utoronto.ca> --- doc/glibc-functions/sethostname.texi | 11 +++++- lib/sethostname.c | 71 ++++++++++++++++++++++++++++++++++ m4/sethostname.m4 | 21 ++++++++++ modules/sethostname | 32 +++++++++++++++ 4 files changed, 134 insertions(+), 1 deletions(-) create mode 100644 lib/sethostname.c create mode 100644 m4/sethostname.m4 create mode 100644 modules/sethostname diff --git a/doc/glibc-functions/sethostname.texi b/doc/glibc-functions/sethostname.texi index 75cc8ca..7960eda 100644 --- a/doc/glibc-functions/sethostname.texi +++ b/doc/glibc-functions/sethostname.texi @@ -2,10 +2,15 @@ @subsection @code{sethostname} @findex sethostname -Gnulib module: --- +Gnulib module: sethostname Portability problems fixed by Gnulib: @itemize +@item +On AIX 7.1, OSF/1 5.1 and Solaris 10 the declaration is missing. On +Minix 3.1.8, Cygwin, mingw, MSVC 9, Interix 3.5, BeOS the function is +missing. Provide a fallback for all platforms that returns -1 and +sets ENOSYS. Provide a specific implementation for Minix 3.1.8 @end itemize Portability problems not fixed by Gnulib: @@ -16,4 +21,8 @@ Minix 3.1.8, Cygwin, mingw, MSVC 9, Interix 3.5, BeOS. @item This function is not declared on some platforms: AIX 7.1, OSF/1 5.1, Solaris 10. +@item +On Solaris 10, the first argument is @code{char *} instead of +@code{const char *} and the second parameter is @code{int} instead of +@code{size_t}. @end itemize diff --git a/lib/sethostname.c b/lib/sethostname.c new file mode 100644 index 0000000..70d5955 --- /dev/null +++ b/lib/sethostname.c @@ -0,0 +1,71 @@ +/* sethostname emulation for glibc compliance. + + Copyright (C) 2011 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 + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* Ben Walton <bwal...@artsci.utoronto.ca> */ + +#include <config.h> + +/* Unix API. */ + +/* Specification. */ +#include <unistd.h> + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <limits.h> + +/* Set up to LEN chars of NAME as system hostname. + Return 0 if ok, -1 if error. */ + +int +sethostname (const char *name, size_t len) +{ + /* glibc seems to allow a zero length hostname: bail on names that + are too long or too short */ + if (len < 0 || len > HOST_NAME_MAX) + { + errno = EINVAL; + return -1; + } + + /* NAME does not need to be null terminated so leave room to terminate + regardless of input. */ + char hostname[len + 1]; + strncpy(hostname, name, len); + hostname[len] = '\0'; + +#ifdef __minix /* Minix */ + FILE *hostf; + + /* leave errno alone in this case as it will provide a more useful error + indication than overriding with ENOSYS */ + if ((hostf = fopen("/etc/hostname.file", "w")) == NULL) + return -1; + else + { + fprintf(hostf, "%s\n", hostname); + fclose(hostf); + return 0; + } +#else + /* For platforms that we don't have a better option for, simply bail + out */ + errno = ENOSYS; + return -1; +#endif +} diff --git a/m4/sethostname.m4 b/m4/sethostname.m4 new file mode 100644 index 0000000..dbdbb39 --- /dev/null +++ b/m4/sethostname.m4 @@ -0,0 +1,21 @@ +# sethostname.m4 serial 1 +dnl Copyright (C) 2011 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +# Ensure +# - the sethostname() function, +AC_DEFUN([gl_FUNC_SETHOSTNAME], +[ + AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) + + AC_REPLACE_FUNCS([sethostname]) + AC_CHECK_DECLS([sethostname]) + if test $ac_cv_func_sethostname = no; then + gl_PREREQ_HOST_NAME_MAX + fi + if test $ac_cv_have_decl_sethostname = no; then + HAVE_DECL_SETHOSTNAME=0 + fi +]) diff --git a/modules/sethostname b/modules/sethostname new file mode 100644 index 0000000..a0f36a2 --- /dev/null +++ b/modules/sethostname @@ -0,0 +1,32 @@ +Description: +sethostname() function: Set machine's hostname. + +Files: +lib/sethostname.c +m4/sethostname.m4 + +Depends-on: +unistd +gethostname +errno [test $HAVE_SETHOSTNAME = 0] + +configure.ac: +gl_FUNC_SETHOSTNAME +if test $HAVE_SETHOSTNAME = 0; then + AC_LIBOBJ([sethostname]) +fi +gl_UNISTD_MODULE_INDICATOR([sethostname]) + +Makefile.am: + +Include: +<unistd.h> + +Link: +$(SETHOSTNAME_LIB) + +License: +LGPLv2+ + +Maintainer: +Ben Walton -- 1.7.4.1