OSDN Git Service

touched all Tcl files to ease next import.
[pf3gnuchains/pf3gnuchains3x.git] / tcl / unix / tclUnixSock.c
1 /* 
2  * tclUnixSock.c --
3  *
4  *      This file contains Unix-specific socket related code.
5  *
6  * Copyright (c) 1995 Sun Microsystems, Inc.
7  *
8  * See the file "license.terms" for information on usage and redistribution
9  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10  *
11  * RCS: @(#) $Id$
12  */
13
14 #include "tcl.h"
15 #include "tclPort.h"
16
17 /*
18  * There is no portable macro for the maximum length
19  * of host names returned by gethostbyname().  We should only
20  * trust SYS_NMLN if it is at least 255 + 1 bytes to comply with DNS
21  * host name limits.
22  *
23  * Note:  SYS_NMLN is a restriction on "uname" not on gethostbyname!
24  *
25  * For example HP-UX 10.20 has SYS_NMLN == 9,  while gethostbyname()
26  * can return a fully qualified name from DNS of up to 255 bytes.
27  *
28  * Fix suggested by Viktor Dukhovni (viktor@esm.com)
29  */
30
31 #if defined(SYS_NMLN) && SYS_NMLEN >= 256
32 #define TCL_HOSTNAME_LEN SYS_NMLEN
33 #else
34 #define TCL_HOSTNAME_LEN 256
35 #endif
36
37
38 /*
39  * The following variable holds the network name of this host.
40  */
41
42 static char hostname[TCL_HOSTNAME_LEN + 1];
43 static int  hostnameInited = 0;
44 TCL_DECLARE_MUTEX(hostMutex)
45
46 \f
47 /*
48  *----------------------------------------------------------------------
49  *
50  * Tcl_GetHostName --
51  *
52  *      Returns the name of the local host.
53  *
54  * Results:
55  *      A string containing the network name for this machine, or
56  *      an empty string if we can't figure out the name.  The caller 
57  *      must not modify or free this string.
58  *
59  * Side effects:
60  *      None.
61  *
62  *----------------------------------------------------------------------
63  */
64
65 char *
66 Tcl_GetHostName()
67 {
68 #ifndef NO_UNAME
69     struct utsname u;
70     struct hostent *hp;
71 #else
72     char buffer[sizeof(hostname)];
73 #endif
74     CONST char *native;
75
76     Tcl_MutexLock(&hostMutex);
77     if (hostnameInited) {
78         Tcl_MutexUnlock(&hostMutex);
79         return hostname;
80     }
81
82     native = NULL;
83 #ifndef NO_UNAME
84     (VOID *) memset((VOID *) &u, (int) 0, sizeof(struct utsname));
85     if (uname(&u) > -1) {                               /* INTL: Native. */
86         hp = gethostbyname(u.nodename);                 /* INTL: Native. */
87         if (hp != NULL) {
88             native = hp->h_name;
89         } else {
90             native = u.nodename;
91         }
92     }
93 #else
94     /*
95      * Uname doesn't exist; try gethostname instead.
96      */
97
98     if (gethostname(buffer, sizeof(buffer)) > -1) {     /* INTL: Native. */
99         native = buffer;
100     }
101 #endif
102
103     if (native == NULL) {
104         hostname[0] = 0;
105     } else {
106         Tcl_ExternalToUtf(NULL, NULL, native, -1, 0, NULL, hostname,
107                 sizeof(hostname), NULL, NULL, NULL);
108     }
109     hostnameInited = 1;
110     Tcl_MutexUnlock(&hostMutex);
111     return hostname;
112 }
113 \f
114 /*
115  *----------------------------------------------------------------------
116  *
117  * TclpHasSockets --
118  *
119  *      Detect if sockets are available on this platform.
120  *
121  * Results:
122  *      Returns TCL_OK.
123  *
124  * Side effects:
125  *      None.
126  *
127  *----------------------------------------------------------------------
128  */
129
130 int
131 TclpHasSockets(interp)
132     Tcl_Interp *interp;         /* Not used. */
133 {
134     return TCL_OK;
135 }
136