xref: /llvm-project/libc/src/string/strdup.cpp (revision 33bdb53d864e3e244d8fd5649062f17b7d4c958d)
165bb6593SMichael Jones //===-- Implementation of strdup ------------------------------------------===//
265bb6593SMichael Jones //
365bb6593SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
465bb6593SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
565bb6593SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
665bb6593SMichael Jones //
765bb6593SMichael Jones //===----------------------------------------------------------------------===//
865bb6593SMichael Jones 
965bb6593SMichael Jones #include "src/string/strdup.h"
10*33bdb53dSJob Henandez Lara #include "hdr/stdlib_macros.h"
115ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
12110ee164SSiva Chandra Reddy #include "src/errno/libc_errno.h"
13d85699ebSJoseph Huber #include "src/string/allocating_string_utils.h"
141f578347SGuillaume Chatelet #include "src/string/memory_utils/inline_memcpy.h"
1565bb6593SMichael Jones 
1665bb6593SMichael Jones #include "src/__support/common.h"
1765bb6593SMichael Jones 
185ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
1965bb6593SMichael Jones 
2065bb6593SMichael Jones LLVM_LIBC_FUNCTION(char *, strdup, (const char *src)) {
21f3400305SSiva Chandra Reddy   auto dup = internal::strdup(src);
22f3400305SSiva Chandra Reddy   if (dup)
23f3400305SSiva Chandra Reddy     return *dup;
24f3400305SSiva Chandra Reddy   if (src != nullptr)
25110ee164SSiva Chandra Reddy     libc_errno = ENOMEM;
26f3400305SSiva Chandra Reddy   return nullptr;
2765bb6593SMichael Jones }
2865bb6593SMichael Jones 
295ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
30