/*
*
* @(#)69        1.4  src/examples/common/os2/gethost.c, examples.src, os2dce21.dss, 960602a.1  7/8/94  10:49:47
*
* COMPONENT_NAME:  examples.src
*
* FUNCTIONS:
*
* ORIGINS: 27
*
* OBJECT CODE ONLY SOURCE MATERIALS
*
* (C) COPYRIGHT International Business Machines Corp. 1992, 1994
*  All Rights Reserved
*  Licensed Materials - Property of IBM
*
*  US Government Users Restricted Rights - Use, duplication or
*  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*/
/*******************************************************************

 PROGRAM NAME: gethost.c

 DESCRIPTIVE NAME: Implements gethostname function


 FUNCTION: Implements gethostname function

 NOTES:

   DEPENDENCIES: (optional)
     This code depends on the HOSTNAME variable being set in the
     environment.    This is likely to be the case for machines
     running TCP/IP, since TCP/IP install modifies CONFIG.SYS
     to set the variable.  However, I am not certain this will
     be true when we are running on top of TLPB.
********************************************************************/

#include <errno.h>       /* errno stuff */
#include <stdlib.h>       /* declaration of getenv */
#include <string.h>       /* declaration of string functions */

const char szHostName[] = "HOSTNAME";  /* the environment variable that */
                       /* contains the host name    */


/********************************************************************

 FUNCTION NAME:  gethostname

 PURPOSE:  Returns the host name of the machine.

 DESCRIPTION:  See DEPENDENCIES section in prolog.  Note that if
       the length of the buffer is smaller than the length of the
       name, the buffer will be filled up to its length with as much
       of the name as can fit.

 INPUT:  NameLength - The length of the output buffer that will
     contain the hostname.


 OUTPUT: Name - A pointer to the output buffer that gethostname will
     copy the host name into.

 RETURN CODES

     0         OK

     -1         HOSTNAME variable is not set.  errno is set
             to ENOTINIT.

********************************************************************/

int gethostname (char * Name, int NameLength)
{

   char * tempbuf;    /* pointer to value of environment variable */
            /* returned from getenv */

   int templen;     /* length of hostname */

   if ((tempbuf = getenv(szHostName)) == NULL) {   /* No HOSTNAME environment */
                           /* variable              */
/*      errno = ENOTINIT;*/                   /* Got a better idea?      */
      return (-1);
   } /* endif */

   templen = strlen(tempbuf) + 1;           /* Get the length of the   */
                           /* name.  If it is bigger  */
                           /* than NameLength, set it */
                           /* to NameLength.          */
   if (templen > NameLength) templen = NameLength;
   strncpy(Name,tempbuf,templen);           /* copy the name to the    */
                           /* users buffer          */
   return(0);
}


