├── dll └── empty.cpp ├── private ├── ntrtl.h └── nturtl.h ├── .gitignore ├── stdlib ├── qsort_s.cpp ├── lfind_s.cpp ├── bsearch_s.cpp ├── llabs.cpp ├── lsearch_s.cpp ├── labs.cpp ├── abs.cpp ├── imaxabs.cpp ├── div.cpp ├── ldiv.cpp ├── lldiv.cpp ├── rand_s.cpp ├── rand.cpp ├── imaxdiv.cpp ├── rotl.cpp ├── rotr.cpp └── byteswap.cpp ├── include ├── memory.h ├── io.h ├── search.h ├── share.h ├── math.h ├── minmax.h ├── stdnoreturn.h ├── stdalign.h ├── wctype.h ├── sys │ ├── locking.h │ └── types.h ├── corecrt_share.h ├── assert.h ├── corecrt_terminate.h ├── corecrt_math_defines.h ├── stddef.h ├── uchar.h ├── corecrt_wdirect.h └── dos.h ├── string ├── memcpy_s.cpp ├── strcspn.c ├── strpbrk.c ├── amd64 │ ├── strcspn.c │ └── strpbrk.c ├── i386 │ ├── strpbrk.asm │ └── strcspn.asm ├── wcscpy_s.cpp ├── strcpy_s.cpp ├── wcsset_s.cpp ├── strncnt.cpp ├── strset_s.cpp ├── strcat_s.cpp ├── wcsncnt.cpp ├── wcscat_s.cpp ├── wcsncat_s.cpp ├── wcsncpy_s.cpp ├── strncat_s.cpp ├── strncpy_s.cpp ├── wcsnset.cpp ├── wcsnset_s.cpp ├── wcsset.cpp ├── strnset_s.cpp ├── wcscpy.cpp ├── wcsrev.cpp ├── wcspbrk.cpp ├── wcstok.cpp ├── wcscat.cpp ├── wcsncmp.cpp ├── arm │ └── strlen.c ├── wmemmove_s.cpp ├── wcscmp.cpp ├── wcscspn.cpp ├── wcsncat.cpp ├── wcsncpy.cpp ├── wcsspn.cpp ├── strtok.cpp ├── strset.c ├── strrev.c ├── strnset.c ├── wmemcpy_s.cpp ├── memccpy.c ├── strncat.c ├── strdup.cpp ├── strncpy.c ├── strcmp.c └── wcsdup.cpp ├── misc ├── getpid.cpp ├── drivemap.cpp ├── debug_fill_threshold.cpp ├── seterrm.cpp ├── is_wctype.cpp ├── slbeep.cpp └── terminate.cpp ├── locale ├── glstatus.cpp └── GetStringTypeW.cpp ├── stdio ├── ncommode.cpp ├── fileno.cpp ├── fsetpos.cpp ├── fgetpos.cpp ├── setbuf.cpp ├── feoferr.cpp ├── clearerr.cpp ├── _freebuf.cpp ├── printf_count_output.cpp ├── putw.cpp ├── closeall.cpp ├── getw.cpp ├── _getbuf.cpp ├── fputs.cpp ├── putws.cpp ├── puts.cpp ├── fputws.cpp └── gettemppath.cpp ├── heap ├── heapmin.cpp ├── heapchk.cpp ├── malloc.cpp ├── new_mode.cpp ├── calloc.cpp ├── free.cpp ├── debug_heap_hook.cpp ├── realloc.cpp ├── msize.cpp ├── heap_handle.cpp └── malloc_base.cpp ├── lowio ├── tell.cpp ├── telli64.cpp ├── isatty.cpp ├── commit.cpp ├── creat.cpp └── eof.cpp ├── convert ├── fp_flags.cpp ├── common_utf8.cpp ├── swab.cpp ├── wctrans.cpp └── wctype.cpp ├── mbstring ├── mbscpy_s.cpp ├── mbspbrk.cpp ├── mbccpy_s.cpp ├── mbscat_s.cpp ├── mbsspnp.cpp ├── mbsset_s.cpp ├── mbsnbcpy_s.cpp ├── mbsncpy_s.cpp ├── mbsncat_s.cpp ├── mbsnbcat_s.cpp ├── mbsnbset_s.cpp ├── mbsnset_s.cpp ├── mbsncpy_s_l.cpp ├── mbsnbcpy_s_l.cpp ├── mbsncat_s_l.cpp ├── mbsnbcat_s_l.cpp ├── mbsnbset_s_l.cpp ├── mbsnset_s_l.cpp ├── mbtokata.cpp ├── mbccpy.cpp ├── ismblgl.cpp ├── mbtohira.cpp ├── mbclen.cpp ├── ismbalph.cpp ├── ismbprn.cpp ├── ismbspc.cpp ├── ismbupr.cpp ├── ismblwr.cpp ├── ismbdgt.cpp ├── ismbgrph.cpp ├── ismbalnm.cpp ├── mbslen.cpp └── mbsninc.cpp ├── filesystem ├── wmkdir.cpp ├── wrmdir.cpp ├── wunlink.cpp ├── wrename.cpp ├── mkdir.cpp ├── unlink.cpp ├── rmdir.cpp ├── chmod.cpp ├── access.cpp ├── rename.cpp └── wchmod.cpp ├── time ├── days.cpp └── difftime.cpp ├── internal ├── peb_access.cpp ├── SetCurrentDirectoryA.cpp ├── LoadLibraryExA.cpp ├── OutputDebugStringA.cpp ├── SetEnvironmentVariableA.cpp └── GetModuleFileNameA.cpp ├── initializers ├── clock_initializer.cpp ├── fma3_initializer.cpp ├── fmode_initializer.cpp ├── locale_initializer.cpp ├── timeset_initializer.cpp ├── tmpfile_initializer.cpp ├── console_input_initializer.cpp ├── console_output_initializer.cpp ├── multibyte_initializer.cpp ├── stdio_initializer.cpp └── i386 │ └── sse2_initializer.cpp ├── conio ├── cputs.cpp ├── putwch.cpp └── cscanf.cpp ├── LICENSE ├── startup └── initterm.cpp └── exec └── loaddll.cpp /dll/empty.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /private/ntrtl.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /private/nturtl.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /out/ 3 | cmake-build-*/ 4 | .vs/ 5 | .vscode/ 6 | .idea/ 7 | -------------------------------------------------------------------------------- /stdlib/qsort_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // qsort_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _qsort_s(), a routine for sorting arrays. 7 | // 8 | #ifdef __USE_CONTEXT 9 | #error __USE_CONTEXT should be undefined 10 | #endif 11 | 12 | #define __USE_CONTEXT 13 | #include "qsort.cpp" 14 | -------------------------------------------------------------------------------- /include/memory.h: -------------------------------------------------------------------------------- 1 | // 2 | // memory.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The buffer (memory) manipulation library. 7 | // 8 | #pragma once 9 | #ifndef _INC_MEMORY // include guard for 3rd party interop 10 | #define _INC_MEMORY 11 | 12 | #include 13 | #endif // _INC_MEMORY 14 | -------------------------------------------------------------------------------- /include/io.h: -------------------------------------------------------------------------------- 1 | // 2 | // io.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // This file declares the low-level I/O and file handling functionality. 7 | // 8 | #pragma once 9 | #ifndef _INC_IO // include guard for 3rd party interop 10 | #define _INC_IO 11 | 12 | #include 13 | #endif // _INC_IO 14 | -------------------------------------------------------------------------------- /stdlib/lfind_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // lfind_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _lfind_s(), which performs a linear search over an array. 7 | // 8 | #ifdef __USE_CONTEXT 9 | #error __USE_CONTEXT should be undefined 10 | #endif 11 | 12 | #define __USE_CONTEXT 13 | #include "lfind.cpp" 14 | -------------------------------------------------------------------------------- /include/search.h: -------------------------------------------------------------------------------- 1 | // 2 | // search.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Declarations of functions for sorting and searching. 7 | // 8 | #pragma once 9 | #ifndef _INC_SEARCH // include guard for 3rd party interop 10 | #define _INC_SEARCH 11 | 12 | #include 13 | #endif // _INC_SEARCH 14 | -------------------------------------------------------------------------------- /stdlib/bsearch_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // bsearch_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _bsearch_s(), which performs a binary search over an array. 7 | // 8 | #ifdef __USE_CONTEXT 9 | #error __USE_CONTEXT should be undefined 10 | #endif 11 | 12 | #define __USE_CONTEXT 13 | #include "bsearch.cpp" 14 | -------------------------------------------------------------------------------- /include/share.h: -------------------------------------------------------------------------------- 1 | // 2 | // share.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines the file sharing modes for the sopen() family of functions. 7 | // 8 | #pragma once 9 | #ifndef _INC_SHARE // include guard for 3rd party interop 10 | #define _INC_SHARE 11 | 12 | #include 13 | #endif // _INC_SHARE 14 | -------------------------------------------------------------------------------- /string/memcpy_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // memcpy_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Provides external definitions of the inline functions memcpy_s and memmove_s 7 | // for use by objects compiled with older versions of the CRT headers. 8 | // 9 | #define _CRT_MEMCPY_S_INLINE extern __inline 10 | #include 11 | -------------------------------------------------------------------------------- /misc/getpid.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // getpid.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The _getpid() function, which gets the current process identifier. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Gets the current process identifier. 13 | extern "C" int __cdecl _getpid() 14 | { 15 | return GetCurrentProcessId(); 16 | } 17 | -------------------------------------------------------------------------------- /string/strcspn.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strcspn.c - Defines the strcspn function. 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * The function strcspn is mostly common code with strspn in strspn.c. 8 | * 9 | *******************************************************************************/ 10 | 11 | #define SSTRCSPN 12 | #include "strspn.c" 13 | -------------------------------------------------------------------------------- /string/strpbrk.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strpbrk.c - Defines the strpbrk function. 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * The function strpbrk is mostly common code with strspn in strspn.c. 8 | * 9 | *******************************************************************************/ 10 | 11 | #define SSTRPBRK 12 | #include "strspn.c" 13 | -------------------------------------------------------------------------------- /stdlib/llabs.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // llabs.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines llabs(), which computes the absolute value of a number. 7 | // 8 | #include 9 | 10 | 11 | 12 | #pragma function(llabs) 13 | 14 | 15 | 16 | extern "C" long long __cdecl llabs(long long const number) 17 | { 18 | return number >= 0 ? number : -number; 19 | } 20 | -------------------------------------------------------------------------------- /string/amd64/strcspn.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strcspn.c - Defines the strcspn function. 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * The function strcspn is mostly common code with strspn in strspn.c. 8 | * 9 | *******************************************************************************/ 10 | 11 | #define SSTRCSPN 12 | #define STRSPN_USE_SSE2 13 | #include "strspn.c" 14 | -------------------------------------------------------------------------------- /string/amd64/strpbrk.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strpbrk.c - Defines the strpbrk function. 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * The function strpbrk is mostly common code with strspn in strspn.c. 8 | * 9 | *******************************************************************************/ 10 | 11 | #define SSTRPBRK 12 | #define STRSPN_USE_SSE2 13 | #include "strspn.c" 14 | -------------------------------------------------------------------------------- /stdlib/lsearch_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // lsearch_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _lsearch_s(), which performs a linear search over an array, appending 7 | // the key to the end of the array if it is not found. 8 | // 9 | #ifdef __USE_CONTEXT 10 | #error __USE_CONTEXT should be undefined 11 | #endif 12 | 13 | #define __USE_CONTEXT 14 | #include "lsearch.cpp" 15 | -------------------------------------------------------------------------------- /locale/glstatus.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *glstatus.c - sets the __globallocalestatus flag 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Sets the __globallocalestatus flag to disable per thread locale 8 | * 9 | *******************************************************************************/ 10 | 11 | #include 12 | 13 | extern "C" int __globallocalestatus = (~_GLOBAL_LOCALE_BIT); 14 | -------------------------------------------------------------------------------- /stdio/ncommode.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ncommode.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _commode, which sets the default file commit mode to nocommit. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Set default file commit mode to nocommit 13 | extern "C" int _commode = 0; 14 | 15 | 16 | 17 | extern "C" int* __cdecl __p__commode() 18 | { 19 | return &_commode; 20 | } 21 | -------------------------------------------------------------------------------- /stdlib/labs.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // labs.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines labs(), which computes the absolute value of a number. 7 | // 8 | #include 9 | 10 | extern "C" { 11 | 12 | 13 | 14 | #pragma function(labs) 15 | 16 | 17 | 18 | long __cdecl labs(long const number) 19 | { 20 | return number >= 0 ? number : -number; 21 | } 22 | 23 | 24 | 25 | } // extern "C" 26 | -------------------------------------------------------------------------------- /heap/heapmin.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // heapmin.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Implementation of _heapmin(). 7 | // 8 | #include 9 | #include 10 | 11 | // Minimizes the heap, freeing as much memory as possible back to the OS. 12 | // Returns 0 on success, -1 on failure. 13 | extern "C" int __cdecl _heapmin() 14 | { 15 | if (!HeapCompact(__acrt_heap, 0)) 16 | return -1; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /lowio/tell.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // tell.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _tell(), which gets the current position of the file pointer. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Gets the current position of the file pointer, without adjustment for 14 | // buffering. Returns -1 on error. 15 | extern "C" long __cdecl _tell(int const fh) 16 | { 17 | return _lseek(fh, 0, SEEK_CUR); 18 | } 19 | -------------------------------------------------------------------------------- /string/i386/strpbrk.asm: -------------------------------------------------------------------------------- 1 | ;*** 2 | ;strpbrk.asm - 3 | ; 4 | ; Copyright (c) Microsoft Corporation. All rights reserved. 5 | ; 6 | ;Purpose: 7 | ; defines strpbrk()- finds the index of the first character in a string 8 | ; that is not in a control string 9 | ; 10 | ; NOTE: This stub module scheme is compatible with NT build 11 | ; procedure. 12 | ; 13 | ;******************************************************************************* 14 | 15 | SSTRPBRK EQU 1 16 | INCLUDE STRSPN.ASM 17 | -------------------------------------------------------------------------------- /stdio/fileno.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // fileno.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _fileno(), which returns the lowio file handle for the given stdio 7 | // stream. 8 | // 9 | #include 10 | 11 | 12 | 13 | extern "C" int __cdecl _fileno(FILE* const public_stream) 14 | { 15 | __crt_stdio_stream const stream(public_stream); 16 | 17 | _VALIDATE_RETURN(stream.valid(), EINVAL, -1); 18 | return stream.lowio_handle(); 19 | } 20 | -------------------------------------------------------------------------------- /lowio/telli64.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // telli64.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _telli64(), which gets the current position of the file pointer. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Gets the current position of the file pointer, without adjustment for 14 | // buffering. Returns -1 on error. 15 | extern "C" __int64 __cdecl _telli64(int const fh) 16 | { 17 | return _lseeki64(fh, 0, SEEK_CUR); 18 | } 19 | -------------------------------------------------------------------------------- /stdlib/abs.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // abs.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines abs() and _abs64(), which compute the absolute value of a number. 7 | // 8 | #include 9 | 10 | 11 | 12 | #pragma function(abs, _abs64) 13 | 14 | 15 | 16 | extern "C" int __cdecl abs(int const number) 17 | { 18 | return number >= 0 ? number : -number; 19 | } 20 | 21 | extern "C" __int64 __cdecl _abs64(__int64 const number) 22 | { 23 | return number >= 0 ? number : -number; 24 | } 25 | -------------------------------------------------------------------------------- /string/i386/strcspn.asm: -------------------------------------------------------------------------------- 1 | ;*** 2 | ;strcspn.asm - 3 | ; 4 | ; Copyright (c) Microsoft Corporation. All rights reserved. 5 | ; 6 | ;Purpose: 7 | ; defines strcspn()- finds the length of the initial substring of 8 | ; a string consisting entirely of characters not in a control string. 9 | ; 10 | ; NOTE: This stub module scheme is compatible with NT build 11 | ; procedure. 12 | ; 13 | ;******************************************************************************* 14 | 15 | SSTRCSPN EQU 1 16 | INCLUDE STRSPN.ASM 17 | -------------------------------------------------------------------------------- /convert/fp_flags.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // fp_flags.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Public data used by the floating point library 7 | // 8 | 9 | extern "C" int __fastflag{0}; 10 | 11 | 12 | 13 | // Routine to set the fast flag in order to speed up computation 14 | // of transcendentals at the expense of limiting error checking 15 | extern "C" int __cdecl __setfflag(int const new_flag) 16 | { 17 | int const old_flag = __fastflag; 18 | __fastflag = new_flag; 19 | return old_flag; 20 | } 21 | -------------------------------------------------------------------------------- /string/wcscpy_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcscpy_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcscpy_s(), which copies a string from one buffer to another. 7 | // 8 | #include 9 | #include 10 | 11 | extern "C" errno_t __cdecl wcscpy_s( 12 | wchar_t* const destination, 13 | size_t const size_in_elements, 14 | wchar_t const* const source 15 | ) 16 | { 17 | return common_tcscpy_s(destination, size_in_elements, source); 18 | } 19 | -------------------------------------------------------------------------------- /include/math.h: -------------------------------------------------------------------------------- 1 | // 2 | // math.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The C Standard Library header. This header consists of two parts: 7 | // contains the math library; contains 8 | // the nonstandard but useful constant definitions. The headers are divided in 9 | // this way for modularity (to support the C++ modules feature). 10 | // 11 | #include 12 | 13 | #ifdef _USE_MATH_DEFINES 14 | #include 15 | #endif 16 | -------------------------------------------------------------------------------- /mbstring/mbscpy_s.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbscpy_s.c - Copy one string to another (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Copy one string to another (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | 16 | _REDIRECT_TO_L_VERSION_3(errno_t, _mbscpy_s, unsigned char *, size_t, const unsigned char *) 17 | -------------------------------------------------------------------------------- /stdlib/imaxabs.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // imaxabs.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines imaxabs(), which computes the absolute value of an intmax_t. 7 | // 8 | #include 9 | 10 | 11 | 12 | extern "C" intmax_t __cdecl imaxabs(intmax_t const number) 13 | { 14 | return number < 0 ? -number : number; 15 | } 16 | 17 | 18 | 19 | /* 20 | * Copyright (c) 1992-2010 by P.J. Plauger. ALL RIGHTS RESERVED. 21 | * Consult your license regarding permissions and restrictions. 22 | V5.30:0009 */ 23 | -------------------------------------------------------------------------------- /string/strcpy_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // strcpy_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines strcpy_s(), which copies a string from one buffer to another. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | extern "C" errno_t __cdecl strcpy_s( 14 | char* const destination, 15 | size_t const size_in_elements, 16 | char const* const source 17 | ) 18 | { 19 | return common_tcscpy_s(destination, size_in_elements, source); 20 | } 21 | -------------------------------------------------------------------------------- /mbstring/mbspbrk.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbspbrk.c - Find first string char in charset, pointer return (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Find first string char in charset, pointer return (MBCS) 8 | * Shares common source file with mbscspn.c. 9 | * 10 | *******************************************************************************/ 11 | #ifndef _MBCS 12 | #error This file should only be compiled with _MBCS defined 13 | #endif 14 | 15 | #define _RETURN_PTR 16 | #include "mbscspn.cpp" 17 | -------------------------------------------------------------------------------- /mbstring/mbccpy_s.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbccpy_s.c - Copy one character to another (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Copy one MBCS character to another (1 or 2 bytes) 8 | * 9 | *******************************************************************************/ 10 | 11 | #include 12 | 13 | #pragma warning(suppress:__WARNING_POSTCONDITION_NULLTERMINATION_VIOLATION) // 26036 14 | _REDIRECT_TO_L_VERSION_4(errno_t, _mbccpy_s, unsigned char *, size_t , int *, const unsigned char *) 15 | -------------------------------------------------------------------------------- /mbstring/mbscat_s.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbscat_s.c - Concatenate one string to another (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Concatenate one string to another (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | 16 | _REDIRECT_TO_L_VERSION_3(errno_t, _mbscat_s, unsigned char *, size_t, const unsigned char *) 17 | -------------------------------------------------------------------------------- /string/wcsset_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsset_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _wcsset_s(), which sets all of the characters of a null-terminated 7 | // string to the given character. 8 | // 9 | #include 10 | #include 11 | 12 | extern "C" errno_t __cdecl _wcsset_s( 13 | wchar_t* const destination, 14 | size_t const size_in_elements, 15 | wchar_t const value 16 | ) 17 | { 18 | return common_tcsset_s(destination, size_in_elements, value); 19 | } 20 | -------------------------------------------------------------------------------- /mbstring/mbsspnp.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsspnp.c - Find first string char in charset, pointer return (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Returns maximum leading segment of string consisting solely 8 | * of characters from charset. Handles MBCS characters correctly. 9 | * 10 | *******************************************************************************/ 11 | #ifndef _MBCS 12 | #error This file should only be compiled with _MBCS defined 13 | #endif 14 | 15 | #define _RETURN_PTR 16 | #include "mbsspn.cpp" 17 | -------------------------------------------------------------------------------- /mbstring/mbsset_s.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsset_s.c - Sets all charcaters of string to given character (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Sets all charcaters of string to given character (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | 16 | _REDIRECT_TO_L_VERSION_3(errno_t, _mbsset_s, unsigned char*, size_t, unsigned int) 17 | -------------------------------------------------------------------------------- /mbstring/mbsnbcpy_s.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsnbcpy_s.c - Copy one string to another, n bytes only (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Copy one string to another, n bytes only (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | 16 | _REDIRECT_TO_L_VERSION_4(errno_t, _mbsnbcpy_s, unsigned char *, size_t, const unsigned char *, size_t) 17 | -------------------------------------------------------------------------------- /mbstring/mbsncpy_s.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsncpy_s.c - Copy one string to another, n chars only (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Copy one string to another, n chars only (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | 16 | _REDIRECT_TO_L_VERSION_4(errno_t, _mbsncpy_s, unsigned char *, size_t, const unsigned char *, size_t) 17 | -------------------------------------------------------------------------------- /string/strncnt.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // strncnt.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines __strncnt(), which returns the number of characters in a string. If 7 | // the string is longer than the given 'count', 'count' is returned. 8 | // 9 | #include 10 | 11 | 12 | 13 | extern "C" size_t __cdecl __strncnt( 14 | char const* const string, 15 | size_t const count 16 | ) 17 | { 18 | char const* it = string; 19 | size_t n = 0; 20 | 21 | for (; *it && n != count; ++it, ++n) { } 22 | 23 | return n; 24 | } 25 | -------------------------------------------------------------------------------- /mbstring/mbsncat_s.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsncat_s.c - concatenate string2 onto string1, max length n 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines mbsncat_s() - concatenate maximum of n characters 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | 16 | _REDIRECT_TO_L_VERSION_4(errno_t, _mbsncat_s, unsigned char *, size_t, const unsigned char *, size_t) 17 | -------------------------------------------------------------------------------- /stdlib/div.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // div.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines div(), which performs a signed divide and returns the quotient and 7 | // remainder. No validation of the arguments is done. 8 | // 9 | #include 10 | 11 | 12 | #ifdef _MSC_VER 13 | #pragma function(div) 14 | #endif 15 | extern "C" div_t __cdecl div(int const numerator, int const denominator) 16 | { 17 | div_t result; 18 | 19 | result.quot = numerator / denominator; 20 | result.rem = numerator % denominator; 21 | 22 | return result; 23 | } 24 | -------------------------------------------------------------------------------- /string/strset_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // strset_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _strset_s(), which sets all of the characters of a null-terminated 7 | // string to the given character. 8 | // 9 | #include 10 | #include 11 | 12 | 13 | 14 | extern "C" errno_t __cdecl _strset_s( 15 | char* const destination, 16 | size_t const size_in_elements, 17 | int const value 18 | ) 19 | { 20 | return common_tcsset_s(destination, size_in_elements, static_cast(value)); 21 | } 22 | -------------------------------------------------------------------------------- /mbstring/mbsnbcat_s.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsnbcat_s.c - concatenate string2 onto string1, max length n bytes 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines mbsnbcat_s() - concatenate maximum of n bytes 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | 16 | _REDIRECT_TO_L_VERSION_4(errno_t, _mbsnbcat_s, unsigned char *, size_t, const unsigned char *, size_t) 17 | -------------------------------------------------------------------------------- /stdlib/ldiv.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // ldiv.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines ldiv(), which performs a signed divide and returns the quotient and 7 | // remainder. No validation of the arguments is done. 8 | // 9 | #include 10 | 11 | 12 | #ifdef _MSC_VER 13 | #pragma function(ldiv) 14 | #endif 15 | extern "C" ldiv_t __cdecl ldiv(long const numerator, long const denominator) 16 | { 17 | ldiv_t result; 18 | 19 | result.quot = numerator / denominator; 20 | result.rem = numerator % denominator; 21 | 22 | return result; 23 | } 24 | -------------------------------------------------------------------------------- /string/strcat_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // strcat_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines strcat_s(), which concatenates (appends) a copy of the source string 7 | // to the end of the destiation string. 8 | // 9 | #include 10 | #include 11 | 12 | 13 | 14 | extern "C" errno_t __cdecl strcat_s( 15 | char* const destination, 16 | size_t const size_in_elements, 17 | char const* const source 18 | ) 19 | { 20 | return common_tcscat_s(destination, size_in_elements, source); 21 | } 22 | -------------------------------------------------------------------------------- /string/wcsncnt.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsncnt.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines __wcsncnt(), which returns the number of characters in a string. If 7 | // the string is longer than the given 'count', 'count' is returned. 8 | // 9 | #include 10 | 11 | 12 | 13 | extern "C" size_t __cdecl __wcsncnt( 14 | wchar_t const* const string, 15 | size_t const count 16 | ) 17 | { 18 | wchar_t const* it = string; 19 | size_t n = 0; 20 | 21 | for (; *it && n != count; ++it, ++n) { } 22 | 23 | return n; 24 | } 25 | -------------------------------------------------------------------------------- /string/wcscat_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcscat_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcscat_s(), which concatenates (appends) a copy of the source string 7 | // to the end of the destiation string. 8 | // 9 | #include 10 | #include 11 | 12 | 13 | 14 | extern "C" errno_t __cdecl wcscat_s( 15 | wchar_t* const destination, 16 | size_t const size_in_elements, 17 | wchar_t const* const source 18 | ) 19 | { 20 | return common_tcscat_s(destination, size_in_elements, source); 21 | } 22 | -------------------------------------------------------------------------------- /filesystem/wmkdir.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wmkdir.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The _wmkdir() function, which creates a directory. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Creates a directory. Returns 0 on success; returns -1 and sets errno and 14 | // _doserrno on failure. 15 | extern "C" int __cdecl _wmkdir(wchar_t const* const path) 16 | { 17 | if (!CreateDirectoryW(path, nullptr)) 18 | { 19 | __acrt_errno_map_os_error(GetLastError()); 20 | return -1; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /stdlib/lldiv.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // lldiv.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines lldiv(), which performs a signed divide and returns the quotient and 7 | // remainder. No validation of the arguments is done. 8 | // 9 | #include 10 | 11 | 12 | #ifdef _MSC_VER 13 | #pragma function(lldiv) 14 | #endif 15 | extern "C" lldiv_t __cdecl lldiv(long long const numerator, long long const denominator) 16 | { 17 | lldiv_t result; 18 | 19 | result.quot = numerator / denominator; 20 | result.rem = numerator % denominator; 21 | 22 | return result; 23 | } 24 | -------------------------------------------------------------------------------- /time/days.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // days.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The global _days and _lpdays arrays, which define the number of days that 7 | // contain the number of days from the beginning of the year to the beginning 8 | // of each month. 9 | // 10 | #include 11 | 12 | 13 | 14 | extern "C" int const _days[] = 15 | { 16 | -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 17 | }; 18 | 19 | 20 | 21 | extern "C" int const _lpdays[] = 22 | { 23 | -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 24 | }; 25 | 26 | 27 | -------------------------------------------------------------------------------- /mbstring/mbsnbset_s.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsnbset_s.c - Sets first n bytes of string to given character (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Sets first n bytes of string to given character (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | _REDIRECT_TO_L_VERSION_4(errno_t, _mbsnbset_s, unsigned char *, size_t, unsigned int, size_t) 18 | -------------------------------------------------------------------------------- /stdlib/rand_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // rand_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The implementation of the rand_s() function, which generates random numbers. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | extern "C" errno_t __cdecl rand_s(unsigned int* const result) 14 | { 15 | _VALIDATE_RETURN_ERRCODE(result != nullptr, EINVAL); 16 | *result = 0; 17 | 18 | if (!__acrt_RtlGenRandom(result, static_cast(sizeof(*result)))) 19 | { 20 | errno = ENOMEM; 21 | return errno; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /mbstring/mbsnset_s.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsnset_s.c - Sets first n characters of string to given character (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Sets first n characters of string to given character (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | _REDIRECT_TO_L_VERSION_4(errno_t, _mbsnset_s, unsigned char*, size_t, unsigned int, size_t) 18 | -------------------------------------------------------------------------------- /string/wcsncat_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsncat_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcsncat_s(), which appends n characters of the source string onto the 7 | // end of the destination string. 8 | // 9 | #include 10 | #include 11 | 12 | extern "C" errno_t __cdecl wcsncat_s( 13 | wchar_t* const destination, 14 | size_t const size_in_elements, 15 | wchar_t const* const source, 16 | size_t const count 17 | ) 18 | { 19 | return common_tcsncat_s(destination, size_in_elements, source, count); 20 | } 21 | -------------------------------------------------------------------------------- /string/wcsncpy_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsncpy_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcsncpy_s(), which appends n characters of the source string onto the 7 | // end of the destination string. 8 | // 9 | #include 10 | #include 11 | 12 | extern "C" errno_t __cdecl wcsncpy_s( 13 | wchar_t* const destination, 14 | size_t const size_in_elements, 15 | wchar_t const* const source, 16 | size_t const count 17 | ) 18 | { 19 | return common_tcsncpy_s(destination, size_in_elements, source, count); 20 | } 21 | -------------------------------------------------------------------------------- /string/strncat_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // strncat_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines strncat_s(), which appends n characters of the source string onto the 7 | // end of the destination string. 8 | // 9 | #include 10 | #include 11 | 12 | 13 | 14 | extern "C" errno_t __cdecl strncat_s( 15 | char* const destination, 16 | size_t const size_in_elements, 17 | char const* const source, 18 | size_t const count 19 | ) 20 | { 21 | return common_tcsncat_s(destination, size_in_elements, source, count); 22 | } 23 | -------------------------------------------------------------------------------- /string/strncpy_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // strncpy_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines strncpy_s(), which appends n characters of the source string onto the 7 | // end of the destination string. 8 | // 9 | #include 10 | #include 11 | 12 | 13 | 14 | extern "C" errno_t __cdecl strncpy_s( 15 | char* const destination, 16 | size_t const size_in_elements, 17 | char const* const source, 18 | size_t const count 19 | ) 20 | { 21 | return common_tcsncpy_s(destination, size_in_elements, source, count); 22 | } 23 | -------------------------------------------------------------------------------- /heap/heapchk.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // heapchk.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Implementation of _heapchk(). 7 | // 8 | #include 9 | #include 10 | 11 | // Performs a consistency check on the heap. The return value is one of the 12 | // following: 13 | // * _HEAPOK The validation of the heap completed successfully 14 | // * _HEAPBADNODE Some node in the heap is malformed and the heap is corrupt 15 | extern "C" int __cdecl _heapchk() 16 | { 17 | if (!HeapValidate(__acrt_heap, 0, nullptr)) 18 | return _HEAPBADNODE; 19 | 20 | return _HEAPOK; 21 | } 22 | -------------------------------------------------------------------------------- /mbstring/mbsncpy_s_l.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsncpy_s_l.c - Copy one string to another, n chars only (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Copy one string to another, n chars only (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | #define _FUNC_NAME _mbsncpy_s_l 18 | #define _COUNT _CountInChars 19 | #define _COUNT_IN_BYTES 0 20 | 21 | #include "mbsncpy_s.inl" 22 | -------------------------------------------------------------------------------- /lowio/isatty.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // isatty.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _isatty(), which tests whether a file refers to a character device. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Tests if the given file refers to a character device (e.g. terminal, console, 13 | // printer, serial port, etc.). Returns nonzero if so; zero if not. 14 | extern "C" int __cdecl _isatty(int const fh) 15 | { 16 | _CHECK_FH_RETURN(fh, EBADF, 0); 17 | _VALIDATE_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, 0); 18 | 19 | return static_cast(_osfile(fh) & FDEV); 20 | } 21 | -------------------------------------------------------------------------------- /string/wcsnset.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsnset.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _wcsnset(), which sets all of the characters of a null-terminated 7 | // string to the given character. This function sets at most 'count' characters. 8 | // 9 | #include 10 | 11 | 12 | 13 | extern "C" wchar_t* __cdecl _wcsnset( 14 | wchar_t* const string, 15 | wchar_t const value, 16 | size_t const count 17 | ) 18 | { 19 | wchar_t* it = string; 20 | for (size_t i = 0; i != count && *it; ++i, ++it) 21 | { 22 | *it = value; 23 | } 24 | 25 | return string; 26 | } 27 | -------------------------------------------------------------------------------- /include/minmax.h: -------------------------------------------------------------------------------- 1 | // 2 | // minmax.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Familiar min() and max() macros 7 | // 8 | #pragma once 9 | #ifndef _INC_MINMAX 10 | #define _INC_MINMAX 11 | 12 | #include 13 | 14 | #pragma warning(push) 15 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 16 | _UCRT_DISABLE_CLANG_WARNINGS 17 | 18 | #ifndef max 19 | #define max(a ,b) (((a) > (b)) ? (a) : (b)) 20 | #endif 21 | 22 | #ifndef min 23 | #define min(a, b) (((a) < (b)) ? (a) : (b)) 24 | #endif 25 | 26 | _UCRT_RESTORE_CLANG_WARNINGS 27 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 28 | #endif // _INC_MINMAX 29 | -------------------------------------------------------------------------------- /mbstring/mbsnbcpy_s_l.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsnbcpy_s_l.c - Copy one string to another, n bytes only (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Copy one string to another, n bytes only (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | #define _FUNC_NAME _mbsnbcpy_s_l 18 | #define _COUNT _CountInBytes 19 | #define _COUNT_IN_BYTES 1 20 | 21 | #include "mbsncpy_s.inl" 22 | -------------------------------------------------------------------------------- /string/wcsnset_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsnset_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _wcsnset_s(), which sets at most the first 'count' characters of a 7 | // string to the given character and ensures that the resulting string is null- 8 | // terminated. 9 | // 10 | #include 11 | #include 12 | 13 | extern "C" errno_t __cdecl _wcsnset_s( 14 | wchar_t* const destination, 15 | size_t const size_in_elements, 16 | wchar_t const value, 17 | size_t const count 18 | ) 19 | { 20 | return common_tcsnset_s(destination, size_in_elements, value, count); 21 | } 22 | -------------------------------------------------------------------------------- /mbstring/mbsncat_s_l.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsncat_s_l.c - concatenate string2 onto string1, max length n 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines mbsncat_s_l() - concatenate maximum of n characters 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | #define _FUNC_NAME _mbsncat_s_l 18 | #define _COUNT _CountInChars 19 | #define _COUNT_IN_BYTES 0 20 | 21 | #include "mbsncat_s.inl" 22 | -------------------------------------------------------------------------------- /string/wcsset.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsset.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _wcsset(), which sets all of the characters in a wide character 7 | // string to the given value. 8 | // 9 | #include 10 | 11 | 12 | 13 | #if defined _M_X64 || defined _M_IX86 || defined _M_ARM || defined _M_ARM64 14 | #pragma warning(disable:4163) 15 | #pragma function(_wcsset) 16 | #endif 17 | 18 | 19 | 20 | extern "C" wchar_t* __cdecl _wcsset( 21 | wchar_t* const string, 22 | wchar_t const value 23 | ) 24 | { 25 | for (wchar_t* p = string; *p; ++p) 26 | *p = value; 27 | 28 | return string; 29 | } 30 | -------------------------------------------------------------------------------- /convert/common_utf8.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // common_utf8.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Common UTF-8 utilities 7 | // 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | namespace __crt_mbstring 14 | { 15 | size_t return_illegal_sequence(mbstate_t* ps, __crt_cached_ptd_host& ptd) 16 | { 17 | *ps = {}; 18 | ptd.get_errno().set(EILSEQ); 19 | return INVALID; 20 | } 21 | 22 | size_t reset_and_return(size_t retval, mbstate_t* ps) 23 | { 24 | *ps = {}; 25 | return retval; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /mbstring/mbsnbcat_s_l.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsnbcat_s_l.c - concatenate string2 onto string1, max length n bytes 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines mbsnbcat_s_l() - concatenate maximum of n bytes 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | #define _FUNC_NAME _mbsnbcat_s_l 18 | #define _COUNT _CountInBytes 19 | #define _COUNT_IN_BYTES 1 20 | 21 | #include "mbsncat_s.inl" 22 | -------------------------------------------------------------------------------- /mbstring/mbsnbset_s_l.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsset_s_l.c - Sets first n bytes of string to given character (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Sets first n bytes of string to given character (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | #define _FUNC_NAME _mbsnbset_s_l 18 | #define _COUNT _CountInBytes 19 | #define _COUNT_IN_BYTES 1 20 | 21 | #include "mbsnset_s.inl" 22 | -------------------------------------------------------------------------------- /mbstring/mbsnset_s_l.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsnset_s_l.c - Sets first n characters of string to given character (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Sets first n characters of string to given character (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | #define _FUNC_NAME _mbsnset_s_l 18 | #define _COUNT _CountInChars 19 | #define _COUNT_IN_BYTES 0 20 | 21 | #include "mbsnset_s.inl" 22 | -------------------------------------------------------------------------------- /string/strnset_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // strnset_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _strnset_s(), which sets at most the first 'count' characters of a 7 | // string to the given character and ensures that the resulting string is null- 8 | // terminated. 9 | // 10 | #include 11 | #include 12 | 13 | 14 | 15 | extern "C" errno_t __cdecl _strnset_s( 16 | char* const destination, 17 | size_t const size_in_elements, 18 | int const value, 19 | size_t const count 20 | ) 21 | { 22 | return common_tcsnset_s(destination, size_in_elements, static_cast(value), count); 23 | } 24 | -------------------------------------------------------------------------------- /string/wcscpy.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcscpy.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcscpy(), which copies a string from one buffer to another. 7 | // 8 | #include 9 | 10 | 11 | 12 | #if defined _M_X64 || defined _M_IX86 || defined _M_ARM || defined _M_ARM64 13 | #pragma warning(disable:4163) 14 | #pragma function(wcscpy) 15 | #endif 16 | 17 | 18 | 19 | extern "C" wchar_t* __cdecl wcscpy( 20 | wchar_t* const destination, 21 | wchar_t const* source) 22 | { 23 | wchar_t* destination_it = destination; 24 | while ((*destination_it++ = *source++) != '\0') { } 25 | 26 | return destination; 27 | } 28 | -------------------------------------------------------------------------------- /internal/peb_access.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // peb_access.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Wrapper functions to access fields in the PEB. 7 | // 8 | 9 | // Using internal headers for definitions. Only call publicly available functions. 10 | #include 11 | #include 12 | #include 13 | 14 | extern "C" bool __cdecl __acrt_app_verifier_enabled() 15 | { 16 | return (NtCurrentTeb()->ProcessEnvironmentBlock->NtGlobalFlag & FLG_APPLICATION_VERIFIER) != 0; 17 | } 18 | 19 | extern "C" bool __cdecl __acrt_is_secure_process() 20 | { 21 | return (NtCurrentTeb()->ProcessEnvironmentBlock->ProcessParameters->Flags & RTL_USER_PROC_SECURE_PROCESS) != 0; 22 | } 23 | -------------------------------------------------------------------------------- /include/stdnoreturn.h: -------------------------------------------------------------------------------- 1 | // 2 | // stdnoreturn.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The C Standard Library header. 7 | // 8 | #pragma once 9 | #ifndef _INC_STDNORETURN // include guard for 3rd party interop 10 | #define _INC_STDNORETURN 11 | 12 | #include 13 | 14 | #pragma warning(push) 15 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 16 | _UCRT_DISABLE_CLANG_WARNINGS 17 | _CRT_BEGIN_C_HEADER 18 | 19 | #if _CRT_HAS_C11 20 | 21 | #define noreturn _Noreturn 22 | 23 | #endif // _CRT_HAS_C11 24 | 25 | _CRT_END_C_HEADER 26 | _UCRT_RESTORE_CLANG_WARNINGS 27 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 28 | #endif // _INC_STDNORETURN 29 | -------------------------------------------------------------------------------- /string/wcsrev.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsrev.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _wcsrev(), which reverses a wide chraacter string in place. The 7 | // pointer to the string is returned. 8 | // 9 | #include 10 | 11 | 12 | 13 | extern "C" wchar_t* __cdecl _wcsrev(wchar_t* const string) 14 | { 15 | // Find the end of the string: 16 | wchar_t* right = string; 17 | while (*right++) { } 18 | right -= 2; 19 | 20 | // Reverse the strong: 21 | wchar_t* left = string; 22 | while (left < right) 23 | { 24 | wchar_t const c = *left; 25 | *left++ = *right; 26 | *right-- = c; 27 | } 28 | 29 | return string; 30 | } 31 | -------------------------------------------------------------------------------- /stdlib/rand.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // rand.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines rand(), which generates psuedorandom numbers. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Seeds the random number generator with the provided integer. 14 | extern "C" void __cdecl srand(unsigned int const seed) 15 | { 16 | __acrt_getptd()->_rand_state = seed; 17 | } 18 | 19 | 20 | 21 | // Returns a pseudorandom number in the range [0,32767]. 22 | extern "C" int __cdecl rand() 23 | { 24 | __acrt_ptd* const ptd = __acrt_getptd(); 25 | 26 | ptd->_rand_state = ptd->_rand_state * 214013 + 2531011; 27 | return (ptd->_rand_state >> 16) & RAND_MAX; 28 | } 29 | -------------------------------------------------------------------------------- /internal/SetCurrentDirectoryA.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // SetCurrentDirectoryA.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Definition of __acrt_SetCurrentDirectoryA. 7 | // 8 | 9 | #include 10 | 11 | BOOL __cdecl __acrt_SetCurrentDirectoryA( 12 | LPCSTR const lpPathName 13 | ) 14 | { 15 | __crt_internal_win32_buffer wide_path_name; 16 | 17 | errno_t const cvt1 = __acrt_mbs_to_wcs_cp( 18 | lpPathName, 19 | wide_path_name, 20 | __acrt_get_utf8_acp_compatibility_codepage() 21 | ); 22 | 23 | if (cvt1 != 0) { 24 | return FALSE; 25 | } 26 | 27 | return ::SetCurrentDirectoryW(wide_path_name.data()); 28 | } 29 | -------------------------------------------------------------------------------- /misc/drivemap.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *drivemap.c - _getdrives 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines _getdrives() 8 | * 9 | *******************************************************************************/ 10 | 11 | #include 12 | #include 13 | 14 | /*** 15 | *void _getdrivemap(void) - Get bit map of all available drives 16 | * 17 | *Purpose: 18 | * 19 | *Entry: 20 | * 21 | *Exit: 22 | * drive map with drive A in bit 0, B in 1, etc. 23 | * 24 | *Exceptions: 25 | * 26 | *******************************************************************************/ 27 | 28 | extern "C" unsigned long __cdecl _getdrives() 29 | { 30 | return GetLogicalDrives(); 31 | } 32 | -------------------------------------------------------------------------------- /stdio/fsetpos.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // fsetpos.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines fsetpos(), which sets the file position using a position previously 7 | // returned by fgetpos(). 8 | // 9 | #include 10 | 11 | 12 | 13 | // Sets the file position to the given position. The position value must have 14 | // been returned by a prior call to fgetpos(). Returns 0 on success; returns 15 | // nonzero on failure. 16 | extern "C" int __cdecl fsetpos(FILE* const stream, fpos_t const* const position) 17 | { 18 | _VALIDATE_RETURN(stream != nullptr, EINVAL, -1); 19 | _VALIDATE_RETURN(position != nullptr, EINVAL, -1); 20 | 21 | return _fseeki64(stream, *position, SEEK_SET); 22 | } 23 | -------------------------------------------------------------------------------- /filesystem/wrmdir.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wrmdir.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The _wrmdir() function, which removes a directory. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Removes the directory specified by the path. The directory must be empty, it 14 | // must not be the current working directory, and it must not be the root of any 15 | // drive. Returns 0 on success; returns -1 and sets errno and _doserrno on 16 | // failure. 17 | extern "C" int __cdecl _wrmdir(wchar_t const* const path) 18 | { 19 | if (!RemoveDirectoryW(path)) 20 | { 21 | __acrt_errno_map_os_error(GetLastError()); 22 | return -1; 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /filesystem/wunlink.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wunlink.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The _wremove() and _wunlink() functions, which remove (delete) files. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Deletes the specified file. Returns zero if successful; returns -1 and sets 14 | // errno and _doserrno on failure. 15 | extern "C" int __cdecl _wremove(wchar_t const* const path) 16 | { 17 | if (!DeleteFileW(path)) 18 | { 19 | __acrt_errno_map_os_error(GetLastError()); 20 | return -1; 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | 27 | 28 | extern "C" int __cdecl _wunlink(wchar_t const* const path) 29 | { 30 | return _wremove(path); 31 | } 32 | -------------------------------------------------------------------------------- /filesystem/wrename.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wrename.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The _wrename() function, which renames a file. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Renames the file named 'old_name' to be named 'new_name'. Returns zero if 14 | // successful; returns -1 and sets errno and _doserrno on failure. 15 | extern "C" int __cdecl _wrename(wchar_t const* const old_name, wchar_t const* const new_name) 16 | { 17 | // The MOVEFILE_COPY_ALLOWED flag alloes moving to a different volume. 18 | if (!MoveFileExW(old_name, new_name, MOVEFILE_COPY_ALLOWED)) 19 | { 20 | __acrt_errno_map_os_error(GetLastError()); 21 | return -1; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /string/wcspbrk.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcspbrk.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcspbrk(), which returns a pointer to the first wide character in 7 | // 'string' that is in the 'control'. 8 | // 9 | #include 10 | 11 | 12 | 13 | extern "C" wchar_t const* __cdecl wcspbrk( 14 | wchar_t const* const string, 15 | wchar_t const* const control 16 | ) 17 | { 18 | for (wchar_t const* string_it = string; *string_it; ++string_it) 19 | { 20 | for (wchar_t const* control_it = control; *control_it; ++control_it) 21 | { 22 | if (*control_it == *string_it) 23 | { 24 | return string_it; 25 | } 26 | } 27 | } 28 | 29 | return nullptr; 30 | } 31 | -------------------------------------------------------------------------------- /include/stdalign.h: -------------------------------------------------------------------------------- 1 | // 2 | // stdalign.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The C Standard Library header. 7 | // 8 | #pragma once 9 | #ifndef _INC_STDALIGN // include guard for 3rd party interop 10 | #define _INC_STDALIGN 11 | 12 | #include 13 | 14 | #pragma warning(push) 15 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 16 | _UCRT_DISABLE_CLANG_WARNINGS 17 | _CRT_BEGIN_C_HEADER 18 | 19 | #if _CRT_HAS_C11 20 | 21 | #define alignas _Alignas 22 | #define alignof _Alignof 23 | #define __alignas_is_defined 1 24 | #define __alignof_is_defined 1 25 | 26 | #endif // _CRT_HAS_C11 27 | 28 | _CRT_END_C_HEADER 29 | _UCRT_RESTORE_CLANG_WARNINGS 30 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 31 | #endif // _INC_STDALIGN 32 | -------------------------------------------------------------------------------- /initializers/clock_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // clock_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | extern "C" int __cdecl __acrt_initialize_clock(); 16 | 17 | extern "C" _CRTALLOC(".CRT$XIC") _PIFV const __acrt_clock_initializer = __acrt_initialize_clock; 18 | -------------------------------------------------------------------------------- /initializers/fma3_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // fma3_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | extern "C" int __cdecl __acrt_initialize_fma3(); 16 | 17 | extern "C" _CRTALLOC(".CRT$XIC") _PIFV const __acrt_tran_fma3_initializer = __acrt_initialize_fma3; 18 | -------------------------------------------------------------------------------- /initializers/fmode_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // fmode_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | extern "C" int __cdecl __acrt_initialize_fmode(); 16 | 17 | extern "C" _CRTALLOC(".CRT$XIC") _PIFV const __acrt_fmode_initializer = __acrt_initialize_fmode; 18 | -------------------------------------------------------------------------------- /initializers/locale_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // locale_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | extern "C" void __cdecl __acrt_uninitialize_locale(); 16 | 17 | extern "C" _CRTALLOC(".CRT$XPX") _PVFV const __acrt_locale_terminator = __acrt_uninitialize_locale; 18 | -------------------------------------------------------------------------------- /initializers/timeset_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // timeset_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | extern "C" int __cdecl __acrt_initialize_timeset(); 16 | 17 | extern "C" _CRTALLOC(".CRT$XIC") _PIFV const __acrt_timeset_initializer = __acrt_initialize_timeset; 18 | -------------------------------------------------------------------------------- /initializers/tmpfile_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // tmpfile_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | extern "C" void __cdecl __acrt_uninitialize_tmpfile(); 16 | 17 | extern "C" _CRTALLOC(".CRT$XPX") _PVFV const __acrt_tmpfile_terminator = __acrt_uninitialize_tmpfile; 18 | -------------------------------------------------------------------------------- /filesystem/mkdir.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // mkdir.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The _mkdir() function, which creates a directory. 7 | // 8 | #include 9 | #include 10 | #include 11 | 12 | // Creates a directory. Returns 0 on success; returns -1 and sets errno and 13 | // _doserrno on failure. 14 | extern "C" int __cdecl _mkdir(char const* const path) 15 | { 16 | if (path == nullptr) { 17 | return _wmkdir(nullptr); 18 | } 19 | 20 | __crt_internal_win32_buffer wide_path; 21 | 22 | errno_t const cvt = __acrt_mbs_to_wcs_cp(path, wide_path, __acrt_get_utf8_acp_compatibility_codepage()); 23 | 24 | if (cvt != 0) { 25 | return -1; 26 | } 27 | 28 | return _wmkdir(wide_path.data()); 29 | } 30 | -------------------------------------------------------------------------------- /internal/LoadLibraryExA.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // LoadLibraryExA.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Definition of __acrt_LoadLibraryExA. 7 | // 8 | 9 | #include 10 | 11 | HMODULE __cdecl __acrt_LoadLibraryExA( 12 | LPCSTR const lpFilename, 13 | HANDLE const hFile, 14 | DWORD const dwFlags 15 | ) 16 | { 17 | __crt_internal_win32_buffer wide_file_name; 18 | 19 | errno_t const cvt1 = __acrt_mbs_to_wcs_cp( 20 | lpFilename, 21 | wide_file_name, 22 | __acrt_get_utf8_acp_compatibility_codepage() 23 | ); 24 | 25 | if (cvt1 != 0) { 26 | return nullptr; 27 | } 28 | 29 | return ::LoadLibraryExW( 30 | wide_file_name.data(), 31 | hFile, 32 | dwFlags 33 | ); 34 | } 35 | -------------------------------------------------------------------------------- /initializers/console_input_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // console_input_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | extern "C" void __cdecl __dcrt_terminate_console_input(); 16 | 17 | extern "C" _CRTALLOC(".CRT$XPX") _PVFV const __dcrt_console_input_terminator = __dcrt_terminate_console_input; 18 | -------------------------------------------------------------------------------- /initializers/console_output_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // console_output_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | extern "C" void __cdecl __dcrt_terminate_console_output(); 16 | 17 | extern "C" _CRTALLOC(".CRT$XPX") _PVFV const __dcrt_console_output_terminator = __dcrt_terminate_console_output; 18 | -------------------------------------------------------------------------------- /misc/debug_fill_threshold.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // debug_fill_threshold.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Functions to control the debug fill threshold used by the secure string 7 | // functions. A threshold of 0 disables filling; a threshold of SIZE_MAX 8 | // means to always fill to the maximum. 9 | // 10 | #include 11 | #include 12 | 13 | 14 | 15 | static size_t __acrt_debug_fill_threshold = SIZE_MAX; 16 | 17 | 18 | 19 | extern "C" size_t __cdecl _CrtSetDebugFillThreshold(size_t const new_threshold) 20 | { 21 | size_t const old_threshold{__acrt_debug_fill_threshold}; 22 | __acrt_debug_fill_threshold = new_threshold; 23 | return old_threshold; 24 | } 25 | 26 | extern "C" size_t __cdecl _CrtGetDebugFillThreshold() 27 | { 28 | return __acrt_debug_fill_threshold; 29 | } 30 | -------------------------------------------------------------------------------- /include/wctype.h: -------------------------------------------------------------------------------- 1 | // 2 | // stdio.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The C Standard Library header. 7 | // 8 | #pragma once 9 | #ifndef _INC_WCTYPE // include guard for 3rd party interop 10 | #define _INC_WCTYPE 11 | 12 | #include 13 | #include 14 | 15 | #pragma warning(push) 16 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 17 | _UCRT_DISABLE_CLANG_WARNINGS 18 | 19 | _CRT_BEGIN_C_HEADER 20 | 21 | 22 | 23 | typedef wchar_t wctrans_t; 24 | _ACRTIMP wint_t __cdecl towctrans(wint_t c, wctrans_t value); 25 | _ACRTIMP wctrans_t __cdecl wctrans(const char *name); 26 | _ACRTIMP wctype_t __cdecl wctype(const char *name); 27 | 28 | 29 | 30 | _CRT_END_C_HEADER 31 | _UCRT_RESTORE_CLANG_WARNINGS 32 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 33 | #endif // _INC_WCTYPE 34 | -------------------------------------------------------------------------------- /internal/OutputDebugStringA.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // OutputDebugStringA.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Definition of __acrt_OutputDebugStringA. 7 | // 8 | 9 | #include 10 | 11 | void WINAPI __acrt_OutputDebugStringA( 12 | LPCSTR const text 13 | ) 14 | { 15 | if (text == nullptr) 16 | { 17 | return; 18 | } 19 | size_t const text_size = strlen(text) + 1; 20 | if (text_size == 0) 21 | { 22 | return; 23 | } 24 | // Use alloca instead of heap, since this code is executed during asserts 25 | auto text_wide = (wchar_t*)_alloca(text_size * sizeof(wchar_t)); 26 | size_t converted; 27 | if (mbstowcs_s(&converted, text_wide, text_size, text, text_size - 1) == 0) 28 | { 29 | OutputDebugStringW(text_wide); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stdio/fgetpos.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // fgetpos.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines fgetpos(), which gets the file position in an opaque internal format. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Gets the file position in the internal fpos_t format. The returned value 13 | // should only be used in a call to fsetpos(). Our implementation happens to 14 | // just wrap _ftelli64() and _fseeki64(). Return zero on success; returns -1 15 | // and sets errno on failure. 16 | extern "C" int __cdecl fgetpos(FILE* const stream, fpos_t* const position) 17 | { 18 | _VALIDATE_RETURN(stream != nullptr, EINVAL, -1); 19 | _VALIDATE_RETURN(position != nullptr, EINVAL, -1); 20 | 21 | *position = _ftelli64(stream); 22 | if (*position == -1) 23 | return -1; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /stdio/setbuf.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // setbuf.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines setbuf(), which enables or disables buffering on a stream. 7 | // 8 | #include 9 | 10 | 11 | 12 | // If the buffer is null, buffering is disabled for the stream. If the buffer is 13 | // non-null, it must point to a buffer of BUFSIZ characters; the stream will be 14 | // configured to use that buffer. The functionality of setbuf() is a strict 15 | // subset of the functionality of setvbuf(). 16 | extern "C" void __cdecl setbuf(FILE* const stream, char* const buffer) 17 | { 18 | _ASSERTE(stream != nullptr); 19 | 20 | if (buffer == nullptr) 21 | { 22 | setvbuf(stream, nullptr, _IONBF, 0); 23 | } 24 | else 25 | { 26 | setvbuf(stream, buffer, _IOFBF, BUFSIZ); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /initializers/multibyte_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // multibyte_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | static int __cdecl initialize_multibyte() throw() 16 | { 17 | return !__acrt_initialize_multibyte(); 18 | } 19 | 20 | extern "C" _CRTALLOC(".CRT$XIC") _PIFV const __acrt_multibyte_initializer = initialize_multibyte; 21 | -------------------------------------------------------------------------------- /heap/malloc.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // malloc.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Implementation of malloc(). 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Allocates a block of memory of size 'size' bytes in the heap. If allocation 14 | // fails, nullptr is returned. 15 | // 16 | // This function supports patching and therefore must be marked noinline. 17 | // Both _malloc_dbg and _malloc_base must also be marked noinline 18 | // to prevent identical COMDAT folding from substituting calls to malloc 19 | // with either other function or vice versa. 20 | extern "C" _CRT_HYBRIDPATCHABLE __declspec(noinline) _CRTRESTRICT void* __cdecl malloc(size_t const size) 21 | { 22 | #ifdef _DEBUG 23 | return _malloc_dbg(size, _NORMAL_BLOCK, nullptr, 0); 24 | #else 25 | return _malloc_base(size); 26 | #endif 27 | } 28 | -------------------------------------------------------------------------------- /convert/swab.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // swab.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines the _swab function, which copies a source buffer into a destination 7 | // buffer, swapping the odd and even bytes of each word as it does so. 8 | // 9 | #include 10 | #include 11 | 12 | 13 | 14 | extern "C" void __cdecl _swab( 15 | char* source, 16 | char* destination, 17 | int bytes 18 | ) 19 | { 20 | _VALIDATE_RETURN_VOID(source != nullptr, EINVAL); 21 | _VALIDATE_RETURN_VOID(destination != nullptr, EINVAL); 22 | _VALIDATE_RETURN_VOID(bytes >= 0, EINVAL); 23 | 24 | while (bytes > 1) 25 | { 26 | char const b1 = *source++; 27 | char const b2 = *source++; 28 | 29 | *destination++ = b2; 30 | *destination++ = b1; 31 | 32 | bytes -= 2; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /stdio/feoferr.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // feoferr.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines feof() and ferror(), which test the end-of-file and error states of a 7 | // stream, respectively. 8 | // 9 | #include 10 | 11 | 12 | 13 | // Tests the stream for the end-of-file condition. Returns nonzero if and only 14 | // if the stream is at end-of-file. 15 | extern "C" int __cdecl feof(FILE* const public_stream) 16 | { 17 | _VALIDATE_RETURN(public_stream != nullptr, EINVAL, 0); 18 | return __crt_stdio_stream(public_stream).eof(); 19 | } 20 | 21 | 22 | 23 | // Tests the stream error indicator. Returns nonzero if and only if the error 24 | // indicator for the stream is set. 25 | extern "C" int __cdecl ferror(FILE* const public_stream) 26 | { 27 | _VALIDATE_RETURN(public_stream != nullptr, EINVAL, 0); 28 | return __crt_stdio_stream(public_stream).error(); 29 | } 30 | -------------------------------------------------------------------------------- /initializers/stdio_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // stdio_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | extern "C" int __cdecl __acrt_initialize_stdio(); 16 | extern "C" void __cdecl __acrt_uninitialize_stdio(); 17 | 18 | extern "C" _CRTALLOC(".CRT$XIC") _PIFV const __acrt_stdio_initializer = __acrt_initialize_stdio; 19 | extern "C" _CRTALLOC(".CRT$XPXA") _PVFV const __acrt_stdio_terminator = __acrt_uninitialize_stdio; 20 | -------------------------------------------------------------------------------- /heap/new_mode.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // new_mode.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Definition of _set_new_mode and _query_new_mode, which provide access to the 7 | // _newmode global flag. 8 | // 9 | #include 10 | #include 11 | 12 | 13 | 14 | static __crt_state_management::dual_state_global __acrt_global_new_mode; 15 | 16 | 17 | 18 | // Sets the _newmode flag to the new value 'mode' and return the old mode. 19 | extern "C" int __cdecl _set_new_mode(int const mode) 20 | { 21 | // The only valid values of _newmode are 0 and 1: 22 | _VALIDATE_RETURN(mode == 0 || mode == 1, EINVAL, -1); 23 | 24 | return static_cast(_InterlockedExchange(&__acrt_global_new_mode.value(), mode)); 25 | } 26 | 27 | // Gets the current value of the _newmode flag. 28 | extern "C" int __cdecl _query_new_mode() 29 | { 30 | return static_cast(__crt_interlocked_read(&__acrt_global_new_mode.value())); 31 | } 32 | -------------------------------------------------------------------------------- /stdio/clearerr.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // clearerr.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines clearerr(), which clears error and EOF flags from a stream. 7 | // 8 | #include 9 | 10 | 11 | 12 | extern "C" errno_t __cdecl clearerr_s(FILE* const public_stream) 13 | { 14 | __crt_stdio_stream const stream(public_stream); 15 | 16 | _VALIDATE_RETURN_ERRCODE(stream.valid(), EINVAL); 17 | 18 | _lock_file(stream.public_stream()); 19 | __try 20 | { 21 | stream.unset_flags(_IOERROR | _IOEOF); // Clear stdio flags 22 | _osfile_safe(_fileno(stream.public_stream())) &= ~(FEOFLAG); // Clear lowio flags 23 | } 24 | __finally 25 | { 26 | _unlock_file(stream.public_stream()); 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | 33 | 34 | extern "C" void __cdecl clearerr(FILE* const stream) 35 | { 36 | clearerr_s(stream); 37 | } 38 | -------------------------------------------------------------------------------- /time/difftime.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // difftime.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The difftime() family of functions, which compute the difference between time 7 | // values. 8 | // 9 | #include 10 | 11 | 12 | 13 | // Computes the difference between two times (b - a). Returns a double with the 14 | // difference in seconds between the two times. Returns zero if the input is 15 | // invalid. 16 | template 17 | static double __cdecl common_difftime(TimeType const b, TimeType const a) throw() 18 | { 19 | _VALIDATE_RETURN_NOEXC(a >= 0 && b >= 0, EINVAL, 0); 20 | 21 | return static_cast(b - a); 22 | } 23 | 24 | extern "C" double __cdecl _difftime32(__time32_t const b, __time32_t const a) 25 | { 26 | return common_difftime(b, a); 27 | } 28 | 29 | extern "C" double __cdecl _difftime64(__time64_t const b, __time64_t const a) 30 | { 31 | return common_difftime(b, a); 32 | } 33 | -------------------------------------------------------------------------------- /filesystem/unlink.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // unlink.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The remove() and unlink() functions, which remove (delete) files. 7 | // 8 | #include 9 | #include 10 | #include 11 | 12 | // Deletes the specified file. Returns zero if successful; returns -1 and sets 13 | // errno and _doserrno on failure. 14 | extern "C" int __cdecl remove(char const* const path) 15 | { 16 | if (path == nullptr) 17 | return _wremove(nullptr); 18 | 19 | __crt_internal_win32_buffer wide_path; 20 | 21 | errno_t const cvt = __acrt_mbs_to_wcs_cp(path, wide_path, __acrt_get_utf8_acp_compatibility_codepage()); 22 | 23 | if (cvt != 0) { 24 | return -1; 25 | } 26 | 27 | return _wremove(wide_path.data()); 28 | } 29 | 30 | 31 | 32 | extern "C" int __cdecl _unlink(char const* const path) 33 | { 34 | return remove(path); 35 | } 36 | -------------------------------------------------------------------------------- /string/wcstok.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcstok.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcstok(), which tokenizes a string via repeated calls. See strtok() 7 | // for more details. 8 | // 9 | #include 10 | #include 11 | 12 | 13 | 14 | _Check_return_ 15 | extern "C" wchar_t* __cdecl __acrt_wcstok_s_novalidation( 16 | _Inout_opt_z_ wchar_t* string, 17 | _In_z_ wchar_t const* control, 18 | _Inout_ _Deref_prepost_opt_z_ wchar_t** context 19 | ); 20 | 21 | 22 | 23 | extern "C" wchar_t* __cdecl wcstok( 24 | wchar_t* const string, 25 | wchar_t const* const control, 26 | wchar_t** const context 27 | ) 28 | { 29 | wchar_t** const context_to_use = context != nullptr 30 | ? context 31 | : &__acrt_getptd()->_wcstok_token; 32 | 33 | return __acrt_wcstok_s_novalidation(string, control, context_to_use); 34 | } 35 | -------------------------------------------------------------------------------- /filesystem/rmdir.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // rmdir.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The _rmdir() function, which removes a directory. 7 | // 8 | #include 9 | #include 10 | #include 11 | 12 | // Removes the directory specified by the path. The directory must be empty, it 13 | // must not be the current working directory, and it must not be the root of any 14 | // drive. Returns 0 on success; returns -1 and sets errno and _doserrno on 15 | // failure. 16 | extern "C" int __cdecl _rmdir(char const* const path) 17 | { 18 | if (path == nullptr) { 19 | return _wrmdir(nullptr); 20 | } 21 | 22 | __crt_internal_win32_buffer wide_path; 23 | 24 | errno_t const cvt = __acrt_mbs_to_wcs_cp(path, wide_path, __acrt_get_utf8_acp_compatibility_codepage()); 25 | 26 | if (cvt != 0) { 27 | return -1; 28 | } 29 | 30 | return _wrmdir(wide_path.data()); 31 | } 32 | -------------------------------------------------------------------------------- /filesystem/chmod.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // chmod.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The _chmod() function, which changes file attributes. 7 | // 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | // Changes the mode of a file. The only supported mode bit is _S_IWRITE, which 14 | // controls the user write (read-only) attribute of the file. Returns zero if 15 | // successful; returns -1 and sets errno and _doserrno on failure. 16 | extern "C" int __cdecl _chmod(char const* const path, int const mode) 17 | { 18 | if (path == nullptr) { 19 | return _wchmod(nullptr, mode); 20 | } 21 | 22 | __crt_internal_win32_buffer wide_path; 23 | 24 | errno_t const cvt = __acrt_mbs_to_wcs_cp(path, wide_path, __acrt_get_utf8_acp_compatibility_codepage()); 25 | 26 | if (cvt != 0) { 27 | return -1; 28 | } 29 | 30 | return _wchmod(wide_path.data(), mode); 31 | } 32 | -------------------------------------------------------------------------------- /include/sys/locking.h: -------------------------------------------------------------------------------- 1 | // 2 | // sys/locking.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // This file defines the flags for the locking() function. 7 | // 8 | #pragma once 9 | 10 | #include 11 | 12 | #pragma warning(push) 13 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 14 | _UCRT_DISABLE_CLANG_WARNINGS 15 | 16 | #define _LK_UNLCK 0 // unlock the file region 17 | #define _LK_LOCK 1 // lock the file region 18 | #define _LK_NBLCK 2 // non-blocking lock 19 | #define _LK_RLCK 3 // lock for writing 20 | #define _LK_NBRLCK 4 // non-blocking lock for writing 21 | 22 | #if (defined _CRT_DECLARE_NONSTDC_NAMES && _CRT_DECLARE_NONSTDC_NAMES) || (!defined _CRT_DECLARE_NONSTDC_NAMES && !__STDC__) 23 | #define LK_UNLCK _LK_UNLCK 24 | #define LK_LOCK _LK_LOCK 25 | #define LK_NBLCK _LK_NBLCK 26 | #define LK_RLCK _LK_RLCK 27 | #define LK_NBRLCK _LK_NBRLCK 28 | #endif 29 | 30 | _UCRT_RESTORE_CLANG_WARNINGS 31 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 32 | -------------------------------------------------------------------------------- /stdio/_freebuf.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // _freebuf.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines __acrt_stdio_free_buffer(), which releaes a buffer from a stream. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Releases a buffer from a stream. If the stream is buffered and the buffer 13 | // was allocated by the CRT, the space is freed. If the buffer was provided by 14 | // the user, it is not freed (since we do not know how to free it). 15 | extern "C" void __cdecl __acrt_stdio_free_buffer_nolock(FILE* const public_stream) 16 | { 17 | _ASSERTE(public_stream != nullptr); 18 | 19 | __crt_stdio_stream const stream(public_stream); 20 | 21 | if (!stream.is_in_use()) 22 | return; 23 | 24 | if (!stream.has_crt_buffer()) 25 | return; 26 | 27 | _free_crt(stream->_base); 28 | 29 | stream.unset_flags(_IOBUFFER_CRT | _IOBUFFER_SETVBUF); 30 | stream->_base = nullptr; 31 | stream->_ptr = nullptr; 32 | stream->_cnt = 0; 33 | } 34 | -------------------------------------------------------------------------------- /initializers/i386/sse2_initializer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // sse2_initializer.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // CRT initializers and terminators have been extracted from the main CRT sources 7 | // to enable the CRT DLL to be built with LTCG enabled. The source files in which 8 | // the CRT initializers and terminators are defined cannot be compiled as /GL 9 | // because the compiler will optimize them away during link-time code generation. 10 | // We inhibit this optimization by defining the initializers and terminators in 11 | // separate source files that are not compiled with /GL. 12 | // 13 | #include 14 | 15 | extern "C" int __cdecl __acrt_initialize_sse2(); 16 | 17 | #ifdef _M_HYBRID_X86_ARM64 18 | #pragma intrinsic(_HybridGenerateThunks) 19 | 20 | extern "C" int __cdecl __acrt_initialize_sse2() 21 | { 22 | _HybridGenerateThunks(__acrt_initialize_sse2, 1); 23 | return 0; 24 | } 25 | #endif 26 | 27 | extern "C" _CRTALLOC(".CRT$XIC") _PIFV const __acrt_tran_sse2_initializer = __acrt_initialize_sse2; 28 | -------------------------------------------------------------------------------- /string/wcscat.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcscat.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcscat(), which concatenates (appends) a copy of the source string to 7 | // the end of the destination string. 8 | // 9 | // This function assumes that the destination buffer is sufficiently large to 10 | // store the appended string. 11 | // 12 | #include 13 | 14 | 15 | 16 | #if defined _M_X64 || defined _M_IX86 || defined _M_ARM || defined _M_ARM64 17 | #pragma warning(disable:4163) 18 | #pragma function(wcscat) 19 | #endif 20 | 21 | 22 | 23 | extern "C" wchar_t * __cdecl wcscat( 24 | wchar_t* const destination, 25 | wchar_t const* source 26 | ) 27 | { 28 | wchar_t* destination_it = destination; 29 | 30 | // Find the end of the destination string: 31 | while (*destination_it) 32 | ++destination_it; 33 | 34 | // Append the source string to the destination string: 35 | while ((*destination_it++ = *source++) != L'\0') { } 36 | 37 | return destination; 38 | } 39 | -------------------------------------------------------------------------------- /internal/SetEnvironmentVariableA.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // SetEnvironmentVariableA.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Definition of __acrt_SetEnvironmentVariableA. 7 | // 8 | 9 | #include 10 | 11 | BOOL __cdecl __acrt_SetEnvironmentVariableA( 12 | LPCSTR const lpName, 13 | LPCSTR const lpValue 14 | ) 15 | { 16 | __crt_internal_win32_buffer wide_name; 17 | __crt_internal_win32_buffer wide_value; 18 | 19 | errno_t const cvt1 = __acrt_mbs_to_wcs_cp( 20 | lpName, 21 | wide_name, 22 | __acrt_get_utf8_acp_compatibility_codepage() 23 | ); 24 | 25 | if (cvt1 != 0) { 26 | return FALSE; 27 | } 28 | 29 | errno_t const cvt2 = __acrt_mbs_to_wcs_cp( 30 | lpValue, 31 | wide_value, 32 | __acrt_get_utf8_acp_compatibility_codepage() 33 | ); 34 | 35 | if (cvt2 != 0) { 36 | return FALSE; 37 | } 38 | 39 | return ::SetEnvironmentVariableW(wide_name.data(), wide_value.data()); 40 | } 41 | -------------------------------------------------------------------------------- /string/wcsncmp.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsncmp.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcsncmp(), which compares two wide character strings, determining 7 | // their ordinal order. The function tests at most 'count' characters. 8 | // 9 | // Note that the comparison is performed with unsigned elements (wchar_t is 10 | // unsigned in this implementation), so the null character (0) is less than 11 | // all other characters. 12 | // 13 | // Returns: 14 | // * a negative value if a < b 15 | // * zero if a == b 16 | // * a positive value if a > b 17 | // 18 | #include 19 | 20 | 21 | 22 | #ifdef _M_ARM 23 | #pragma function(wcsncmp) 24 | #endif 25 | 26 | 27 | 28 | extern "C" int __cdecl wcsncmp( 29 | wchar_t const* a, 30 | wchar_t const* b, 31 | size_t count 32 | ) 33 | { 34 | if (count == 0) 35 | return 0; 36 | 37 | while (--count != 0 && *a && *a == *b) 38 | { 39 | ++a; 40 | ++b; 41 | } 42 | 43 | return static_cast(*a - *b); 44 | } 45 | -------------------------------------------------------------------------------- /heap/calloc.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // calloc.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Implementation of calloc(). Note that _calloc_base is defined in its own 7 | // source file to resolve various issues when linking. 8 | // 9 | #include 10 | #include 11 | 12 | // Allocates a block of memory of size 'count * size' in the heap. The newly 13 | // allocated block is zero-initialized. If allocation fails, nullptr is 14 | // returned. 15 | // 16 | // This function supports patching and therefore must be marked noinline. 17 | // Both _calloc_dbg and _calloc_base must also be marked noinline 18 | // to prevent identical COMDAT folding from substituting calls to calloc 19 | // with either other function or vice versa. 20 | extern "C" _CRT_HYBRIDPATCHABLE __declspec(noinline) _CRTRESTRICT void* __cdecl calloc( 21 | size_t const count, 22 | size_t const size 23 | ) 24 | { 25 | #ifdef _DEBUG 26 | return _calloc_dbg(count, size, _NORMAL_BLOCK, nullptr, 0); 27 | #else 28 | return _calloc_base(count, size); 29 | #endif 30 | } 31 | -------------------------------------------------------------------------------- /lowio/commit.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // commit.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _commit(), which flushes a file buffer to disk. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Flushes the buffer for the given file to disk. 13 | // 14 | // On success, 0 is returned. On failure, -1 is returned and errno is set. 15 | extern "C" int __cdecl _commit(int const fh) 16 | { 17 | _CHECK_FH_RETURN(fh, EBADF, -1); 18 | _VALIDATE_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1); 19 | _VALIDATE_RETURN((_osfile(fh) & FOPEN), EBADF, -1); 20 | 21 | return __acrt_lowio_lock_fh_and_call(fh, [&]() 22 | { 23 | if (_osfile(fh) & FOPEN) 24 | { 25 | if (FlushFileBuffers(reinterpret_cast(_get_osfhandle(fh)))) 26 | return 0; // Success 27 | 28 | _doserrno = GetLastError(); 29 | } 30 | 31 | errno = EBADF; 32 | 33 | _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0)); 34 | return -1; 35 | }); 36 | } 37 | -------------------------------------------------------------------------------- /filesystem/access.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // access.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The _access() and _access_s() functions, which test file accessibility. 7 | // 8 | #include 9 | #include 10 | #include 11 | 12 | // See _waccess_s for information about this function's behavior. 13 | extern "C" errno_t __cdecl _access_s(char const* const path, int const access_mode) 14 | { 15 | if (path == nullptr) { 16 | return _waccess_s(nullptr, access_mode); 17 | } 18 | 19 | __crt_internal_win32_buffer wide_path; 20 | 21 | errno_t const cvt = __acrt_mbs_to_wcs_cp(path, wide_path, __acrt_get_utf8_acp_compatibility_codepage()); 22 | 23 | if (cvt != 0) { 24 | return -1; 25 | } 26 | 27 | return _waccess_s(wide_path.data(), access_mode); 28 | } 29 | 30 | // The same as _access_s, but transforms all errors into a -1 return value. 31 | extern "C" int __cdecl _access(char const* const path, int const access_mode) 32 | { 33 | return _access_s(path, access_mode) == 0 ? 0 : -1; 34 | } 35 | -------------------------------------------------------------------------------- /include/corecrt_share.h: -------------------------------------------------------------------------------- 1 | // 2 | // corecrt_share.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines the file sharing modes for the sopen() family of functions. These 7 | // declarations are split out to support the Windows build. 8 | // 9 | #pragma once 10 | 11 | #include 12 | 13 | #pragma warning(push) 14 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 15 | _UCRT_DISABLE_CLANG_WARNINGS 16 | 17 | #define _SH_DENYRW 0x10 // deny read/write mode 18 | #define _SH_DENYWR 0x20 // deny write mode 19 | #define _SH_DENYRD 0x30 // deny read mode 20 | #define _SH_DENYNO 0x40 // deny none mode 21 | #define _SH_SECURE 0x80 // secure mode 22 | 23 | 24 | 25 | #if (defined _CRT_DECLARE_NONSTDC_NAMES && _CRT_DECLARE_NONSTDC_NAMES) || (!defined _CRT_DECLARE_NONSTDC_NAMES && !__STDC__) 26 | #define SH_DENYRW _SH_DENYRW 27 | #define SH_DENYWR _SH_DENYWR 28 | #define SH_DENYRD _SH_DENYRD 29 | #define SH_DENYNO _SH_DENYNO 30 | #endif 31 | 32 | _UCRT_RESTORE_CLANG_WARNINGS 33 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 34 | -------------------------------------------------------------------------------- /misc/seterrm.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *seterrm.c - Set mode for handling critical errors 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Defines signal() and raise(). 8 | * 9 | *******************************************************************************/ 10 | 11 | #include 12 | #include 13 | 14 | /*** 15 | *void _seterrormode(mode) - set the critical error mode 16 | * 17 | *Purpose: 18 | * 19 | *Entry: 20 | * int mode - error mode: 21 | * 22 | * 0 means system displays a prompt asking user how to 23 | * respond to the error. Choices differ depending on the 24 | * error but may include Abort, Retry, Ignore, and Fail. 25 | * 26 | * 1 means the call system call causing the error will fail 27 | * and return an error indicating the cause. 28 | * 29 | *Exit: 30 | * none 31 | * 32 | *Exceptions: 33 | * 34 | *******************************************************************************/ 35 | 36 | extern "C" void __cdecl _seterrormode(int const mode) 37 | { 38 | SetErrorMode(mode); 39 | } 40 | -------------------------------------------------------------------------------- /string/arm/strlen.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strlen.c - contains strlen() routine 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * strlen returns the length of a null-terminated string, 8 | * not including the null byte itself. 9 | * 10 | *******************************************************************************/ 11 | 12 | #include 13 | 14 | #pragma function(strlen) 15 | 16 | /*** 17 | *strlen - return the length of a null-terminated string 18 | * 19 | *Purpose: 20 | * Finds the length in bytes of the given string, not including 21 | * the final null character. 22 | * 23 | *Entry: 24 | * const char * str - string whose length is to be computed 25 | * 26 | *Exit: 27 | * length of the string "str", exclusive of the final null byte 28 | * 29 | *Exceptions: 30 | * 31 | *******************************************************************************/ 32 | 33 | size_t __cdecl strlen ( 34 | const char * str 35 | ) 36 | { 37 | const char *eos = str; 38 | 39 | while( *eos++ ) ; 40 | 41 | return( eos - str - 1 ); 42 | } 43 | -------------------------------------------------------------------------------- /string/wmemmove_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wmemmove_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wmemmove_s(), which copies wide characters from a source buffer to a 7 | // destination buffer. Overlapping buffers are treated specially, to avoid 8 | // propagation. 9 | // 10 | // Returns 0 if successful; an error code on failure. 11 | // 12 | #include 13 | #include 14 | 15 | 16 | 17 | extern "C" errno_t __cdecl wmemmove_s( 18 | wchar_t* const destination, 19 | size_t const size_in_elements, 20 | wchar_t const* const source, 21 | size_t const count 22 | ) 23 | { 24 | if (count == 0) 25 | return 0; 26 | 27 | #pragma warning(suppress:__WARNING_HIGH_PRIORITY_OVERFLOW_POSTCONDITION) 28 | _VALIDATE_RETURN_ERRCODE(destination != nullptr, EINVAL); 29 | _VALIDATE_RETURN_ERRCODE(source != nullptr, EINVAL); 30 | _VALIDATE_RETURN_ERRCODE(size_in_elements >= count, ERANGE); 31 | 32 | #pragma warning(suppress:__WARNING_BANNED_API_USAGEL2) /* 28726 */ 33 | wmemmove(destination, source, count); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /internal/GetModuleFileNameA.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // GetModuleFileNameA.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Definition of __acrt_GetModuleFileNameA. 7 | // 8 | 9 | #include 10 | 11 | DWORD __cdecl __acrt_GetModuleFileNameA( 12 | HMODULE const hModule, 13 | char * const lpFilename, 14 | DWORD const nSize 15 | ) 16 | { 17 | size_t const wide_buffer_size = MAX_PATH + 1; 18 | wchar_t wide_buffer[wide_buffer_size]; 19 | 20 | DWORD const amount_copied = GetModuleFileNameW( 21 | hModule, 22 | wide_buffer, 23 | wide_buffer_size 24 | ); 25 | 26 | if (amount_copied == 0) { 27 | __acrt_errno_map_os_error(GetLastError()); 28 | return 0; 29 | } 30 | 31 | __crt_no_alloc_win32_buffer filename_buffer(lpFilename, static_cast(nSize)); 32 | 33 | errno_t const cvt = __acrt_wcs_to_mbs_cp( 34 | wide_buffer, 35 | filename_buffer, 36 | __acrt_get_utf8_acp_compatibility_codepage() 37 | ); 38 | 39 | return static_cast(filename_buffer.size()); 40 | } 41 | -------------------------------------------------------------------------------- /stdlib/imaxdiv.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // imaxdiv.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines imaxdiv(), which performs a signed divide using intmax_t operands 7 | // and returns the quotient and remainder. No validation of the arguments is 8 | // done. 9 | // 10 | #include 11 | 12 | 13 | 14 | extern "C" imaxdiv_t __cdecl imaxdiv(intmax_t const numerator, intmax_t const denominator) 15 | { 16 | imaxdiv_t result; 17 | 18 | result.quot = numerator / denominator; 19 | result.rem = numerator - denominator * result.quot; 20 | 21 | // Fix incorrect truncation: 22 | #pragma warning(push) 23 | #pragma warning(disable: 4127) 24 | static bool const fix_required = (-1 / 2) < 0; 25 | if (fix_required && result.quot < 0 && result.rem != 0) 26 | { 27 | result.quot += 1; 28 | result.rem -= denominator; 29 | } 30 | #pragma warning(pop) 31 | 32 | return result; 33 | } 34 | 35 | 36 | 37 | /* 38 | * Copyright (c) 1992-2010 by P.J. Plauger. ALL RIGHTS RESERVED. 39 | * Consult your license regarding permissions and restrictions. 40 | V5.30:0009 */ 41 | -------------------------------------------------------------------------------- /conio/cputs.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // cputs.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _cputs(), which writes a string directly to the console. 7 | // 8 | #include 9 | #include 10 | 11 | // Writes the given string directly to the console. No newline is appended. 12 | // Returns 0 on success; nonzero on failure. 13 | extern "C" int __cdecl _cputs(char const* const string) 14 | { 15 | _VALIDATE_CLEAR_OSSERR_RETURN(string != nullptr, EINVAL, -1); 16 | 17 | __acrt_lock(__acrt_conio_lock); 18 | int result = 0; 19 | __try 20 | { 21 | // Write the string directly to the console. Each character is written 22 | // individually, as performance of this function is not considered 23 | // critical. 24 | for (char const* p = string; *p; ++p) 25 | { 26 | if (_putch_nolock(*p) == EOF) 27 | { 28 | result = -1; 29 | __leave; 30 | } 31 | } 32 | } 33 | __finally 34 | { 35 | __acrt_unlock(__acrt_conio_lock); 36 | } 37 | return result; 38 | } 39 | -------------------------------------------------------------------------------- /string/wcscmp.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcscmp.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcscmp(), which compares two wide character strings, determining 7 | // their ordinal order. 8 | // 9 | // Note that the comparison is performed with unsigned elements (wchar_t is 10 | // unsigned in this implementation), so the null character (0) is less than 11 | // all other characters. 12 | // 13 | // Returns: 14 | // * -1 if a < b 15 | // * 0 if a == b 16 | // * 1 if a > b 17 | // 18 | #include 19 | 20 | 21 | 22 | #if defined _M_X64 || defined _M_IX86 || defined _M_ARM || defined _M_ARM64 23 | #pragma warning(disable: 4163) 24 | #pragma function(wcscmp) 25 | #endif 26 | 27 | 28 | 29 | extern "C" int __cdecl wcscmp(wchar_t const* a, wchar_t const* b) 30 | { 31 | int result = 0; 32 | #pragma warning(suppress:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018 33 | while ((result = (int)(*a - *b)) == 0 && *b) 34 | { 35 | ++a; 36 | ++b; 37 | } 38 | 39 | return ((-result) < 0) - (result < 0); // (if positive) - (if negative) generates branchless code 40 | } 41 | -------------------------------------------------------------------------------- /string/wcscspn.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcscspn.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcscspn(), which searches a string for an initial substring that does 7 | // not contain any of the specified control characters. 8 | // 9 | // This function returns the index of the first character in the string that 10 | // belongs to the set of characters specified by 'control'. This is equivalent 11 | // to the length of the initial substring of 'string' composed entirely of 12 | // characters that are not in 'control'. 13 | // 14 | #include 15 | 16 | 17 | 18 | extern "C" size_t __cdecl wcscspn( 19 | wchar_t const* const string, 20 | wchar_t const* const control 21 | ) 22 | { 23 | wchar_t const* string_it = string; 24 | for (; *string_it; ++string_it) 25 | { 26 | for (wchar_t const* control_it = control; *control_it; ++control_it) 27 | { 28 | if (*string_it == *control_it) 29 | { 30 | return static_cast(string_it - string); 31 | } 32 | } 33 | } 34 | 35 | return static_cast(string_it - string); 36 | } 37 | -------------------------------------------------------------------------------- /stdlib/rotl.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // rotl.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _lrotl(), _rotl(), and _rotl64(), which perform a rotate-left on an 7 | // integer. 8 | // 9 | #include 10 | #include 11 | 12 | 13 | 14 | #pragma function(_lrotl, _rotl, _rotl64) 15 | 16 | #if UINT_MAX != 0xffffffff 17 | #error This source file assumes 32-bit integers 18 | #endif 19 | 20 | #if UINT_MAX != ULONG_MAX 21 | #error This source file assumes sizeof(int) == sizeof(long) 22 | #endif 23 | 24 | 25 | 26 | extern "C" unsigned long __cdecl _lrotl(unsigned long value, int shift) 27 | { 28 | shift &= 0x1f; 29 | value = (value >> (0x20 - shift)) | (value << shift); 30 | return value; 31 | } 32 | 33 | extern "C" unsigned __cdecl _rotl(unsigned value, int shift) 34 | { 35 | shift &= 0x1f; 36 | value = (value >> (0x20 - shift)) | (value << shift); 37 | return value; 38 | } 39 | 40 | extern "C" unsigned __int64 __cdecl _rotl64(unsigned __int64 value, int shift) 41 | { 42 | shift &= 0x3f; 43 | value = (value >> (0x40 - shift)) | (value << shift); 44 | return value; 45 | } 46 | -------------------------------------------------------------------------------- /stdlib/rotr.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // rotr.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _lrotl(), _rotl(), and _rotl64(), which perform a rotate-right on an 7 | // integer. 8 | // 9 | #include 10 | #include 11 | 12 | 13 | 14 | #pragma function(_lrotr, _rotr, _rotr64) 15 | 16 | #if UINT_MAX != 0xffffffff 17 | #error This source file assumes 32-bit integers 18 | #endif 19 | 20 | #if UINT_MAX != ULONG_MAX 21 | #error This source file assumes sizeof(int) == sizeof(long) 22 | #endif 23 | 24 | 25 | 26 | extern "C" unsigned long __cdecl _lrotr(unsigned long value, int shift) 27 | { 28 | shift &= 0x1f; 29 | value = (value << (0x20 - shift)) | (value >> shift); 30 | return value; 31 | } 32 | 33 | extern "C" unsigned __cdecl _rotr(unsigned value, int shift) 34 | { 35 | shift &= 0x1f; 36 | value = (value << (0x20 - shift)) | (value >> shift); 37 | return value; 38 | } 39 | 40 | extern "C" unsigned __int64 __cdecl _rotr64(unsigned __int64 value, int shift) 41 | { 42 | shift &= 0x3f; 43 | value = (value << (0x40 - shift)) | (value >> shift); 44 | return value; 45 | } 46 | -------------------------------------------------------------------------------- /string/wcsncat.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsncat.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcsncat(), which appends n characters of the source string onto the 7 | // end of the destination string. The destination string is always null 8 | // terminated. Unlike wcsncpy, this function does not pad out to 'count' 9 | // characters. 10 | // 11 | #include 12 | 13 | 14 | #pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018 15 | 16 | extern "C" wchar_t* __cdecl wcsncat( 17 | wchar_t* const destination, 18 | wchar_t const* const source, 19 | size_t const count 20 | ) 21 | { 22 | wchar_t* destination_it = destination; 23 | 24 | // Find the end of the destination string: 25 | while (*destination_it) 26 | ++destination_it; 27 | 28 | // Append the source string: 29 | wchar_t const* source_it = source; 30 | for (size_t i = 0; i != count; ++i) 31 | { 32 | if ((*destination_it++ = *source_it++) == 0) 33 | return destination; 34 | } 35 | 36 | *destination_it = 0; 37 | 38 | return destination; 39 | } 40 | -------------------------------------------------------------------------------- /stdio/printf_count_output.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // printf_count_output.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines the functions that control the enable state of %n. 7 | // 8 | #include 9 | 10 | 11 | 12 | static UINT_PTR enable_percent_n = 0; 13 | 14 | 15 | 16 | // Enables or disables the %n format specifier for the printf family of functions. 17 | // Note the use of the __security_cookie: if the static variable was set to a 18 | // known value, an attacker could potentially modify that value and then provide 19 | // a malicious %n specifier. The cookie is or'ed with 1 becuase a cookie with a 20 | // value of zero is possible. 21 | extern "C" int __cdecl _set_printf_count_output(int const value) 22 | { 23 | int const old = (enable_percent_n == (__security_cookie | 1)); 24 | enable_percent_n = (value ? (__security_cookie | 1) : 0); 25 | return old; 26 | } 27 | 28 | 29 | 30 | // Tests whether the %n format specifier for the printf family of functions is 31 | // enabled. 32 | extern "C" int __cdecl _get_printf_count_output() 33 | { 34 | return enable_percent_n == (__security_cookie | 1); 35 | } 36 | -------------------------------------------------------------------------------- /convert/wctrans.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wctrans.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Implementations of the towctrans and wctrans functions. 7 | // 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #pragma warning(disable:4244) 14 | 15 | 16 | 17 | typedef wchar_t wctrans_t; 18 | 19 | static struct wctab 20 | { 21 | char const* s; 22 | wctype_t value; 23 | } 24 | const tab[] = 25 | { 26 | { "tolower", 2 }, 27 | { "toupper", 1 }, 28 | { nullptr, 0 } 29 | }; 30 | 31 | 32 | 33 | extern "C" wint_t __cdecl towctrans(wint_t const c, wctrans_t const value) 34 | { 35 | return value == 1 ? towupper(c) : towlower(c); 36 | } 37 | 38 | extern "C" wctrans_t __cdecl wctrans(char const* const name) 39 | { 40 | for (unsigned n = 0; tab[n].s != 0; ++n) 41 | { 42 | if (strcmp(tab[n].s, name) == 0) 43 | return tab[n].value; 44 | } 45 | 46 | return 0; 47 | } 48 | 49 | /* 50 | * Copyright (c) 1992-2007 by P.J. Plauger. ALL RIGHTS RESERVED. 51 | * Consult your license regarding permissions and restrictions. 52 | V5.03:0009 */ 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /filesystem/rename.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // rename.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The rename() function, which renames a file. 7 | // 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | 14 | // See _wrename() for details about the behavior of this function. (This 15 | // function simply converts the multibyte strings to wide strings and calls 16 | // _wrename().) 17 | extern "C" int __cdecl rename(char const* const old_name, char const* const new_name) 18 | { 19 | unsigned int const code_page = __acrt_get_utf8_acp_compatibility_codepage(); 20 | 21 | __crt_internal_win32_buffer wide_old_name; 22 | 23 | errno_t cvt1 = __acrt_mbs_to_wcs_cp(old_name, wide_old_name, code_page); 24 | if (cvt1 != 0) 25 | { 26 | errno = cvt1; 27 | return -1; 28 | } 29 | 30 | __crt_internal_win32_buffer wide_new_name; 31 | errno_t cvt2 = __acrt_mbs_to_wcs_cp(new_name, wide_new_name, code_page); 32 | if (cvt2 != 0) 33 | { 34 | errno = cvt2; 35 | return -1; 36 | } 37 | 38 | return _wrename(wide_old_name.data(), wide_new_name.data()); 39 | } 40 | -------------------------------------------------------------------------------- /string/wcsncpy.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsncpy.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcsncpy(), which copies a string from one buffer to another. This 7 | // function copies at most 'count' characters. If fewer than 'count' characters 8 | // are copied, the rest of the buffer is padded with null characters. 9 | // 10 | #include 11 | 12 | #pragma warning(disable:__WARNING_POSTCONDITION_NULLTERMINATION_VIOLATION) // 26036 13 | 14 | 15 | #ifdef _M_ARM 16 | #pragma function(wcsncpy) 17 | #endif 18 | 19 | 20 | 21 | extern "C" wchar_t * __cdecl wcsncpy( 22 | wchar_t* const destination, 23 | wchar_t const* const source, 24 | size_t const count 25 | ) 26 | { 27 | size_t remaining = count; 28 | 29 | wchar_t* destination_it = destination; 30 | wchar_t const* source_it = source; 31 | while (remaining != 0 && (*destination_it++ = *source_it++) != 0) 32 | { 33 | --remaining; 34 | } 35 | 36 | if (remaining != 0) 37 | { 38 | while (--remaining != 0) 39 | { 40 | *destination_it++ = L'\0'; 41 | } 42 | } 43 | 44 | return destination; 45 | } 46 | -------------------------------------------------------------------------------- /conio/putwch.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // putwch.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _putwch(), which writes a wide character to the console. 7 | // 8 | #include 9 | #include 10 | #include 11 | 12 | // Writes a wide character to the console. Returns the character on success, 13 | // WEOF on failure. 14 | extern "C" wint_t __cdecl _putwch(wchar_t const c) 15 | { 16 | return __acrt_lock_and_call(__acrt_conio_lock, [&] 17 | { 18 | return _putwch_nolock(c); 19 | }); 20 | } 21 | 22 | extern "C" wint_t __cdecl _putwch_nolock(wchar_t const c) 23 | { 24 | if (__dcrt_lowio_ensure_console_output_initialized() == FALSE) 25 | return WEOF; 26 | 27 | // Write character to console: 28 | DWORD charsWritten; 29 | if (__dcrt_write_console(&c, 1, &charsWritten) == FALSE) 30 | return WEOF; 31 | 32 | return c; 33 | } 34 | 35 | extern "C" wint_t __cdecl _putwch_nolock_internal(wchar_t const c, __crt_cached_ptd_host&) 36 | { 37 | // Currently _putwch_nolock does not require any PTD access. Do not need to propagate __crt_cached_ptd_host&. 38 | return _putwch_nolock(c); 39 | } 40 | -------------------------------------------------------------------------------- /string/wcsspn.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsspn.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wcsspn(), which finds the length of the initial substring of 'string' 7 | // that consists entirely of characters in 'control'. This function returns the 8 | // index of the first character in the string that does not belong to the set of 9 | // characters in the control string. If there is no such character, the length 10 | // of the string is returned. 11 | // 12 | #include 13 | 14 | 15 | 16 | extern "C" size_t __cdecl wcsspn( 17 | wchar_t const* const string, 18 | wchar_t const* const control 19 | ) 20 | { 21 | wchar_t const* string_it = string; 22 | for (; *string_it; ++string_it) 23 | { 24 | for (wchar_t const* control_it = control; *string_it != *control_it; ++control_it) 25 | { 26 | // Reached the end of the control string without finding a match: 27 | if (*control_it == 0) 28 | { 29 | return static_cast(string_it - string); 30 | } 31 | } 32 | } 33 | 34 | // The whole string consisted of characters from the control: 35 | return static_cast(string_it - string); 36 | } 37 | -------------------------------------------------------------------------------- /misc/is_wctype.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *iswctype.c - support isw* wctype functions/macros for wide characters 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Defines iswctype - support isw* wctype functions/macros for 8 | * wide characters (esp. > 255). 9 | * 10 | *******************************************************************************/ 11 | #include 12 | #include 13 | 14 | /*** 15 | *is_wctype - support obsolete name 16 | * 17 | *Purpose: 18 | * Name changed from is_wctype to iswctype. is_wctype must be supported. 19 | * 20 | *Entry: 21 | * wchar_t c - the wide character whose type is to be tested 22 | * wchar_t mask - the mask used by the isw* functions/macros 23 | * corresponding to each character class property 24 | * 25 | *Exit: 26 | * Returns non-zero if c is of the character class. 27 | * Returns 0 if c is not of the character class. 28 | * 29 | *Exceptions: 30 | * Returns 0 on any error. 31 | * 32 | *******************************************************************************/ 33 | extern "C" int __cdecl is_wctype ( 34 | wint_t c, 35 | wctype_t mask 36 | ) 37 | { 38 | return iswctype(c, mask); 39 | } -------------------------------------------------------------------------------- /stdio/putw.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // putw.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _putw(), which writes a binary integer to a stream. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Writes the binary int value to the stream, byte-by-byte. On success, returns 13 | // the value written; on failure, returns EOF. Note, however, that the value may 14 | // be EOF--that is, after all, a valid integer--so be sure to test ferror() and 15 | // feof() to check for error conditions. 16 | extern "C" int __cdecl _putw(int const value, FILE* const stream) 17 | { 18 | _VALIDATE_RETURN(stream != nullptr, EINVAL, EOF); 19 | 20 | int return_value = EOF; 21 | 22 | _lock_file(stream); 23 | __try 24 | { 25 | char const* const first = reinterpret_cast(&value); 26 | char const* const last = first + sizeof(value); 27 | for (char const* it = first; it != last; ++it) 28 | { 29 | _fputc_nolock(*it, stream); 30 | } 31 | 32 | if (ferror(stream)) 33 | __leave; 34 | 35 | return_value = value; 36 | } 37 | __finally 38 | { 39 | _unlock_file(stream); 40 | } 41 | 42 | return return_value; 43 | } 44 | -------------------------------------------------------------------------------- /stdio/closeall.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // closeall.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _fcloseall(), which closes all opened files. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Closes all streams currently open except for stdin, stdout, and stderr. All 13 | // tmpfile()-created streams are closed as well. Returns EOF on failure; returns 14 | // the number of closed streams on success. 15 | extern "C" int __cdecl _fcloseall() 16 | { 17 | int count = 0; 18 | 19 | __acrt_lock(__acrt_stdio_index_lock); 20 | __try 21 | { 22 | for (int i = _IOB_ENTRIES; i != _nstream; ++i) 23 | { 24 | if (__piob[i] == nullptr) 25 | continue; 26 | 27 | // If the stream is in use, close it: 28 | if (__crt_stdio_stream(__piob[i]).is_in_use() && fclose(&__piob[i]->_public_file) != EOF) 29 | { 30 | ++count; 31 | } 32 | 33 | DeleteCriticalSection(&__piob[i]->_lock); 34 | _free_crt(__piob[i]); 35 | __piob[i] = nullptr; 36 | } 37 | } 38 | __finally 39 | { 40 | __acrt_unlock(__acrt_stdio_index_lock); 41 | } 42 | 43 | return count; 44 | } 45 | -------------------------------------------------------------------------------- /heap/free.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // free.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Implementation of free(). 7 | // 8 | #include 9 | #include 10 | 11 | // Frees a block in the heap. The 'block' pointer must either be a null pointer 12 | // or must point to a valid block in the heap. 13 | // 14 | // This function supports patching and therefore must be marked noinline. 15 | // Both _free_dbg and _free_base must also be marked noinline 16 | // to prevent identical COMDAT folding from substituting calls to free 17 | // with either other function or vice versa. 18 | extern "C" _CRT_HYBRIDPATCHABLE __declspec(noinline) void __cdecl free(void* const block) 19 | { 20 | // Some libraries that hook memory allocation routines (such as libtcmalloc) 21 | // look for an appropriate place inside free to place a patch to its version 22 | // of free. Without these extra instructions padding the call to _free_base, 23 | // some libraries may choose to insert this patch in _free_base instead. 24 | volatile int extra_instructions_for_patching_libraries = 0; 25 | (void) extra_instructions_for_patching_libraries; 26 | 27 | #ifdef _DEBUG 28 | _free_dbg(block, _NORMAL_BLOCK); 29 | #else 30 | _free_base(block); 31 | #endif 32 | } 33 | -------------------------------------------------------------------------------- /string/strtok.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // strtok.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines strtok(), which tokenizes a string via repeated calls. 7 | // 8 | // strtok() considers the string to consist of a sequence of zero or more text 9 | // tokens separated by spans of one or more control characters. The first call, 10 | // with a string specified, returns a pointer to the first character of the 11 | // first token, and will write a null character into the string immediately 12 | // following the returned token. Subsequent calls with a null string argument 13 | // will work through the string until no tokens remain. The control string 14 | // may be different from call to call. When no tokens remain in the string, a 15 | // null pointer is returned. 16 | // 17 | #include 18 | #include 19 | 20 | 21 | 22 | extern "C" char* __cdecl __acrt_strtok_s_novalidation( 23 | _Inout_opt_z_ char* string, 24 | _In_z_ char const* control, 25 | _Inout_ _Deref_prepost_opt_z_ char** context 26 | ); 27 | 28 | 29 | 30 | extern "C" char* __cdecl strtok(char* const string, char const* const control) 31 | { 32 | return __acrt_strtok_s_novalidation(string, control, &__acrt_getptd()->_strtok_token); 33 | } 34 | -------------------------------------------------------------------------------- /include/assert.h: -------------------------------------------------------------------------------- 1 | // 2 | // assert.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines the assert macro and related functionality. 7 | // 8 | #if defined _VCRT_BUILD && !defined _ASSERT_OK 9 | #error assert.h not for CRT internal use 10 | #endif 11 | 12 | #include 13 | 14 | #pragma warning(push) 15 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 16 | _UCRT_DISABLE_CLANG_WARNINGS 17 | _CRT_BEGIN_C_HEADER 18 | 19 | #if _CRT_HAS_C11 20 | 21 | #define static_assert _Static_assert 22 | 23 | #endif // _CRT_HAS_C11 24 | 25 | #undef assert 26 | 27 | #ifdef NDEBUG 28 | 29 | #define assert(expression) ((void)0) 30 | 31 | #else 32 | 33 | _ACRTIMP void __cdecl _wassert( 34 | _In_z_ wchar_t const* _Message, 35 | _In_z_ wchar_t const* _File, 36 | _In_ unsigned _Line 37 | ); 38 | 39 | #define assert(expression) (void)( \ 40 | (!!(expression)) || \ 41 | (_wassert(_CRT_WIDE(#expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) \ 42 | ) 43 | 44 | #endif 45 | 46 | 47 | 48 | _CRT_END_C_HEADER 49 | _UCRT_RESTORE_CLANG_WARNINGS 50 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 51 | -------------------------------------------------------------------------------- /string/strset.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strset.c - sets all characters of string to given character 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines _strset() - sets all of the characters in a string (except 8 | * the '\0') equal to a given character. 9 | * 10 | *******************************************************************************/ 11 | 12 | #include 13 | 14 | #if defined _M_X64 || defined _M_ARM || defined _M_ARM64 || _M_HYBRID 15 | #pragma function(_strset) 16 | #endif 17 | 18 | /*** 19 | *char *_strset(string, val) - sets all of string to val 20 | * 21 | *Purpose: 22 | * Sets all of characters in string (except the terminating '/0' 23 | * character) equal to val. 24 | * 25 | * 26 | *Entry: 27 | * char *string - string to modify 28 | * char val - value to fill string with 29 | * 30 | *Exit: 31 | * returns string -- now filled with val's 32 | * 33 | *Uses: 34 | * 35 | *Exceptions: 36 | * 37 | *******************************************************************************/ 38 | 39 | char * __cdecl _strset ( 40 | char * string, 41 | int val 42 | ) 43 | { 44 | char *start = string; 45 | 46 | while (*string) 47 | *string++ = (char)val; 48 | 49 | return(start); 50 | } 51 | -------------------------------------------------------------------------------- /heap/debug_heap_hook.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // debug_heap_hook.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Definition of the default debug heap allocation hook. This is in its own 7 | // object so that it may be replaced by a client hook at link time when the 8 | // static CRT is used. 9 | // 10 | #ifndef _DEBUG 11 | #error This file is supported only in debug builds 12 | #endif 13 | 14 | #include 15 | 16 | 17 | 18 | // A default heap allocation hook that permits all allocations 19 | extern "C" int __cdecl _CrtDefaultAllocHook( 20 | int const allocation_type, 21 | void* const data, 22 | size_t const size, 23 | int const block_use, 24 | long const request, 25 | unsigned char const* const file_name, 26 | int const line_number 27 | ) 28 | { 29 | UNREFERENCED_PARAMETER(allocation_type); 30 | UNREFERENCED_PARAMETER(data); 31 | UNREFERENCED_PARAMETER(size); 32 | UNREFERENCED_PARAMETER(block_use); 33 | UNREFERENCED_PARAMETER(request); 34 | UNREFERENCED_PARAMETER(file_name); 35 | UNREFERENCED_PARAMETER(line_number); 36 | 37 | return 1; // Allow all heap operations 38 | } 39 | 40 | extern "C" _CRT_ALLOC_HOOK _pfnAllocHook = _CrtDefaultAllocHook; 41 | -------------------------------------------------------------------------------- /stdio/getw.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // getw.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _getw(), which reads a binary integer to a stream. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Reads a binary int value from the stream, byte-by-byte. On success, returns 13 | // the value written; on failure (error or eof), returns EOF. Note, however, 14 | // that the value may be EOF--that is, after all, a valid integer--so be sure to 15 | // test ferror() and feof() to check for error conditions. 16 | extern "C" int __cdecl _getw(FILE* const stream) 17 | { 18 | _VALIDATE_RETURN(stream != nullptr, EINVAL, EOF); 19 | 20 | int return_value = EOF; 21 | 22 | _lock_file(stream); 23 | __try 24 | { 25 | int value = 0; 26 | char* const first = reinterpret_cast(&value); 27 | char* const last = first + sizeof(value); 28 | 29 | for (char* it = first; it != last; ++it) 30 | { 31 | *it = static_cast(_getc_nolock(stream)); 32 | } 33 | 34 | if (feof(stream)) 35 | __leave; 36 | 37 | if (ferror(stream)) 38 | __leave; 39 | 40 | return_value = value; 41 | } 42 | __finally 43 | { 44 | _unlock_file(stream); 45 | } 46 | 47 | return return_value; 48 | } 49 | -------------------------------------------------------------------------------- /string/strrev.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strrev.c - reverse a string in place 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines _strrev() - reverse a string in place (not including 8 | * '\0' character) 9 | * 10 | *******************************************************************************/ 11 | 12 | #include 13 | 14 | /*** 15 | *char *_strrev(string) - reverse a string in place 16 | * 17 | *Purpose: 18 | * Reverses the order of characters in the string. The terminating 19 | * null character remains in place. 20 | * 21 | *Entry: 22 | * char *string - string to reverse 23 | * 24 | *Exit: 25 | * returns string - now with reversed characters 26 | * 27 | *Exceptions: 28 | * 29 | *******************************************************************************/ 30 | 31 | char * __cdecl _strrev ( 32 | char * string 33 | ) 34 | { 35 | char *start = string; 36 | char *left = string; 37 | char ch; 38 | 39 | while (*string++) /* find end of string */ 40 | ; 41 | string -= 2; 42 | 43 | while (left < string) 44 | { 45 | ch = *left; 46 | *left++ = *string; 47 | *string-- = ch; 48 | } 49 | 50 | return(start); 51 | } 52 | -------------------------------------------------------------------------------- /string/strnset.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strnset.c - set first n characters to single character 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines _strnset() - sets at most the first n characters of a string 8 | * to a given character. 9 | * 10 | *******************************************************************************/ 11 | 12 | #include 13 | 14 | /*** 15 | *char *_strnset(string, val, count) - set at most count characters to val 16 | * 17 | *Purpose: 18 | * Sets the first count characters of string the character value. 19 | * If the length of string is less than count, the length of 20 | * string is used in place of n. 21 | * 22 | *Entry: 23 | * char *string - string to set characters in 24 | * char val - character to fill with 25 | * unsigned count - count of characters to fill 26 | * 27 | *Exit: 28 | * returns string, now filled with count copies of val. 29 | * 30 | *Exceptions: 31 | * 32 | *******************************************************************************/ 33 | 34 | char * __cdecl _strnset ( 35 | char * string, 36 | int val, 37 | size_t count 38 | ) 39 | { 40 | char *start = string; 41 | 42 | while (count-- && *string) 43 | *string++ = (char)val; 44 | 45 | return(start); 46 | } 47 | -------------------------------------------------------------------------------- /filesystem/wchmod.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wchmod.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The _wchmod() function, which changes file attributes. 7 | // 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | 14 | // Changes the mode of a file. The only supported mode bit is _S_IWRITE, which 15 | // controls the user write (read-only) attribute of the file. Returns zero if 16 | // successful; returns -1 and sets errno and _doserrno on failure. 17 | extern "C" int __cdecl _wchmod(wchar_t const* const path, int const mode) 18 | { 19 | _VALIDATE_CLEAR_OSSERR_RETURN(path != nullptr, EINVAL, -1); 20 | 21 | WIN32_FILE_ATTRIBUTE_DATA attributes; 22 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &attributes)) 23 | { 24 | __acrt_errno_map_os_error(GetLastError()); 25 | return -1; 26 | } 27 | 28 | // Set or clear the read-only flag: 29 | if (mode & _S_IWRITE) 30 | { 31 | attributes.dwFileAttributes &= ~FILE_ATTRIBUTE_READONLY; 32 | } 33 | else 34 | { 35 | attributes.dwFileAttributes |= FILE_ATTRIBUTE_READONLY; 36 | } 37 | 38 | if (!SetFileAttributesW(path, attributes.dwFileAttributes)) 39 | { 40 | __acrt_errno_map_os_error(GetLastError()); 41 | return -1; 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /stdio/_getbuf.cpp: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // _getbuf.cpp 4 | // 5 | // Copyright (c) Microsoft Corporation. All rights reserved. 6 | // 7 | // Defines __acrt_stdio_allocate_buffer_nolock(), which allocates a buffer for a stream. 8 | // 9 | #include 10 | 11 | 12 | 13 | // Allocates a buffer for the provided stream. This function assumes that the 14 | // caller has already checked to ensure that the stream does not already have a 15 | // buffer. 16 | extern "C" void __cdecl __acrt_stdio_allocate_buffer_nolock(FILE* const public_stream) 17 | { 18 | _ASSERTE(public_stream != nullptr); 19 | 20 | __crt_stdio_stream const stream(public_stream); 21 | 22 | #ifndef CRTDLL 23 | ++_cflush; // Force the library pre-termination procedure to run 24 | #endif 25 | 26 | // Try to get a big buffer: 27 | stream->_base = _calloc_crt_t(char, _INTERNAL_BUFSIZ).detach(); 28 | if (stream->_base != nullptr) 29 | { 30 | stream.set_flags(_IOBUFFER_CRT); 31 | stream->_bufsiz = _INTERNAL_BUFSIZ; 32 | } 33 | // If we couldn't get a big buffer, use single character buffering: 34 | else 35 | { 36 | stream.set_flags(_IOBUFFER_NONE); 37 | stream->_base = reinterpret_cast(&stream->_charbuf); 38 | stream->_bufsiz = 2; 39 | } 40 | 41 | stream->_ptr = stream->_base; 42 | stream->_cnt = 0; 43 | } 44 | -------------------------------------------------------------------------------- /mbstring/mbtokata.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbtokata.c - Converts character to katakana. 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Converts a character from hiragana to katakana. 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | 18 | /*** 19 | *unsigned short _mbctokata(c) - Converts character to katakana. 20 | * 21 | *Purpose: 22 | * If the character c is hiragana, convert to katakana. 23 | * 24 | *Entry: 25 | * unsigned int c - Character to convert. 26 | * 27 | *Exit: 28 | * Returns converted character. 29 | * 30 | *Exceptions: 31 | * 32 | *******************************************************************************/ 33 | 34 | extern "C" unsigned int __cdecl _mbctokata_l( 35 | unsigned int c, 36 | _locale_t plocinfo 37 | ) 38 | { 39 | if (_ismbchira_l(c, plocinfo)) { 40 | c += 0xa1; 41 | if (c >= 0x837f) 42 | c++; 43 | } 44 | return(c); 45 | } 46 | 47 | extern "C" unsigned int (__cdecl _mbctokata)( 48 | unsigned int c 49 | ) 50 | { 51 | return _mbctokata_l(c, nullptr); 52 | } 53 | -------------------------------------------------------------------------------- /mbstring/mbccpy.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbccpy.c - Copy one character to another (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Copy one MBCS character to another (1 or 2 bytes) 8 | * 9 | *******************************************************************************/ 10 | 11 | #include 12 | #include 13 | 14 | /*** 15 | * _mbccpy - Copy one character to another (MBCS) 16 | * 17 | *Purpose: 18 | * Copies exactly one MBCS character from src to dst. Copies _mbclen(src) 19 | * bytes from src to dst. 20 | * 21 | *Entry: 22 | * unsigned char *dst = destination for copy 23 | * unsigned char *src = source for copy 24 | * 25 | *Exit: 26 | * 27 | *Exceptions: 28 | * Input parameters are validated. Refer to the validation section of the function. 29 | * 30 | *******************************************************************************/ 31 | 32 | extern "C" void __cdecl _mbccpy_l( 33 | unsigned char *dst, 34 | const unsigned char *src, 35 | _locale_t plocinfo 36 | ) 37 | { 38 | /* _mbccpy_s_l sets errno */ 39 | _mbccpy_s_l(dst, 2, nullptr, src, plocinfo); 40 | } 41 | 42 | extern "C" void (__cdecl _mbccpy)( 43 | unsigned char *dst, 44 | const unsigned char *src 45 | ) 46 | { 47 | _mbccpy_s_l(dst, 2, nullptr, src, nullptr); 48 | } 49 | -------------------------------------------------------------------------------- /include/corecrt_terminate.h: -------------------------------------------------------------------------------- 1 | // 2 | // corecrt_terminate.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The terminate handler 7 | // 8 | #pragma once 9 | 10 | #include 11 | 12 | #pragma warning(push) 13 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 14 | _UCRT_DISABLE_CLANG_WARNINGS 15 | 16 | #ifndef RC_INVOKED 17 | 18 | _CRT_BEGIN_C_HEADER 19 | 20 | // terminate_handler is the standard name; terminate_function is defined for 21 | // source compatibility. 22 | typedef void (__CRTDECL* terminate_handler )(void); 23 | typedef void (__CRTDECL* terminate_function)(void); 24 | 25 | #ifdef _M_CEE 26 | typedef void (__clrcall* __terminate_function_m)(); 27 | typedef void (__clrcall* __terminate_handler_m )(); 28 | #endif 29 | 30 | #ifdef __cplusplus 31 | 32 | _ACRTIMP __declspec(noreturn) void __cdecl abort(); 33 | _ACRTIMP __declspec(noreturn) void __cdecl terminate() throw(); 34 | 35 | #ifndef _M_CEE_PURE 36 | 37 | _ACRTIMP terminate_handler __cdecl set_terminate( 38 | _In_opt_ terminate_handler _NewTerminateHandler 39 | ) throw(); 40 | 41 | _ACRTIMP terminate_handler __cdecl _get_terminate(); 42 | 43 | #endif 44 | 45 | #endif // __cplusplus 46 | 47 | _CRT_END_C_HEADER 48 | 49 | #endif // RC_INVOKED 50 | _UCRT_RESTORE_CLANG_WARNINGS 51 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 52 | -------------------------------------------------------------------------------- /string/wmemcpy_s.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wmemcpy_s.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines wmemcpy_s(), which copies wide characters from a source buffer to a 7 | // destination buffer. Overlapping buffers are not treated specially, so 8 | // propagation may occur. If propagation must be avoided, wmemmove_s() must be 9 | // used. 10 | // 11 | // Returns 0 if successful; an error code on failure. 12 | // 13 | #include 14 | #include 15 | 16 | 17 | 18 | extern "C" errno_t __cdecl wmemcpy_s( 19 | wchar_t* const destination, 20 | size_t const size_in_elements, 21 | wchar_t const* const source, 22 | size_t const count 23 | ) 24 | { 25 | if (count == 0) 26 | return 0; 27 | 28 | _VALIDATE_RETURN_ERRCODE(destination != nullptr, EINVAL); 29 | 30 | if (source == nullptr || size_in_elements < count) 31 | { 32 | // Zero the destination buffer: 33 | wmemset(destination, 0, size_in_elements); 34 | 35 | _VALIDATE_RETURN_ERRCODE(source != nullptr, EINVAL); 36 | _VALIDATE_RETURN_ERRCODE(size_in_elements >= count, ERANGE); 37 | 38 | // Useless, but prefast is confused: 39 | return EINVAL; 40 | } 41 | 42 | #pragma warning(suppress:__WARNING_BANNED_API_USAGEL2) /* 28726 */ 43 | wmemcpy(destination, source, count); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /lowio/creat.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // creat.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _creat() (and _wcreat(), via inclusion), which creates a new file. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Creates a new file. 13 | // 14 | // If the file does not exist, this function creates a new file with the given 15 | // permission setting and opens it for writing. If the file already exists and 16 | // its permission allows writing, this function truncates it to zero length and 17 | // opens it for writing. 18 | // 19 | // The only XENIX mode bit supported is user write (S_IWRITE). 20 | // 21 | // On success, the handle for the newly created file is returned. On failure, 22 | // -1 is returned and errno is set. 23 | template 24 | static int __cdecl common_creat(Character const* const path, int const pmode) throw() 25 | { 26 | typedef __crt_char_traits traits; 27 | // creat is just the same as open... 28 | int fh = -1; 29 | errno_t e = traits::tsopen_s(&fh, path, _O_CREAT + _O_TRUNC + _O_RDWR, _SH_DENYNO, pmode); 30 | return e == 0 ? fh : -1; 31 | } 32 | 33 | extern "C" int __cdecl _creat(char const* const path, int const pmode) 34 | { 35 | return common_creat(path, pmode); 36 | } 37 | 38 | extern "C" int __cdecl _wcreat(wchar_t const* const path, int const pmode) 39 | { 40 | return common_creat(path, pmode); 41 | } 42 | -------------------------------------------------------------------------------- /mbstring/ismblgl.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *ismblgl.c - Tests to see if a given character is a legal MBCS char. 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Tests to see if a given character is a legal MBCS character. 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | 18 | /*** 19 | *int _ismbclegal(c) - tests for a valid MBCS character. 20 | * 21 | *Purpose: 22 | * Tests to see if a given character is a legal MBCS character. 23 | * 24 | *Entry: 25 | * unsigned int c - character to test 26 | * 27 | *Exit: 28 | * returns non-zero if Microsoft Kanji code, else 0 29 | * 30 | *Exceptions: 31 | * 32 | ******************************************************************************/ 33 | 34 | extern "C" int __cdecl _ismbclegal_l( 35 | unsigned int c, 36 | _locale_t plocinfo 37 | ) 38 | { 39 | _LocaleUpdate _loc_update(plocinfo); 40 | 41 | return( (_ismbblead_l(c >> 8, _loc_update.GetLocaleT())) && 42 | (_ismbbtrail_l(c & 0x0ff, _loc_update.GetLocaleT())) ); 43 | } 44 | extern "C" int (__cdecl _ismbclegal)( 45 | unsigned int c 46 | ) 47 | { 48 | return _ismbclegal_l(c, nullptr); 49 | } 50 | -------------------------------------------------------------------------------- /stdio/fputs.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // fputs.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines fputs(), which writes a string to a stream. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Writes the given string to the given stream. Does not write the string's null 14 | // terminator, and does not append a '\n' to the file. Returns a nonnegative 15 | // value on success; EOF on failure. 16 | static int __cdecl _fputs_internal(char const* const string, FILE* const stream, __crt_cached_ptd_host& ptd) 17 | { 18 | _UCRT_VALIDATE_RETURN(ptd, string != nullptr, EINVAL, EOF); 19 | _UCRT_VALIDATE_RETURN(ptd, stream != nullptr, EINVAL, EOF); 20 | _UCRT_VALIDATE_STREAM_ANSI_RETURN(ptd, stream, EINVAL, EOF); 21 | 22 | size_t const length = strlen(string); 23 | 24 | return __acrt_lock_stream_and_call(stream, [&]() -> int 25 | { 26 | __acrt_stdio_temporary_buffering_guard const buffering(stream, ptd); 27 | 28 | size_t const bytes_written = _fwrite_nolock_internal(string, 1, length, stream, ptd); 29 | if (bytes_written == length) 30 | { 31 | return 0; 32 | } 33 | 34 | return EOF; 35 | }); 36 | } 37 | 38 | extern "C" int __cdecl fputs(char const* const string, FILE* const stream) 39 | { 40 | __crt_cached_ptd_host ptd; 41 | return _fputs_internal(string, stream, ptd); 42 | } -------------------------------------------------------------------------------- /mbstring/mbtohira.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbtohira.c - Convert character from katakana to hiragana (Japanese). 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines _jtohira() - convert character to hiragana. 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | 18 | /*** 19 | *unsigned int _mbctohira(c) - Converts character to hiragana. 20 | * 21 | *Purpose: 22 | * Converts the character c from katakana to hiragana, if possible. 23 | * 24 | *Entry: 25 | * unsigned int c - Character to convert. 26 | * 27 | *Exit: 28 | * Returns the converted character. 29 | * 30 | *Exceptions: 31 | * 32 | *******************************************************************************/ 33 | 34 | extern "C" unsigned int __cdecl _mbctohira_l( 35 | unsigned int c, 36 | _locale_t plocinfo 37 | ) 38 | { 39 | if (_ismbckata_l(c, plocinfo) && c <= 0x8393) { 40 | if (c < 0x837f) 41 | c -= 0xa1; 42 | else 43 | c -= 0xa2; 44 | } 45 | return(c); 46 | } 47 | 48 | extern "C" unsigned int __cdecl _mbctohira(unsigned int c) 49 | { 50 | return _mbctohira_l(c, nullptr); 51 | } 52 | -------------------------------------------------------------------------------- /include/sys/types.h: -------------------------------------------------------------------------------- 1 | // 2 | // sys/types.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Types used for returning file status and time information. 7 | // 8 | #pragma once 9 | 10 | #include 11 | 12 | #pragma warning(push) 13 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 14 | _UCRT_DISABLE_CLANG_WARNINGS 15 | 16 | #ifndef _INO_T_DEFINED 17 | #define _INO_T_DEFINED 18 | 19 | typedef unsigned short _ino_t; // inode number (unused on Windows) 20 | 21 | #if (defined _CRT_DECLARE_NONSTDC_NAMES && _CRT_DECLARE_NONSTDC_NAMES) || (!defined _CRT_DECLARE_NONSTDC_NAMES && !__STDC__) 22 | typedef _ino_t ino_t; 23 | #endif 24 | #endif 25 | 26 | 27 | 28 | #ifndef _DEV_T_DEFINED 29 | #define _DEV_T_DEFINED 30 | 31 | typedef unsigned int _dev_t; // device code 32 | 33 | #if (defined _CRT_DECLARE_NONSTDC_NAMES && _CRT_DECLARE_NONSTDC_NAMES) || (!defined _CRT_DECLARE_NONSTDC_NAMES && !__STDC__) 34 | typedef _dev_t dev_t; 35 | #endif 36 | #endif 37 | 38 | 39 | 40 | #ifndef _OFF_T_DEFINED 41 | #define _OFF_T_DEFINED 42 | 43 | typedef long _off_t; // file offset value 44 | 45 | #if (defined _CRT_DECLARE_NONSTDC_NAMES && _CRT_DECLARE_NONSTDC_NAMES) || (!defined _CRT_DECLARE_NONSTDC_NAMES && !__STDC__) 46 | typedef _off_t off_t; 47 | #endif 48 | #endif 49 | 50 | _UCRT_RESTORE_CLANG_WARNINGS 51 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 52 | -------------------------------------------------------------------------------- /mbstring/mbclen.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbclen.c - Find length of MBCS character 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Find length of MBCS character 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | #pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018 18 | 19 | /*** 20 | * _mbclen - Find length of MBCS character 21 | * 22 | *Purpose: 23 | * Find the length of the MBCS character (in bytes). 24 | * 25 | *Entry: 26 | * unsigned char *c = MBCS character 27 | * 28 | *Exit: 29 | * Returns the number of bytes in the MBCS character 30 | * 31 | *Exceptions: 32 | * 33 | *******************************************************************************/ 34 | 35 | extern "C" size_t __cdecl _mbclen_l(unsigned char const* c, _locale_t locale) 36 | { 37 | /* Don't return two if we have leadbyte, EOS. 38 | Don't assert here; too low level 39 | */ 40 | return ((_ismbblead_l)(*c, locale) && c[1] != '\0') ? 2 : 1; 41 | } 42 | 43 | extern "C" size_t __cdecl _mbclen(unsigned char const* c) 44 | { 45 | /* Don't return two if we have leadbyte, EOS. 46 | Don't assert here; too low level 47 | */ 48 | return (_ismbblead(*c) && c[1]!='\0') ? 2 : 1; 49 | } 50 | -------------------------------------------------------------------------------- /stdio/putws.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // putws.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _putws(), which writes a wide character string to stdout. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Writes a wide character string to stdout. Does not write the string's null 14 | // terminator, but _does_ append a newline to the output. Returns 0 on success; 15 | // returns WEOF on failure. 16 | static int __cdecl _putws_internal(wchar_t const* const string, __crt_cached_ptd_host& ptd) 17 | { 18 | _UCRT_VALIDATE_RETURN(ptd, string != nullptr, EINVAL, WEOF); 19 | 20 | FILE* const stream = stdout; 21 | 22 | return __acrt_lock_stream_and_call(stream, [&]() -> int 23 | { 24 | __acrt_stdio_temporary_buffering_guard const buffering(stream, ptd); 25 | 26 | // Write the string, character-by-character: 27 | for (wchar_t const* it = string; *it; ++it) 28 | { 29 | if (_fputwc_nolock_internal(*it, stream, ptd) == WEOF) 30 | { 31 | return WEOF; 32 | } 33 | } 34 | 35 | if (_fputwc_nolock_internal(L'\n', stream, ptd) == WEOF) 36 | { 37 | return WEOF; 38 | } 39 | 40 | return 0; 41 | }); 42 | } 43 | 44 | extern "C" int __cdecl _putws(wchar_t const* const string) 45 | { 46 | __crt_cached_ptd_host ptd; 47 | return _putws_internal(string, ptd); 48 | } 49 | -------------------------------------------------------------------------------- /startup/initterm.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // initterm.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // _initterm and _initterm_e functions used during dynamic initialization. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Calls each function in [first, last). [first, last) must be a valid range of 13 | // function pointers. Each function is called, in order. 14 | extern "C" void __cdecl _initterm(_PVFV* const first, _PVFV* const last) 15 | { 16 | for (_PVFV* it = first; it != last; ++it) 17 | { 18 | if (*it == nullptr) 19 | continue; 20 | 21 | (**it)(); 22 | } 23 | } 24 | 25 | // Calls each function in [first, last). [first, last) must be a valid range of 26 | // function pointers. Each function must return zero on success, nonzero on 27 | // failure. If any function returns nonzero, iteration stops immediately and 28 | // the nonzero value is returned. Otherwise all functions are called and zero 29 | // is returned. 30 | // 31 | // If a nonzero value is returned, it is expected to be one of the runtime error 32 | // values (_RT_{NAME}, defined in the internal header files). 33 | extern "C" int __cdecl _initterm_e(_PIFV* const first, _PIFV* const last) 34 | { 35 | for (_PIFV* it = first; it != last; ++it) 36 | { 37 | if (*it == nullptr) 38 | continue; 39 | 40 | int const result = (**it)(); 41 | if (result != 0) 42 | return result; 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /misc/slbeep.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *slbeep.c - Sleep and beep 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines _sleep() and _beep() 8 | * 9 | *******************************************************************************/ 10 | 11 | #include 12 | #include 13 | 14 | /*** 15 | *void _sleep(duration) - Length of sleep 16 | * 17 | *Purpose: 18 | * 19 | *Entry: 20 | * unsigned long duration - length of sleep in milliseconds or 21 | * one of the following special values: 22 | * 23 | * _SLEEP_MINIMUM - Sends a yield message without any delay 24 | * _SLEEP_FOREVER - Never return 25 | * 26 | *Exit: 27 | * None 28 | * 29 | *Exceptions: 30 | * 31 | *******************************************************************************/ 32 | 33 | extern "C" void __cdecl _sleep(unsigned long duration) 34 | { 35 | if (duration == 0) 36 | ++duration; 37 | 38 | Sleep(duration); 39 | } 40 | 41 | /*** 42 | *void _beep(frequency, duration) - Length of sleep 43 | * 44 | *Purpose: 45 | * 46 | *Entry: 47 | * unsigned frequency - frequency in hertz 48 | * unsigned duration - length of beep in milliseconds 49 | * 50 | *Exit: 51 | * None 52 | * 53 | *Exceptions: 54 | * 55 | *******************************************************************************/ 56 | 57 | extern "C" void __cdecl _beep(unsigned const frequency, unsigned const duration) 58 | { 59 | Beep(frequency, duration); 60 | } 61 | -------------------------------------------------------------------------------- /stdio/puts.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // puts.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines puts(), which writes a string to stdout. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Writes a string to stdout. Does not write the string's null terminator, but 14 | // _does_ append a newline to the output. Return 0 on success; EOF on failure. 15 | static int __cdecl _puts_internal(char const* const string, __crt_cached_ptd_host& ptd) 16 | { 17 | _UCRT_VALIDATE_RETURN(ptd, string != nullptr, EINVAL, EOF); 18 | 19 | FILE* const stream = stdout; 20 | _UCRT_VALIDATE_STREAM_ANSI_RETURN(ptd, stream, EINVAL, EOF); 21 | 22 | size_t const length = strlen(string); 23 | 24 | return __acrt_lock_stream_and_call(stream, [&]() -> int 25 | { 26 | __acrt_stdio_temporary_buffering_guard const buffering(stream, ptd); 27 | 28 | size_t const bytes_written = _fwrite_nolock_internal(string, 1, length, stream, ptd); 29 | 30 | // If we failed to write the entire string, or if we fail to write the 31 | // newline, reset the buffering and return failure: 32 | if (bytes_written != length || _fputc_nolock_internal('\n', stream, ptd) == EOF) 33 | { 34 | return EOF; 35 | } 36 | 37 | return 0; 38 | }); 39 | } 40 | 41 | extern "C" int __cdecl puts(char const* const string) 42 | { 43 | __crt_cached_ptd_host ptd; 44 | return _puts_internal(string, ptd); 45 | } -------------------------------------------------------------------------------- /string/memccpy.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *memccpy.c - copy bytes until a character is found 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines _memccpy() - copies bytes until a specifed character 8 | * is found, or a maximum number of characters have been copied. 9 | * 10 | *******************************************************************************/ 11 | 12 | #include 13 | 14 | /*** 15 | *char *_memccpy(dest, src, c, count) - copy bytes until character found 16 | * 17 | *Purpose: 18 | * Copies bytes from src to dest until count bytes have been 19 | * copied, or up to and including the character c, whichever 20 | * comes first. 21 | * 22 | *Entry: 23 | * void *dest - pointer to memory to receive copy 24 | * void *src - source of bytes 25 | * int c - character to stop copy at 26 | * size_t count - max number of bytes to copy 27 | * 28 | *Exit: 29 | * returns pointer to byte immediately after c in dest 30 | * returns NULL if c was never found 31 | * 32 | *Exceptions: 33 | * 34 | *******************************************************************************/ 35 | 36 | void * __cdecl _memccpy ( 37 | void * dest, 38 | const void * src, 39 | int c, 40 | size_t count 41 | ) 42 | { 43 | while ( count && (*((char *)(dest = (char *)dest + 1) - 1) = 44 | *((char *)(src = (char *)src + 1) - 1)) != (char)c ) 45 | count--; 46 | 47 | return(count ? dest : NULL); 48 | } 49 | -------------------------------------------------------------------------------- /stdio/fputws.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // fputws.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines fputws(), which writes a wide character string to a stream. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // Writes the given wide character string to the given stream. Does not write 14 | // the string's null terminator, and does not append a '\n' to the file. Returns 15 | // a nonnegative value on success; EOF on failure. (Note well that we return EOF 16 | // and not WEOF on failure. This is intentional, and is the correct behavior per 17 | // the C Language Standard). 18 | static int __cdecl _fputws_internal(wchar_t const* const string, FILE* const stream, __crt_cached_ptd_host& ptd) 19 | { 20 | _UCRT_VALIDATE_RETURN(ptd, string != nullptr, EINVAL, EOF); 21 | _UCRT_VALIDATE_RETURN(ptd, stream != nullptr, EINVAL, EOF); 22 | 23 | return __acrt_lock_stream_and_call(stream, [&]() -> int 24 | { 25 | __acrt_stdio_temporary_buffering_guard const buffering(stream, ptd); 26 | 27 | for (wchar_t const* it = string; *it != L'\0'; ++it) 28 | { 29 | if (_fputwc_nolock_internal(*it, stream, ptd) == WEOF) 30 | { 31 | return EOF; 32 | } 33 | } 34 | 35 | return 0; 36 | }); 37 | } 38 | 39 | extern "C" int __cdecl fputws(wchar_t const* const string, FILE* const stream) 40 | { 41 | __crt_cached_ptd_host ptd; 42 | return _fputws_internal(string, stream, ptd); 43 | } 44 | -------------------------------------------------------------------------------- /locale/GetStringTypeW.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // MIT License 3 | // 4 | // Copyright (c) 2024 Huang Qinjin (huangqinjin@gmail.com) 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | // 24 | #include 25 | 26 | extern "C" BOOL __cdecl __acrt_GetStringTypeW( 27 | DWORD const info_type, 28 | PCWCH const string, 29 | int const string_size_in_chars, 30 | LPWORD const char_type 31 | ) 32 | { 33 | return GetStringTypeW(info_type, string, string_size_in_chars, char_type); 34 | } 35 | -------------------------------------------------------------------------------- /mbstring/ismbalph.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *ismbalph.c - Test if character is alphabetic (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Test if character is alphabetic (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | 18 | /*** 19 | * _ismbcalpha - Test if character is alphabetic (MBCS) 20 | * 21 | *Purpose: 22 | * Test if character is alphabetic. 23 | * Handles MBCS chars correctly. 24 | * 25 | * Note: Use test against 0x00FF instead of _ISLEADBYTE 26 | * to ensure that we don't call SBCS routine with a two-byte 27 | * value. 28 | * 29 | *Entry: 30 | * unsigned int c = character to test 31 | * 32 | *Exit: 33 | * Returns TRUE if c is alphabetic, else FALSE 34 | * 35 | *Exceptions: 36 | * 37 | *******************************************************************************/ 38 | 39 | extern "C" int __cdecl _ismbcalpha_l(unsigned int const c, _locale_t const locale) 40 | { 41 | _LocaleUpdate locale_update(locale); 42 | 43 | if (c <= 0x00FF) 44 | { 45 | return _ismbbalpha_l(c, locale_update.GetLocaleT()); 46 | } 47 | 48 | return __dcrt_multibyte_check_type(c, locale_update.GetLocaleT(), _ALPHA, true); 49 | } 50 | 51 | extern "C" int __cdecl _ismbcalpha(unsigned int const c) 52 | { 53 | return _ismbcalpha_l(c, nullptr); 54 | } 55 | -------------------------------------------------------------------------------- /lowio/eof.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // eof.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _eof(), which tests whether a file is at EOF. 7 | // 8 | #include 9 | 10 | 11 | 12 | // Tests if a file is at EOF. Returns: 13 | // * 1 if at EOF 14 | // * 0 if not at EOF 15 | // * -1 on failure (errno is set) 16 | extern "C" int __cdecl _eof(int const fh) 17 | { 18 | _CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1); 19 | _VALIDATE_CLEAR_OSSERR_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1); 20 | _VALIDATE_CLEAR_OSSERR_RETURN((_osfile(fh) & FOPEN), EBADF, -1); 21 | 22 | return __acrt_lowio_lock_fh_and_call(fh, [&]() 23 | { 24 | if ((_osfile(fh) & FOPEN) == 0) 25 | { 26 | errno = EBADF; 27 | _doserrno = 0; 28 | _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0)); 29 | return -1; 30 | } 31 | 32 | __int64 const here = _lseeki64_nolock(fh, 0i64, SEEK_CUR); 33 | if (here == -1i64) 34 | return -1; 35 | 36 | 37 | __int64 const end = _lseeki64_nolock(fh, 0i64, SEEK_END); 38 | if (end == -1i64) 39 | return -1; 40 | 41 | // Now we can test if we're at the end: 42 | if (here == end) 43 | return 1; 44 | 45 | // If we aren't at the end, we need to reset the stream to its original 46 | // state before we return: 47 | _lseeki64_nolock(fh, here, SEEK_SET); 48 | return 0; 49 | }); 50 | } 51 | -------------------------------------------------------------------------------- /mbstring/ismbprn.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *ismbprn.c - Test character for display character (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Test character for display character (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | 18 | /*** 19 | * _ismbcprint - Test character for display character (MBCS) 20 | * 21 | *Purpose: 22 | * Test if the character is a display character. 23 | * Handles MBCS chars correctly. 24 | * 25 | * Note: Use test against 0x00FF to ensure that we don't 26 | * call SBCS routine with a two-byte value. 27 | * 28 | *Entry: 29 | * unsigned int c = character to test 30 | * 31 | *Exit: 32 | * Returns TRUE if character is display character, else FALSE 33 | * 34 | *Exceptions: 35 | * 36 | *******************************************************************************/ 37 | 38 | extern "C" int __cdecl _ismbcprint_l(unsigned int const c, _locale_t const locale) 39 | { 40 | _LocaleUpdate locale_update(locale); 41 | 42 | if (c <= 0x00FF) 43 | { 44 | return _ismbbprint_l(c, locale_update.GetLocaleT()); 45 | } 46 | 47 | return __dcrt_multibyte_check_type(c, locale_update.GetLocaleT(), _CONTROL, false); 48 | } 49 | 50 | extern "C" int __cdecl _ismbcprint(unsigned int const c) 51 | { 52 | return _ismbcprint_l(c, nullptr); 53 | } 54 | -------------------------------------------------------------------------------- /mbstring/ismbspc.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *ismbspc.c - Test is character is whitespace (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Test is character is whitespace (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | /*** 18 | * _ismbcspace - Test is character is whitespace (MBCS) 19 | * 20 | *Purpose: 21 | * Test if the character is a whitespace character. 22 | * Handles MBCS chars correctly. 23 | * 24 | * Note: Use test against 0x00FF instead of _ISLEADBYTE 25 | * to ensure that we don't call SBCS routine with a two-byte 26 | * value. 27 | * 28 | *Entry: 29 | * unsigned int c = character to test 30 | * 31 | *Exit: 32 | * Returns TRUE if character is whitespace, else FALSE 33 | * 34 | *Exceptions: 35 | * 36 | *******************************************************************************/ 37 | 38 | extern "C" int __cdecl _ismbcspace_l(unsigned int const c, _locale_t const locale) 39 | { 40 | _LocaleUpdate locale_update(locale); 41 | 42 | if (c <= 0x00FF) 43 | { 44 | return _isspace_l(c, locale_update.GetLocaleT()); 45 | } 46 | 47 | return __dcrt_multibyte_check_type(c, locale_update.GetLocaleT(), _SPACE, true); 48 | } 49 | 50 | extern "C" int __cdecl _ismbcspace(unsigned int const c) 51 | { 52 | return _ismbcspace_l(c, nullptr); 53 | } 54 | -------------------------------------------------------------------------------- /include/corecrt_math_defines.h: -------------------------------------------------------------------------------- 1 | // 2 | // corecrt_math_defines.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Definitions of useful mathematical constants 7 | // 8 | #pragma once 9 | #ifndef _MATH_DEFINES_DEFINED 10 | #define _MATH_DEFINES_DEFINED 11 | 12 | #include 13 | 14 | #pragma warning(push) 15 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 16 | _UCRT_DISABLE_CLANG_WARNINGS 17 | 18 | // Definitions of useful mathematical constants 19 | // 20 | // Define _USE_MATH_DEFINES before including to expose these macro 21 | // definitions for common math constants. These are placed under an #ifdef 22 | // since these commonly-defined names are not part of the C or C++ standards 23 | #define M_E 2.71828182845904523536 // e 24 | #define M_LOG2E 1.44269504088896340736 // log2(e) 25 | #define M_LOG10E 0.434294481903251827651 // log10(e) 26 | #define M_LN2 0.693147180559945309417 // ln(2) 27 | #define M_LN10 2.30258509299404568402 // ln(10) 28 | #define M_PI 3.14159265358979323846 // pi 29 | #define M_PI_2 1.57079632679489661923 // pi/2 30 | #define M_PI_4 0.785398163397448309616 // pi/4 31 | #define M_1_PI 0.318309886183790671538 // 1/pi 32 | #define M_2_PI 0.636619772367581343076 // 2/pi 33 | #define M_2_SQRTPI 1.12837916709551257390 // 2/sqrt(pi) 34 | #define M_SQRT2 1.41421356237309504880 // sqrt(2) 35 | #define M_SQRT1_2 0.707106781186547524401 // 1/sqrt(2) 36 | 37 | _UCRT_RESTORE_CLANG_WARNINGS 38 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 39 | #endif // _MATH_DEFINES_DEFINED 40 | -------------------------------------------------------------------------------- /mbstring/ismbupr.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *ismbupr - Test if character is upper case (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Test if character is upper case (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | /*** 17 | * _ismbcupper - Test if character is upper case (MBCS) 18 | * 19 | *Purpose: 20 | * Test if the supplied character is upper case or not. 21 | * Handles MBCS characters correctly. 22 | * 23 | * Note: Use test against 0x00FF instead of _ISLEADBYTE 24 | * to ensure that we don't call SBCS routine with a two-byte 25 | * value. 26 | * 27 | *Entry: 28 | * unsigned int c = character to test 29 | * 30 | *Exit: 31 | * Returns TRUE if c is an upper case character; else FALSE 32 | * 33 | *Exceptions: 34 | * 35 | *******************************************************************************/ 36 | 37 | extern "C" int __cdecl _ismbcupper_l(unsigned int const c, _locale_t const locale) 38 | { 39 | _LocaleUpdate locale_update(locale); 40 | 41 | if (c <= 0x00FF) 42 | { 43 | return _mbbisupper_l(c, locale_update.GetLocaleT()); 44 | } 45 | 46 | return __dcrt_multibyte_check_type(c, locale_update.GetLocaleT(), _UPPER, true); 47 | } 48 | 49 | extern "C" int __cdecl _ismbcupper(unsigned int const c) 50 | { 51 | return _ismbcupper_l(c, nullptr); 52 | } 53 | -------------------------------------------------------------------------------- /conio/cscanf.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // cscanf.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The core formatted input functions for direct console I/O. 7 | // 8 | #define _ALLOW_OLD_VALIDATE_MACROS 9 | #include 10 | 11 | using namespace __crt_stdio_input; 12 | 13 | template 14 | static int __cdecl common_cscanf( 15 | unsigned __int64 const options, 16 | Character const* const format, 17 | _locale_t const locale, 18 | va_list const arglist 19 | ) throw() 20 | { 21 | typedef input_processor< 22 | Character, 23 | console_input_adapter 24 | > processor_type; 25 | 26 | _LocaleUpdate locale_update(locale); 27 | 28 | processor_type processor( 29 | console_input_adapter(), 30 | options, 31 | format, 32 | locale_update.GetLocaleT(), 33 | arglist); 34 | 35 | return processor.process(); 36 | } 37 | 38 | extern "C" int __cdecl __conio_common_vcscanf( 39 | unsigned __int64 const options, 40 | char const* const format, 41 | _locale_t const locale, 42 | va_list const arglist 43 | ) 44 | { 45 | return common_cscanf(options, format, locale, arglist); 46 | } 47 | 48 | extern "C" int __cdecl __conio_common_vcwscanf( 49 | unsigned __int64 const options, 50 | wchar_t const* const format, 51 | _locale_t const locale, 52 | va_list const arglist 53 | ) 54 | { 55 | return common_cscanf(options, format, locale, arglist); 56 | } 57 | -------------------------------------------------------------------------------- /mbstring/ismblwr.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *ismblwr - Test if character is lower case (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Test if character is lower case (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | 18 | /*** 19 | * _ismbclower - Test if character is lower case (MBCS) 20 | * 21 | *Purpose: 22 | * Test if the supplied character is lower case or not. 23 | * Handles MBCS characters correctly. 24 | * 25 | * Note: Use test against 0x00FF instead of _ISLEADBYTE 26 | * to ensure that we don't call SBCS routine with a two-byte 27 | * value. 28 | * 29 | *Entry: 30 | * unsigned int c = character to test 31 | * 32 | *Exit: 33 | * returns TRUE if character is lower case, else FALSE 34 | * 35 | *Exceptions: 36 | * 37 | *******************************************************************************/ 38 | 39 | extern "C" int __cdecl _ismbclower_l(unsigned int const c, _locale_t const locale) 40 | { 41 | _LocaleUpdate locale_update(locale); 42 | 43 | if (c <= 0x00FF) 44 | { 45 | return _mbbislower_l(c, locale_update.GetLocaleT()); 46 | } 47 | 48 | return __dcrt_multibyte_check_type(c, locale_update.GetLocaleT(), _LOWER, true); 49 | } 50 | 51 | extern "C" int __cdecl _ismbclower(unsigned int const c) 52 | { 53 | return _ismbclower_l(c, nullptr); 54 | } 55 | -------------------------------------------------------------------------------- /heap/realloc.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // realloc.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Implementation of realloc(). 7 | // 8 | #include 9 | #include 10 | 11 | // Reallocates a block of memory in the heap. 12 | // 13 | // This function reallocates the block pointed to by 'block' such that it is 14 | // 'size' bytes in size. The new size may be either greater or less than the 15 | // original size of the block. The reallocation may result in the block being 16 | // moved to a new location in memory. If the block is moved, the contents of 17 | // the original block are copied. 18 | // 19 | // Standard behavior notes: 20 | // [1] realloc(nullptr, new_size) is equivalent to malloc(new_size) 21 | // [2] realloc(p, 0) is equivalent to free(p), and nullptr is returned 22 | // [3] If reallocation fails, the original block is left unchanged 23 | // 24 | // If 'block' is non-null, it must point to a valid block of memory allocated in 25 | // the heap. 26 | // 27 | // This function supports patching and therefore must be marked noinline. 28 | // Both _realloc_dbg and _realloc_base must also be marked noinline 29 | // to prevent identical COMDAT folding from substituting calls to realloc 30 | // with either other function or vice versa. 31 | extern "C" _CRT_HYBRIDPATCHABLE __declspec(noinline) _CRTRESTRICT void* __cdecl realloc( 32 | void* const block, 33 | size_t const size 34 | ) 35 | { 36 | #ifdef _DEBUG 37 | return _realloc_dbg(block, size, _NORMAL_BLOCK, nullptr, 0); 38 | #else 39 | return _realloc_base(block, size); 40 | #endif 41 | } 42 | -------------------------------------------------------------------------------- /mbstring/ismbdgt.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *ismbdgt.cpp - Test if character is a digit (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Test if character is a digit (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | 18 | /*** 19 | * _ismbcdigit - Test if character is a digit (MBCS) 20 | * 21 | *Purpose: 22 | * Tests the character to see if it is a digit. 23 | * Handles MBCS chars correctly. 24 | * 25 | * Note: Use test against 0x00FF instead of _ISLEADBYTE 26 | * to ensure that we don't call SBCS routine with a two-byte 27 | * value. 28 | * 29 | *Entry: 30 | * unsigned int *c = character to test 31 | * 32 | *Exit: 33 | * Returns TRUE if character is a digit, else FALSE 34 | * 35 | *Exceptions: 36 | * 37 | *******************************************************************************/ 38 | 39 | extern "C" int __cdecl _ismbcdigit_l(unsigned int const c, _locale_t const locale) 40 | { 41 | _LocaleUpdate locale_update(locale); 42 | 43 | if (c <= 0x00FF) 44 | { 45 | return _isdigit_fast_internal(static_cast(c), locale_update.GetLocaleT()); 46 | } 47 | 48 | return __dcrt_multibyte_check_type(c, locale_update.GetLocaleT(), _DIGIT, true); 49 | } 50 | 51 | extern "C" int __cdecl _ismbcdigit(unsigned int const c) 52 | { 53 | return _ismbcdigit_l(c, nullptr); 54 | } 55 | -------------------------------------------------------------------------------- /mbstring/ismbgrph.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *ismbgrph - Test if character is graphical (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Test if character is graphical (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | 18 | /*** 19 | * _ismbcgraph - Test if character is graphical (MBCS) 20 | * 21 | *Purpose: 22 | * Test if the supplied character is graphical or not. 23 | * Handles MBCS characters correctly. 24 | * 25 | * Note: Use test against 0x00FF instead of _ISLEADBYTE 26 | * to ensure that we don't call SBCS routine with a two-byte 27 | * value. 28 | * 29 | *Entry: 30 | * unsigned int c = character to test 31 | * 32 | *Exit: 33 | * Returns TRUE if c is an graphical character; else FALSE 34 | * 35 | *Exceptions: 36 | * 37 | *******************************************************************************/ 38 | 39 | extern "C" int __cdecl _ismbcgraph_l(unsigned int const c, _locale_t const locale) 40 | { 41 | _LocaleUpdate locale_update(locale); 42 | 43 | if (c <= 0x00FF) 44 | { 45 | return _ismbbgraph_l(c, locale_update.GetLocaleT()); 46 | } 47 | 48 | return __dcrt_multibyte_check_type(c, locale_update.GetLocaleT(), _PUNCT | _ALPHA | _DIGIT, true); 49 | } 50 | 51 | extern "C" int __cdecl _ismbcgraph(unsigned int const c) 52 | { 53 | return _ismbcgraph_l(c, nullptr); 54 | } 55 | -------------------------------------------------------------------------------- /mbstring/ismbalnm.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *ismbalnm - Test if character is alpha numeric (MBCS) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Test if character is alpha numeric (MBCS) 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | 17 | 18 | /*** 19 | * _ismbcalnum - Test if character is alpha numeric (MBCS) 20 | * 21 | *Purpose: 22 | * Test if the supplied character is alpha numeric or not. 23 | * Handles MBCS characters correctly. 24 | * 25 | * Note: Use test against 0x00FF instead of _ISLEADBYTE 26 | * to ensure that we don't call SBCS routine with a two-byte 27 | * value. 28 | * 29 | *Entry: 30 | * unsigned int c = character to test 31 | * 32 | *Exit: 33 | * Returns TRUE if c is an alpha numeric character; else FALSE 34 | * 35 | *Exceptions: 36 | * 37 | *******************************************************************************/ 38 | 39 | extern "C" int __cdecl _ismbcalnum_l(unsigned int const c, _locale_t const locale) 40 | { 41 | _LocaleUpdate locale_update(locale); 42 | 43 | if (c <= 0x00FF) 44 | { 45 | return _ismbbalnum_l(c, locale_update.GetLocaleT()); 46 | } 47 | 48 | return __dcrt_multibyte_check_type(c, locale_update.GetLocaleT(), _ALPHA | _DIGIT, true); 49 | } 50 | 51 | extern "C" int __cdecl _ismbcalnum(unsigned int const c) 52 | { 53 | return _ismbcalnum_l(c, nullptr); 54 | } 55 | -------------------------------------------------------------------------------- /stdio/gettemppath.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // gettemppath.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The __acrt_GetTempPath2A() function, which calls GetTempPathW and converts the string to multibyte. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // This function simply calls __acrt_GetTempPath2W() and converts the wide string to multibyte. 14 | // Note that GetTempPathA is not UTF-8 aware. This is because APIs using temporary paths 15 | // must not depend on the current locale setting and must use the ACP or OEMCP since 16 | // the returned data must be a static buffer and this behavior is guaranteed by MSDN documentation. 17 | extern "C" DWORD __cdecl __acrt_GetTempPath2A(DWORD nBufferLength, LPSTR lpBuffer) 18 | { 19 | wchar_t wide_buffer[_MAX_PATH + 1] = {}; 20 | 21 | DWORD const get_temp_path_result = __acrt_GetTempPath2W(_countof(wide_buffer), wide_buffer); 22 | if (get_temp_path_result == 0) 23 | { 24 | return 0; 25 | } 26 | 27 | bool const use_oem_code_page = !__acrt_AreFileApisANSI(); 28 | int const code_page = use_oem_code_page ? CP_OEMCP : CP_ACP; 29 | #pragma warning(suppress:__WARNING_W2A_BEST_FIT) // 38021 Prefast recommends WC_NO_BEST_FIT_CHARS. 30 | int const wctmb_result = __acrt_WideCharToMultiByte(code_page, 0, wide_buffer, -1, lpBuffer, nBufferLength, nullptr, nullptr); 31 | if (wctmb_result == 0) 32 | { 33 | return 0; 34 | } 35 | 36 | // The return value of WideCharToMultiByte includes the null terminator; the 37 | // return value of GetTempPathA does not. 38 | return wctmb_result - 1; 39 | } 40 | -------------------------------------------------------------------------------- /stdlib/byteswap.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // byteswap.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines functions that swap the bytes of an unsigned integer. 7 | // 8 | #include 9 | 10 | 11 | 12 | #pragma function(_byteswap_ulong, _byteswap_uint64, _byteswap_ushort) 13 | 14 | /*** 15 | *unsigned long _byteswap_ulong(i) - long byteswap 16 | * 17 | *Purpose: 18 | * Performs a byte swap on an unsigned integer. 19 | * 20 | *Entry: 21 | * unsigned long i: value to swap 22 | * 23 | *Exit: 24 | * returns swaped 25 | * 26 | *Exceptions: 27 | * None. 28 | * 29 | *******************************************************************************/ 30 | 31 | extern "C" unsigned long __cdecl _byteswap_ulong(unsigned long const i) 32 | { 33 | unsigned int j; 34 | j = (i << 24); 35 | j += (i << 8) & 0x00FF0000; 36 | j += (i >> 8) & 0x0000FF00; 37 | j += (i >> 24); 38 | return j; 39 | } 40 | 41 | extern "C" unsigned short __cdecl _byteswap_ushort(unsigned short const i) 42 | { 43 | unsigned short j; 44 | j = (i << 8); 45 | j += (i >> 8); 46 | return j; 47 | } 48 | 49 | extern "C" unsigned __int64 __cdecl _byteswap_uint64(unsigned __int64 const i) 50 | { 51 | unsigned __int64 j; 52 | j = (i << 56); 53 | j += (i << 40) & 0x00FF000000000000; 54 | j += (i << 24) & 0x0000FF0000000000; 55 | j += (i << 8) & 0x000000FF00000000; 56 | j += (i >> 8) & 0x00000000FF000000; 57 | j += (i >> 24) & 0x0000000000FF0000; 58 | j += (i >> 40) & 0x000000000000FF00; 59 | j += (i >> 56); 60 | return j; 61 | 62 | } 63 | -------------------------------------------------------------------------------- /heap/msize.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // msize.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Implementation of msize(). 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // This function implements the logic of _msize(). It is called only in the 14 | // Release CRT. The Debug CRT has its own implementation of this function. 15 | // 16 | // This function must be marked noinline, otherwise _msize and 17 | // _msize_base will have identical COMDATs, and the linker will fold 18 | // them when calling one from the CRT. This is necessary because _msize 19 | // needs to support users patching in custom implementations. 20 | extern "C" __declspec(noinline) size_t __cdecl _msize_base(void* const block) noexcept 21 | { 22 | // Validation section 23 | _VALIDATE_RETURN(block != nullptr, EINVAL, static_cast(-1)); 24 | 25 | return static_cast(HeapSize(__acrt_heap, 0, block)); 26 | } 27 | 28 | // Calculates the size of the specified block in the heap. 'block' must be a 29 | // pointer to a valid block of heap-allocated memory (it must not be nullptr). 30 | // 31 | // This function supports patching and therefore must be marked noinline. 32 | // Both _msize_dbg and _msize_base must also be marked noinline 33 | // to prevent identical COMDAT folding from substituting calls to _msize 34 | // with either other function or vice versa. 35 | extern "C" _CRT_HYBRIDPATCHABLE __declspec(noinline) size_t __cdecl _msize(void* const block) 36 | { 37 | #ifdef _DEBUG 38 | return _msize_dbg(block, _NORMAL_BLOCK); 39 | #else 40 | return _msize_base(block); 41 | #endif 42 | } 43 | -------------------------------------------------------------------------------- /include/stddef.h: -------------------------------------------------------------------------------- 1 | // 2 | // stddef.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The C Standard Library header. 7 | // 8 | #pragma once 9 | #ifndef _INC_STDDEF // include guard for 3rd party interop 10 | #define _INC_STDDEF 11 | 12 | #include 13 | 14 | #pragma warning(push) 15 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 16 | _UCRT_DISABLE_CLANG_WARNINGS 17 | 18 | _CRT_BEGIN_C_HEADER 19 | 20 | 21 | 22 | #ifdef __cplusplus 23 | namespace std 24 | { 25 | typedef decltype(__nullptr) nullptr_t; 26 | } 27 | 28 | using ::std::nullptr_t; 29 | #endif 30 | 31 | 32 | 33 | #if _CRT_FUNCTIONS_REQUIRED 34 | 35 | _ACRTIMP int* __cdecl _errno(void); 36 | #define errno (*_errno()) 37 | 38 | _ACRTIMP errno_t __cdecl _set_errno(_In_ int _Value); 39 | _ACRTIMP errno_t __cdecl _get_errno(_Out_ int* _Value); 40 | 41 | #endif // _CRT_FUNCTIONS_REQUIRED 42 | 43 | 44 | 45 | #if defined _MSC_VER && !defined _CRT_USE_BUILTIN_OFFSETOF 46 | #ifdef __cplusplus 47 | #define offsetof(s,m) ((::size_t)&reinterpret_cast((((s*)0)->m))) 48 | #else 49 | #define offsetof(s,m) ((size_t)&(((s*)0)->m)) 50 | #endif 51 | #else 52 | #define offsetof(s,m) __builtin_offsetof(s,m) 53 | #endif 54 | 55 | _ACRTIMP extern unsigned long __cdecl __threadid(void); 56 | #define _threadid (__threadid()) 57 | _ACRTIMP extern uintptr_t __cdecl __threadhandle(void); 58 | 59 | 60 | 61 | _CRT_END_C_HEADER 62 | _UCRT_RESTORE_CLANG_WARNINGS 63 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 64 | #endif // _INC_STDDEF 65 | -------------------------------------------------------------------------------- /mbstring/mbslen.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbslen.c - Find length of MBCS string 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Find length of MBCS string 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | /*** 20 | * _mbslen - Find length of MBCS string 21 | * 22 | *Purpose: 23 | * Find the length of the MBCS string (in characters). 24 | * 25 | *Entry: 26 | * unsigned char *s = string 27 | * 28 | *Exit: 29 | * Returns the number of MBCS chars in the string. 30 | * 31 | *Exceptions: 32 | * 33 | *******************************************************************************/ 34 | 35 | extern "C" size_t __cdecl _mbslen_l( 36 | const unsigned char *s, 37 | _locale_t plocinfo 38 | ) 39 | { 40 | int n; 41 | _LocaleUpdate _loc_update(plocinfo); 42 | 43 | if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0) 44 | return strlen((const char *)s); 45 | 46 | for (n = 0; *s; n++, s++) { 47 | if ( _ismbblead_l(*s, _loc_update.GetLocaleT()) ) { 48 | if (*++s == '\0') 49 | break; 50 | } 51 | } 52 | 53 | return(n); 54 | } 55 | 56 | extern "C" size_t (__cdecl _mbslen)( 57 | const unsigned char *s 58 | ) 59 | { 60 | return _mbslen_l(s, nullptr); 61 | } 62 | -------------------------------------------------------------------------------- /misc/terminate.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // terminate.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // The terminate handler 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | static terminate_handler __cdecl get_terminate_or_default( 14 | __acrt_ptd const* const ptd 15 | ) throw() 16 | { 17 | return ptd->_terminate ? ptd->_terminate : &abort; 18 | } 19 | 20 | extern "C" terminate_handler __cdecl _get_terminate() 21 | { 22 | return get_terminate_or_default(__acrt_getptd()); 23 | } 24 | 25 | extern "C" terminate_handler __cdecl set_terminate( 26 | terminate_handler const new_handler 27 | ) throw() 28 | { 29 | __acrt_ptd* const ptd = __acrt_getptd(); 30 | 31 | terminate_handler const old_handler = get_terminate_or_default(ptd); 32 | 33 | ptd->_terminate = new_handler; 34 | 35 | return old_handler; 36 | } 37 | 38 | extern "C" void __cdecl terminate() throw() 39 | { 40 | terminate_handler const handler = __acrt_getptd()->_terminate; 41 | if (handler) 42 | { 43 | // Note: We cannot allow any exceptions to propagate from a user- 44 | // registered terminate handler, so if any structured exception escapes 45 | // the user handler we abort. 46 | __try 47 | { 48 | handler(); 49 | } 50 | __except(EXCEPTION_EXECUTE_HANDLER) 51 | { 52 | ; // Deliberately do nothing 53 | } 54 | } 55 | 56 | // If the terminate handler returned, faulted, or otherwise failed to end 57 | // execution, we will do it: 58 | abort(); 59 | } 60 | -------------------------------------------------------------------------------- /convert/wctype.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wctype.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Implementations of the wctype function. 7 | // 8 | #include 9 | #include 10 | #include 11 | 12 | 13 | 14 | static struct wctab 15 | { 16 | char const* s; 17 | wctype_t value; 18 | } 19 | const tab[] = 20 | { 21 | { "alnum", _ALPHA | _DIGIT }, 22 | { "alpha", _ALPHA }, 23 | { "cntrl", _CONTROL }, 24 | { "digit", _DIGIT }, 25 | { "graph", _PUNCT | _ALPHA | _DIGIT }, 26 | { "lower", _LOWER }, 27 | { "print", _BLANK | _PUNCT | _ALPHA | _DIGIT }, 28 | { "punct", _PUNCT }, 29 | { "blank", _BLANK }, 30 | { "space", _SPACE }, 31 | { "upper", _UPPER }, 32 | { "xdigit", _HEX }, 33 | { nullptr, 0 } 34 | }; 35 | 36 | 37 | 38 | #pragma warning(push) 39 | #pragma warning(disable: 4273) 40 | 41 | extern "C" wctype_t __cdecl wctype(char const* const name) 42 | { 43 | for (unsigned n = 0; tab[n].s != 0; ++n) 44 | { 45 | if (strcmp(tab[n].s, name) == 0) 46 | return tab[n].value; 47 | } 48 | 49 | return 0; 50 | } 51 | 52 | #pragma warning(pop) 53 | 54 | /* 55 | * Copyright (c) 1992-2007 by P.J. Plauger. ALL RIGHTS RESERVED. 56 | * Consult your license regarding permissions and restrictions. 57 | V5.03:0009 */ 58 | -------------------------------------------------------------------------------- /string/strncat.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strncat.c - append n chars of string to new string 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines strncat() - appends n characters of string onto 8 | * end of other string 9 | * 10 | *******************************************************************************/ 11 | 12 | #include 13 | 14 | /*** 15 | *char *strncat(front, back, count) - append count chars of back onto front 16 | * 17 | *Purpose: 18 | * Appends at most count characters of the string back onto the 19 | * end of front, and ALWAYS terminates with a null character. 20 | * If count is greater than the length of back, the length of back 21 | * is used instead. (Unlike strncpy, this routine does not pad out 22 | * to count characters). 23 | * 24 | *Entry: 25 | * char *front - string to append onto 26 | * char *back - string to append 27 | * unsigned count - count of max characters to append 28 | * 29 | *Exit: 30 | * returns a pointer to string appended onto (front). 31 | * 32 | *Uses: 33 | * 34 | *Exceptions: 35 | * 36 | *******************************************************************************/ 37 | 38 | char * __cdecl strncat ( 39 | char * front, 40 | const char * back, 41 | size_t count 42 | ) 43 | { 44 | char *start = front; 45 | 46 | while (*front++) 47 | ; 48 | front--; 49 | 50 | while (count--) 51 | if ((*front++ = *back++) == 0) 52 | return(start); 53 | 54 | *front = '\0'; 55 | return(start); 56 | } 57 | -------------------------------------------------------------------------------- /string/strdup.cpp: -------------------------------------------------------------------------------- 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 14 | #include 15 | #include 16 | 17 | 18 | 19 | #ifdef _DEBUG 20 | 21 | extern "C" char* __cdecl _strdup(char const* const string) 22 | { 23 | return _strdup_dbg(string, _NORMAL_BLOCK, nullptr, 0); 24 | } 25 | 26 | extern "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 | 35 | extern "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(_malloc_dbg( 48 | size, 49 | block_use, 50 | file_name, 51 | line_number)); 52 | #else 53 | char* const memory = static_cast(malloc(size)); 54 | #endif 55 | 56 | if (memory == nullptr) 57 | return nullptr; 58 | 59 | _ERRCHECK(strcpy_s(memory, size, string)); 60 | return memory; 61 | } 62 | -------------------------------------------------------------------------------- /include/uchar.h: -------------------------------------------------------------------------------- 1 | // 2 | // uchar.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | 7 | #pragma once 8 | #ifndef _UCHAR // include guard for 3rd party interop 9 | #define _UCHAR 10 | 11 | #include 12 | 13 | #pragma warning(push) 14 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 15 | _UCRT_DISABLE_CLANG_WARNINGS 16 | 17 | _CRT_BEGIN_C_HEADER 18 | 19 | #ifndef __STDC_UTF_16__ 20 | #define __STDC_UTF_16__ 1 21 | #endif 22 | 23 | #ifndef __STDC_UTF_32__ 24 | #define __STDC_UTF_32__ 1 25 | #endif 26 | 27 | typedef unsigned short _Char16_t; 28 | typedef unsigned int _Char32_t; 29 | 30 | #if !defined __cplusplus || (defined _MSC_VER && _MSC_VER < 1900) 31 | typedef unsigned short char16_t; 32 | typedef unsigned int char32_t; 33 | #endif 34 | 35 | 36 | _Check_return_ _ACRTIMP size_t __cdecl mbrtoc16(_Out_opt_ char16_t *_Pc16, _In_reads_or_z_opt_(_N) const char *_S, _In_ size_t _N, _Inout_ mbstate_t *_Ps); 37 | _Check_return_ _ACRTIMP size_t __cdecl c16rtomb(_Out_writes_opt_(4) char *_S, _In_ char16_t _C16, _Inout_ mbstate_t *_Ps); 38 | 39 | _Check_return_ _ACRTIMP size_t __cdecl mbrtoc32(_Out_opt_ char32_t *_Pc32, _In_reads_or_z_opt_(_N) const char *_S, _In_ size_t _N, _Inout_ mbstate_t *_Ps); 40 | _Check_return_ _ACRTIMP size_t __cdecl c32rtomb(_Out_writes_opt_(4) char *_S, _In_ char32_t _C32, _Inout_ mbstate_t *_Ps); 41 | 42 | _CRT_END_C_HEADER 43 | _UCRT_RESTORE_CLANG_WARNINGS 44 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 45 | 46 | /* 47 | * Copyright (c) 1992-2013 by P.J. Plauger. ALL RIGHTS RESERVED. 48 | * Consult your license regarding permissions and restrictions. 49 | V6.40:0009 */ 50 | #endif // _UCHAR 51 | -------------------------------------------------------------------------------- /heap/heap_handle.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // heap_handle.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines the global CRT heap handle and initialization/termination functions. 7 | // 8 | #include 9 | #include 10 | 11 | 12 | 13 | // The CRT heap handle. This global variable is modified only during CRT 14 | // startup and CRT shutdown. 15 | extern "C" HANDLE __acrt_heap = nullptr; 16 | 17 | 18 | 19 | // Initializes the heap. This function must be called during CRT startup, and 20 | // must be called before any user code that might use the heap is executed. 21 | extern "C" bool __cdecl __acrt_initialize_heap() 22 | { 23 | __acrt_heap = GetProcessHeap(); 24 | if (__acrt_heap == nullptr) 25 | return false; 26 | 27 | return true; 28 | } 29 | 30 | // Uninitializes the heap. This function should be called during CRT shutdown, 31 | // after any user code that might use the heap has stopped running. 32 | extern "C" bool __cdecl __acrt_uninitialize_heap(bool const /* terminating */) 33 | { 34 | __acrt_heap = nullptr; 35 | return true; 36 | } 37 | 38 | // Gets the HANDLE of the CRT heap. Because the CRT always uses the process 39 | // heap, this function always returns the same thing as GetProcessHeap(). 40 | extern "C" intptr_t __cdecl _get_heap_handle() 41 | { 42 | _ASSERTE(__acrt_heap != nullptr); 43 | return reinterpret_cast(__acrt_heap); 44 | } 45 | 46 | // Internal CRT function to get the HANDLE of the CRT heap, that returns a 47 | // HANDLE instead of an intptr_t. 48 | extern "C" HANDLE __acrt_getheap() 49 | { 50 | _ASSERTE(__acrt_heap != nullptr); 51 | return __acrt_heap; 52 | } 53 | -------------------------------------------------------------------------------- /string/strncpy.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strncpy.c - copy at most n characters of string 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines strncpy() - copy at most n characters of string 8 | * 9 | *******************************************************************************/ 10 | 11 | #include 12 | 13 | #if defined _M_ARM 14 | #pragma function(strncpy) 15 | #endif 16 | 17 | /*** 18 | *char *strncpy(dest, source, count) - copy at most n characters 19 | * 20 | *Purpose: 21 | * Copies count characters from the source string to the 22 | * destination. If count is less than the length of source, 23 | * NO NULL CHARACTER is put onto the end of the copied string. 24 | * If count is greater than the length of sources, dest is padded 25 | * with null characters to length count. 26 | * 27 | * 28 | *Entry: 29 | * char *dest - pointer to destination 30 | * char *source - source string for copy 31 | * unsigned count - max number of characters to copy 32 | * 33 | *Exit: 34 | * returns dest 35 | * 36 | *Exceptions: 37 | * 38 | *******************************************************************************/ 39 | 40 | char * __cdecl strncpy ( 41 | char * dest, 42 | const char * source, 43 | size_t count 44 | ) 45 | { 46 | char *start = dest; 47 | 48 | while (count && (*dest++ = *source++) != '\0') /* copy string */ 49 | count--; 50 | 51 | if (count) /* pad out with zeroes */ 52 | while (--count) 53 | *dest++ = '\0'; 54 | 55 | return(start); 56 | } 57 | -------------------------------------------------------------------------------- /string/strcmp.c: -------------------------------------------------------------------------------- 1 | /*** 2 | *strcmp.c - routine to compare two strings (for equal, less, or greater) 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Compares two string, determining their ordinal order. 8 | * 9 | *******************************************************************************/ 10 | 11 | #include 12 | 13 | #pragma function(strcmp) 14 | 15 | /*** 16 | *strcmp - compare two strings, returning less than, equal to, or greater than 17 | * 18 | *Purpose: 19 | * STRCMP compares two strings and returns an integer 20 | * to indicate whether the first is less than the second, the two are 21 | * equal, or whether the first is greater than the second. 22 | * 23 | * Comparison is done byte by byte on an UNSIGNED basis, which is to 24 | * say that Null (0) is less than any other character (1-255). 25 | * 26 | *Entry: 27 | * const char * src - string for left-hand side of comparison 28 | * const char * dst - string for right-hand side of comparison 29 | * 30 | *Exit: 31 | * returns -1 if src < dst 32 | * returns 0 if src == dst 33 | * returns +1 if src > dst 34 | * 35 | *Exceptions: 36 | * 37 | *******************************************************************************/ 38 | 39 | int __cdecl strcmp ( 40 | const char * src, 41 | const char * dst 42 | ) 43 | { 44 | int ret = 0 ; 45 | 46 | while((ret = *(unsigned char *)src - *(unsigned char *)dst) == 0 && *dst) 47 | { 48 | ++src, ++dst; 49 | } 50 | 51 | return ((-ret) < 0) - (ret < 0); // (if positive) - (if negative) generates branchless code 52 | } 53 | -------------------------------------------------------------------------------- /include/corecrt_wdirect.h: -------------------------------------------------------------------------------- 1 | // 2 | // corecrt_wdirect.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // This file declares the wide character (wchar_t) directory functionality, shared 7 | // by and . 8 | // 9 | #pragma once 10 | 11 | #include 12 | 13 | #pragma warning(push) 14 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 15 | _UCRT_DISABLE_CLANG_WARNINGS 16 | 17 | _CRT_BEGIN_C_HEADER 18 | 19 | #pragma push_macro("_wgetcwd") 20 | #pragma push_macro("_wgetdcwd") 21 | #undef _wgetcwd 22 | #undef _wgetdcwd 23 | 24 | _Success_(return != 0) 25 | _Check_return_ _Ret_maybenull_z_ 26 | _ACRTIMP _CRTALLOCATOR wchar_t* __cdecl _wgetcwd( 27 | _Out_writes_opt_z_(_SizeInWords) wchar_t* _DstBuf, 28 | _In_ int _SizeInWords 29 | ); 30 | 31 | _Success_(return != 0) 32 | _Check_return_ _Ret_maybenull_z_ 33 | _ACRTIMP _CRTALLOCATOR wchar_t* __cdecl _wgetdcwd( 34 | _In_ int _Drive, 35 | _Out_writes_opt_z_(_SizeInWords) wchar_t* _DstBuf, 36 | _In_ int _SizeInWords 37 | ); 38 | 39 | #define _wgetdcwd_nolock _wgetdcwd 40 | 41 | #pragma pop_macro("_wgetcwd") 42 | #pragma pop_macro("_wgetdcwd") 43 | 44 | _Check_return_ 45 | _ACRTIMP int __cdecl _wchdir( 46 | _In_z_ wchar_t const* _Path 47 | ); 48 | 49 | _Check_return_ 50 | _ACRTIMP int __cdecl _wmkdir( 51 | _In_z_ wchar_t const* _Path 52 | ); 53 | 54 | _Check_return_ 55 | _ACRTIMP int __cdecl _wrmdir( 56 | _In_z_ wchar_t const* _Path 57 | ); 58 | 59 | 60 | 61 | _CRT_END_C_HEADER 62 | _UCRT_RESTORE_CLANG_WARNINGS 63 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 64 | -------------------------------------------------------------------------------- /exec/loaddll.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *loaddll.c - load or free a Dynamic Link Library 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * defines _loaddll() and _unloaddll() - load and unload DLL 8 | * 9 | *******************************************************************************/ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define _CRT_ENABLE_OBSOLETE_LOADLIBRARY_FUNCTIONS 16 | 17 | #include 18 | 19 | /*** 20 | *int _loaddll(filename) - Load a dll 21 | * 22 | *Purpose: 23 | * Load a DLL into memory 24 | * 25 | *Entry: 26 | * char *filename - file to load 27 | * 28 | *Exit: 29 | * returns a unique DLL (module) handle if succeeds 30 | * returns 0 if fails 31 | * 32 | *Exceptions: 33 | * 34 | *******************************************************************************/ 35 | 36 | extern "C" intptr_t __cdecl _loaddll(char* szName) 37 | { 38 | return reinterpret_cast(__acrt_LoadLibraryExA(szName, nullptr, 0)); 39 | } 40 | 41 | /*** 42 | *int _unloaddll(handle) - Unload a dll 43 | * 44 | *Purpose: 45 | * Unloads a DLL. The resources of the DLL will be freed if no other 46 | * processes are using it. 47 | * 48 | *Entry: 49 | * int handle - handle from _loaddll 50 | * 51 | *Exit: 52 | * returns 0 if succeeds 53 | * returns DOS error if fails 54 | * 55 | *Exceptions: 56 | * 57 | *******************************************************************************/ 58 | 59 | extern "C" int __cdecl _unloaddll(intptr_t hMod) 60 | { 61 | if (!FreeLibrary(reinterpret_cast(hMod))) { 62 | return ((int)GetLastError()); 63 | } 64 | return (0); 65 | } 66 | -------------------------------------------------------------------------------- /heap/malloc_base.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // malloc_base.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Implementation of _malloc_base(). This is defined in a different source file 7 | // from the malloc() function to allow malloc() to be replaced by the user. 8 | // 9 | #include 10 | #include 11 | #include 12 | 13 | 14 | 15 | // This function implements the logic of malloc(). It is called directly by the 16 | // malloc() function in the Release CRT and is called by the debug heap in the 17 | // Debug CRT. 18 | // 19 | // This function must be marked noinline, otherwise malloc and 20 | // _malloc_base will have identical COMDATs, and the linker will fold 21 | // them when calling one from the CRT. This is necessary because malloc 22 | // needs to support users patching in custom implementations. 23 | extern "C" __declspec(noinline) _CRTRESTRICT void* __cdecl _malloc_base(size_t const size) 24 | { 25 | // Ensure that the requested size is not too large: 26 | _VALIDATE_RETURN_NOEXC(_HEAP_MAXREQ >= size, ENOMEM, nullptr); 27 | 28 | // Ensure we request an allocation of at least one byte: 29 | size_t const actual_size = size == 0 ? 1 : size; 30 | 31 | for (;;) 32 | { 33 | void* const block = HeapAlloc(__acrt_heap, 0, actual_size); 34 | if (block) 35 | return block; 36 | 37 | // Otherwise, see if we need to call the new handler, and if so call it. 38 | // If the new handler fails, just return nullptr: 39 | if (_query_new_mode() == 0 || !_callnewh(actual_size)) 40 | { 41 | errno = ENOMEM; 42 | return nullptr; 43 | } 44 | 45 | // The new handler was successful; try to allocate again... 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /string/wcsdup.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // wcsdup.cpp 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // Defines _wcsdup() and _wcsdup_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 14 | #include 15 | #include 16 | 17 | #ifdef _DEBUG 18 | 19 | extern "C" wchar_t* __cdecl _wcsdup(wchar_t const* const string) 20 | { 21 | return _wcsdup_dbg(string, _NORMAL_BLOCK, nullptr, 0); 22 | } 23 | 24 | extern "C" wchar_t* __cdecl _wcsdup_dbg( 25 | wchar_t const* const string, 26 | int const block_use, 27 | char const* const file_name, 28 | int const line_number 29 | ) 30 | 31 | #else // ^^^ _DEBUG ^^^ // vvv !_DEBUG vvv // 32 | 33 | extern "C" wchar_t* __cdecl _wcsdup( 34 | wchar_t const* string 35 | ) 36 | 37 | #endif // !_DEBUG 38 | { 39 | if (string == nullptr) 40 | return nullptr; 41 | 42 | size_t const size_in_elements = wcslen(string) + 1; 43 | 44 | #ifdef _DEBUG 45 | wchar_t* const memory = static_cast(_malloc_dbg( 46 | size_in_elements * sizeof(wchar_t), 47 | block_use, 48 | file_name, 49 | line_number)); 50 | #else 51 | wchar_t* const memory = static_cast(malloc( 52 | size_in_elements * sizeof(wchar_t))); 53 | #endif 54 | 55 | if (memory == nullptr) 56 | return nullptr; 57 | 58 | _ERRCHECK(wcscpy_s(memory, size_in_elements, string)); 59 | return memory; 60 | } 61 | -------------------------------------------------------------------------------- /mbstring/mbsninc.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | *mbsninc.c - Increment MBCS string pointer by specified char count. 3 | * 4 | * Copyright (c) Microsoft Corporation. All rights reserved. 5 | * 6 | *Purpose: 7 | * Increment MBCS string pointer by specified char count. 8 | * 9 | *******************************************************************************/ 10 | #ifndef _MBCS 11 | #error This file should only be compiled with _MBCS defined 12 | #endif 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | /*** 19 | *_mbsninc - Increment MBCS string pointer by specified char count. 20 | * 21 | *Purpose: 22 | * Increment the supplied string pointer by the specified number 23 | * of characters. MBCS characters are handled correctly. 24 | * 25 | *Entry: 26 | * const unsigned char *string = pointer to string 27 | * unsigned int ccnt = number of char to advance the pointer 28 | * 29 | *Exit: 30 | * Returns pointer after advancing it. 31 | * Returns pointer to end of string if string is not ccnt chars long. 32 | * Returns nullptr is supplied pointer is nullptr. 33 | * 34 | *Exceptions: 35 | * 36 | *******************************************************************************/ 37 | 38 | extern "C" unsigned char * __cdecl _mbsninc_l( 39 | const unsigned char *string, 40 | size_t ccnt, 41 | _locale_t plocinfo 42 | ) 43 | { 44 | if (string == nullptr) 45 | return nullptr; 46 | 47 | return const_cast(string) + (unsigned int)_mbsnbcnt_l(string, ccnt, plocinfo); 48 | } 49 | 50 | extern "C" unsigned char * (__cdecl _mbsninc)( 51 | const unsigned char *string, 52 | size_t ccnt 53 | ) 54 | { 55 | return _mbsninc_l(string, ccnt, nullptr); 56 | } 57 | -------------------------------------------------------------------------------- /include/dos.h: -------------------------------------------------------------------------------- 1 | // 2 | // dos.h 3 | // 4 | // Copyright (c) Microsoft Corporation. All rights reserved. 5 | // 6 | // This file declares the structures, constants, and functions used for the 7 | // legacy DOS interface. 8 | // 9 | #pragma once 10 | #ifndef _INC_DOS // include guard for 3rd party interop 11 | #define _INC_DOS 12 | 13 | #include 14 | 15 | #pragma warning(push) 16 | #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 17 | _UCRT_DISABLE_CLANG_WARNINGS 18 | 19 | _CRT_BEGIN_C_HEADER 20 | 21 | // File attribute constants 22 | #define _A_NORMAL 0x00 // Normal file - No read/write restrictions 23 | #define _A_RDONLY 0x01 // Read only file 24 | #define _A_HIDDEN 0x02 // Hidden file 25 | #define _A_SYSTEM 0x04 // System file 26 | #define _A_SUBDIR 0x10 // Subdirectory 27 | #define _A_ARCH 0x20 // Archive file 28 | 29 | #ifdef _CRT_USE_WINAPI_FAMILY_DESKTOP_APP 30 | 31 | #ifndef _DISKFREE_T_DEFINED 32 | #define _DISKFREE_T_DEFINED 33 | 34 | struct _diskfree_t 35 | { 36 | unsigned total_clusters; 37 | unsigned avail_clusters; 38 | unsigned sectors_per_cluster; 39 | unsigned bytes_per_sector; 40 | }; 41 | #endif 42 | 43 | #if _CRT_FUNCTIONS_REQUIRED 44 | _Success_(return == 0) 45 | _Check_return_ 46 | _DCRTIMP unsigned __cdecl _getdiskfree( 47 | _In_ unsigned _Drive, 48 | _Out_ struct _diskfree_t* _DiskFree 49 | ); 50 | #endif 51 | 52 | #if defined(_CRT_INTERNAL_NONSTDC_NAMES) && _CRT_INTERNAL_NONSTDC_NAMES 53 | #define diskfree_t _diskfree_t 54 | #endif 55 | 56 | #endif 57 | 58 | _CRT_END_C_HEADER 59 | _UCRT_RESTORE_CLANG_WARNINGS 60 | #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 61 | #endif // _INC_DOS 62 | --------------------------------------------------------------------------------