Reactos
at master 61 lines 1.4 kB view raw
1// 2// strdup.cpp 3// 4// Copyright (c) Microsoft Corporation. All rights reserved. 5// 6// Defines _strdup() and _strdup_dbg(), which dynamically allocate a buffer and 7// duplicate a string into it. 8// 9// These functions allocate storage via malloc() or _malloc_dbg(). The caller 10// is responsible for free()ing the returned array. If the input string is null 11// or if sufficient memory could not be allocated, these functions return null. 12// 13#include <corecrt_internal.h> 14#include <malloc.h> 15#include <string.h> 16 17 18 19#ifdef _DEBUG 20 21extern "C" char* __cdecl _strdup(char const* const string) 22{ 23 return _strdup_dbg(string, _NORMAL_BLOCK, nullptr, 0); 24} 25 26extern "C" char* __cdecl _strdup_dbg( 27 char const* const string, 28 int const block_use, 29 char const* const file_name, 30 int const line_number 31 ) 32 33#else // ^^^ _DEBUG ^^^ // vvv !_DEBUG vvv // 34 35extern "C" char* __cdecl _strdup( 36 char const* string 37 ) 38 39#endif // !_DEBUG 40{ 41 if (string == nullptr) 42 return nullptr; 43 44 size_t const size = strlen(string) + 1; 45 46#ifdef _DEBUG 47 char* const memory = static_cast<char*>(_malloc_dbg( 48 size, 49 block_use, 50 file_name, 51 line_number)); 52#else 53 char* const memory = static_cast<char*>(malloc(size)); 54#endif 55 56 if (memory == nullptr) 57 return nullptr; 58 59 _ERRCHECK(strcpy_s(memory, size, string)); 60 return memory; 61}