/*
 * W-NEWS       A simple NEWS processing package.
 *              Hostname.c tries to get the hostname.
 *
 * Version:     @(#)hostname.c	4.50    03-Feb-1995
 *
 * Author:      Miquel van Smoorenburg, <miquels@drinkel.ow.org>
 *		Copyright 1994,1995 Cistron Software C.V.
 *
 *              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
 *              2 of the License, or (at your option) any later version.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libwnews.h"
#ifdef HAVE_GETHOSTBYNAME
#  include <netdb.h>
#endif

/* Read the hostname. */
char *hostname()
{
  static char myname[128];
#if defined(HAVE_UUDOM) || defined(HAVE_HOSTENV)
  char *sp;
#endif
#if HAVE_UUHOST || HAVE_DOM
  char buff[128];
#endif
#if HAVE_UUHOST
  FILE *fp;
#endif
#if HAVE_GETHOSTBYNAME
  struct hostent *h;
#endif

#if HAVE_HOST
  if (gethostname(myname, 128) < 0) {
	logmsg(LOGERR,"cannot get HOSTNAME (%d)\n", errno);
	exit(1);
  }
#endif	/* HAVE_HOSTNAME */
#if HAVE_DOM
  if (getdomainname(buff, 128) == 0) {
	/* A few checks.
	 * 1. Domainname must not be empty or (none)
	 * 2. Hostname may not contain any dots yet.
	 */
	if (strchr(myname, '.') == (char *)NULL && buff[0] &&
	    strcmp(buff, "(none)") != 0) {
		strcat(myname, ".");
		strcat(myname, buff);
	}
  }
#endif	/* HAVE_DOM */
#ifdef HAVE_HOSTENV
  if ((sp = getenv(HAVE_HOSTENV)) == (char *)NULL) {
	logmsg(LOGERR, "cannot get HOSTNAME from env\n");
	exit(1);
  }
  strcpy(myname, sp);
#endif	/* HAVE_HOSTENV */
#ifdef HAVE_UUHOST
  if ((fp = fopen(HAVE_UUHOST, "r")) == (FILE *)NULL) {
	logmsg(LOGERR, "cannot open(%s)\n", HAVE_UUHOST);
	exit(1);
  }
  if ((sp = fgets(buff, 128, fp)) == (char *)NULL || buff[0] == '\n') {
	logmsg(LOGERR,  "cannot get HOSTNAME from %s\n", HAVE_UUHOST);
	exit(1);
  } else {
	if ((sp = strchr(buff, '\n')) != NULL) *sp = '\0';
	strcpy(myname, buff);
  }
#ifndef HAVE_UUDOM
  /* Next, fetch our domain name. */
  if ((sp = fgets(buff, 128, fp)) == (char *)NULL || buff[0] == '\n') {
	logmsg(LOGERR, "cannot get DOMAINNAME from %s\n", HAVE_UUHOST);
	exit(1);
  } else {
	if ((sp = strchr(buff, '\n')) != NULL) *sp = '\0';
	strcat(myname, ".");
	strcat(myname, buff);
  }
#else
  (void) fclose(fp);
  if ((fp = fopen(HAVE_UUDOM, "r")) == (FILE *)0) {
	logmsg(LOGERR, "cannot open(%s)\n", HAVE_UUDOM);
	exit(1);
  }
  if ((sp = fgets(buff, 128, fp)) == NULL || buff[0] == '\n') {
	logmsg(LOGERR, "cannot get DOMAINNAME from %s\n", HAVE_UUDOM);
	exit(1);
  } else {
	if ((sp = strchr(buff, '\n')) != NULL) *sp = '\0';
	strcat(myname, ".");
	strcat(myname, buff);
  }
#endif	/* HAVE_UUDOM */
  (void) fclose(fp);
#endif	/* HAVE_UUHOST */
#if HAVE_GETHOSTBYNAME
  /* If our hostname doesn't contain any dots, use gethostbyname() */
  if (strchr(myname, '.') == NULL) {
	h = gethostbyname(myname);
	if (h == NULL) {
		herror(myname);
		exit(1);
	}
	strcpy(myname, h->h_name);
  }
#endif
  return(myname);
}
