Hostname: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|J}}: add lang tags)
m (Fixed lang tags.)
Line 19: Line 19:
<!-- {{does not works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - No such library function.}} -->
<!-- {{does not works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - No such library function.}} -->
{{works with|POSIX|.1}}
{{works with|POSIX|.1}}
<lang algol>STRING hostname;
<lang algol68>STRING hostname;
get(read OF execve child pipe("/bin/hostname","hostname",""), hostname);
get(read OF execve child pipe("/bin/hostname","hostname",""), hostname);
print(("hostname: ", hostname, new line))</lang>
print(("hostname: ", hostname, new line))</lang>
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>MsgBox % A_ComputerName </lang>
<lang AutoHotkey>MsgBox % A_ComputerName</lang>


=={{header|AWK}}==
=={{header|AWK}}==
$ awk 'BEGIN{print ENVIRON["HOST"]}'
<lang awk>$ awk 'BEGIN{print ENVIRON["HOST"]}'
E51A08ZD
E51A08ZD</lang>


=={{header|C}}==
=={{header|C}}==
Line 51: Line 51:
#+sbcl (machine-instance)
#+sbcl (machine-instance)
#+clisp (let ((s (machine-instance))) (subseq s 0 (position #\Space s)))
#+clisp (let ((s (machine-instance))) (subseq s 0 (position #\Space s)))
#-(or sbcl clisp) (error "get-host-name not implemented")) </lang>
#-(or sbcl clisp) (error "get-host-name not implemented"))</lang>


{{libheader|CFFI}}
{{libheader|CFFI}}
Line 76: Line 76:


=={{header|Factor}}==
=={{header|Factor}}==
host-name
<lang factor>host-name</lang>


=={{header|Forth}}==
=={{header|Forth}}==
Line 82: Line 82:
<lang forth>include unix/socket.fs
<lang forth>include unix/socket.fs


hostname type </lang>
hostname type</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==
Line 100: Line 100:
=={{header|Haskell}}==
=={{header|Haskell}}==
{{libheader|network}}
{{libheader|network}}
import Network.BSD
<lang haskell>import Network.BSD
main = do hostName <- getHostName
main = do hostName <- getHostName
putStrLn hostName
putStrLn hostName</lang>


=={{header|Icon}}==
=={{header|Icon}}==


procedure main()
<lang icon>procedure main()
write(&host)
write(&host)
end
end</lang>


=={{header|IDL}}==
=={{header|IDL}}==
hostname = GETENV('computername')
<lang idl>hostname = GETENV('computername')</lang>


=={{header|J}}==
=={{header|J}}==
Line 127: Line 127:
}
}
}
}
} </lang>
}</lang>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Line 156: Line 156:
Similarly to [[Discover the Hostname#MATLAB|MATLAB]], we could call system command <tt>hostname</tt> to know the hostname. But we can also call the internal function <tt>uname()</tt> which returns a structure holding several informations, among these the hostname (nodename):
Similarly to [[Discover the Hostname#MATLAB|MATLAB]], we could call system command <tt>hostname</tt> to know the hostname. But we can also call the internal function <tt>uname()</tt> which returns a structure holding several informations, among these the hostname (nodename):


<lang octave>uname().nodename </lang>
<lang octave>uname().nodename</lang>


=={{header|Perl}}==
=={{header|Perl}}==
Line 176: Line 176:


=={{header|PL/SQL}}==
=={{header|PL/SQL}}==
<lang PL/SQL>
<lang plsql>SET serveroutput on
SET serveroutput on
BEGIN
BEGIN
DBMS_OUTPUT.PUT_LINE(UTL_INADDR.GET_HOST_NAME);
DBMS_OUTPUT.PUT_LINE(UTL_INADDR.GET_HOST_NAME);
END;
END;</lang>
</lang>


=={{header|Pop11}}==
=={{header|Pop11}}==
lvars host = sys_host_name();
<lang pop11>lvars host = sys_host_name();</lang>


=={{header|PowerBASIC}}==
=={{header|PowerBASIC}}==
Line 208: Line 206:
=={{header|R}}==
=={{header|R}}==
Sys.info provides information about the platform that R is running on. The following code returns the hostname as a string.
Sys.info provides information about the platform that R is running on. The following code returns the hostname as a string.
<lang R>Sys.info()[["nodename"]] </lang>
<lang R>Sys.info()[["nodename"]]</lang>
Note that Sys.info isn't available on all platforms. As an alternative, you can call an OS command. The following code prints the hostname to the console (or other connection), but returns an int.
Note that Sys.info isn't available on all platforms. As an alternative, you can call an OS command. The following code prints the hostname to the console (or other connection), but returns an int.
<lang R>system("hostname") </lang>
<lang R>system("hostname")</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 233: Line 231:


=={{header|Toka}}==
=={{header|Toka}}==
2 import gethostname
<lang toka>2 import gethostname
1024 chars is-array foo
1024 chars is-array foo
foo 1024 gethostname
foo 1024 gethostname
foo type
foo type</lang>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
hostname
<lang bash>hostname</lang>
or
or
uname -n
<lang bash>uname -n</lang>


=={{header|Ursala}}==
=={{header|Ursala}}==

Revision as of 23:17, 19 November 2009

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 <lang ada>with Ada.Text_IO; use Ada.Text_IO; with GNAT.Sockets;

procedure Demo is begin

  Put_Line (GNAT.Sockets.Host_Name);

end Demo;</lang>

ALGOL 68

Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386
Works with: POSIX version .1

<lang algol68>STRING hostname; get(read OF execve child pipe("/bin/hostname","hostname",""), hostname); print(("hostname: ", hostname, new line))</lang>

AutoHotkey

<lang AutoHotkey>MsgBox % A_ComputerName</lang>

AWK

<lang awk>$ awk 'BEGIN{print ENVIRON["HOST"]}' E51A08ZD</lang>

C

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

<lang 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;

}</lang>

C#

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

Common Lisp

Another operating system feature that is implemented differently across lisp implementations. Here we show how to create a function that obtains the required result portably by working differently for each supported implementation. This technique is heavily used to make portable lisp libraries. <lang lisp>(defun get-host-name ()

   #+sbcl (machine-instance)
   #+clisp (let ((s (machine-instance))) (subseq s 0 (position #\Space s)))
   #-(or sbcl clisp) (error "get-host-name not implemented"))</lang>
Library: CFFI

Another way is to use the FFI to access POSIX' gethostname(2):

<lang lisp>(cffi:defcfun ("gethostname" c-gethostname) :int

 (buf :pointer) (len :unsigned-long))

(defun get-hostname ()

 (cffi:with-foreign-object (buf :char 256)
   (unless (zerop (c-gethostname buf 256))
     (error "Can't get hostname"))
   (values (cffi:foreign-string-to-lisp buf))))</lang>

<lang lisp>BOA> (get-hostname) "aurora"</lang>

E

<lang e>makeCommand("hostname")()[0].trim()</lang>

Not exactly a good way to do it. A better way ought to be introduced along with a proper socket interface.

Factor

<lang factor>host-name</lang>

Forth

Works with: GNU Forth version 0.7.0

<lang forth>include unix/socket.fs

hostname type</lang>

Fortran

Works with: gfortran

The function/subroutine HOSTNM is a GNU extension. <lang fortran>program HostTest

 character(len=128) :: name 
 call hostnm(name)
 print *, name

end program HostTest</lang>

Groovy

<lang groovy>println InetAddress.localHost.hostName</lang>

Haskell

Library: network

<lang haskell>import Network.BSD main = do hostName <- getHostName

         putStrLn hostName</lang>

Icon

<lang icon>procedure main()

 write(&host)

end</lang>

IDL

<lang idl>hostname = GETENV('computername')</lang>

J

<lang j> > {: sdgethostname coinsert 'jsocket' [ load 'socket'</lang>

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>

JavaScript

Works with: JScript

<lang javascript>var network = new ActiveXObject('WScript.Network'); var hostname = network.computerName; WScript.echo(hostname);</lang>

MATLAB

<lang Matlab>system('hostname')</lang>

Modula-3

<lang modula3>MODULE Hostname EXPORTS Main;

IMPORT IO, OSConfig;

BEGIN

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

END Hostname.</lang>

Objective-C

<lang objc>NSLog(@"%@", [[NSHost currentHost] name]);</lang>

OCaml

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

Octave

Similarly to MATLAB, we could call system command hostname to know the hostname. But we can also call the internal function uname() which returns a structure holding several informations, among these the hostname (nodename):

<lang octave>uname().nodename</lang>

Perl

Works with: Perl version 5.8.6

<lang perl>use Sys::Hostname;

$name = hostname;</lang>

PHP

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

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

Works with: PHP version 5.3+

<lang php>echo gethostname();</lang>


PL/SQL

<lang plsql>SET serveroutput on BEGIN

 DBMS_OUTPUT.PUT_LINE(UTL_INADDR.GET_HOST_NAME);  

END;</lang>

Pop11

<lang pop11>lvars host = sys_host_name();</lang>

PowerBASIC

This retreives the localhost's name:

<lang powerbasic>HOST NAME TO hostname$</lang>

This attempts to retreive the name of any given machine on the network:

<lang powerbasic>HOST NAME ipAddress& TO hostname$</lang>

PowerShell

Windows systems have the ComputerName environment variable which can be used: <lang powershell>$Env:COMPUTERNAME</lang> Also PowerShell can use .NET classes and methods: <lang powershell>[Net.Dns]::GetHostName()</lang>

Python

Works with: Python version 2.5

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

R

Sys.info provides information about the platform that R is running on. The following code returns the hostname as a string. <lang R>Sys.info()"nodename"</lang> Note that Sys.info isn't available on all platforms. As an alternative, you can call an OS command. The following code prints the hostname to the console (or other connection), but returns an int. <lang R>system("hostname")</lang>

Ruby

<lang ruby>require 'socket' host = Socket.gethostname</lang>

Scheme

Works with: Chicken Scheme

<lang scheme>(use posix) (get-host-name)</lang>

Slate

<lang slate>Platform current nodeName</lang>

Standard ML

<lang sml>NetHostDB.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:

<lang Tcl>set hname [info hostname]</lang>

Toka

<lang toka>2 import gethostname 1024 chars is-array foo foo 1024 gethostname foo type</lang>

UNIX Shell

<lang bash>hostname</lang> or <lang bash>uname -n</lang>

Ursala

The user-defined hostname function ignores its argument and returns a string. <lang Ursala>#import cli

hostname = ~&hmh+ (ask bash)/<>+ <'hostname'>!</lang> For example, the following function returns the square root of its argument if it's running on host kremvax, but otherwise returns the square. <lang Ursala>#import flo

creative_accounting = (hostname== 'kremvax')?(sqrt,sqr)</lang>