1996-06-26 06:06:43 +00:00
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
1997-11-27 19:49:05 +00:00
|
|
|
|
* SCCS: @(#) tclUnixSock.c 1.9 97/10/09 18:24:49
|
1996-06-26 06:06:43 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "tcl.h"
|
|
|
|
|
#include "tclPort.h"
|
|
|
|
|
|
|
|
|
|
/*
|
1997-10-01 13:19:13 +00:00
|
|
|
|
* There is no portable macro for the maximum length
|
|
|
|
|
* of host names returned by gethostbyname(). We should only
|
|
|
|
|
* trust SYS_NMLN if it is at least 255 + 1 bytes to comply with DNS
|
|
|
|
|
* host name limits.
|
|
|
|
|
*
|
|
|
|
|
* Note: SYS_NMLN is a restriction on "uname" not on gethostbyname!
|
|
|
|
|
*
|
|
|
|
|
* For example HP-UX 10.20 has SYS_NMLN == 9, while gethostbyname()
|
|
|
|
|
* can return a fully qualified name from DNS of up to 255 bytes.
|
|
|
|
|
*
|
|
|
|
|
* Fix suggested by Viktor Dukhovni (viktor@esm.com)
|
1996-06-26 06:06:43 +00:00
|
|
|
|
*/
|
|
|
|
|
|
1997-10-01 13:19:13 +00:00
|
|
|
|
#if defined(SYS_NMLN) && SYS_NMLEN >= 256
|
|
|
|
|
#define TCL_HOSTNAME_LEN SYS_NMLEN
|
|
|
|
|
#else
|
|
|
|
|
#define TCL_HOSTNAME_LEN 256
|
1996-06-26 06:06:43 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
1997-10-01 13:19:13 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The following variable holds the network name of this host.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static char hostname[TCL_HOSTNAME_LEN + 1];
|
1996-06-26 06:06:43 +00:00
|
|
|
|
static int hostnameInited = 0;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
*----------------------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* Tcl_GetHostName --
|
|
|
|
|
*
|
1997-11-27 19:49:05 +00:00
|
|
|
|
* Returns the name of the local host.
|
1996-06-26 06:06:43 +00:00
|
|
|
|
*
|
|
|
|
|
* Results:
|
1997-07-25 19:27:55 +00:00
|
|
|
|
* A string containing the network name for this machine, or
|
1997-11-27 19:49:05 +00:00
|
|
|
|
* an empty string if we can't figure out the name. The caller
|
|
|
|
|
* must not modify or free this string.
|
1996-06-26 06:06:43 +00:00
|
|
|
|
*
|
|
|
|
|
* Side effects:
|
|
|
|
|
* None.
|
|
|
|
|
*
|
|
|
|
|
*----------------------------------------------------------------------
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
Tcl_GetHostName()
|
|
|
|
|
{
|
1997-07-25 19:27:55 +00:00
|
|
|
|
#ifndef NO_UNAME
|
1996-06-26 06:06:43 +00:00
|
|
|
|
struct utsname u;
|
|
|
|
|
struct hostent *hp;
|
1997-07-25 19:27:55 +00:00
|
|
|
|
#endif
|
1996-06-26 06:06:43 +00:00
|
|
|
|
|
|
|
|
|
if (hostnameInited) {
|
|
|
|
|
return hostname;
|
|
|
|
|
}
|
1997-07-25 19:27:55 +00:00
|
|
|
|
|
|
|
|
|
#ifndef NO_UNAME
|
1997-11-27 19:49:05 +00:00
|
|
|
|
(VOID *) memset((VOID *) &u, (int) 0, sizeof(struct utsname));
|
1996-06-26 06:06:43 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
1997-07-25 19:27:55 +00:00
|
|
|
|
#else
|
|
|
|
|
/*
|
|
|
|
|
* Uname doesn't exist; try gethostname instead.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (gethostname(hostname, sizeof(hostname)) > -1) {
|
|
|
|
|
hostnameInited = 1;
|
|
|
|
|
return hostname;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
hostname[0] = 0;
|
|
|
|
|
return hostname;
|
1996-06-26 06:06:43 +00:00
|
|
|
|
}
|