23 errno_t fopen_s(FILE **pfile,
const char *filename,
const char *mode)
25 if ((pfile ==
nullptr) || (filename ==
nullptr) || (mode ==
nullptr)) {
29 FILE *file = fopen(filename, mode);
30 if (file ==
nullptr) {
38 errno_t localtime_s(
struct tm *_tm,
const time_t *time)
42 if ((_tm ==
nullptr) || (time ==
nullptr) || (*time == 0)) {
57 struct tm *curTime = localtime(time);
63 errno_t mbstowcs_s(
size_t *pReturnValue,
wchar_t *wcstr,
size_t sizeInWords,
const char *mbstr,
size_t count)
65 if ((mbstr ==
nullptr) || ((wcstr ==
nullptr) && (sizeInWords > 0)) || ((wcstr !=
nullptr) && (sizeInWords != 0))) {
66 if (wcstr !=
nullptr) {
72 size_t numMaxChars = sizeInWords;
73 if (count != _TRUNCATE) {
74 numMaxChars = std::min(numMaxChars, count);
77 size_t numCharsWritten = mbstowcs(wcstr, mbstr, numMaxChars);
78 if (numCharsWritten == (
size_t)-1) {
80 if (pReturnValue !=
nullptr) {
83 if (wcstr !=
nullptr) {
89 if (numCharsWritten == numMaxChars) {
90 if (wcstr !=
nullptr) {
96 if (pReturnValue !=
nullptr) {
97 *pReturnValue = numCharsWritten + 1;
101 if (wcstr !=
nullptr) {
102 wcstr[numCharsWritten] = L
'\0';
108 int sprintf_s(
char *buffer,
size_t sizeOfBuffer,
const char *format, ...)
110 if ((buffer ==
nullptr) || (sizeOfBuffer == 0) || (format ==
nullptr)) {
115 va_start(arglist, format);
116 int numCharsWritten = vsnprintf(buffer, sizeOfBuffer, format, arglist);
119 if (numCharsWritten == -1) {
124 if (numCharsWritten >= sizeOfBuffer) {
128 return numCharsWritten;
131 errno_t strcat_s(
char *strDestination,
size_t numberOfElements,
const char *strSource)
133 if ((strDestination ==
nullptr) || (strSource ==
nullptr)) {
134 if (strDestination !=
nullptr) {
135 strDestination[0] =
'\0';
140 if (numberOfElements == 0) {
141 strDestination[0] =
'\0';
145 const size_t destLen = strlen(strDestination);
146 const size_t sourceLen = strlen(strSource);
147 if ((destLen > numberOfElements - 1) || ((sourceLen > 0) && (destLen == numberOfElements - 1)) || (sourceLen > numberOfElements - destLen - 1)) {
148 strDestination[0] =
'\0';
152 (void)strcat(strDestination, strSource);
156 errno_t strcpy_s(
char* strDestination,
size_t numberOfElements,
const char *strSource)
158 if ((strDestination ==
nullptr) || (strSource ==
nullptr)) {
159 if (strDestination !=
nullptr) {
160 strDestination[0] =
'\0';
165 if ((numberOfElements == 0) || (strlen(strSource) >= numberOfElements)) {
166 strDestination[0] =
'\0';
170 (void)strcpy(strDestination, strSource);
174 errno_t strerror_s(
char* buffer,
size_t numberOfElements,
int errnum)
177 if ((buffer ==
nullptr) || (numberOfElements == 0)) {
181 const char *errorMessage = strerror(errnum);
182 return strcpy_s(buffer, numberOfElements, errorMessage);
185 errno_t strncat_s(
char *strDest,
size_t numberOfElements,
const char *strSource,
size_t count)
188 if ((strDest ==
nullptr) || (strSource ==
nullptr)) {
192 if (numberOfElements == 0) {
197 const size_t sourceLen = strlen(strSource);
198 if (count == _TRUNCATE) {
199 charsToWrite = sourceLen;
202 charsToWrite = std::min(count, sourceLen);
205 const size_t destLen = strlen(strDest);
206 const size_t sizeLeft = numberOfElements - destLen;
208 if (((count != _TRUNCATE) && (charsToWrite > sizeLeft - 1)) || ((sourceLen > 0) && (destLen == numberOfElements - 1))) {
213 (void)strncat(strDest, strSource, charsToWrite);
217 errno_t strncpy_s(
char *strDest,
size_t numberOfElements,
const char *strSource,
size_t count)
220 if ((numberOfElements == 0) || (strDest ==
nullptr) || (strSource ==
nullptr)) {
221 if (strDest !=
nullptr) {
228 bool truncated =
false;
229 if (count == _TRUNCATE) {
231 numChars = strlen(strSource);
234 if (numChars >= numberOfElements) {
235 numChars = numberOfElements - 1;
241 if ((count > numberOfElements) || ((count == numberOfElements) && (strSource[count] !=
'\0'))) {
248 (void)strncpy(strDest, strSource, numChars);
251 strDest[numChars] =
'\0';
253 return truncated ? 80 : 0;
256 int vsnprintf_s(
char *buffer,
size_t sizeOfBuffer,
size_t count,
const char *format, va_list argptr)
258 if ((buffer ==
nullptr) || (format ==
nullptr) || (sizeOfBuffer == 0)) {
262 size_t maxChars = sizeOfBuffer;
263 if (count != _TRUNCATE) {
264 if (count >= sizeOfBuffer) {
271 int numCharsWritten = vsnprintf(buffer, maxChars, format, argptr);
272 if (numCharsWritten >= maxChars) {
273 if (count != _TRUNCATE) {
279 buffer[sizeOfBuffer] =
'\0';
282 return numCharsWritten;
285 errno_t wcscat_s(
wchar_t *strDestination,
size_t numberOfElements,
const wchar_t *strSource)
287 if ((strDestination ==
nullptr) || (strSource ==
nullptr)) {
288 if (strDestination !=
nullptr) {
289 strDestination[0] = L
'\0';
294 if (numberOfElements == 0) {
295 strDestination[0] = L
'\0';
299 const size_t destLen = wcslen(strDestination);
300 const size_t sourceLen = wcslen(strSource);
301 if ((destLen > numberOfElements - 1) || ((sourceLen > 0) && (destLen == numberOfElements)) || (sourceLen > numberOfElements - destLen - 1)) {
302 strDestination[0] = L
'\0';
306 (void)wcscat(strDestination, strSource);
310 errno_t wcscpy_s(
wchar_t* strDestination,
size_t numberOfElements,
const wchar_t *strSource)
312 if ((strDestination ==
nullptr) || (strSource ==
nullptr)) {
313 if (strDestination !=
nullptr) {
314 strDestination[0] = L
'\0';
319 if ((numberOfElements == 0) || (wcslen(strSource) >= numberOfElements)) {
320 strDestination[0] = L
'\0';
324 (void)wcscpy(strDestination, strSource);