PHP, 22 bytes
`<?=PATH_SEPARATOR>":";`
prints 1
if the path separator is semicolon (colon or empty for all other OSs except for DOS and OS/2), else nothing.
also 22 bytes, but not that safe:
<?=strpos(__FILE__,92);
prints a positive integer if the file path contains a backslash; else nothing.
A safe alternative with 27 bytes: <?=DIRECTORY_SEPARATOR>"/";
prints 1
or nothing.
A strange find: <?=__FILE__[1]==":";
(20 bytes) should be, not safe either, but ok.
But although __FILE__
pretends to be a string (I tried var_dump
and gettype
), indexing it throws an error, unless you copy it somewhere else (concatenation also works) or use it as a function parameter.
Edit:
<?=(__FILE__)[1]==":";
(also 22 bytes) works in PHP 7; but that´s because the parentheses copy the constant´s value to a temporary variable.
27 bytes: <?=stripos(PHP_OS,win)===0;
tests if predefined PHP_OS
constant starts with win
(case insensitive; Windows,WIN32,WINNT, but not CYGWIN or Darwin); prints 1
for Windows, else nothing.
17/18 bytes:
<?=strlen("
")-1;
prints 1
if it was stored with Windows linebreak (also on DOS, OS/2 and Atari TOS - although I doubt that anyone ever compiled PHP for TOS), else 0
.
You could also check the constant PHP_EOL
.
more options:
PHP_SHLIB_SUFFIX
is dll
on Windows, but not necessarily only there.
php_uname()
returns info on the operating system and more; starts with Windows
for Windows.
$_SERVER['HTTP_USER_AGENT']
will contain Windows
when called in a browser on Windows.
<?=defined(PHP_WINDOWS_VERSION_BUILD);
(38 bytes) works in PHP>=5.3
conclusion
The only failsafe way to tell if it´s really Windows, not anything looking like it, seems to be a check on the OS name. For PHP: php_os()
may be disabled for security reasons; but PHP_OS
will probably always contain the desired info.
7
Can the programs output by exit code? (normally allowed)
– FlipTack – 2017-01-08T12:33:08.957I'm gonna say yes @FlipTack – Daniel – 2017-01-08T13:43:32.780
14Can you give a definite list of which operating systems this needs to work on? – FlipTack – 2017-01-08T14:46:51.930
is
True
andFalse
valid outputs? – Brad Gilbert b2gills – 2017-01-08T15:52:28.7372What should the result be under Windows RT? – Adám – 2017-01-08T18:56:17.787
1Possible duplicate – Digital Trauma – 2017-01-08T19:47:48.480
7You may want to specify a few specific non-Windows systems that must be supported. There's some debates in comments about things like DOS and OS2. – jpmc26 – 2017-01-09T11:12:33.543
2We probably need a consensus about what counts as truthy and falsey for exit codes; the normal convention is 0 for true and anything else for false, but many answers are treating it as the opposite, and the "if statement" definition doesn't obviously apply. – None – 2017-01-10T06:17:52.287
1Such discrimination... Windows? Shudder :) – RudolfJelin – 2017-01-10T19:15:42.273
English: 1 thought: Are you looking at a Blue Screen Of Death? – Dennis Jaheruddin – 2017-01-12T14:36:35.843
WIN32 macros. Its magic! – Matthew Roh – 2017-02-13T14:37:45.303
I wonder if ReactOS gives false positives from some of these answers – Stan Strum – 2018-05-25T00:32:34.100