os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/unix/tclUnixSock.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /* 
     2  * tclUnixSock.c --
     3  *
     4  *	This file contains Unix-specific socket related code.
     5  *
     6  * Copyright (c) 1995 Sun Microsystems, Inc.
     7  * Portions Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiaries. All rights reserved.  
     8  *
     9  * See the file "license.terms" for information on usage and redistribution
    10  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
    11  *
    12  * RCS: @(#) $Id: tclUnixSock.c,v 1.6.2.4 2006/09/07 09:01:07 vasiljevic Exp $
    13  */
    14 
    15 #include "tcl.h"
    16 #include "tclPort.h"
    17 
    18 /*
    19  * There is no portable macro for the maximum length
    20  * of host names returned by gethostbyname().  We should only
    21  * trust SYS_NMLN if it is at least 255 + 1 bytes to comply with DNS
    22  * host name limits.
    23  *
    24  * Note:  SYS_NMLN is a restriction on "uname" not on gethostbyname!
    25  *
    26  * For example HP-UX 10.20 has SYS_NMLN == 9,  while gethostbyname()
    27  * can return a fully qualified name from DNS of up to 255 bytes.
    28  *
    29  * Fix suggested by Viktor Dukhovni (viktor@esm.com)
    30  */
    31 
    32 #if defined(SYS_NMLN) && SYS_NMLEN >= 256
    33 #define TCL_HOSTNAME_LEN SYS_NMLEN
    34 #else
    35 #define TCL_HOSTNAME_LEN 256
    36 #endif
    37 
    38 
    39 /*
    40  * The following variable holds the network name of this host.
    41  */
    42 
    43 static char hostname[TCL_HOSTNAME_LEN + 1];
    44 static int  hostnameInited = 0;
    45 TCL_DECLARE_MUTEX(hostMutex)
    46 
    47 
    48 /*
    49  *----------------------------------------------------------------------
    50  *
    51  * Tcl_GetHostName --
    52  *
    53  *	Returns the name of the local host.
    54  *
    55  * Results:
    56  *	A string containing the network name for this machine, or
    57  *	an empty string if we can't figure out the name.  The caller 
    58  *	must not modify or free this string.
    59  *
    60  * Side effects:
    61  *	None.
    62  *
    63  *----------------------------------------------------------------------
    64  */
    65 
    66 EXPORT_C CONST char *
    67 Tcl_GetHostName()
    68 {
    69 #ifndef NO_UNAME
    70     struct utsname u;
    71     struct hostent *hp;
    72 #else
    73     char buffer[sizeof(hostname)];
    74 #endif
    75     CONST char *native;
    76 
    77     Tcl_MutexLock(&hostMutex);
    78     if (hostnameInited) {
    79 	Tcl_MutexUnlock(&hostMutex);
    80         return hostname;
    81     }
    82 
    83     native = NULL;
    84 #ifndef NO_UNAME
    85     (VOID *) memset((VOID *) &u, (int) 0, sizeof(struct utsname));
    86     if (uname(&u) > -1) {				/* INTL: Native. */
    87         hp = TclpGetHostByName(u.nodename);			/* INTL: Native. */
    88 	if (hp == NULL) {
    89 	    /*
    90 	     * Sometimes the nodename is fully qualified, but gets truncated
    91 	     * as it exceeds SYS_NMLN.  See if we can just get the immediate
    92 	     * nodename and get a proper answer that way.
    93 	     */
    94 	    char *dot = strchr(u.nodename, '.');
    95 	    if (dot != NULL) {
    96 		char *node = ckalloc((unsigned) (dot - u.nodename + 1));
    97 		memcpy(node, u.nodename, (size_t) (dot - u.nodename));
    98 		node[dot - u.nodename] = '\0';
    99 		hp = TclpGetHostByName(node);
   100 		ckfree(node);
   101 	    }
   102 	}
   103         if (hp != NULL) {
   104 	    native = hp->h_name;
   105         } else {
   106 	    native = u.nodename;
   107         }
   108     }
   109 #else
   110     /*
   111      * Uname doesn't exist; try gethostname instead.
   112      */
   113 
   114     if (gethostname(buffer, sizeof(buffer)) > -1) {	/* INTL: Native. */
   115 	native = buffer;
   116     }
   117 #endif
   118 
   119     if (native == NULL) {
   120 	hostname[0] = 0;
   121     } else {
   122 	Tcl_ExternalToUtf(NULL, NULL, native, -1, 0, NULL, hostname,
   123 		sizeof(hostname), NULL, NULL, NULL);
   124     }
   125     hostnameInited = 1;
   126     Tcl_MutexUnlock(&hostMutex);
   127     return hostname;
   128 }
   129 
   130 /*
   131  *----------------------------------------------------------------------
   132  *
   133  * TclpHasSockets --
   134  *
   135  *	Detect if sockets are available on this platform.
   136  *
   137  * Results:
   138  *	Returns TCL_OK.
   139  *
   140  * Side effects:
   141  *	None.
   142  *
   143  *----------------------------------------------------------------------
   144  */
   145 
   146 int
   147 TclpHasSockets(interp)
   148     Tcl_Interp *interp;		/* Not used. */
   149 {
   150     return TCL_OK;
   151 }
   152 
   153 /*
   154  *----------------------------------------------------------------------
   155  *
   156  * TclpFinalizeSockets --
   157  *
   158  *	Performs per-thread socket subsystem finalization.
   159  *
   160  * Results:
   161  *	None.
   162  *
   163  * Side effects:
   164  *	None.
   165  *
   166  *----------------------------------------------------------------------
   167  */
   168 
   169 void
   170 TclpFinalizeSockets()
   171 {
   172     return;
   173 }