Hostname: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Toka)
(→‎[[Toka]]: Slightly more readable example)
Line 62: Line 62:


2 import gethostname
2 import gethostname
1024 malloc is-data foo
1024 chars is-array foo
foo 1024 gethostname
foo 1024 gethostname
foo type
foo type

Revision as of 06:39, 11 September 2007

Task
Hostname
You are encouraged to solve this task according to the task description, using any language you may know.

Find the name of the host on which the routine is running.

C

Compiler: GCC 4.0.1

Standard: POSIX.1

#include <limits.h>
#include <unistd.h>

int main()
{
    char name[_POSIX_HOST_NAME_MAX+1];
    gethostname(name, _POSIX_HOST_NAME_MAX+1);
}

C#

System.Net.Dns.GetHostName();

IDL

 hostname = GETENV('computername')

Java

 String hostname = java.net.InetAddress.getLocalHost().getHostName();

Perl

Interpreter: perl 5.8.6

use Sys::Hostname;

$name = hostname;

Pop11

lvars host = sys_host_name();

Python

Interpreter: Python 2.5

 import socket
 host = socket.gethostname()

Tcl

The basic introspection tool in TCL is the info command. It can be used to find out about the version of the current Tcl or Tk, the available commands and libraries, variables, functions, the level of recursive interpreter invocation, and, amongst a myriad other things, the name of the current machine:

 set hname [info hostname]

Toka

2 import gethostname
1024 chars is-array foo
foo 1024 gethostname
foo type