xref: /llvm-project/libc/src/string/strdup.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1 //===-- Implementation of strdup ------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/string/strdup.h"
10 #include "src/__support/macros/config.h"
11 #include "src/errno/libc_errno.h"
12 #include "src/string/allocating_string_utils.h"
13 #include "src/string/memory_utils/inline_memcpy.h"
14 
15 #include "src/__support/common.h"
16 
17 #include <stdlib.h>
18 
19 namespace LIBC_NAMESPACE_DECL {
20 
21 LLVM_LIBC_FUNCTION(char *, strdup, (const char *src)) {
22   auto dup = internal::strdup(src);
23   if (dup)
24     return *dup;
25   if (src != nullptr)
26     libc_errno = ENOMEM;
27   return nullptr;
28 }
29 
30 } // namespace LIBC_NAMESPACE_DECL
31