2 * msvcrt.dll wide-char functions
4 * Copyright 1999 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 static BOOL n_format_enabled = TRUE;
43 /* _get_printf_count_output - not exported in native msvcrt */
44 int CDECL MSVCRT__get_printf_count_output( void )
46 return n_format_enabled ? 1 : 0;
49 /* _set_printf_count_output - not exported in native msvcrt */
50 int CDECL MSVCRT__set_printf_count_output( int enable )
52 BOOL old = n_format_enabled;
53 n_format_enabled = enable != 0;
57 /*********************************************************************
60 MSVCRT_wchar_t* CDECL MSVCRT__wcsdup( const MSVCRT_wchar_t* str )
62 MSVCRT_wchar_t* ret = NULL;
65 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
66 ret = MSVCRT_malloc( size );
67 if (ret) memcpy( ret, str, size );
72 /*********************************************************************
73 * _wcsicoll (MSVCRT.@)
75 INT CDECL MSVCRT__wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
77 return CompareStringW(get_locinfo()->lc_handle[MSVCRT_LC_COLLATE],
78 NORM_IGNORECASE, str1, -1, str2, -1)-CSTR_EQUAL;
81 /*********************************************************************
82 * _wcsnicoll (MSVCRT.@)
84 INT CDECL MSVCRT__wcsnicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count )
86 return CompareStringW(get_locinfo()->lc_handle[MSVCRT_LC_COLLATE],
87 NORM_IGNORECASE, str1, count, str2, count)-CSTR_EQUAL;
90 /*********************************************************************
93 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
95 MSVCRT_wchar_t* ret = str;
96 while ((n-- > 0) && *str) *str++ = c;
100 /*********************************************************************
103 MSVCRT_wchar_t* CDECL MSVCRT__wcsrev( MSVCRT_wchar_t* str )
105 MSVCRT_wchar_t* ret = str;
106 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
109 MSVCRT_wchar_t t = *end;
116 /*********************************************************************
119 MSVCRT_wchar_t* CDECL MSVCRT__wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
121 MSVCRT_wchar_t* ret = str;
122 while (*str) *str++ = c;
126 /******************************************************************
127 * _wcsupr_s_l (MSVCRT.@)
129 int CDECL MSVCRT__wcsupr_s_l( MSVCRT_wchar_t* str, MSVCRT_size_t n,
130 MSVCRT__locale_t locale )
132 MSVCRT_wchar_t* ptr = str;
136 if (str) *str = '\0';
137 *MSVCRT__errno() = MSVCRT_EINVAL;
138 return MSVCRT_EINVAL;
144 /* FIXME: add locale support */
145 *ptr = toupperW(*ptr);
149 /* MSDN claims that the function should return and set errno to
150 * ERANGE, which doesn't seem to be true based on the tests. */
152 *MSVCRT__errno() = MSVCRT_EINVAL;
153 return MSVCRT_EINVAL;
156 /******************************************************************
157 * _wcsupr_s (MSVCRT.@)
160 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
162 return MSVCRT__wcsupr_s_l( str, n, NULL );
165 /******************************************************************
166 * _wcslwr_s (MSVCRT.@)
168 int CDECL MSVCRT__wcslwr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
170 MSVCRT_wchar_t* ptr = str;
174 if (str) *str = '\0';
175 *MSVCRT__errno() = MSVCRT_EINVAL;
176 return MSVCRT_EINVAL;
182 *ptr = tolowerW(*ptr);
186 /* MSDN claims that the function should return and set errno to
187 * ERANGE, which doesn't seem to be true based on the tests. */
189 *MSVCRT__errno() = MSVCRT_EINVAL;
190 return MSVCRT_EINVAL;
193 /*********************************************************************
194 * _wcstod_l - not exported in native msvcrt
196 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
197 MSVCRT__locale_t locale)
199 MSVCRT_pthreadlocinfo locinfo;
200 unsigned __int64 d=0, hlp;
203 const MSVCRT_wchar_t *p;
205 long double lret=1, expcnt = 10;
206 BOOL found_digit = FALSE, negexp;
208 if (!MSVCRT_CHECK_PMT(str != NULL)) return 0;
211 locinfo = get_locinfo();
213 locinfo = locale->locinfo;
225 while(isdigitW(*p)) {
227 hlp = d*10+*(p++)-'0';
228 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
234 while(isdigitW(*p)) {
238 if(*p == *locinfo->lconv->decimal_point)
241 while(isdigitW(*p)) {
243 hlp = d*10+*(p++)-'0';
244 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
255 *end = (MSVCRT_wchar_t*)str;
259 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
270 while(isdigitW(*p)) {
271 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
277 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
278 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
281 if(*p=='-' || *p=='+')
287 fpcontrol = _control87(0, 0);
288 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
289 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
298 expcnt = expcnt*expcnt;
300 ret = (long double)sign * (negexp ? d/lret : d*lret);
302 _control87(fpcontrol, 0xffffffff);
304 if((d && ret==0.0) || isinf(ret))
305 *MSVCRT__errno() = MSVCRT_ERANGE;
308 *end = (MSVCRT_wchar_t*)p;
313 /*********************************************************************
314 * wcsrtombs_l (INTERNAL)
316 static MSVCRT_size_t CDECL MSVCRT_wcsrtombs_l(char *mbstr, const MSVCRT_wchar_t **wcstr,
317 MSVCRT_size_t count, MSVCRT__locale_t locale)
319 MSVCRT_pthreadlocinfo locinfo;
320 MSVCRT_size_t tmp = 0;
324 locinfo = get_locinfo();
326 locinfo = locale->locinfo;
329 tmp = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
330 *wcstr, -1, NULL, 0, NULL, &used_default)-1;
338 MSVCRT_size_t i, size;
340 size = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
341 *wcstr, 1, buf, 3, NULL, &used_default);
347 for(i=0; i<size; i++)
348 mbstr[tmp++] = buf[i];
359 /*********************************************************************
360 * _wcstombs_l (MSVCRT.@)
362 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
363 MSVCRT_size_t count, MSVCRT__locale_t locale)
365 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, locale);
368 /*********************************************************************
369 * wcstombs (MSVCRT.@)
371 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
374 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, NULL);
377 /*********************************************************************
378 * wcsrtombs (MSVCRT.@)
380 MSVCRT_size_t CDECL MSVCRT_wcsrtombs(char *mbstr, const MSVCRT_wchar_t **wcstr,
381 MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
386 return MSVCRT_wcsrtombs_l(mbstr, wcstr, count, NULL);
389 /*********************************************************************
390 * MSVCRT_wcsrtombs_s_l (INTERNAL)
392 static int MSVCRT_wcsrtombs_s_l(MSVCRT_size_t *ret, char *mbstr,
393 MSVCRT_size_t size, const MSVCRT_wchar_t **wcstr,
394 MSVCRT_size_t count, MSVCRT__locale_t locale)
398 if(!mbstr && !size && wcstr) {
399 conv = MSVCRT_wcsrtombs_l(NULL, wcstr, 0, locale);
405 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) return MSVCRT_EINVAL;
406 if (size) mbstr[0] = '\0';
407 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
408 if (!MSVCRT_CHECK_PMT(*wcstr != NULL)) return MSVCRT_EINVAL;
410 if(count==MSVCRT__TRUNCATE || size<count)
415 conv = MSVCRT_wcsrtombs_l(mbstr, wcstr, conv, locale);
417 mbstr[conv++] = '\0';
418 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
419 mbstr[conv-1] = '\0';
421 MSVCRT_INVALID_PMT("mbstr[size] is too small", MSVCRT_ERANGE);
424 return MSVCRT_ERANGE;
432 /*********************************************************************
433 * _wcstombs_s_l (MSVCRT.@)
435 int CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
436 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
437 MSVCRT_size_t count, MSVCRT__locale_t locale)
439 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr,count, locale);
442 /*********************************************************************
443 * wcstombs_s (MSVCRT.@)
445 int CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
446 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
448 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr, count, NULL);
451 /*********************************************************************
452 * wcsrtombs_s (MSVCRT.@)
454 int CDECL MSVCRT_wcsrtombs_s(MSVCRT_size_t *ret, char *mbstr, MSVCRT_size_t size,
455 const MSVCRT_wchar_t **wcstr, MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
460 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, wcstr, count, NULL);
463 /*********************************************************************
466 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
468 return MSVCRT__wcstod_l(lpszStr, end, NULL);
471 /*********************************************************************
474 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
476 return MSVCRT__wcstod_l(str, NULL, NULL);
479 /*********************************************************************
482 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
484 return MSVCRT__wcstod_l(str, NULL, locale);
487 /*********************************************************************
488 * arg_clbk_valist (INTERNAL)
490 printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valist)
495 ret.get_longlong = va_arg(*valist, LONGLONG);
496 else if(type == VT_INT)
497 ret.get_int = va_arg(*valist, int);
498 else if(type == VT_R8)
499 ret.get_double = va_arg(*valist, double);
500 else if(type == VT_PTR)
501 ret.get_ptr = va_arg(*valist, void*);
503 ERR("Incorrect type\n");
510 /*********************************************************************
511 * arg_clbk_positional (INTERNAL)
513 static printf_arg arg_clbk_positional(void *ctx, int pos, int type, __ms_va_list *valist)
515 printf_arg *args = ctx;
519 /*********************************************************************
520 * _vsnprintf (MSVCRT.@)
522 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
523 const char *format, __ms_va_list valist )
525 static const char nullbyte = '\0';
526 struct _str_ctx_a ctx = {len, str};
529 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, FALSE, FALSE,
530 arg_clbk_valist, NULL, &valist);
531 puts_clbk_str_a(&ctx, 1, &nullbyte);
535 /*********************************************************************
536 * _vsnprintf_l (MSVCRT.@)
538 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
539 MSVCRT__locale_t locale, __ms_va_list valist )
541 static const char nullbyte = '\0';
542 struct _str_ctx_a ctx = {len, str};
545 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, FALSE,
546 arg_clbk_valist, NULL, &valist);
547 puts_clbk_str_a(&ctx, 1, &nullbyte);
551 /*********************************************************************
552 * _vsnprintf_s_l (MSVCRT.@)
554 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
555 MSVCRT_size_t count, const char *format,
556 MSVCRT__locale_t locale, __ms_va_list valist )
558 static const char nullbyte = '\0';
559 struct _str_ctx_a ctx;
562 if(sizeOfBuffer<count+1 || count==-1)
569 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, TRUE,
570 arg_clbk_valist, NULL, &valist);
571 puts_clbk_str_a(&ctx, 1, &nullbyte);
573 if(ret<0 || ret==len) {
574 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
575 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
576 memset(str, 0, sizeOfBuffer);
586 /*********************************************************************
587 * _vsnprintf_s (MSVCRT.@)
589 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
590 MSVCRT_size_t count, const char *format, __ms_va_list valist )
592 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
595 /*********************************************************************
596 * vsprintf (MSVCRT.@)
598 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
600 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
603 /*********************************************************************
604 * vsprintf_s (MSVCRT.@)
606 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
608 return MSVCRT_vsnprintf(str, num, format, valist);
611 /*********************************************************************
612 * _vscprintf (MSVCRT.@)
614 int CDECL MSVCRT__vscprintf( const char *format, __ms_va_list valist )
616 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
619 /*********************************************************************
620 * _snprintf (MSVCRT.@)
622 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
626 __ms_va_start(valist, format);
627 retval = MSVCRT_vsnprintf(str, len, format, valist);
632 /*********************************************************************
633 * _snprintf_s (MSVCRT.@)
635 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
636 const char *format, ...)
640 __ms_va_start(valist, format);
641 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
646 /*********************************************************************
647 * _scprintf (MSVCRT.@)
649 int CDECL MSVCRT__scprintf(const char *format, ...)
653 __ms_va_start(valist, format);
654 retval = MSVCRT__vscprintf(format, valist);
659 /*********************************************************************
660 * _vsnwprintf (MSVCRT.@)
662 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
663 const MSVCRT_wchar_t *format, __ms_va_list valist)
665 static const MSVCRT_wchar_t nullbyte = '\0';
666 struct _str_ctx_w ctx = {len, str};
669 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, FALSE, FALSE,
670 arg_clbk_valist, NULL, &valist);
671 puts_clbk_str_w(&ctx, 1, &nullbyte);
675 /*********************************************************************
676 * _vsnwprintf_l (MSVCRT.@)
678 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
679 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
682 static const MSVCRT_wchar_t nullbyte = '\0';
683 struct _str_ctx_w ctx = {len, str};
686 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, FALSE,
687 arg_clbk_valist, NULL, &valist);
688 puts_clbk_str_w(&ctx, 1, &nullbyte);
692 /*********************************************************************
693 * _vswprintf_p_l (MSVCRT.@)
695 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
696 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
698 static const MSVCRT_wchar_t nullbyte = '\0';
699 printf_arg args_ctx[MSVCRT__ARGMAX+1];
700 struct _str_ctx_w puts_ctx = {length, buffer};
703 memset(args_ctx, 0, sizeof(args_ctx));
705 ret = create_positional_ctx_w(args_ctx, format, args);
707 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
708 *MSVCRT__errno() = MSVCRT_EINVAL;
711 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, FALSE, TRUE,
712 arg_clbk_valist, NULL, &args);
714 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, TRUE, TRUE,
715 arg_clbk_positional, args_ctx, NULL);
717 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
721 /*********************************************************************
722 * _vsnwprintf_s_l (MSVCRT.@)
724 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
725 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
726 MSVCRT__locale_t locale, __ms_va_list valist)
728 static const MSVCRT_wchar_t nullbyte = '\0';
729 struct _str_ctx_w ctx;
733 if(count!=-1 && len>count+1)
738 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, TRUE,
739 arg_clbk_valist, NULL, &valist);
740 puts_clbk_str_w(&ctx, 1, &nullbyte);
742 if(ret<0 || ret==len) {
743 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
744 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
745 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
755 /*********************************************************************
756 * _vsnwprintf_s (MSVCRT.@)
758 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
759 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
761 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
762 format, NULL, valist);
765 /*********************************************************************
766 * _snwprintf (MSVCRT.@)
768 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
772 __ms_va_start(valist, format);
773 retval = MSVCRT_vsnwprintf(str, len, format, valist);
778 /*********************************************************************
779 * _snwprintf_l (MSVCRT.@)
781 int CDECL MSVCRT__snwprintf_l( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format,
782 MSVCRT__locale_t locale, ...)
786 __ms_va_start(valist, locale);
787 retval = MSVCRT_vsnwprintf_l(str, len, format, locale, valist);
792 /*********************************************************************
793 * _snwprintf_s (MSVCRT.@)
795 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
796 const MSVCRT_wchar_t *format, ...)
800 __ms_va_start(valist, format);
801 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
806 /*********************************************************************
809 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
814 __ms_va_start( ap, format );
815 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
820 /*********************************************************************
821 * sprintf_s (MSVCRT.@)
823 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
828 __ms_va_start( ap, format );
829 r = MSVCRT_vsnprintf( str, num, format, ap );
834 /*********************************************************************
835 * _scwprintf (MSVCRT.@)
837 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
842 __ms_va_start( ap, format );
843 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
848 /*********************************************************************
849 * swprintf (MSVCRT.@)
851 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
856 __ms_va_start( ap, format );
857 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
862 /*********************************************************************
863 * swprintf_s (MSVCRT.@)
865 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
866 const MSVCRT_wchar_t *format, ... )
871 __ms_va_start(ap, format);
872 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
878 /*********************************************************************
879 * _vswprintf (MSVCRT.@)
881 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
883 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
886 /*********************************************************************
887 * _vswprintf (MSVCRT.@)
889 int CDECL MSVCRT_vswprintf_l( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format,
890 MSVCRT__locale_t locale, __ms_va_list args )
892 return MSVCRT_vsnwprintf_l( str, INT_MAX, format, locale, args );
895 /*********************************************************************
896 * _vscwprintf (MSVCRT.@)
898 int CDECL MSVCRT__vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
900 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
903 /*********************************************************************
904 * _vscwprintf_l (MSVCRT.@)
906 int CDECL MSVCRT__vscwprintf_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
908 return MSVCRT_vsnwprintf_l( NULL, INT_MAX, format, locale, args );
911 /*********************************************************************
912 * _vscwprintf_p_l (MSVCRT.@)
914 int CDECL MSVCRT__vscwprintf_p_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
916 return MSVCRT_vswprintf_p_l( NULL, INT_MAX, format, locale, args );
919 /*********************************************************************
920 * vswprintf_s (MSVCRT.@)
922 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
923 const MSVCRT_wchar_t* format, __ms_va_list args)
925 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
928 /*********************************************************************
929 * _vswprintf_s_l (MSVCRT.@)
931 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
932 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
934 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
935 format, locale, args );
938 /*********************************************************************
939 * _vsprintf_p_l (MSVCRT.@)
941 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
942 MSVCRT__locale_t locale, __ms_va_list args)
944 static const char nullbyte = '\0';
945 printf_arg args_ctx[MSVCRT__ARGMAX+1];
946 struct _str_ctx_a puts_ctx = {length, buffer};
949 memset(args_ctx, 0, sizeof(args_ctx));
951 ret = create_positional_ctx_a(args_ctx, format, args);
953 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
954 *MSVCRT__errno() = MSVCRT_EINVAL;
957 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, FALSE, TRUE,
958 arg_clbk_valist, NULL, &args);
960 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, TRUE, TRUE,
961 arg_clbk_positional, args_ctx, NULL);
963 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
967 /*********************************************************************
968 * _vsprintf_p (MSVCRT.@)
970 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
971 const char *format, __ms_va_list args)
973 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
976 /*********************************************************************
977 * _sprintf_p_l (MSVCRT.@)
979 int CDECL MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
980 const char *format, MSVCRT__locale_t locale, ...)
985 __ms_va_start(valist, locale);
986 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
992 /*********************************************************************
993 * _swprintf_p_l (MSVCRT.@)
995 int CDECL MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
996 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
1001 __ms_va_start(valist, locale);
1002 r = MSVCRT_vswprintf_p_l(buffer, length, format, locale, valist);
1003 __ms_va_end(valist);
1008 /*********************************************************************
1009 * wcscoll (MSVCRT.@)
1011 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1013 return CompareStringW(get_locinfo()->lc_handle[MSVCRT_LC_COLLATE],
1014 0, str1, -1, str2, -1)-CSTR_EQUAL;
1017 /*********************************************************************
1018 * wcspbrk (MSVCRT.@)
1020 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1022 const MSVCRT_wchar_t* p;
1026 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1032 /*********************************************************************
1033 * wcstok_s (MSVCRT.@)
1035 MSVCRT_wchar_t * CDECL wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1036 MSVCRT_wchar_t **next_token )
1038 MSVCRT_wchar_t *ret;
1040 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1041 if (!MSVCRT_CHECK_PMT(next_token != NULL)) return NULL;
1042 if (!MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) return NULL;
1044 if (!str) str = *next_token;
1046 while (*str && strchrW( delim, *str )) str++;
1047 if (!*str) return NULL;
1049 while (*str && !strchrW( delim, *str )) str++;
1050 if (*str) *str++ = 0;
1055 /*********************************************************************
1058 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1060 return wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1063 /*********************************************************************
1066 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1070 UINT codepage = get_locinfo()->lc_codepage;
1074 return (signed char)wchar;
1077 } else if(WideCharToMultiByte( codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1082 /*********************************************************************
1085 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1090 size = WideCharToMultiByte(get_locinfo()->lc_codepage, 0, &ch, 1, dst, dst ? 6 : 0, NULL, &error);
1091 if(!size || error) {
1092 *MSVCRT__errno() = MSVCRT_EINVAL;
1098 /*********************************************************************
1099 * wcrtomb (MSVCRT.@)
1101 MSVCRT_size_t CDECL MSVCRT_wcrtomb( char *dst, MSVCRT_wchar_t ch, MSVCRT_mbstate_t *s)
1105 return MSVCRT_wctomb(dst, ch);
1108 /*********************************************************************
1109 * iswalnum (MSVCRT.@)
1111 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1113 return isalnumW( wc );
1116 /*********************************************************************
1117 * iswalpha (MSVCRT.@)
1119 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1121 return isalphaW( wc );
1124 /*********************************************************************
1125 * iswalpha_l (MSVCRT.@)
1127 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1129 return isalphaW( wc );
1132 /*********************************************************************
1133 * iswcntrl (MSVCRT.@)
1135 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1137 return iscntrlW( wc );
1140 /*********************************************************************
1141 * iswdigit (MSVCRT.@)
1143 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1145 return isdigitW( wc );
1148 /*********************************************************************
1149 * _iswdigit_l (MSVCRT.@)
1151 INT CDECL MSVCRT__iswdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1153 return isdigitW( wc );
1156 /*********************************************************************
1157 * iswgraph (MSVCRT.@)
1159 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1161 return isgraphW( wc );
1164 /*********************************************************************
1165 * iswlower (MSVCRT.@)
1167 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1169 return islowerW( wc );
1172 /*********************************************************************
1173 * iswprint (MSVCRT.@)
1175 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1177 return isprintW( wc );
1180 /*********************************************************************
1181 * iswpunct (MSVCRT.@)
1183 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1185 return ispunctW( wc );
1188 /*********************************************************************
1189 * iswspace (MSVCRT.@)
1191 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1193 return isspaceW( wc );
1196 /*********************************************************************
1197 * iswupper (MSVCRT.@)
1199 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1201 return isupperW( wc );
1204 /*********************************************************************
1205 * iswxdigit (MSVCRT.@)
1207 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1209 return isxdigitW( wc );
1212 /*********************************************************************
1213 * wcscpy_s (MSVCRT.@)
1215 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1217 MSVCRT_size_t size = 0;
1219 if(!wcDest || !numElement)
1220 return MSVCRT_EINVAL;
1226 return MSVCRT_EINVAL;
1229 size = strlenW(wcSrc) + 1;
1231 if(size > numElement)
1233 return MSVCRT_ERANGE;
1236 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1241 /******************************************************************
1242 * wcsncpy_s (MSVCRT.@)
1244 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1245 MSVCRT_size_t count )
1248 BOOL truncate = (count == MSVCRT__TRUNCATE);
1250 if(!wcDest && !numElement && !count)
1253 if (!wcDest || !numElement)
1254 return MSVCRT_EINVAL;
1259 return count ? MSVCRT_EINVAL : 0;
1262 while (numElement && count && *wcSrc)
1268 if (!numElement && truncate)
1271 return MSVCRT_STRUNCATE;
1273 else if (!numElement)
1276 return MSVCRT_ERANGE;
1283 /******************************************************************
1284 * wcscat_s (MSVCRT.@)
1287 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1289 MSVCRT_wchar_t* ptr = dst;
1291 if (!dst || elem == 0) return MSVCRT_EINVAL;
1295 return MSVCRT_EINVAL;
1298 /* seek to end of dst string (or elem if no end of string is found */
1299 while (ptr < dst + elem && *ptr != '\0') ptr++;
1300 while (ptr < dst + elem)
1302 if ((*ptr++ = *src++) == '\0') return 0;
1304 /* not enough space */
1306 return MSVCRT_ERANGE;
1309 /*********************************************************************
1310 * wcsncat_s (MSVCRT.@)
1313 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1314 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1316 MSVCRT_size_t srclen;
1317 MSVCRT_wchar_t dststart;
1320 if (!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
1321 if (!MSVCRT_CHECK_PMT(elem > 0)) return MSVCRT_EINVAL;
1322 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return MSVCRT_EINVAL;
1327 for (dststart = 0; dststart < elem; dststart++)
1329 if (dst[dststart] == '\0')
1332 if (dststart == elem)
1334 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", MSVCRT_EINVAL);
1335 return MSVCRT_EINVAL;
1338 if (count == MSVCRT__TRUNCATE)
1340 srclen = strlenW(src);
1341 if (srclen >= (elem - dststart))
1343 srclen = elem - dststart - 1;
1344 ret = MSVCRT_STRUNCATE;
1348 srclen = min(strlenW(src), count);
1349 if (srclen < (elem - dststart))
1351 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1352 dst[dststart+srclen] = '\0';
1355 MSVCRT_INVALID_PMT("dst[elem] is too small", MSVCRT_ERANGE);
1357 return MSVCRT_ERANGE;
1360 /*********************************************************************
1361 * _wcstoi64_l (MSVCRT.@)
1363 * FIXME: locale parameter is ignored
1365 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1366 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1368 BOOL negative = FALSE;
1371 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1373 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1374 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1375 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1377 while(isspaceW(*nptr)) nptr++;
1382 } else if(*nptr == '+')
1385 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1398 char cur = tolowerW(*nptr);
1406 if(cur<'a' || cur>='a'+base-10)
1416 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1417 ret = MSVCRT_I64_MAX;
1418 *MSVCRT__errno() = MSVCRT_ERANGE;
1419 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1420 ret = MSVCRT_I64_MIN;
1421 *MSVCRT__errno() = MSVCRT_ERANGE;
1427 *endptr = (MSVCRT_wchar_t*)nptr;
1432 /*********************************************************************
1433 * _wcstoi64 (MSVCRT.@)
1435 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1436 MSVCRT_wchar_t **endptr, int base)
1438 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1441 /*********************************************************************
1442 * _wtoi_l (MSVCRT.@)
1444 int MSVCRT__wtoi_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
1446 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
1450 *MSVCRT__errno() = MSVCRT_ERANGE;
1451 } else if(ret < INT_MIN) {
1453 *MSVCRT__errno() = MSVCRT_ERANGE;
1458 /*********************************************************************
1459 * _wcstoui64_l (MSVCRT.@)
1461 * FIXME: locale parameter is ignored
1463 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1464 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1466 BOOL negative = FALSE;
1467 unsigned __int64 ret = 0;
1469 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1471 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1472 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1473 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1475 while(isspaceW(*nptr)) nptr++;
1480 } else if(*nptr == '+')
1483 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1496 char cur = tolowerW(*nptr);
1504 if(cur<'a' || cur>='a'+base-10)
1511 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1512 ret = MSVCRT_UI64_MAX;
1513 *MSVCRT__errno() = MSVCRT_ERANGE;
1519 *endptr = (MSVCRT_wchar_t*)nptr;
1521 return negative ? -ret : ret;
1524 /*********************************************************************
1525 * _wcstoui64 (MSVCRT.@)
1527 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1528 MSVCRT_wchar_t **endptr, int base)
1530 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
1533 /******************************************************************
1534 * wcsnlen (MSVCRT.@)
1536 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
1540 for (i = 0; i < maxlen; i++)
1545 /*********************************************************************
1546 * _towupper_l (MSVCRT.@)
1548 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
1553 /*********************************************************************
1554 * _towlower_l (MSVCRT.@)
1556 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)