From 8fcc5d7f43931e3ec9053b20f5e050b7a4008bc9 Mon Sep 17 00:00:00 2001 From: David Levine Date: Tue, 13 Mar 2012 21:20:47 -0500 Subject: [PATCH] Added test/getfqnd.c, a replacement for "hostname" that always tries to provide the fully qualified domainname of the host, even on Cygwin. It uses the code that LocalName in sbr/mts.c uses. --- .gitignore | 3 ++- Makefile.am | 5 ++++- test/format/test-myhost | 3 ++- test/format/test-mymbox | 2 +- test/getfqdn.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 test/getfqdn.c diff --git a/.gitignore b/.gitignore index 87e31cb..d1dd750 100644 --- a/.gitignore +++ b/.gitignore @@ -45,7 +45,7 @@ a.out.DSYM/ /mts/libmts.a /sbr/*.a /sbr/sigmsg.h -/test/testdir +/test/testdir/ /uip/ali /uip/anno /uip/ap @@ -97,6 +97,7 @@ a.out.DSYM/ /uip/whom /uip/*.exe /test/getfullname +/test/getfqdn # Removed by mostlyclean: *.o diff --git a/Makefile.am b/Makefile.am index c87160c..9ca7740 100644 --- a/Makefile.am +++ b/Makefile.am @@ -65,7 +65,7 @@ TESTS = test/bad-input/test-header \ test/cleanup ## The "cleanup" test should always be last. check_SCRIPTS = test/common.sh -check_PROGRAMS = test/getfullname +check_PROGRAMS = test/getfullname test/getfqdn ## ## Stuff that should be cleaned via "make clean" @@ -363,6 +363,9 @@ uip_viamail_SOURCES = uip/viamail.c uip/mhmisc.c uip/mhoutsbr.c uip/sendsbr.c \ test_getfullname_SOURCES = test/getfullname.c test_getfullname_LDADD = +test_getfqdn_SOURCES = test/getfqdn.c +test_getfqdn_LDADD = + ## ## Our rebuild rules for files that aren't built via the normal mechanisms ## diff --git a/test/format/test-myhost b/test/format/test-myhost index 27069af..635f35f 100755 --- a/test/format/test-myhost +++ b/test/format/test-myhost @@ -13,7 +13,8 @@ fi setup_test -run_test "${MH_LIB_DIR}/ap -format %(myhost) ignore" "`hostname`" \ +host=`${MH_OBJ_DIR}/test/getfqdn` +run_test "${MH_LIB_DIR}/ap -format %(myhost) ignore" "$host" \ "local hostname test" cp ${MHMTSCONF} ${MH_TEST_DIR}/Mail/mts.conf || exit 1 diff --git a/test/format/test-mymbox b/test/format/test-mymbox index e3decd9..c154a8b 100755 --- a/test/format/test-mymbox +++ b/test/format/test-mymbox @@ -14,7 +14,7 @@ fi setup_test user="`id -un`" -host="`hostname`" +host=`${MH_OBJ_DIR}/test/getfqdn` run_test "${MH_LIB_DIR}/ap -format %(mymbox{text}) ${user}" \ 1 "Basic user test" diff --git a/test/getfqdn.c b/test/getfqdn.c new file mode 100644 index 0000000..23685da --- /dev/null +++ b/test/getfqdn.c @@ -0,0 +1,51 @@ +/* + * getfqdn.c - Print the FQDN of a host, default to localhost. + * + * This code is Copyright (c) 2012, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. + */ + +#include /* for getaddrinfo */ +#include +#include +#include /* for gethostname */ +#include /* for _POSIX_HOST_NAME_MAX */ +#include /* for memset */ +#include + + +int +main(int argc, char *argv[]) +{ + char buf[_POSIX_HOST_NAME_MAX + 1]; + const char *hostname = buf; + struct addrinfo hints, *res; + int status = 0; + + /* Borrowed the important code below from LocalName() in sbr/mts.c. */ + + if (argc < 2) { + /* First get our local name. */ + status = gethostname(buf, sizeof buf); + } else if (argc == 2) { + hostname = argv[1]; + } else if (argc > 2) { + fprintf (stderr, "usage: %s [hostname]\n", argv[0]); + return 1; + } + + if (status == 0) { + /* Now fully qualify the hostname. */ + memset(&hints, 0, sizeof hints); + hints.ai_flags = AI_CANONNAME; + hints.ai_family = AF_UNSPEC; + + if ((status = getaddrinfo(hostname, NULL, &hints, &res)) == 0) { + printf ("%s\n", res->ai_canonname); + freeaddrinfo(res); + } + } + + return status; +} -- 1.7.10.4