1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-07-04 16:50:25 +02:00

Use gnulib for gethostname

From gnulib-0.1 b03f418b

* lib/Makefile.am: updated
* lib/gethostname.c: new file
* lib/gethostname.m4: new file
* m4/gnulib-cache.m4: updated
* m4/gnulib-comp.m4: updated
This commit is contained in:
Michael Gran 2018-04-10 15:40:45 -07:00
parent b9731ee5ee
commit c90b9cdf8e
5 changed files with 236 additions and 2 deletions

View file

@ -21,7 +21,7 @@
# the same distribution terms as the rest of that program.
#
# Generated by gnulib-tool.
# Reproduce by: gnulib-tool --import --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=lock --avoid=unistr/base --avoid=unistr/u8-mbtouc --avoid=unistr/u8-mbtouc-unsafe --avoid=unistr/u8-mbtoucr --avoid=unistr/u8-prev --avoid=unistr/u8-uctomb --avoid=unitypes --lgpl=3 --conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept4 alignof alloca-opt announce-gen autobuild bind byteswap c-strcase canonicalize-lgpl ceil clock-time close connect copysign dirfd dirname-lgpl duplocale environ extensions flock floor fpieee frexp fstat fsync full-read full-write func gendocs getaddrinfo getlogin getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isfinite isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring link listen localcharset locale log1p lstat maintainer-makefile malloc-gnu malloca mkdir mkostemp nl_langinfo nproc open pipe-posix pipe2 poll putenv readlink recv recvfrom regex rename rmdir select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat time times trunc unistd verify vsnprintf warnings wchar
# Reproduce by: gnulib-tool --import --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=lock --avoid=unistr/base --avoid=unistr/u8-mbtouc --avoid=unistr/u8-mbtouc-unsafe --avoid=unistr/u8-mbtoucr --avoid=unistr/u8-prev --avoid=unistr/u8-uctomb --avoid=unitypes --lgpl=3 --conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept4 alignof alloca-opt announce-gen autobuild bind byteswap c-strcase canonicalize-lgpl ceil clock-time close connect copysign dirfd dirname-lgpl duplocale environ extensions flock floor fpieee frexp fstat fsync full-read full-write func gendocs getaddrinfo gethostname getlogin getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isfinite isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring link listen localcharset locale log1p lstat maintainer-makefile malloc-gnu malloca mkdir mkostemp nl_langinfo nproc open pipe-posix pipe2 poll putenv readlink recv recvfrom regex rename rmdir select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat time times trunc unistd verify vsnprintf warnings wchar
AUTOMAKE_OPTIONS = 1.9.6 gnits
@ -54,6 +54,7 @@ libgnu_la_LDFLAGS += $(COPYSIGN_LIBM)
libgnu_la_LDFLAGS += $(FLOOR_LIBM)
libgnu_la_LDFLAGS += $(FREXP_LIBM)
libgnu_la_LDFLAGS += $(GETADDRINFO_LIB)
libgnu_la_LDFLAGS += $(GETHOSTNAME_LIB)
libgnu_la_LDFLAGS += $(HOSTENT_LIB)
libgnu_la_LDFLAGS += $(INET_NTOP_LIB)
libgnu_la_LDFLAGS += $(INET_PTON_LIB)
@ -640,6 +641,15 @@ EXTRA_libgnu_la_SOURCES += gai_strerror.c getaddrinfo.c
## end gnulib module getaddrinfo
## begin gnulib module gethostname
EXTRA_DIST += gethostname.c w32sock.h
EXTRA_libgnu_la_SOURCES += gethostname.c
## end gnulib module gethostname
## begin gnulib module getlogin

104
lib/gethostname.c Normal file
View file

@ -0,0 +1,104 @@
/* gethostname emulation for SysV and POSIX.1.
Copyright (C) 1992, 2003, 2006, 2008-2017 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* David MacKenzie <djm@gnu.ai.mit.edu>
Windows port by Simon Josefsson <simon@josefsson.org> */
#include <config.h>
#if !((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
/* Unix API. */
/* Specification. */
#include <unistd.h>
#ifdef HAVE_UNAME
# include <sys/utsname.h>
#endif
#include <string.h>
/* Put up to LEN chars of the host name into NAME.
Null terminate it if the name is shorter than LEN.
Return 0 if ok, -1 if error. */
#include <stddef.h>
int
gethostname (char *name, size_t len)
{
#ifdef HAVE_UNAME
struct utsname uts;
if (uname (&uts) == -1)
return -1;
if (len > sizeof (uts.nodename))
{
/* More space than we need is available. */
name[sizeof (uts.nodename)] = '\0';
len = sizeof (uts.nodename);
}
strncpy (name, uts.nodename, len);
#else
strcpy (name, ""); /* Hardcode your system name if you want. */
#endif
return 0;
}
#else
/* Native Windows API. Which primitive to choose?
- gethostname() requires linking with -lws2_32.
- GetComputerName() does not return the right kind of hostname.
- GetComputerNameEx(ComputerNameDnsHostname,...) returns the right hostname,
but it is hard to use portably:
- It requires defining _WIN32_WINNT to at least 0x0500.
- With mingw, it also requires
"#define GetComputerNameEx GetComputerNameExA".
- With older versions of mingw, none of the declarations are present at
all, not even of the enum value ComputerNameDnsHostname.
So we use gethostname(). Linking with -lws2_32 is the least evil. */
#define WIN32_LEAN_AND_MEAN
/* Get winsock2.h. */
#include <unistd.h>
/* Get INT_MAX. */
#include <limits.h>
/* Get set_winsock_errno. */
#include "w32sock.h"
#include "sockets.h"
#undef gethostname
int
rpl_gethostname (char *name, size_t len)
{
int r;
if (len > INT_MAX)
len = INT_MAX;
gl_sockets_startup (SOCKETS_1_1);
r = gethostname (name, (int) len);
if (r < 0)
set_winsock_errno ();
return r;
}
#endif

107
m4/gethostname.m4 Normal file
View file

@ -0,0 +1,107 @@
# gethostname.m4 serial 13
dnl Copyright (C) 2002, 2008-2017 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 gethostname() function,
# - the HOST_NAME_MAX macro in <limits.h>.
AC_DEFUN([gl_FUNC_GETHOSTNAME],
[
AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
gl_PREREQ_SYS_H_WINSOCK2
dnl Where is gethostname() defined?
dnl - On native Windows, it is in ws2_32.dll.
dnl - Otherwise it is in libc.
GETHOSTNAME_LIB=
AC_CHECK_FUNCS([gethostname], , [
AC_CACHE_CHECK([for gethostname in winsock2.h and -lws2_32],
[gl_cv_w32_gethostname],
[gl_cv_w32_gethostname=no
gl_save_LIBS="$LIBS"
LIBS="$LIBS -lws2_32"
AC_LINK_IFELSE([AC_LANG_PROGRAM([[
#ifdef HAVE_WINSOCK2_H
#include <winsock2.h>
#endif
#include <stddef.h>
]], [[gethostname(NULL, 0);]])], [gl_cv_w32_gethostname=yes])
LIBS="$gl_save_LIBS"
])
if test "$gl_cv_w32_gethostname" = "yes"; then
GETHOSTNAME_LIB="-lws2_32"
fi
])
AC_SUBST([GETHOSTNAME_LIB])
if test "$ac_cv_func_gethostname" = no; then
HAVE_GETHOSTNAME=0
fi
gl_PREREQ_HOST_NAME_MAX
])
# Provide HOST_NAME_MAX when <limits.h> lacks it.
AC_DEFUN([gl_PREREQ_HOST_NAME_MAX], [
dnl - On most Unix systems, use MAXHOSTNAMELEN from <sys/param.h> instead.
dnl - On Solaris, Cygwin, BeOS, use MAXHOSTNAMELEN from <netdb.h> instead.
dnl - On mingw, use 256, because
dnl <http://msdn.microsoft.com/en-us/library/ms738527.aspx> says:
dnl "if a buffer of 256 bytes is passed in the name parameter and
dnl the namelen parameter is set to 256, the buffer size will always
dnl be adequate."
dnl With this, there is no need to use sysconf (_SC_HOST_NAME_MAX), which
dnl is not a compile-time constant.
dnl We cannot override <limits.h> using the usual technique, because
dnl gl_CHECK_NEXT_HEADERS does not work for <limits.h>. Therefore retrieve
dnl the value of HOST_NAME_MAX at configure time.
AC_CHECK_HEADERS_ONCE([sys/param.h])
AC_CHECK_HEADERS_ONCE([sys/socket.h])
AC_CHECK_HEADERS_ONCE([netdb.h])
AC_CACHE_CHECK([for HOST_NAME_MAX], [gl_cv_decl_HOST_NAME_MAX], [
gl_cv_decl_HOST_NAME_MAX=
AC_EGREP_CPP([lucky], [
#include <limits.h>
#ifdef HOST_NAME_MAX
lucky
#endif
], [gl_cv_decl_HOST_NAME_MAX=yes])
if test -z "$gl_cv_decl_HOST_NAME_MAX"; then
dnl It's not defined in <limits.h>. Substitute it.
if test "$gl_cv_w32_gethostname" = yes; then
dnl mingw.
gl_cv_decl_HOST_NAME_MAX=256
else
_AC_COMPUTE_INT([MAXHOSTNAMELEN], [gl_cv_decl_HOST_NAME_MAX], [
#include <sys/types.h>
#if HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#if HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#if HAVE_NETDB_H
# include <netdb.h>
#endif
],
[dnl The system does not define MAXHOSTNAMELEN in any of the common
dnl headers. Use a safe fallback.
gl_cv_decl_HOST_NAME_MAX=256
])
fi
fi
])
if test "$gl_cv_decl_HOST_NAME_MAX" != yes; then
AC_DEFINE_UNQUOTED([HOST_NAME_MAX], [$gl_cv_decl_HOST_NAME_MAX],
[Define HOST_NAME_MAX when <limits.h> does not define it.])
fi
])
# Prerequisites of lib/gethostname.c.
AC_DEFUN([gl_PREREQ_GETHOSTNAME], [
if test "$gl_cv_w32_gethostname" != "yes"; then
AC_CHECK_FUNCS([uname])
fi
])

View file

@ -27,7 +27,7 @@
# Specification in the form of a command-line invocation:
# gnulib-tool --import --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=lock --avoid=unistr/base --avoid=unistr/u8-mbtouc --avoid=unistr/u8-mbtouc-unsafe --avoid=unistr/u8-mbtoucr --avoid=unistr/u8-prev --avoid=unistr/u8-uctomb --avoid=unitypes --lgpl=3 --conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept4 alignof alloca-opt announce-gen autobuild bind byteswap c-strcase canonicalize-lgpl ceil clock-time close connect copysign dirfd dirname-lgpl duplocale environ extensions flock floor fpieee frexp fstat fsync full-read full-write func gendocs getaddrinfo getlogin getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isfinite isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring link listen localcharset locale log1p lstat maintainer-makefile malloc-gnu malloca mkdir mkostemp nl_langinfo nproc open pipe-posix pipe2 poll putenv readlink recv recvfrom regex rename rmdir select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat time times trunc unistd verify vsnprintf warnings wchar
# gnulib-tool --import --local-dir=gnulib-local --lib=libgnu --source-base=lib --m4-base=m4 --doc-base=doc --tests-base=tests --aux-dir=build-aux --avoid=lock --avoid=unistr/base --avoid=unistr/u8-mbtouc --avoid=unistr/u8-mbtouc-unsafe --avoid=unistr/u8-mbtoucr --avoid=unistr/u8-prev --avoid=unistr/u8-uctomb --avoid=unitypes --lgpl=3 --conditional-dependencies --libtool --macro-prefix=gl --no-vc-files accept4 alignof alloca-opt announce-gen autobuild bind byteswap c-strcase canonicalize-lgpl ceil clock-time close connect copysign dirfd dirname-lgpl duplocale environ extensions flock floor fpieee frexp fstat fsync full-read full-write func gendocs getaddrinfo gethostname getlogin getpeername getsockname getsockopt git-version-gen gitlog-to-changelog gnu-web-doc-update gnupload havelib iconv_open-utf inet_ntop inet_pton isfinite isinf isnan ldexp lib-symbol-versions lib-symbol-visibility libunistring link listen localcharset locale log1p lstat maintainer-makefile malloc-gnu malloca mkdir mkostemp nl_langinfo nproc open pipe-posix pipe2 poll putenv readlink recv recvfrom regex rename rmdir select send sendto setenv setsockopt shutdown socket stat-time stdlib strftime striconveh string sys_stat time times trunc unistd verify vsnprintf warnings wchar
# Specification in the form of a few gnulib-tool.m4 macro invocations:
gl_LOCAL_DIR([gnulib-local])
@ -62,6 +62,7 @@ gl_MODULES([
func
gendocs
getaddrinfo
gethostname
getlogin
getpeername
getsockname

View file

@ -96,6 +96,7 @@ AC_DEFUN([gl_EARLY],
# Code from module func:
# Code from module gendocs:
# Code from module getaddrinfo:
# Code from module gethostname:
# Code from module getlogin:
# Code from module getpeername:
# Code from module getsockname:
@ -373,6 +374,12 @@ AC_DEFUN([gl_INIT],
AC_LIBOBJ([gai_strerror])
fi
gl_NETDB_MODULE_INDICATOR([getaddrinfo])
gl_FUNC_GETHOSTNAME
if test $HAVE_GETHOSTNAME = 0; then
AC_LIBOBJ([gethostname])
gl_PREREQ_GETHOSTNAME
fi
gl_UNISTD_MODULE_INDICATOR([gethostname])
gl_FUNC_GETLOGIN
if test $HAVE_GETLOGIN = 0; then
AC_LIBOBJ([getlogin])
@ -1263,6 +1270,9 @@ AC_SUBST([LTALLOCA])
if test $HAVE_GETADDRINFO = 0; then
func_gl_gnulib_m4code_sockets
fi
if test $HAVE_GETHOSTNAME = 0; then
func_gl_gnulib_m4code_sockets
fi
if test $REPLACE_ISFINITE = 1; then
func_gl_gnulib_m4code_b1df7117b479d2da59d76deba468ee21
fi
@ -1617,6 +1627,7 @@ AC_DEFUN([gl_FILE_LIST], [
lib/full-write.h
lib/gai_strerror.c
lib/getaddrinfo.c
lib/gethostname.c
lib/getlogin.c
lib/getpeername.c
lib/getsockname.c
@ -1827,6 +1838,7 @@ AC_DEFUN([gl_FILE_LIST], [
m4/fsync.m4
m4/func.m4
m4/getaddrinfo.m4
m4/gethostname.m4
m4/getlogin.m4
m4/gettimeofday.m4
m4/glibc21.m4