Hostname: Difference between revisions
No edit summary |
No edit summary |
||
Line 27: | Line 27: | ||
{{works with|gcc|4.0.1}} |
{{works with|gcc|4.0.1}} |
||
{{works with|POSIX|.1}} |
{{works with|POSIX|.1}} |
||
<c>#include <limits.h> |
<lang c>#include <limits.h> |
||
#include <unistd.h> |
#include <unistd.h> |
||
Line 35: | Line 35: | ||
gethostname(name, _POSIX_HOST_NAME_MAX+1); |
gethostname(name, _POSIX_HOST_NAME_MAX+1); |
||
return 0; |
return 0; |
||
}</ |
}</lang> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<csharp>System.Net.Dns.GetHostName();</ |
<lang csharp>System.Net.Dns.GetHostName();</lang> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 62: | Line 62: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
<java> |
<lang java> |
||
import java.net.*; |
import java.net.*; |
||
Line 74: | Line 74: | ||
} |
} |
||
} |
} |
||
</ |
</lang> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
Line 88: | Line 88: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<ocaml>Unix.gethostname()</ |
<lang ocaml>Unix.gethostname()</lang> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{works with|Perl|5.8.6}} |
{{works with|Perl|5.8.6}} |
||
{{libheader|Sys::Hostname}} |
{{libheader|Sys::Hostname}} |
||
<perl>use Sys::Hostname; |
<lang perl>use Sys::Hostname; |
||
$name = hostname;</ |
$name = hostname;</lang> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<php>echo $_SERVER['HTTP_HOST'];</ |
<lang php>echo $_SERVER['HTTP_HOST'];</lang> |
||
<php>echo php_uname('n');</ |
<lang php>echo php_uname('n');</lang> |
||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
Line 107: | Line 107: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|2.5}} |
{{works with|Python|2.5}} |
||
<python>import socket |
<lang python>import socket |
||
host = socket.gethostname()</ |
host = socket.gethostname()</lang> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<ruby>require 'socket' |
<lang ruby>require 'socket' |
||
host = Socket.gethostname</ |
host = Socket.gethostname</lang> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
Revision as of 15:30, 3 February 2009
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
<lang c>#include <limits.h>
- include <unistd.h>
int main() {
char name[_POSIX_HOST_NAME_MAX+1]; gethostname(name, _POSIX_HOST_NAME_MAX+1); return 0;
}</lang>
C#
<lang csharp>System.Net.Dns.GetHostName();</lang>
Factor
host-name
Haskell
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
<lang 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. } }
} </lang>
Modula-3
MODULE Hostname EXPORTS Main; IMPORT IO, OSConfig; BEGIN IO.Put(OSConfig.HostName() & "\n"); END Hostname.
OCaml
<lang ocaml>Unix.gethostname()</lang>
Perl
<lang perl>use Sys::Hostname;
$name = hostname;</lang>
PHP
<lang php>echo $_SERVER['HTTP_HOST'];</lang>
<lang php>echo php_uname('n');</lang>
Pop11
lvars host = sys_host_name();
Python
<lang python>import socket host = socket.gethostname()</lang>
Ruby
<lang ruby>require 'socket' host = Socket.gethostname</lang>
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