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
<lang algol>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
$ awk 'BEGIN{print ENVIRON["HOST"]}' E51A08ZD
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>
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>
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
host-name
Forth
<lang forth> include unix/socket.fs
hostname type </lang>
Fortran
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>
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>
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>
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>
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
<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
2 import gethostname 1024 chars is-array foo foo 1024 gethostname foo type
UNIX Shell
hostname
or
uname -n
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>