xref: /llvm-project/libc/src/stdio/asprintf.cpp (revision 545e0593f8c59376a7ec8c6eb558babf6c9f93c1)
1a5e67fbaSTsz Chan //===-- Implementation of asprintf -----------------------------*- C++ -*-===//
2a5e67fbaSTsz Chan //
3a5e67fbaSTsz Chan // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a5e67fbaSTsz Chan // See https://llvm.org/LICENSE.txt for license information.
5a5e67fbaSTsz Chan // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a5e67fbaSTsz Chan //
7a5e67fbaSTsz Chan //===----------------------------------------------------------------------===//
8a5e67fbaSTsz Chan 
9a5e67fbaSTsz Chan #include "src/stdio/asprintf.h"
10a5e67fbaSTsz Chan #include "src/__support/arg_list.h"
11a5e67fbaSTsz Chan #include "src/__support/macros/config.h"
12a5e67fbaSTsz Chan #include "src/stdio/printf_core/vasprintf_internal.h"
13a5e67fbaSTsz Chan 
14*545e0593SJoseph Huber namespace LIBC_NAMESPACE_DECL {
15a5e67fbaSTsz Chan 
16a5e67fbaSTsz Chan LLVM_LIBC_FUNCTION(int, asprintf,
17*545e0593SJoseph Huber                    (char **__restrict buffer, const char *__restrict format,
18*545e0593SJoseph Huber                     ...)) {
19a5e67fbaSTsz Chan   va_list vlist;
20a5e67fbaSTsz Chan   va_start(vlist, format);
21a5e67fbaSTsz Chan   internal::ArgList args(vlist); // This holder class allows for easier copying
22a5e67fbaSTsz Chan                                  // and pointer semantics, as well as handling
23a5e67fbaSTsz Chan                                  // destruction automatically.
24a5e67fbaSTsz Chan   va_end(vlist);
25a5e67fbaSTsz Chan   int ret = printf_core::vasprintf_internal(buffer, format, args);
26a5e67fbaSTsz Chan   return ret;
27a5e67fbaSTsz Chan }
28a5e67fbaSTsz Chan 
29*545e0593SJoseph Huber } // namespace LIBC_NAMESPACE_DECL
30