Esempio n. 1
0
char *
Tcl_GetHostName()
{
#ifndef NO_UNAME
    struct utsname u;
    struct hostent *hp;
#else
    char buffer[sizeof(hostname)];
#endif
    CONST char *native;

    Tcl_MutexLock(&hostMutex);
    if (hostnameInited) {
	Tcl_MutexUnlock(&hostMutex);
        return hostname;
    }

    native = NULL;
#ifndef NO_UNAME
    (VOID *) memset((VOID *) &u, (int) 0, sizeof(struct utsname));
    if (uname(&u) > -1) {				/* INTL: Native. */
        hp = gethostbyname(u.nodename);			/* INTL: Native. */
        if (hp != NULL) {
	    native = hp->h_name;
        } else {
	    native = u.nodename;
        }
    }
#else
    /*
     * Uname doesn't exist; try gethostname instead.
     */

    if (gethostname(buffer, sizeof(buffer)) > -1) {	/* INTL: Native. */
	native = buffer;
    }
#endif

    if (native == NULL) {
	hostname[0] = 0;
    } else {
	Tcl_ExternalToUtf(NULL, NULL, native, -1, 0, NULL, hostname,
		sizeof(hostname), NULL, NULL, NULL);
    }
    hostnameInited = 1;
    Tcl_MutexUnlock(&hostMutex);
    return hostname;
}
Esempio n. 2
0
CONST char *
Tcl_GetHostName()
{
#ifndef NO_UNAME
    struct utsname u;
    struct hostent *hp;
#else
    char buffer[sizeof(hostname)];
#endif
    CONST char *native;

    Tcl_MutexLock(&hostMutex);
    if (hostnameInited) {
	Tcl_MutexUnlock(&hostMutex);
        return hostname;
    }

    native = NULL;
#ifndef NO_UNAME
    (VOID *) memset((VOID *) &u, (int) 0, sizeof(struct utsname));
    if (uname(&u) > -1) {				/* INTL: Native. */
        hp = gethostbyname(u.nodename);			/* INTL: Native. */
	if (hp == NULL) {
	    /*
	     * Sometimes the nodename is fully qualified, but gets truncated
	     * as it exceeds SYS_NMLN.  See if we can just get the immediate
	     * nodename and get a proper answer that way.
	     */
	    char *dot = strchr(u.nodename, '.');
	    if (dot != NULL) {
		char *node = ckalloc((unsigned) (dot - u.nodename + 1));
		memcpy(node, u.nodename, (size_t) (dot - u.nodename));
		node[dot - u.nodename] = '\0';
		hp = gethostbyname(node);
		ckfree(node);
	    }
	}
        if (hp != NULL) {
	    native = hp->h_name;
        } else {
	    native = u.nodename;
        }
    }
#else
    /*
     * Uname doesn't exist; try gethostname instead.
     */

    if (gethostname(buffer, sizeof(buffer)) > -1) {	/* INTL: Native. */
	native = buffer;
    }
#endif

    if (native == NULL) {
	hostname[0] = 0;
    } else {
	Tcl_ExternalToUtf(NULL, NULL, native, -1, 0, NULL, hostname,
		sizeof(hostname), NULL, NULL, NULL);
    }
    hostnameInited = 1;
    Tcl_MutexUnlock(&hostMutex);
    return hostname;
}
Esempio n. 3
0
int sn_encoded_input(char *buf, int max_size)
{
	char *rawbuf, *utf8buf;
	int srcRead, dstWrote, nbytes, flags = 0;

	static Tcl_EncodingState utf8_state, ascii_state;

	if(encoding == NULL) {
		/*
		 No translation necessary. Just look for CRLF
		 sequences and remove the CR character.
		 */
		size_t read = fread(buf, sizeof(char), max_size, yyin);
		read -= translate_crlf(buf, read);
		return read;
	}

	if(ascii == NULL) {
		ascii = Tcl_GetEncoding(NULL, "ascii");
		if(ascii == NULL) {
			fprintf(stderr, "Unable to locate `ascii' encoding\n");
			return 0;
		}
	}

	if(start_of_file) {
		flags |= TCL_ENCODING_START;
	}

	if((rawbuf = (char *) ckalloc(max_size)) == NULL) {
		/* Insufficient memory. */
		return 0;
	}

	/* FIXME: This ought to do it. */
	if((utf8buf = (char *) ckalloc(2 * max_size)) == NULL) {
		/* Insufficient memory. */
		return 0;
	}

	/* Read max_size bytes from disk. */
	nbytes = fread(rawbuf, sizeof(unsigned char), sizeof(rawbuf), yyin);
	if(nbytes == 0) {
		/*
		 Continue on with an empty buffer; this allows the Tcl
		 encoding routines to do any necessary finalisation.
		 See the Encoding(n) man page.
		 */
		flags = TCL_ENCODING_END;
	}

	/* Translate encoded file data into UTF-8. */
	Tcl_ExternalToUtf(NULL, encoding, rawbuf, nbytes, flags,
			  &utf8_state, utf8buf, 2 * max_size,
			  &srcRead, &dstWrote, NULL);

	/* Look for CRLF sequences and remove the CR characters */
	dstWrote -= translate_crlf(utf8buf, dstWrote);


	/*
	 FIXME This code assumes that an encoded stream `n' bytes long
	 will always reduce down to an ASCII stream no longer than `n'
	 bytes. This is a reasonable assumption, but probably not
	 foolproof.
	 */

	/* Translate this from UTF-8 to ASCII. */
	Tcl_UtfToExternal(NULL, ascii, utf8buf, dstWrote, flags,
			  &ascii_state, buf, max_size,
			  &srcRead, &dstWrote, NULL);

	if(dstWrote > 0 && start_of_file) {
		start_of_file = 0;
	}

	ckfree(utf8buf);
	ckfree(rawbuf);

	return dstWrote;
}