char RCS_ID_GETHOSTNAME_C[] = "$Id: gethostname.c,v 1.5 1994/01/12 18:35:20 jasegler Exp jasegler $";
/*
 * gethostname.c -- get host name from the environment variable "HOSTNAME"
 *
 * Author: jraja <Jarno.Rajahalme@hut.fi>
 *
 * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
 *                  Helsinki University of Technology, Finland.
 *                  All rights reserved.
 *
 * Created      : Tue Apr 27 17:49:15 1993 jraja
 * Last modified: Fri Jun  4 01:58:48 1993 ppessi
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

/****** net.lib/gethostname ******************************************

 *   NAME
 *       gethostname -- get the name of the host
 *
 *   SYNOPSIS
 *       error = gethostname(name, namelen);
 *
 *       int gethostname(char *, int);
 *
 *   FUNCTION
 *       Get the name of the host to the buffer name of length namelen.
 *       The name is taken from the environment variable "HOSTNAME"
 *       where it SHOULD reside.
 *
 *   INPUTS
 *       name    - Pointer to the buffer where the name should be
 *                 stored.
 *       namelen - Length of the buffer name.
 *
 *   RESULT
 *       error   - 0 on success, -1 in case of an error. The global
 *                 variable errno will be set to indicate the error as
 *                 follows:
 *
 *                 ENOENT - The environment variable "HOSTNAME" is not
 *                          found.
 *
 *   EXAMPLE
 *       char hostname[MAXHOSTNAMELEN];
 *       int error;
 *
 *       error = gethostname(hostname, sizeof(hostname));
 *       if (error < 0)
 *         exit(10);
 *
 *       printf("My name is \"%s\".\n", hostname);
 *
 *   NOTES
 *       This function is included for source compatibility with Unix
 *       systems.
 *       The ENOENT errno value is AmiTCP/IP addition.
 *
 *   BUGS
 *       Unlike the Unix version, this version assures that the
 *       resulting string is always NULL-terminated.
 *
 *   SEE ALSO
 *       getenv()
 *****************************************************************************
 *
 */

int
gethostname (char *name, int namelen)
{
  char Buffer[80];
  char *cp = getenv ("HOSTNAME");

  if (cp == NULL)
    {
      errno = ENOENT;
      return -1;
    }
  /* used to use a function called stccpy.. didn't have it but I figured it
     does something like this.. :) */
  sprintf (Buffer, "%%%ds", namelen);
  sprintf (name, Buffer, cp);
  free (cp);
  return 0;
}
