Hostname: Difference between revisions

From Rosetta Code
Content added Content deleted
(Modula-3)
No edit summary
Line 99: Line 99:
=={{header|PHP}}==
=={{header|PHP}}==
<php>echo $_SERVER['HTTP_HOST'];</php>
<php>echo $_SERVER['HTTP_HOST'];</php>

<php>echo php_uname('n');</php>


=={{header|Pop11}}==
=={{header|Pop11}}==
Line 122: Line 124:
foo 1024 gethostname
foo 1024 gethostname
foo type
foo type

=={{header|UNIX Shell}}==
hostname
or
uname -n

Revision as of 05:45, 31 December 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

  with GNAT.Sockets;
  ....
  Name : constant string := GNAT.Sockets.Host_Name);
--------
  with GNAT.Sockets
  with Ada.Strings.Unbounded;use Ada.Strings.Unbounded;
  ...
  Name : Unbounded_String;
  ...
  Name  := TO_Unbounded(GNAT.Sockets.Host_Name);
---------
  with GNAT.Sockets
  ...
  Name : Access String;
  ...
  Name := new String'(GNAT.Sockets.Host_Name);

C

Works with: gcc version 4.0.1
Works with: POSIX version .1

<c>#include <limits.h>

  1. include <unistd.h>

int main() {

   char name[_POSIX_HOST_NAME_MAX+1];
   gethostname(name, _POSIX_HOST_NAME_MAX+1);
   return 0;

}</c>

C#

<csharp>System.Net.Dns.GetHostName();</csharp>

Factor

host-name

Haskell

Library: network
import Network.BSD
main = do hostName <- getHostName
          putStrLn hostName

Icon

procedure main()
  write(&host)
end

IDL

 hostname = GETENV('computername')

J

 > {: sdgethostname coinsert 'jsocket' [ load 'socket'

Java

<java> import java.net.*;

class DiscoverHostName {

   public static void main(String[] args) {
       try {
           String hostName = InetAddress.getLocalHost().getHostName();
           System.out.println(hostName);
       } catch (UnknownHostException e) { // Doesn't actually happen, but Java requires it be handled.
       }
   }

} </java>

Modula-3

MODULE Hostname EXPORTS Main;

IMPORT IO, OSConfig;

BEGIN
  IO.Put(OSConfig.HostName() & "\n");
END Hostname.

OCaml

<ocaml>Unix.gethostname()</ocaml>

Perl

Works with: Perl version 5.8.6

<perl>use Sys::Hostname;

$name = hostname;</perl>

PHP

<php>echo $_SERVER['HTTP_HOST'];</php>

<php>echo php_uname('n');</php>

Pop11

lvars host = sys_host_name();

Python

Works with: Python version 2.5

<python>import socket host = socket.gethostname()</python>

Ruby

require '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

UNIX Shell

hostname

or

uname -n