Create the stdio.h portability header
authorChristopher Dykes <cdykes@fb.com>
Fri, 6 May 2016 16:31:20 +0000 (09:31 -0700)
committerFacebook Github Bot 4 <facebook-github-bot-4-bot@fb.com>
Fri, 6 May 2016 16:37:07 +0000 (09:37 -0700)
Summary: Windows doesn't define a couple of functions.

Reviewed By: yfeldblum

Differential Revision: D2990676

fb-gh-sync-id: 1a1354bd7976cb8b4747436d6907d65210ba506c
fbshipit-source-id: 1a1354bd7976cb8b4747436d6907d65210ba506c

folly/Makefile.am
folly/portability/Stdio.cpp [new file with mode: 0755]
folly/portability/Stdio.h [new file with mode: 0755]

index 70c2d31d672be28ad5fe461f21a4eaefbca4cb00..d6511ccd571236aecf55e88c79188b9469b9c51f 100644 (file)
@@ -286,6 +286,7 @@ nobase_follyinclude_HEADERS = \
        portability/Memory.h \
        portability/PThread.h \
        portability/Sockets.h \
+       portability/Stdio.h \
        portability/Stdlib.h \
        portability/String.h \
        portability/Syslog.h \
@@ -434,6 +435,7 @@ libfolly_la_SOURCES = \
        portability/Malloc.cpp \
        portability/Memory.cpp \
        portability/Sockets.cpp \
+       portability/Stdio.cpp \
        portability/Stdlib.cpp \
        portability/String.cpp \
        portability/SysFile.cpp \
diff --git a/folly/portability/Stdio.cpp b/folly/portability/Stdio.cpp
new file mode 100755 (executable)
index 0000000..d6fdd23
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2016 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <folly/portability/Stdio.h>
+
+#ifdef _WIN32
+#include <cstdlib>
+#include <folly/ScopeGuard.h>
+#include <folly/portability/Unistd.h>
+
+extern "C" {
+int dprintf(int fd, const char* fmt, ...) {
+  va_list args;
+  va_start(args, fmt);
+  SCOPE_EXIT { va_end(args); };
+
+  int len = vsnprintf(nullptr, 0, fmt, args);
+  if (len <= 0) {
+    return -1;
+  }
+  char* buf = new char[len + 1];
+  SCOPE_EXIT { delete[] buf; };
+  if (vsnprintf(buf, len + 1, fmt, args) == len && write(fd, buf, len) == len) {
+    return len;
+  }
+
+  return -1;
+}
+
+int pclose(FILE* f) { return _pclose(f); }
+
+FILE* popen(const char* name, const char* mode) { return _popen(name, mode); }
+
+void setbuffer(FILE* f, char* buf, size_t size) {
+  setvbuf(f, buf, _IOFBF, size);
+}
+
+int vasprintf(char** dest, const char* format, va_list ap) {
+  int len = vsnprintf(nullptr, 0, format, ap);
+  if (len <= 0) {
+    return -1;
+  }
+  char* buf = *dest = (char*)malloc(len + 1);
+  if (vsnprintf(buf, len + 1, format, ap) == len) {
+    return len;
+  }
+  free(buf);
+  return -1;
+}
+}
+#endif
diff --git a/folly/portability/Stdio.h b/folly/portability/Stdio.h
new file mode 100755 (executable)
index 0000000..a30fa6d
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2016 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <cstdio>
+
+#ifdef _WIN32
+#include <cstdarg>
+#include <cstdint>
+
+extern "C" {
+int dprintf(int fd, const char* fmt, ...);
+int pclose(FILE* f);
+FILE* popen(const char* name, const char* mode);
+void setbuffer(FILE* f, char* buf, size_t size);
+int vasprintf(char** dest, const char* format, va_list ap);
+}
+#endif