2 * Copyright 2016-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/portability/Stdio.h>
23 #include <folly/ScopeGuard.h>
24 #include <folly/portability/Unistd.h>
27 int dprintf(int fd, const char* fmt, ...) {
34 int ret = vsnprintf(nullptr, 0, fmt, args);
38 size_t len = size_t(ret);
39 char* buf = new char[len + 1];
43 if (size_t(vsnprintf(buf, len + 1, fmt, args)) == len &&
44 write(fd, buf, len) == ssize_t(len)) {
55 FILE* popen(const char* name, const char* mode) {
56 return _popen(name, mode);
59 void setbuffer(FILE* f, char* buf, size_t size) {
60 setvbuf(f, buf, _IOFBF, size);
63 int vasprintf(char** dest, const char* format, va_list ap) {
64 int len = vsnprintf(nullptr, 0, format, ap);
68 char* buf = *dest = (char*)malloc(size_t(len + 1));
69 if (vsnprintf(buf, size_t(len + 1), format, ap) == len) {