freebsd-dev/contrib/tcl/unix/tclUnixSock.c
1997-07-25 19:27:55 +00:00

82 lines
1.6 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* tclUnixSock.c --
*
* This file contains Unix-specific socket related code.
*
* Copyright (c) 1995 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tclUnixSock.c 1.6 96/08/08 08:48:51
*/
#include "tcl.h"
#include "tclPort.h"
/*
* The following variable holds the network name of this host.
*/
#ifndef SYS_NMLN
# define SYS_NMLN 100
#endif
static char hostname[SYS_NMLN + 1];
static int hostnameInited = 0;
/*
*----------------------------------------------------------------------
*
* Tcl_GetHostName --
*
* Get the network name for this machine, in a system dependent way.
*
* Results:
* A string containing the network name for this machine, or
* an empty string if we can't figure out the name.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char *
Tcl_GetHostName()
{
#ifndef NO_UNAME
struct utsname u;
struct hostent *hp;
#endif
if (hostnameInited) {
return hostname;
}
#ifndef NO_UNAME
if (uname(&u) > -1) {
hp = gethostbyname(u.nodename);
if (hp != NULL) {
strcpy(hostname, hp->h_name);
} else {
strcpy(hostname, u.nodename);
}
hostnameInited = 1;
return hostname;
}
#else
/*
* Uname doesn't exist; try gethostname instead.
*/
if (gethostname(hostname, sizeof(hostname)) > -1) {
hostnameInited = 1;
return hostname;
}
#endif
hostname[0] = 0;
return hostname;
}