| 1 |
# gethostname.m4 |
| 2 |
# serial 16 |
| 3 |
dnl Copyright (C) 2002, 2008-2026 Free Software Foundation, Inc. |
| 4 |
dnl This file is free software; the Free Software Foundation |
| 5 |
dnl gives unlimited permission to copy and/or distribute it, |
| 6 |
dnl with or without modifications, as long as this notice is preserved. |
| 7 |
dnl This file is offered as-is, without any warranty. |
| 8 |
|
| 9 |
# Ensure |
| 10 |
# - the gethostname() function, |
| 11 |
# - the HOST_NAME_MAX macro in <limits.h>. |
| 12 |
AC_DEFUN([gl_FUNC_GETHOSTNAME], |
| 13 |
[ |
| 14 |
AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) |
| 15 |
gl_PREREQ_SYS_H_WINSOCK2 |
| 16 |
|
| 17 |
dnl Where is gethostname() defined? |
| 18 |
dnl - On native Windows, it is in ws2_32.dll. |
| 19 |
dnl - Otherwise it is in libc. |
| 20 |
GETHOSTNAME_LIB= |
| 21 |
AC_CHECK_FUNCS([gethostname], , [ |
| 22 |
AC_CACHE_CHECK([for gethostname in winsock2.h and -lws2_32], |
| 23 |
[gl_cv_w32_gethostname], |
| 24 |
[gl_cv_w32_gethostname=no |
| 25 |
gl_saved_LIBS="$LIBS" |
| 26 |
LIBS="$LIBS -lws2_32" |
| 27 |
AC_LINK_IFELSE([AC_LANG_PROGRAM([[ |
| 28 |
#ifdef HAVE_WINSOCK2_H |
| 29 |
#include <winsock2.h> |
| 30 |
#endif |
| 31 |
#include <stddef.h> |
| 32 |
]], [[gethostname(NULL, 0);]])], [gl_cv_w32_gethostname=yes]) |
| 33 |
LIBS="$gl_saved_LIBS" |
| 34 |
]) |
| 35 |
if test "$gl_cv_w32_gethostname" = "yes"; then |
| 36 |
GETHOSTNAME_LIB="-lws2_32" |
| 37 |
fi |
| 38 |
]) |
| 39 |
AC_SUBST([GETHOSTNAME_LIB]) |
| 40 |
|
| 41 |
if test "$ac_cv_func_gethostname" = no; then |
| 42 |
HAVE_GETHOSTNAME=0 |
| 43 |
fi |
| 44 |
|
| 45 |
gl_PREREQ_HOST_NAME_MAX |
| 46 |
]) |
| 47 |
|
| 48 |
# Provide HOST_NAME_MAX when <limits.h> lacks it. |
| 49 |
AC_DEFUN([gl_PREREQ_HOST_NAME_MAX], [ |
| 50 |
dnl - On most Unix systems, use MAXHOSTNAMELEN from <sys/param.h> instead. |
| 51 |
dnl - On Solaris, Cygwin, BeOS, use MAXHOSTNAMELEN from <netdb.h> instead. |
| 52 |
dnl - On mingw, use 256, because |
| 53 |
dnl <https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-gethostname> says: |
| 54 |
dnl "if a buffer of 256 bytes is passed in the name parameter and |
| 55 |
dnl the namelen parameter is set to 256, the buffer size will always |
| 56 |
dnl be adequate." |
| 57 |
dnl With this, there is no need to use sysconf (_SC_HOST_NAME_MAX), which |
| 58 |
dnl is not a compile-time constant. |
| 59 |
dnl We cannot override <limits.h> using the usual technique, because |
| 60 |
dnl gl_CHECK_NEXT_HEADERS does not work for <limits.h>. Therefore retrieve |
| 61 |
dnl the value of HOST_NAME_MAX at configure time. |
| 62 |
AC_CHECK_HEADERS_ONCE([sys/param.h]) |
| 63 |
AC_CHECK_HEADERS_ONCE([sys/socket.h]) |
| 64 |
AC_CHECK_HEADERS_ONCE([netdb.h]) |
| 65 |
AC_CACHE_CHECK([for HOST_NAME_MAX], [gl_cv_decl_HOST_NAME_MAX], [ |
| 66 |
gl_cv_decl_HOST_NAME_MAX= |
| 67 |
AC_EGREP_CPP([lucky], [ |
| 68 |
#include <limits.h> |
| 69 |
#ifdef HOST_NAME_MAX |
| 70 |
lucky |
| 71 |
#endif |
| 72 |
], [gl_cv_decl_HOST_NAME_MAX=yes]) |
| 73 |
if test -z "$gl_cv_decl_HOST_NAME_MAX"; then |
| 74 |
dnl It's not defined in <limits.h>. Substitute it. |
| 75 |
if test "$gl_cv_w32_gethostname" = yes; then |
| 76 |
dnl mingw. |
| 77 |
gl_cv_decl_HOST_NAME_MAX=256 |
| 78 |
else |
| 79 |
AC_COMPUTE_INT([gl_cv_decl_HOST_NAME_MAX], [MAXHOSTNAMELEN], [ |
| 80 |
#include <sys/types.h> |
| 81 |
#if HAVE_SYS_PARAM_H |
| 82 |
# include <sys/param.h> |
| 83 |
#endif |
| 84 |
#if HAVE_SYS_SOCKET_H |
| 85 |
# include <sys/socket.h> |
| 86 |
#endif |
| 87 |
#if HAVE_NETDB_H |
| 88 |
# include <netdb.h> |
| 89 |
#endif |
| 90 |
], |
| 91 |
[dnl The system does not define MAXHOSTNAMELEN in any of the common |
| 92 |
dnl headers. Use a safe fallback. |
| 93 |
gl_cv_decl_HOST_NAME_MAX=256 |
| 94 |
]) |
| 95 |
fi |
| 96 |
fi |
| 97 |
]) |
| 98 |
if test "$gl_cv_decl_HOST_NAME_MAX" != yes; then |
| 99 |
AC_DEFINE_UNQUOTED([HOST_NAME_MAX], [$gl_cv_decl_HOST_NAME_MAX], |
| 100 |
[Define HOST_NAME_MAX when <limits.h> does not define it.]) |
| 101 |
fi |
| 102 |
]) |
| 103 |
|
| 104 |
# Prerequisites of lib/gethostname.c. |
| 105 |
AC_DEFUN([gl_PREREQ_GETHOSTNAME], [ |
| 106 |
if test "$gl_cv_w32_gethostname" != "yes"; then |
| 107 |
AC_CHECK_FUNCS([uname]) |
| 108 |
fi |
| 109 |
]) |