Host introspection: Difference between revisions

Content added Content deleted
(→‎{{header|Go}}: Put back completely non-portable /proc hack but and some comments)
Line 1: Line 1:
{{task|Programming environment operations}}
{{task|Programming environment operations}}
{{omit from|Brlcad}}
{{omit from|E}}
{{omit from|JavaScript}}
{{omit from|Maxima}}
{{omit from|ML/I}}
{{omit from|Openscad}}
{{omit from|TPP}}
{{omit from|Unlambda}}

Print the [[wp:Word_size#Word_size_choice|word size]] and [[wp:Endianness|endianness]] of the host machine.
Print the [[wp:Word_size#Word_size_choice|word size]] and [[wp:Endianness|endianness]] of the host machine.


See also: [[Variable size/Get]]
See also: [[Variable size/Get]]

=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
Line 12: Line 22:
Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
Put_Line ("Endianness " & Bit_Order'Image (Default_Bit_Order));
end Host_Introspection;</lang>
end Host_Introspection;</lang>

Sample output on a Pentium machine:
{{out|Sample output on a Pentium machine}}
<pre>
<pre>
Word size 32
Word size 32
Endianness LOW_ORDER_FIRST
Endianness LOW_ORDER_FIRST
</pre>
</pre>

=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
Line 46: Line 58:
OD;
OD;
printf(($"int byte order: "g,", Hex:",16r8dl$,int byte order, BIN abcdi))</lang>
printf(($"int byte order: "g,", Hex:",16r8dl$,int byte order, BIN abcdi))</lang>
Output (Intel i686):
{{out}} (Intel i686):
<pre>
<pre>
states per bit: 2
states per bit: 2
Line 198: Line 210:
4</lang>
4</lang>


In the case of endianness, Erlang's bit syntax by default has a 'native' option which lets you use what is supported natively. As such, there is no function to find endianness. However, one could write one by using bit syntax, setting endianness and then comparing to the native format:
In the case of endianness, Erlang's bit syntax by default has a 'native' option which lets you use what is supported natively.
As such, there is no function to find endianness.
However, one could write one by using bit syntax, setting endianness and then comparing to the native format:


<lang erlang>1> <<1:4/native-unit:8>>.
<lang erlang>1> <<1:4/native-unit:8>>.
Line 387: Line 401:
println "endianness: ${System.getProperty('sun.cpu.endian')}"</lang>
println "endianness: ${System.getProperty('sun.cpu.endian')}"</lang>


{{out}}
Output:
<pre>word size: 64
<pre>word size: 64
endianness: little</pre>
endianness: little</pre>
Line 457: Line 471:
$SystemWordLength "bits"</lang>
$SystemWordLength "bits"</lang>


x86 Output:
{{out}} x86
<pre>
<pre>
Little endian
Little endian
Line 488: Line 502:
</lang>
</lang>


{{out}}
Output:
<pre> octave:128> computer
<pre> octave:128> computer
x86_64-unknown-linux-gnu
x86_64-unknown-linux-gnu
octave:129> endian
octave:129> endian
endian = little</pre>
endian = little</pre>





Line 510: Line 523:
END Host.</lang>
END Host.</lang>


Output (on an x86):
{{out}} (on an x86):
<pre>
<pre>
Word Size: 32
Word Size: 32
Endianness: Little
Endianness: Little
</pre>
</pre>
'''Bold text'''


=={{header|NetRexx}}==
=={{header|NetRexx}}==
Line 645: Line 657:
writeln('This host is little endian.');
writeln('This host is little endian.');
end.</lang>
end.</lang>
{{out}}
Output:
<pre>
<pre>
>: ./HostIntrospection
>: ./HostIntrospection
Line 656: Line 668:
<lang perl>use Config;
<lang perl>use Config;
print "UV size: $Config{uvsize}, byte order: $Config{byteorder}\n";</lang>
print "UV size: $Config{uvsize}, byte order: $Config{byteorder}\n";</lang>
{{out}}
Example output:
<pre>
<pre>
UV size: 4, byte order: 1234
UV size: 4, byte order: 1234
Line 671: Line 683:
}
}
say "UV size: $size, byte order: $order ($end-endian)";</lang>
say "UV size: $size, byte order: $order ($end-endian)";</lang>
{{out}}
Example outputs:
<pre>
<pre>
UV size: 4, byte order: 1234 (little-endian)
UV size: 4, byte order: 1234 (little-endian)
Line 704: Line 716:
(2 "Big endian")
(2 "Big endian")
(T "Bad EI_DATA") ) ) )</lang>
(T "Bad EI_DATA") ) ) )</lang>
{{out}}
Output:
<pre>64 bits
<pre>64 bits
Little endian</pre>
Little endian</pre>
Line 722: Line 734:
end details;
end details;
</lang>
</lang>
{{out}}
output:
<pre>
<pre>
word size= 32
word size= 32
Line 736: Line 748:
Write-Host Big-Endian
Write-Host Big-Endian
}</lang>
}</lang>
Note that endianness is essentially a moot point with PowerShell, as there is only a Windows implementation currently and current Windows versions don't run on big-endian systems. But in theory this check should work.
Note that endianness is essentially a moot point with PowerShell,
as there is only a Windows implementation currently
and current Windows versions don't run on big-endian systems.
But in theory this check should work.

=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Enumeration
<lang PureBasic>Enumeration
Line 840: Line 856:


=={{header|Scala}}==
=={{header|Scala}}==
[[Category:Scala Implementations]]
{{libheader|Scala}}<lang Scala>import java.nio.ByteOrder
{{libheader|Scala}}<lang Scala>import java.nio.ByteOrder


Line 889: Line 904:
<lang slate>inform: 'Endianness: ' ; Platform current endianness.
<lang slate>inform: 'Endianness: ' ; Platform current endianness.
inform: 'Word Size: ' ; (Platform current bytesPerWord * 8) printString.</lang>
inform: 'Word Size: ' ; (Platform current bytesPerWord * 8) printString.</lang>
{{out}}
Output:
<pre>
<pre>
Endianness: LittleEndian
Endianness: LittleEndian
Line 929: Line 944:
]</lang>
]</lang>


{{out}}
Output:
<pre>
<pre>
4
4
Little endian
Little endian
</pre>
</pre>

{{omit from|Brlcad}}
{{omit from|E}}
{{omit from|JavaScript}}
{{omit from|Maxima}}
{{omit from|ML/I}}
{{omit from|Openscad}}
{{omit from|TPP}}
{{omit from|Unlambda}}