Detect MS Windows

80

10

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.

Daniel

Posted 2017-01-08T11:53:58.970

Reputation: 1 808

7

Can the programs output by exit code? (normally allowed)

– FlipTack – 2017-01-08T12:33:08.957

I'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 and False valid outputs? – Brad Gilbert b2gills – 2017-01-08T15:52:28.737

2What 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

Answers

78

Vim, 2 bytes

<C-a>1

On Windows, <C-a> (ctrl+a) is mapped by default to Select All. If you type a 1 in select mode in Windows, it replaces the selection with what you typed (1) leaving a 1 in the buffer.

On other operating systems, <C-a> by default is mapped to Increment number. Because there's no number to increment, it's a no-op, and then the 1 increases the count but in terms of the buffer is a no-op.

1 is truthy in Vim, and an empty string is falsy

nmjcman101

Posted 2017-01-08T11:53:58.970

Reputation: 3 274

Looks like 3 keystrokes. Ctrl+a+1 – Pavel – 2017-01-08T23:51:36.067

12

I think per this meta post http://meta.codegolf.stackexchange.com/questions/8995/how-should-vim-answers-be-scored Vim answers are generally scored without the modifiers (especially given that the first answer on the post uses <ctrl+a> as an example for 1 byte)

– nmjcman101 – 2017-01-08T23:54:43.657

4@Pavel it's Ctrl + a, 1. If it were Ctrl + a + 1 it'd be counted as 1 keystroke. – Captain Man – 2017-01-09T21:34:18.940

7Beautiful, I love this answer! – James – 2017-01-10T07:08:00.653

That's rather elegant, I like it. – Dan – 2017-01-11T18:20:01.917

109

MATLAB, 4 bytes

ispc

From the documentation:

tf = ispc returns logical 1 (true) if the version of MATLAB® software is for the Microsoft® Windows® platform. Otherwise, it returns logical 0 (false).

There are also the functions ismac and isunix. I'll leave it to the reader to figure out what those functions do. Mego kindly asked for diagrams explaining ismac and isunix so I've tried to illustrate it here:

enter image description here

It was not asked for a diagram of ispc but I can reveal that the behaviour is pretty similar, except substitute OSX and Unix with Windows.


Second approach:

Here's a second approach with getenv using 23 bytes that should be bullet proof, unless there's another operating system starting with W:

x=getenv('OS');x(1)==87

getenv 'name' searches the underlying operating system environment list for text of the form name=value, where name is the input character vector. If found, MATLAB® returns the character vector value. If the specified name cannot be found, an empty matrix is returned.

Stewie Griffin

Posted 2017-01-08T11:53:58.970

Reputation: 43 471

4

Comments are not for extended discussion; this conversation has been moved to chat.

– Dennis – 2017-01-11T20:46:08.757

55

Python 2.7.10, 24 bytes

import os
0/('['>os.sep)

Thanks to FlipTack for 3 bytes

This program takes advantage of the fact that Windows is the only OS to use \ as a path separator. Normally this is frustrating and bad, but for once it is actually an advantage. On Windows, '['>os.sep is false, and thus 0/0 is computed, causing a ZeroDivisionError and exiting with a non-zero exit code. On non-Windows platforms, '['>os.sep is true, making the expression 0/1, which does nothing, and the program exits with exit code 0.

Mego

Posted 2017-01-08T11:53:58.970

Reputation: 32 998

DOS also uses a backslash as the path separator and it has at least one Python 2 implementation. – isanae – 2017-01-08T15:50:00.857

5@isanae I've edited the title to specify Python 2.7 - the only Python 2 implementation on DOS is an archaic, buggy 2.4.2 – Mego – 2017-01-08T15:56:29.033

OS/2 also uses a backslash and has a Python 2.7 implementation ;) – isanae – 2017-01-08T16:02:25.230

@isanae The only Python 2 port I see for OS/2 is 2.4.4: https://www.python.org/download/other/

– Mego – 2017-01-08T16:03:39.937

This port is 2.7.5. – isanae – 2017-01-08T16:05:14.883

9@isanae There, I specified 2.7.10. Good luck finding a port of that. – Mego – 2017-01-09T00:08:51.243

2apparently it only needs to give correct results on 3 recent windows versions and presumably similarly recent versios of the three other systems listed, OS2 and DOS don't matter, – Jasen – 2017-01-09T01:22:31.337

I love that 'non-zero exit code' --> truthy value – Luigi – 2017-01-11T15:20:44.453

@Luigi Whoops, I said it backwards. It is a bit odd, honestly, that 0 is truthy in shell contexts, but falsey basically everywhere else. The > could be swapped for a <, and then it will exit with 0 on Windows and non-zero elsewhere. – Mego – 2017-01-11T15:35:32.853

Would doing os.sep=="\\" be ok ? With import, that line adds up to 19 bytes. What do you guys think ? – Sergiy Kolodyazhnyy – 2017-01-13T07:17:02.943

@Serg That's a snippet, not a function or full program. – Mego – 2017-01-13T07:17:52.833

@Mego I'm not saying to use that line alone, of course it needs import. – Sergiy Kolodyazhnyy – 2017-01-13T07:19:43.053

@Serg That's not the problem. It doesn't utilize one of the acceptable I/O methods. The program I have, for example, outputs via exit code. Previously, I used STDOUT for output. – Mego – 2017-01-13T07:20:40.697

@Mego ah, I see what you mean – Sergiy Kolodyazhnyy – 2017-01-13T07:21:30.877

Interesting solution. So all we need is a language which is only implemented for Windows, and then return true. – Jonas Schäfer – 2017-01-13T07:24:45.617

45

x86 Assembly, 7 bytes (Inspired by Runemoro's answer)

31 DB 89 D8 40 CD 80

Or

xor ebx, ebx 
mov eax, ebx
inc eax
int 0x80

Description

First of all, we'll set eax to 1 (the system call number for exit(int val) for Linux, FreeBSD and OSX). Then, we'll call the interrupt gate 0x80 which is the system call gate for Linux, FreeBSD and OSX. That would cause the program to exit with status of ebx which is 0 (false).

On Windows int 0x80 is an invalid gate (It uses 2e as a syscall gate) and would crash the program, causing it to end with a positive exit code (true).

Edit: Would not work on OSX since it has a different argument-passing convention on 32 bit (by the stack).

References and further reading

Shmuel H.

Posted 2017-01-08T11:53:58.970

Reputation: 551

3That's brilliant! – z0rberg's – 2017-01-09T17:46:16.047

Why does the crash cases a truthy value? Is it because EAX (typically the return value) is 1? Also, is EAX guaranteed to be 0 at program start? Or do you need xor eax, eax in there? – Cole Johnson – 2017-01-09T22:13:14.673

3@ColeJohnson: OS-detected crashes (on the operating systems typically used with x86) never leave an exit code of 0, because that's reserved for successful termination. (Normally the exit code is some wonky value that the OS reserves specifically for this circumstance.) However, I'm not sure it makes sense to count 0 as falsey and 1 as truthy in program exit codes, given that the normal convention is the exact opposite (with 0 being the only truthy vaue, e.g. the standard UNIX/Linux/POSIX program false exits with code 1 whilst true exits with code 0). – None – 2017-01-10T06:10:11.393

@ais523 The relevant answer on the Interpretation of Truthy/Falsey post on meta also states

that 0 is truthy and nonzero is falsy.

– user2428118 – 2017-01-11T10:14:02.990

1This answer implicitly assumes that registers eax and ebx are cleared to zero on program start (on non-Windows), which is not guaranteed if I remember correctly. It also explicitly assumes that the syscall argument-passing convention for Linux matches that for FreeBSD and OSX, which would be very surprising indeed. – zwol – 2017-01-11T21:45:13.480

@zwol You're right about the eax and ebx. I'll update the answer. The argument-passing convention is the same for Linux and FreeBSD (with linux emulation). However, I could not verify about OSX. I will fix these problems, thank you for the corrections. – Shmuel H. – 2017-01-12T05:45:59.320

Why not just mov eax, 1 to get rid of the inc call? – Sebastian-Laurenţiu Plesciuc – 2017-01-13T07:37:01.963

1@Sebastian-LaurenţiuPlesciuc Good question. mov eax, 1 would be translated to \xB8\x01\x00\x00\x00, which is a lot longer that just moving register and calling inc. – Shmuel H. – 2017-01-13T12:48:35.707

29

C, 44 43 38 36 bytes

Thanks to @Downgoat for a byte! crossed out 44 is still regular 44
Thanks to @Neil for two bytes!

f(){return
#ifdef WIN32
!
#endif
0;}

betseg

Posted 2017-01-08T11:53:58.970

Reputation: 8 493

Originally I was going to suggest that you can save a bunch of bytes by moving the 0 out of the ifdef and changing the 1 to !, but I think _WIN32+0 works even better still. – Neil – 2017-01-08T13:52:42.900

If c99 is OK you can change f to main and stick return 1; inside the ifdef and remove the else, since main without return in c99 must return 0. – simon – 2017-01-08T13:56:38.900

11That's a compiler directive. If it's compiled on a Windows system and run on a Linux system, for example, it will still return 1. – Micheal Johnson – 2017-01-08T14:44:26.750

Do you need _ in WIN32? I never added it on my code – Downgoat – 2017-01-08T14:44:55.453

@Neil, "Code that fails to run/compile on a platform doesn't count as a falsey value.", it won't compile on a Linux/macOS/etc system (TIO link, click debug).

– betseg – 2017-01-08T15:47:50.783

@Mego ^ (i can't ping you both in a single comment) – betseg – 2017-01-08T15:51:49.270

@Downgoat _WIN32 is a always defined by the compiler, while WIN32 is set by some project templates in Visual Studio. _WIN32 is more reliable. – isanae – 2017-01-08T15:54:11.647

@isanae in PPCG, portability isn't an issue. It count if it works in one implementation. – betseg – 2017-01-08T15:55:44.707

@betseg Ahh, I didn't think about that. – Mego – 2017-01-08T16:00:31.207

@betseg Sorry, I thought it would be empty or zero for some reason. – Neil – 2017-01-08T16:19:18.103

My comment about ! still stands though, and saves 2 bytes. – Neil – 2017-01-08T16:22:04.940

@Neil oooh i got it now. Didn't understand when I first read it. – betseg – 2017-01-08T16:23:41.740

4@MichealJohnson no. I can compile it on linux (using mingw32gcc msvc) snd get code that returns true when run on windows. I don't know of any windows-hosted linux compiler. if you want to argue emulation layers like "wine" all the other answers probably suffer the same problem – Jasen – 2017-01-09T01:29:08.280

@Jasen My point is, the return value is still set at compilation time not at execution time, so technically it doesn't answer the question. – Micheal Johnson – 2017-01-09T07:25:24.643

all the other answers here are also using values set at compile time. – Jasen – 2017-01-09T07:41:51.987

I'm not sure your count is accurate. It looks like you've got 32 printable ASCII characters plus four line feeds. A proper Windows text file would, of course, have CR+LF as line separators, so it would be 4 bytes more. And a proper POSIX text file uses LF as line terminators not separators, so you'd need one more byte for the last LF. ;-) – Adrian McCarthy – 2017-01-09T21:37:46.413

1Not sure if WIN32 is just defined, but is defined to non-zero. If it's the later case, you can just say #if WIN32. On non-windows, since WIN32 is not defined, the preprocessor is required to treat it as 0. – Shahbaz – 2017-01-09T23:15:53.447

27

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.

Titus

Posted 2017-01-08T11:53:58.970

Reputation: 13 814

4File names on *nix can contain backslashes, so this isn't really foolproof. The rules don't say it has to be foolproof, though, so ¯\(ツ) – Jordan – 2017-01-08T15:10:54.320

@Jordan: You´re right. I added that info to the description. Thanks. – Titus – 2017-01-08T15:57:23.297

4An alternative: <?=class_exists(COM);. The class COM is only available under Windows, as far as I know. That should save you one byte. – Ismael Miguel – 2017-01-08T22:28:12.000

@IsmaelMiguel That's enough of a different answer for you to post it as such. (However, it may not be worth it to do so; this answer is very well written.) – wizzwizz4 – 2017-01-09T18:35:33.900

1@wizzwizz4 It isn't worth it. The answer would be pushed to oblivion. That's why I simply left the comment, instead of writting my own answer. – Ismael Miguel – 2017-01-09T18:45:01.880

The strlen one won't tell you the OS you're using, only the OS the file was saved on, and even that isn't certain, because Windows editors can save in LF, and non-Windows editors can save in CRLF. – Andrea – 2017-01-14T19:38:14.327

@Andrea Tell me something I don´t know ... or that´s not in the description. – Titus – 2017-01-15T14:55:59.217

16

Befunge-98, 7 bytes

6y2%!.@

Try it online!

This works by querying the system path separator, which is \ on Windows and / on other operating systems.

6y            System information query: #6 returns the path separator.
  2%          Test the low bit - this will be 1 for '/' and 0 for '\'.
    !         Not the value, so it becomes 0 for '/' and 1 for '\'.   
     .@       Output the result and exit.

James Holderness

Posted 2017-01-08T11:53:58.970

Reputation: 8 298

15

Mathematica, 28 bytes

$OperatingSystem=="Windows"&

alephalpha

Posted 2017-01-08T11:53:58.970

Reputation: 23 988

What's the point in making it a function? You could remove the ampersand saving one byte, and the code would just directly evaluate whether it's executed is on a Windows-ish system. – Ruslan – 2017-01-08T18:01:45.360

@Ruslan All answers must be either full programs that print the result or callable functions. If this is declared a Mathematica notebook answer, then you might get away with calling it a full program, but if I invoke the thing from the command-line without the &, it won't print anything (and it's then also not a callable function, but merely a snippet/expression). – Martin Ender – 2017-01-08T19:04:14.260

@MartinEnder Really no output? I get Out[1]= False output from this: ~/opt/Mathematica/11.0/Executables/math <<< '$OperatingSystem=="Windows"' – Ruslan – 2017-01-08T19:33:54.497

@Ruslan I believe that also starts a notebook environment (just a command-line based one). What I mean by running a program from the command-line is using script mode. – Martin Ender – 2017-01-08T20:07:31.280

13

Java 8, 33 bytes

Special thanks to Olivier Grégoire for suggesting separatorChar, and Kritixi Lithos for -1 byte!

This is a lambda expression which returns a boolean. This can be assigned to Supplier<Boolean> f = ...; and called with f.get().

()->java.io.File.separatorChar>90

Try it online! - the server isn't windows, so this prints false. However, in my windows machine, the same code prints true.

What this code does is get the System's file seperator, and check whether its codepoint is larger than the character [. This true for Windows, as it uses \ as the seperator - but every other OS uses /, which has a lower code in the ASCII table.

FlipTack

Posted 2017-01-08T11:53:58.970

Reputation: 13 242

Won't this break on other OSes which start with W? – Downgoat – 2017-01-08T14:44:10.477

()->java.io.File.separatorChar=='\\' is only 36 bytes. – Olivier Grégoire – 2017-01-08T15:11:40.907

1@OlivierGrégoire nice one - and I can golf it to 34 using ()->java.io.File.separatorChar>'['! – FlipTack – 2017-01-08T15:16:22.637

@KritixiLithos thanks, don't know why I didn't remember that... – FlipTack – 2017-01-08T15:58:31.720

@FlipTack I'm not very familiar with Java's lambdas, but don't you have to include the semicolon at the end? – user41805 – 2017-01-08T16:02:17.610

@KritixiLithos This expression itself is a literal (function literals are allowed), just like 1. When assigning it to a variable, the assignment is a statement and therefore requires the semicolon, like int i = 1; - but the lambda itself doesn't need the semicolon. – FlipTack – 2017-01-08T16:03:15.643

@Downgoat: Is there any other OS that starts with W? – Titus – 2017-01-08T16:10:40.207

2@Titus WebOS, Whonix. Probably even more. – Olivier Grégoire – 2017-01-08T16:54:37.343

3

@Titus Wait, what about WAITS?

– NoOneIsHere – 2017-01-09T17:33:38.207

11

J, 7 bytes

6=9!:12

This is a verb (similar to a function) that uses the builtin foreign conjunction 9!:12 to acquire the system type where 5 is Unix and 6 is Windows32.

miles

Posted 2017-01-08T11:53:58.970

Reputation: 15 654

J Official documentation shows it returns this value for older Windows. "6 Windows32 (95/98/2000/NT)" Does the documentation need to be updated? What happens when it is 64 bit Windows? http://jsoftware.com/help/dictionary/dx009.htm

– Keeta - reinstate Monica – 2017-01-12T14:05:27.087

Tested on 64 bit Windows 7 and it returns a 6. Documentation seems to be quite old. – Keeta - reinstate Monica – 2017-01-12T14:17:59.517

@Keeta Yes I think it is old but it still returned a 6 for me on Windows 10 64 bit. – miles – 2017-01-12T20:23:19.730

11

R, 15 bytes

.Platform$O>"v"

Thanks to plannapus for the suggestion to use partial matching for list element extraction.

.Platform is a list with some details of the platform under which R was built. There is an element OS.type (the only element with name starting with "O") which is character string, giving the Operating System (family) of the computer. One of "unix" or "windows".

So "unix" is less then "v", but "windows" is greater then "v". Other valid 15 bytes answers are

.Platform$O>"V"
.Platform$O>"w"
.Platform$O>"W"

R is being developed for the Unix-like, Windows and Mac families of operating systems. Other OS families are not supported.

djhurio

Posted 2017-01-08T11:53:58.970

Reputation: 1 113

1there are platforms other than unix that aren't windows you know... – Blue – 2017-01-08T20:28:33.030

2

@muddyfish: .Platform[[1]] is defined as either "unix" or "windows" in R documentation. https://github.com/wch/r-source/blob/af7f52f70101960861e5d995d3a4bec010bc89e6/src/library/base/man/Platform.Rd#L22

– liori – 2017-01-08T21:20:11.680

Sorry, that's ok then. The answer should probably be modified to include this fact to stop that being asked again – Blue – 2017-01-08T22:37:05.183

10

Perl, 11 bytes

print$^O=~MS

^O should be replaced by a literal Control-O.

Outputs 1 on windows, nothing on another OS.

Note that I'm not using say as it adds a trailing newline, which is truthy in Perl.

-2 bytes thanks to primo. (and fixed potential issues)
-1 bytes thanks to ais523.

Dada

Posted 2017-01-08T11:53:58.970

Reputation: 8 279

AFAIR this won't work in Cygwin Perl. – Igor Skochinsky – 2017-01-09T11:43:40.643

This won't work in mingw Perl either. Perl treats those both as distinct operating systems from Windows, though (as they generally obey UNIX rather than Windows conventions), and it's not clear whether they should count for the purpose of the question. In other news, you can save a byte here by using a literal control-O character rather than ^O. – None – 2017-01-10T06:16:16.507

@ais523 I edited that, thanks. As for Cygwin and Mingw, I'll delete the post if they should be considered as Windows, but as you say, it would make more sense to consider them like separate OS (or at least, like not-Windows OS). – Dada – 2017-01-10T06:40:54.537

Regex delimiters shouldn't be necessary $^O=~W, although, I would probably match against MS. Alternatively, you could also match $^X=~':'. – primo – 2017-01-11T12:21:52.633

@primo right, thanks. I don't know any other OS with a W in it so I assumed checking for a W for fine.. any reasons why you suggest MS instead? – Dada – 2017-01-11T12:24:27.923

@Dada less likely to hit a false positive: Cygwin and Mingw have both already been mentioned. – primo – 2017-01-11T12:25:29.330

9

julia, 10 bytes

is_windows

A function that returns true for windows

rahnema1

Posted 2017-01-08T11:53:58.970

Reputation: 5 435

9

x86 machine code, 9 bytes

40 39 04 24 75 02 CD 80 C3

Compiled from:

inc eax        ; set eax to 1
cmp [esp], eax ; check if [esp] == 1 (linux)
jne windows    ; jump over "int 0x80" if on windows
int 0x80       ; exit with exit code 0 (ebx)
windows:
ret            ; exit with exit code 1 (eax)

user23125

Posted 2017-01-08T11:53:58.970

Reputation:

3pure binary (COM) won't run on Windows or Linux so not sure if this is valid – Igor Skochinsky – 2017-01-09T11:44:50.577

@IgorSkochinsky There must be an interpreter for assembly. – Shmuel H. – 2017-01-09T14:59:57.237

You can make the code even shorter by leaving only inc eax and int 0x80, I think it should fail on windows and terminate the process. – Shmuel H. – 2017-01-09T15:00:48.623

@ShmuelH. We aren't allowed to do that: "Code that fails to run/compile on a platform doesn't count as a falsey value." – None – 2017-01-09T15:05:26.887

@Runemoro You're right. However, this is a true value (on windows, the program would end with positive status code; status code is allowed, see the first comment). – Shmuel H. – 2017-01-09T15:07:19.447

Well, surely you can assemble and link it into a valid executable for the platform (ELF or PE) but then you'd have to count either the executable size with all the headers or the source code size. – Igor Skochinsky – 2017-01-09T15:12:03.090

@IgorSkochinsky, No, just like you can run python (with an interpreter), there are programs that run assembly (see here)

– Shmuel H. – 2017-01-09T15:13:57.337

I'm not aware of any such, but even if they exist you still have to count the source code, not raw binary. – Igor Skochinsky – 2017-01-09T15:15:56.330

The exit code is set to non-zero, but you get a dialog saying "program.exe has stopped working" first, so I'm not sure if that's ok. – None – 2017-01-09T15:16:33.837

2@IgorSkochinsky There are programs that run raw binary too. See the link in my previous comment. – Shmuel H. – 2017-01-09T15:18:56.423

Technically, this answer checks for the Linux Kernel (afaics). If I run GNU/Hurd (just like GNU/Linux, but using a different kernel) and compile this program for it, it would say that it was Windows. Equally if I ran this on my x86 smart TV it would say that it was Windows. (Both untested, feel free to prove me wrong!) – wizzwizz4 – 2017-01-09T20:01:01.327

1Bochs and QEMU simulate bare metal environment and do not run the binary code under the host OS. So they won't work IMO. But this all could be a discussion for the meta. – Igor Skochinsky – 2017-01-10T23:53:44.810

8

JavaScript, 42 30 26 25 bytes

console.log((
//Begin
_=>navigator.oscpu[0]>'V'
//End
)())

Tested with Firefox. (Chrome doesn't have the oscpu property.) Since lowercase letters have a higher character code than uppercase letters, this depends on the first letter of navigator.oscpu being uppercase and not being W, X, Y or Z on any platform that Firefox supports (other than Windows, of course). According to this post, that is the case.

Edits

  1. Saved 12 bytes thanks to Neil.
  2. Saved another four bytes
  3. Saved another byte thanks to Blender.

user2428118

Posted 2017-01-08T11:53:58.970

Reputation: 2 000

oscpu is probably the shortest navigator property that you can use. Also, testing a regexp will probably work out shorter, but I haven't measured it. – Neil – 2017-01-08T13:54:14.350

You can remove !=-1 and add a ~ right after the fat arrow, saving 3 bytes. – Luke – 2017-01-08T16:06:10.433

Do you have to create a function? Can't you just console.log the regex test? Also would navigator.oscpu[0]=='W' work or is there another OS that also starts with W. – None – 2017-01-09T11:17:52.410

2Hmm, for some reason my Chrome doesn't have oscpu. – Muzer – 2017-01-09T13:46:44.213

If it wasn't for WebTV OS, I would suggest navigator.platform[0]=='W' as a more robust alternative. – Patrick Roberts – 2017-01-10T10:03:40.897

2navigator.oscpu>'V' might work as well – Blender – 2017-01-11T03:03:08.353

navigator.oscpu>'V' could produce a false positive on some *nix systems, which derive navigator.oscpu from the output of uname. Examples – asgallant – 2017-07-19T22:25:04.240

8

C#, 61 48 bytes

()=>(int)System.Environment.OSVersion.Platform<4

Saved 13 bytes thanks to TheLethalCoder

Or a full program at 83 bytes:

class P{static int Main(){return(int)System.Environment.OSVersion.Platform<4?1:0;}}

Various Windows variants use enum values 0 to 3 in the Microsoft .NET implementation. 4 is Unix, 5 is Xbox [360] (which I won't consider "Windows"), 6 is MacOSX. Mono uses the same values, adding 128 for Unix/Linux in earlier versions.

Therefore, anything < 4 is Windows, and everything else is not Windows.

Bob

Posted 2017-01-08T11:53:58.970

Reputation: 844

2Not sure if I'm missing something, but why are you casting the value to an int? – auhmaan – 2017-01-10T15:18:01.937

@auhmaan CS0019 Operator '<' cannot be applied to operands of type 'PlatformID' and 'int' -- basically, C#'s typing rules says I can't compare a PlatformID and int directly, and there's no implicit cast from PlatformID to int. But there is an explicit cast from all enums to their values, which I take advantage of here... – Bob – 2017-01-10T21:59:08.583

Depends on which xbox you're running this on. XBox ONE runs on Windows 10 therefore correct answer should be returning true. – conquistador – 2017-01-11T11:43:34.093

@MustafaAKTAŞ According to documentation, Xbox explicitly means "The development platform is Xbox 360.". Also, arguably, by including WinCE I'm already going beyond the scope of the question (which is presumably traditional computers only). IMO, and lacking further clarification from the question author, I will assume "Windows" does not include, say, "Windows Phone".

– Bob – 2017-01-11T12:05:20.407

1

@MustafaAKTAŞ Also, I have to point out that this is targeting C#/.NET Framework/.NET Core. On Xbox One it's only possible to run UWP apps, which use a different API not including System.Environment.OSVersion at all. If you're going to take issue with that, then you should also delete every other non-UWP answer. It also turns out that you can't (currently) run UWP apps on non-Windows platforms, so you can go delete all those too. Which leaves you with 0 answers, and an unanswerable question.

– Bob – 2017-01-11T12:08:31.510

1You can compile to an Action<bool> in the first example for 48 bytes (I haven't tested it but believe it will work) _=>(int)System.Environment.OSVersion.Platform<4; It might need to be ()=>... for 49 bytes though – TheLethalCoder – 2017-01-11T12:58:45.367

@TheLethalCoder Hm, is that allowed? Without a (implicit or explicit) cast to Action<bool>, it's not really a delegate yet... not sure how acceptable that is on PCCG. – Bob – 2017-01-11T14:19:20.977

1Compiling to anonymous functions such as Funcs and Actions are used all the time here. I believe it's in the golfing tips page and I use them almost all the time. Also anonymous functions are used in other languages a lot so I think it is safe to use them here – TheLethalCoder – 2017-01-11T14:23:08.017

Although if the statement specifies you must write a full-program then obviously Actions and Funcs aren't going to help you. But this challenge just says program and I believe that means you can use an anonymous method (although I may be wrong on that). Though some other answers appear to be using anonymous functions/methods. – TheLethalCoder – 2017-01-11T14:29:53.000

@TheLethalCoder Ah, ok. Thanks! I do think it needs to be ()=> because _=> takes a param. Also, Func<bool> in this case, but apparently that's not part of the function, so... still 48 bytes assuming the semicolon isn't required (it's not part of the anon function, technically). – Bob – 2017-01-11T14:31:55.957

I usually include the semi-colon, same as if it was multiline I'd include the braces {}. And the challenge doesn't state if you aren't allowed to take input so you could compile to a Func<int, bool> and you just don't use the int you pass in. Again still 48 bytes – TheLethalCoder – 2017-01-11T14:42:21.783

7

Batch, 50 bytes

@if %OS%==Windows_NT if not exist Z:\bin\sh echo 1

Edit: Fixed to ignore DOS instead of claiming that it's Windows.

The only other way I know of running Batch outside of Windows is to use WINE which by default will map Z: to /. Therefore if Z:\bin\sh exists, chances are that it's /bin/sh, so not MS Windows.

I don't know what WINE sets %OS% to, but if it's not Windows_NT then I could save 23 bytes.

Neil

Posted 2017-01-08T11:53:58.970

Reputation: 95 035

1Another way is DOS, which is not Windows. – Ruslan – 2017-01-08T18:03:19.197

Not only does this fail under DOS, but also, on a computer where Z: is mapped, and happens to contain such a path. – Adám – 2017-01-08T18:49:39.087

This doesn't work if cygwin is install to the root of drive Z: – DavidPostill – 2017-01-08T18:50:07.337

1At least I'm trying to detect WINE. None of the other answers will give the correct result when run under WINE either. – Neil – 2017-01-08T18:51:43.910

DOS: set OS=Windows_NT & subst Z: C:\ & mkdir Z:\bin & echo X > Z:\bin\sh. – Adám – 2017-01-08T18:54:56.420

3@Adám Sure and if you compile the C answer with -DWIN32=1 then it fails too. Your point? – Neil – 2017-01-08T19:13:59.103

1yes, wine sets OS=Windows_NT – Jasen – 2017-01-09T01:31:52.067

Doesn't work under MS-DOS 6.22 -- it fails with a syntax error. – Mark – 2017-01-09T02:54:35.727

there can be hybrid scripts like this

– phuclv – 2017-01-09T06:05:06.320

@Mark Strange, it worked for me when I tried it. – Neil – 2017-01-09T08:43:28.837

@Neil, are you trying with actual MS-DOS, or with a Windows command prompt? – Mark – 2017-01-09T09:08:08.710

@Mark I have an MS-DOS 6.22 VM. – Neil – 2017-01-09T19:29:00.250

7

Node.js, 27 16 15 13 bytes

Thanks to @Patrick, who shaved 12 bytes off my solution using Node's REPL:

_=>os.EOL>`
`

Original solution:

_=>require('path').sep!='/'

GilZ

Posted 2017-01-08T11:53:58.970

Reputation: 179

If you change this to Node.js REPL, you can save 16 bytes by just using _=>path.sep!='/' – Patrick Roberts – 2017-01-10T09:44:14.727

I'm new to codegolf. Am I allowed to do that? – GilZ – 2017-01-10T09:45:33.527

Yes, otherwise I wouldn't have suggested it. REPL means read, execute, print loop, the program that runs when you enter node on the console. From there, all the system node modules are available without the need to require() them. – Patrick Roberts – 2017-01-10T09:48:12.323

Oh yeah sorry. I meant 11. You can save another byte by changing != to > since the ASCII index for \ is 92 and / is 47. – Patrick Roberts – 2017-01-13T09:13:26.870

7

Excel VBA, 41 40 30 29 26 24 Bytes

Immediate windows function that returns true if the system's OS code starts is longer than length 3, because the info is restricted to output either else mac or pcdos this returns true only on windows pcs

?[Len(Info("SYSTEM"))>3]

Previous Versions

''# Ignore the second `"` that follows every `\` - its only there for highlighting 

?Left(Environ("OS"),1)="W"                 # 24 Bytes

?InStr(ThisWorkbook.Path,"\"")             # 29 Bytes

?Mid(ThisWorkbook.Path,3,1)="\""           # 30 Bytes, Restricted to local Files

?Application.PathSeparator="\""            # 30 Bytes

?Left(Application.OperatingSystem,1)="W"   # 40 Bytes

Changes

-1 Thanks to Neil for using Left(...,1) over Mid(...,1,1)

-10 Thanks to ChrisH for pointing out @Mego's Path Separator Trick

-1 For Checking the WorkbookPath for "\" rather than using Application.Path Separator

-4 For switching to Environ()

-2 For switching to [Len(Info(...

Novel Solution, 51 bytes

Novel subroutine that outputs, to the VBE immediates window, a 1 (truthy) under windows and 0 (falsey) under mac by method of conditional compilation.

Sub a
i=1
#If Mac Then
i=0
#End If
Debug.?i
End Sub

Taylor Scott

Posted 2017-01-08T11:53:58.970

Reputation: 6 709

1left saves you a byte. – Neil – 2017-01-09T08:49:45.510

Have you tried it on office365 online? Does that even support VBA? Just curious. – Chris H – 2017-01-10T11:21:51.193

@ChrisH To my knowledge office.com does not support online VBA scripting (though if anyone else knows better please do tell me, that would make my life significantly easier); However, with O365 you have the rights to download a copy of Office 2016 (or whatever is current) to your desktop, and that does support VBA scripting. – Taylor Scott – 2017-01-10T15:08:01.660

I've so far manage to avoid it; given your comment that looks set to continue (the only windows machines I use have a desktop copy of office, personal machines are all linux) – Chris H – 2017-01-10T15:13:19.220

1@Mego's path separator trick (?Application.PathSeparator)="\" would be 32 as it's a single char) – Chris H – 2017-01-10T15:18:13.127

7

QBasic, 31 bytes

?INSTR(ENVIRON$("COMSPEC"),"W")

Prints non-zero under Windows, 0 under everything else.

COMSPEC is an environment variable unique to Microsoft OSs. It points to the command interpreter, typically command.com or cmd.exe. Under Windows, the command interpreter sits somewhere in the Windows directory; under MS-DOS, it sits in the DOS directory or on the root of the disk, and under any other OS, it doesn't exist.

By checking to see if the value of COMSPEC contains a "W", we can tell the difference between Windows and not-Windows.

Mark

Posted 2017-01-08T11:53:58.970

Reputation: 2 099

COMSPEC isn't reserved to mean anything in particular under Linux (meaning it's under user control by default), so isn't it possible that the user's set it to a value that they're using for their own purposes (and happens to contain a W)? Admittedly, that's a bit of an edge case. – None – 2017-01-10T06:12:50.923

1@ais523: Also, the Windows directory doesn't have to contain a W. It's brittle in either case. – Joey – 2017-01-10T08:13:20.997

6

Perl 6,  19  18 bytes

put $*DISTRO.is-win
put ?($*CWD~~/\\/)

Both output True␤ or False␤ depending on the system it is run on.

Brad Gilbert b2gills

Posted 2017-01-08T11:53:58.970

Reputation: 12 713

the second one relies on the non windows values of CWD not containing any \ - there's no guarantee of that, – Jasen – 2017-01-09T01:39:48.160

5

APL (Dyalog), 21 bytes

'W'∊∊#⎕WG'APLVersion'

Try it online!

#⎕WG'APLVersion' Root (#) Window Get property APL&hairsp;Version

 enlist (flatten)

'W'∊ is W a member? (no non-Windows return values contain a capital W)

Adám

Posted 2017-01-08T11:53:58.970

Reputation: 37 779

4

Haskell, 39 31 bytes

import System.Info
f=os!!0=='m'

I check for the first letter output of "m", which should be "mingw" for windows. As far as I could tell, there is no other OS which starts with M. The information comes from https://github.com/ghc/ghc/blob/master/compiler/utils/Platform.hs

Dylan Meeus

Posted 2017-01-08T11:53:58.970

Reputation: 220

1On my system (Windows 10 64-bit, GHC 8.0.1 64-bit), os gives "mingw32". – Mego – 2017-01-08T15:44:19.913

@Mego you are right, corrected for that – Dylan Meeus – 2017-01-09T10:04:46.727

4

tcl, 38 bytes

 expr [lsearch $tcl_platform windows]>0

hdrz

Posted 2017-01-08T11:53:58.970

Reputation: 321

4

PHP 17 Bytes

The following will output 1 if windows and nothing if anything else. Ignoring notices of string convertion.

<?=PHP_OS==WINNT;

Try online Online tests for linux because the sandbox is linux for PoC.

DrWhat

Posted 2017-01-08T11:53:58.970

Reputation: 41

Sure that is enough? Asking because Possible Values For: PHP_OS.

– manatwork – 2017-01-09T08:05:41.453

1'<?=PHP_OS[0]==W;is both 1 byte shorter and catches all the other windows values in the question linked by manatwork.>V` might work too. – user59178 – 2017-01-09T10:18:18.317

manatwork depends on which windows version PHP was compiled on, since Windows Visa\7, Windows version has been represented with WINNT because of the NT Authority Kernel.

before windows XP and below was WIN32 and Windows server 2003 was Windows. – DrWhat – 2017-01-24T11:17:33.060

4

8th, 11 bytes

 
os 1- not .
 

Prints true on Windows, false on Linux and macOS. Other platforms supported by 8th are Android, iOS and Raspberry Pi, but I am not able to test on them.

Ungolfed version (with comments)

 
G:os  \ Return a number n indicating the operating system 
      \ 0 for Linux
      \ 1 for Windows 
      \ 2 for macOS
      \ 3 for Android 
      \ 4 for iOS 
      \ 5 for Raspberry Pi
n:1-  \ Subtract 1
G:not \ If Windows --> true, otherwise --> false
.     \ Print result
 

Chaos Manor

Posted 2017-01-08T11:53:58.970

Reputation: 521

4

Java 8, 49 bytes

()->System.getenv().get("OS").contains("Windows")

Longer than the other Java answer, but takes a different approach.

This lambda fits in a Supplier<Boolean> and can be tested with the following program:

public class DetectMSWindows {

  public static void main(String[] args) {
    System.out.println(f(() -> System.getenv().get("OS").contains("Windows")));
  }

  private static boolean f(java.util.function.Supplier<Boolean> s) {
    return s.get();
  }

}

user18932

Posted 2017-01-08T11:53:58.970

Reputation:

It's very, very similar to the initial answer that you link (before the edits). – Olivier Grégoire – 2017-01-09T11:45:59.940

Why not just .contains("W")? – Cyoce – 2017-01-11T00:11:35.640

@Cyoce actually, the variable OS appears to be Windows-specific. – None – 2017-01-11T02:43:34.877

4

bash + coreutils, 5 bytes

rm $0

Also works in most other POSIXy shells. (Note that Windows ports of bash and rm exist; even though they're only normally used with more heavily POSIXy operating systems, this isn't an entirely vacuous entry.) Outputs via exit code (0 = false, 1 = true). Can be counted as 4 bytes if you're allowed to assume a filename (e.g. rm a). Note that this can potentially fail in the case of very weird filenames (which rm will interpret as arguments due to the lack of quoting, and possibly delete files you care about, so I'd advise against running this program from a file with a weird name).

Note: deletes the program from disk as a side effect, or at least tries to. In the case where we're running on Windows, the OS will fail to delete the running file (an operation that Windows disallows either by default or full stop), and thus rm will error out. bash catches the error and converts it into an exit code (thus the program as a whole terminates normally). Most of the other entries here are using 0 for falsey and 1 for truthy in exit codes, so this does the same; note that bash's if statement doesn't accept integers at all (rather, it accepts commands and branches based on whether they run successfully, and arithmetic tests are done via the means of programs like test that intentionally report a "crash" on a failed comparison), so this is on shakier ground in terms of legality than programs that output via exit code in languages where 0 is valid in an if statement test and sends the program to the else branch.

user62131

Posted 2017-01-08T11:53:58.970

Reputation:

4

Python 3 (13 bytes)

import winreg

Returns with exit code zero (generally true in shells) if on windows, and with a non-zero exit code otherwise.

If you prefer it the other way round, there is a 12 bytes solution: import posix.

Jonas Schäfer

Posted 2017-01-08T11:53:58.970

Reputation: 200

I think this will not work if there is a file called winreg.py in the same directory. – Zacharý – 2017-07-20T16:00:13.333

2Also, per the rules: "Code that fails to run/compile on a platform doesn't count as a falsey value." – Zacharý – 2017-07-20T16:01:00.523

4

TrumpScript, 17 bytes

America is great.

Try it online!


This program, if run on windows, will print:

The big problem this country has is being PC

This is considered a truthy value.


Empty output and the following value are falsy:

Boycott all Apple products  until such time as Apple gives cellphone info to authorities regarding radical Islamic terrorist couple from Cal

The empty output will occur on any linux system (for this program), the long apple response obviously occurs on Mac (for any program). On TIO, the backend (I'm assuming) is a Unix operating system, so you can only get the falsy value; on my computer I get the PC message.


Not 100% sure if this counts as an error message (which would invalidate the answer), but if you didn't know about this it was probably worth a laugh for you.

Magic Octopus Urn

Posted 2017-01-08T11:53:58.970

Reputation: 19 422

What has the world come to... – ooransoy – 2017-07-20T12:52:30.903

@avaragecoder MAKE PYTHON GREAT AGAIN! – Magic Octopus Urn – 2017-07-20T14:47:37.233

3

FPC, 61 chars

begin{$ifdef win32}write('f');{$else}write('nf');{$endif}end;

user64239

Posted 2017-01-08T11:53:58.970

Reputation:

you can shave some bytes by using only one write begin write({$ifdef win32}1{$else}0{$endif});end. – hdrz – 2017-01-09T06:21:36.710

Or to work on win64 as well: begin write({$ifdef windows}1{$else}0{$endif})end. – hdrz – 2017-01-09T06:42:22.370

@hdrz, yes, you right, thanks ;) – None – 2017-01-09T07:47:35.087

@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. – manatwork – 2017-01-09T08:02:52.527

@manatwork, FPC and Delphi supports various types in writeln. And boolean is can be only 0 or 1 in pascal – None – 2017-01-09T08:21:46.280

@manatwork - well i'm streching the rules a bit here... anyhow i like your solution much better than mine – hdrz – 2017-01-09T08:51:03.940

@hdrz, yes, but you left ;. – None – 2017-01-09T09:22:12.280

You seem to have created a second account with the same name. If that wasn't on purpose, you can follow the steps in I accidentally created two accounts; how do I merge them?

– Dennis – 2017-01-09T18:47:42.630

@Dennis, no, i'm post first and the same answer by unlogged nick. My first account is monobogdan, but it's blocked by system because haters downvote some posts – None – 2017-01-09T21:27:18.647

3

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.

Grant Miller

Posted 2017-01-08T11:53:58.970

Reputation: 706

+1 for LibreOffice – ElPedro – 2018-10-08T16:48:02.490

2

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.

sergiol

Posted 2017-01-08T11:53:58.970

Reputation: 3 055

2

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.

ElPedro

Posted 2017-01-08T11:53:58.970

Reputation: 5 301

2

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!

Ken Y-N

Posted 2017-01-08T11:53:58.970

Reputation: 396

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. – manatwork – 2017-01-11T07:33:23.087

Oops! Filtering out darwin makes things lengthier. And let me add an exit... – Ken Y-N – 2017-01-11T08:20:28.677

2

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.

Andrea

Posted 2017-01-08T11:53:58.970

Reputation: 318

2

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

ooransoy

Posted 2017-01-08T11:53:58.970

Reputation: 163

2

Powershell 6, 10 bytes

$isWindows

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

mazzy

Posted 2017-01-08T11:53:58.970

Reputation: 4 832

1

Elixir, 19 bytes

{:win32,_}=:os.type

Exit code 0 for Windows, 1 for others.

movsx

Posted 2017-01-08T11:53:58.970

Reputation: 21

1

Scala, 71 bytes

object W extends App{print(sys.env.get("OS").get.contains("Windows"))}

Archmage stands with Monica

Posted 2017-01-08T11:53:58.970

Reputation: 171

1

Lua, 21 Bytes

os.execute"dir c:\\"

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

J. H

Posted 2017-01-08T11:53:58.970

Reputation: 11

Welcome to Programming Puzzles and Code Golf! – Pavel – 2017-01-13T04:55:15.413

1

Dartlang, 43 bytes

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

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

MicroTransactionsMatterToo

Posted 2017-01-08T11:53:58.970

Reputation: 11

1

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.

Zacharý

Posted 2017-01-08T11:53:58.970

Reputation: 5 710

1

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.

BradC

Posted 2017-01-08T11:53:58.970

Reputation: 6 099

Nice solution!! – Daniel – 2018-10-18T13:44:13.743

1

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

famous1622

Posted 2017-01-08T11:53:58.970

Reputation: 451

1-n+1 <-> 1-n? – Jonathan Frech – 2018-10-20T19:52:02.420

@JonathanFrech true, although I found even more savings by not being fancy :( – famous1622 – 2018-10-20T20:02:11.503

0

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))

Winny

Posted 2017-01-08T11:53:58.970

Reputation: 1 120

0

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.

Euan T

Posted 2017-01-08T11:53:58.970

Reputation: 101

0

PowerShell, 17

gdr|? P* -like *y

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

Joey

Posted 2017-01-08T11:53:58.970

Reputation: 12 260

0

Node.js, 23 bytes

_=>os.platform=="win32"

username.ak

Posted 2017-01-08T11:53:58.970

Reputation: 411

3

This only appears to be a snippet, as opposed to a full program or a function.

– Martin Ender – 2017-01-13T12:15:08.387

0

Rust, 14 bytes

||!cfg!(unix);

The target_family attribute is windows on Windows and (most) others have unix.

betseg

Posted 2017-01-08T11:53:58.970

Reputation: 8 493