Special-case /dev/null in open in the portability header
authorChristopher Dykes <cdykes@fb.com>
Wed, 10 Aug 2016 19:35:31 +0000 (12:35 -0700)
committerFacebook Github Bot 8 <facebook-github-bot-8-bot@fb.com>
Wed, 10 Aug 2016 19:38:26 +0000 (12:38 -0700)
Summary: `/dev/null` doesn't exist on Windows, but thankfully, `NUL` does, and has the same semantics.

Reviewed By: meyering

Differential Revision: D3698007

fbshipit-source-id: 5ef31c6576f988dd747ea3c39e296c244bc640b7

folly/portability/Fcntl.cpp

index 015f4a6d54ec7637700290bc57676ec0630adbba..9cbfa5bf6c010212d65cec0f833a43bf44b8d7e3 100755 (executable)
@@ -96,6 +96,11 @@ int open(char const* fn, int of, int pm) {
     // none are.
     return -1;
   }
+  if (!strcmp(fn, "/dev/null")) {
+    // Windows doesn't have a /dev/null, but it does have
+    // NUL, which achieves the same result.
+    fn = "NUL";
+  }
   errno_t res = _sopen_s(&fh, fn, of, _SH_DENYNO, realMode);
   return res ? -1 : fh;
 }