78
\$\begingroup\$

Challenge

Create a program that returns a truthy value when run on Microsoft Windows (for simplicity we'll stick with Windows 7, 8.1 and 10) and a falsey value when run on any other operating system (OSX, FreeBSD, Linux).

Rules

  • Code that fails to run/compile on a platform doesn't count as a falsey value.

Winning criteria

I'm labelling this as , so lowest score wins, but I'm also very interested in seeing creative solutions to this problem.

\$\endgroup\$
  • 7
    \$\begingroup\$ Can the programs output by exit code? (normally allowed) \$\endgroup\$ – FlipTack Jan 8 '17 at 12:33
  • 13
    \$\begingroup\$ Can you give a definite list of which operating systems this needs to work on? \$\endgroup\$ – FlipTack Jan 8 '17 at 14:46
  • 2
    \$\begingroup\$ What should the result be under Windows RT? \$\endgroup\$ – Adám Jan 8 '17 at 18:56
  • 6
    \$\begingroup\$ You 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. \$\endgroup\$ – jpmc26 Jan 9 '17 at 11:12
  • 2
    \$\begingroup\$ We 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. \$\endgroup\$ – user62131 Jan 10 '17 at 6:17

49 Answers 49

3
\$\begingroup\$

FPC, 61 chars

begin{$ifdef win32}write('f');{$else}write('nf');{$endif}end;
\$\endgroup\$
  • \$\begingroup\$ you can shave some bytes by using only one write begin write({$ifdef win32}1{$else}0{$endif});end. \$\endgroup\$ – hdrz Jan 9 '17 at 6:21
  • \$\begingroup\$ Or to work on win64 as well: begin write({$ifdef windows}1{$else}0{$endif})end. \$\endgroup\$ – hdrz Jan 9 '17 at 6:42
  • \$\begingroup\$ @hdrz, yes, you right, thanks ;) \$\endgroup\$ – user64239 Jan 9 '17 at 7:47
  • \$\begingroup\$ @hdrz, sure that would be correct? 1 and 0 are not truthy/falsey in Pascal. I would go with begin write(1={$ifdef windows}1{$endif}+0)end. \$\endgroup\$ – manatwork Jan 9 '17 at 8:02
  • \$\begingroup\$ @manatwork, FPC and Delphi supports various types in writeln. And boolean is can be only 0 or 1 in pascal \$\endgroup\$ – user64239 Jan 9 '17 at 8:21
3
\$\begingroup\$

LibreOffice Calc / OpenOffice Calc, 21 bytes

Code:

=INFO("system")="WNT"

Microsoft Excel, 23 bytes

Code:

=INFO("system")="pcdos"

Result:

Returns TRUE if Windows; FALSE otherwise.

\$\endgroup\$
  • \$\begingroup\$ +1 for LibreOffice \$\endgroup\$ – ElPedro Oct 8 '18 at 16:48
2
\$\begingroup\$

tcl, 51

puts [string match windows $tcl_platform(platform)]

I don't have a Windows machine online, but on http://rextester.com/live/OVTY1488 replace windows by unix to see it output 1 instead of 0.

2nd attempt:

tcl, 40

puts [string match W* $tcl_platform(os)]

assuming Windows is the only system the name begins on a W.

3rd attempt:

tcl, 26

puts [info exists env(OS)]

assuming Windows is the only system the OS environment variable is defined.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 38 bytes

Stealing the code from here:

exit (RUBY_PLATFORM=~/(?<!r)win/)!=nil

Return 0 on OK, to keep with shell conventions. We need the funny (?<!r) negative look-behind to avoid matching darwin, although if Microsoft device to make a vrwin Windows version for virtual reality the code will fail... On JRuby, though, this doesn't work so well, so instead:

Ruby including JRuby, 52 bytes

exit (RbConfig::CONFIG['host_os']=~/(?<!r)win/)!=nil

I don't have raw Windows, just Cygwin, but if the host_os is windows then we need to distinguish nil and 0.

Ruby plus gem install os, 25 (+9?)

This needs an extra gem, so we might have to add an extra 9 bytes for gem i os at the command line:

require 'os'
exit OS.windows?

This also avoids the darwin problem completely!

\$\endgroup\$
  • \$\begingroup\$ According to other answer in the question you linked to, your code will reach the conclusion that “darwin” is also “win”. If the question not requires otherwise, by default the answers are expected to be either full programs or functions (or whatever callable entities exist in your language), which should return or output the result explicitly or rely on the interpreter doing it implicitly. As it looks now, your code is a snippet, leaving the generated value in the memory. \$\endgroup\$ – manatwork Jan 11 '17 at 7:33
  • \$\begingroup\$ Oops! Filtering out darwin makes things lengthier. And let me add an exit... \$\endgroup\$ – Ken Y-N Jan 11 '17 at 8:20
2
\$\begingroup\$

PHP, 18 bytes

<?=!(PHP_EOL^'=');

Explanation

PHP_EOL, as its name might suggest, returns the line separator on a given system. On Windows, this is CRLF, and on other platforms, this is LF.

^ is binary XOR. '=' XOR "\r" is '0', and PHP's string bitwise operations truncate to the length of the shortest string, so "\r\n" XOR '=' is also '0'.

! is boolean NOT. In PHP, "0" is considered falsy. So, when we're on Windows, PHP will negate this to true. When we're not on Windows, we get a truthy value ('7') which PHP negates to false.

<?= is the short opening tag for echo.

\$\endgroup\$
2
\$\begingroup\$

IBM/Lotus Notes Formula Language, 22 21 bytes

@Like(@Platform;"W%")

Previous versions:

@Left(@Platform;1)="W"

or

@Begins(@Platform;"W")

Computed field formula on a Notes form. @Platform returns 1 of:

AIX/64
Linux/64
Macintosh
OS/400®
UNIX
Windows/32
Windows/64

So the formula returns 1 (@True) if the platform starts with "W" and 0 (@False) if not.

\$\endgroup\$
2
\$\begingroup\$

Ruby 2, 17 14 bytes

17 byte solution: Gem.win_platform? This only works on the newer versions of ruby, as in the older versions you would have to require rubygems.


14 byte solution:

system 'del a' This command outputs true if on windows, because there is a command called del in Windows, but outputs nil (falsey) on Linux and Mac because there is no such command as del in Unix. Oh, and if you are on windows it will delete a file called a in your current directory, that's an unintended side effect :P

\$\endgroup\$
2
\$\begingroup\$

Powershell 6, 10 bytes

$isWindows

Powershell 6 intoduce 3 predefined variables: $isWindows, $isLinux and $IsMacOs.

\$\endgroup\$
1
\$\begingroup\$

Elixir, 19 bytes

{:win32,_}=:os.type

Exit code 0 for Windows, 1 for others.

\$\endgroup\$
1
\$\begingroup\$

Scala, 71 bytes

object W extends App{print(sys.env.get("OS").get.contains("Windows"))}
\$\endgroup\$
1
\$\begingroup\$

Lua, 21 Bytes

os.execute"dir c:\\"

It will error if not on windows and list the C:\ directory if on windows.

\$\endgroup\$
  • \$\begingroup\$ Welcome to Programming Puzzles and Code Golf! \$\endgroup\$ – Pavel Jan 13 '17 at 4:55
1
\$\begingroup\$

Dartlang, 43 bytes

import'dart:io';main()=>Platform.isWindows;

First answer one here, so I hope this is okay :)

\$\endgroup\$
1
\$\begingroup\$

D, 43 bytes

int f(){version(Windows)return 0;return 1;}

D ... is horrible at golfing. But it does have a version construct, so I thought I'd post an answer in D.

And ... there's really no point in putting a TIO link, since TIO is ran on only one type of OS.

\$\endgroup\$
1
\$\begingroup\$

SQL 2017, 61 bytes

SELECT IIF(host_platform='Linux',0,1)FROM sys.dm_os_host_info

SQL 2017 is the first MS SQL version that can run on a platform other than Windows. The system view sys.dm_os_host_info returns either Linux or Windows, so (at least in this version) anything that isn't on Linux is on Windows.

\$\endgroup\$
  • \$\begingroup\$ Nice solution!! \$\endgroup\$ – Daniel Oct 18 '18 at 13:44
1
\$\begingroup\$

Python 3, 29 bytes

import os;exit(os.name!='nt')

Probably should have tryed that first....

Previous answer (41 bytes)

Two answers I found that are the same size

import os;exit(-hasattr(os,"P_DETACH")+1)

and

import sys;exit(-hasattr(sys,"winver")+1)

also -+1 is a neat way to negate the output, gonna keep that one noted

\$\endgroup\$
  • \$\begingroup\$ -n+1 <-> 1-n? \$\endgroup\$ – Jonathan Frech Oct 20 '18 at 19:52
  • \$\begingroup\$ @JonathanFrech true, although I found even more savings by not being fancy :( \$\endgroup\$ – famous1622 Oct 20 '18 at 20:02
0
\$\begingroup\$

Racket, 26 17 bytes

Shameless plug for the TCL answer I saw that used the OS environment variable.

(and(getenv"OS"))

(Old answer)

Pretty straight forward. system-type with no argument returns a symbol indicating the system operating system type.

(eq?'windows(system-type))
\$\endgroup\$
0
\$\begingroup\$

Nim, 37 bytes

quit when defined(windows): 0 else: 1

Returns exit code 0 for Windows, 1 for all other operating systems. Uses conditional compilation with when.

\$\endgroup\$
0
\$\begingroup\$

PowerShell, 17

gdr|? P* -like *y

Abusing the fact that there's no Registry PSProvider on non-Windows systems.

\$\endgroup\$
0
\$\begingroup\$

Node.js, 23 bytes

_=>os.platform=="win32"
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.