Hostname: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 4: Line 4:


=={{header|Ada}}==
=={{header|Ada}}==
with GNAT.Sockets;
Works with GCC/GNAT
Depending on need.
with Ada.Text_IO;
<pre>
<pre>
Name : constant string := GNAT.Sockets.Host_Name);
procedure Hostname is

begin
Name : Unbounded_String;
Ada.Text_IO.Put_Line(GNAT.Sockets.Host_Name);
Name := TO_Unbounded(GNAT.Sockets.Host_Name);
end Hostname

Name : Access String;
Name := new String'(GNAT.Sockets.Host_Name);
</pre>
</pre>



Revision as of 06:26, 30 April 2008

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.

Ada

Works with GCC/GNAT Depending on need.

  Name : constant string := GNAT.Sockets.Host_Name);

  Name : Unbounded_String;
  Name  := TO_Unbounded(GNAT.Sockets.Host_Name);

  Name : Access String;
  Name := new String'(GNAT.Sockets.Host_Name);

C

Works with: gcc version 4.0.1
Works with: POSIX version .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();

Factor

host-name

IDL

 hostname = GETENV('computername')

Java

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

Perl

Works with: Perl version 5.8.6
use Sys::Hostname;

$name = hostname;

PHP

echo $_SERVER['HTTP_HOST'];

Pop11

lvars host = sys_host_name();

Python

Works with: Python version 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