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 _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 _set_printf_count_output( int enable )
52 BOOL old = n_format_enabled;
53 n_format_enabled = (enable ? TRUE : FALSE);
57 /*********************************************************************
60 MSVCRT_wchar_t* CDECL _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 _wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
77 /* FIXME: handle collates */
78 return strcmpiW( str1, str2 );
81 /*********************************************************************
82 * _wcsnicoll (MSVCRT.@)
84 INT CDECL _wcsnicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count )
86 /* FIXME: handle collates */
87 return strncmpiW( str1, str2, count );
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 _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 _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 BOOL found_digit = FALSE;
207 if (!MSVCRT_CHECK_PMT(str != NULL)) {
208 *MSVCRT__errno() = MSVCRT_EINVAL;
213 locinfo = get_locinfo();
215 locinfo = locale->locinfo;
227 while(isdigitW(*p)) {
229 hlp = d*10+*(p++)-'0';
230 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
236 while(isdigitW(*p)) {
240 if(*p == *locinfo->lconv->decimal_point)
243 while(isdigitW(*p)) {
245 hlp = d*10+*(p++)-'0';
246 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
257 *end = (MSVCRT_wchar_t*)str;
261 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
272 while(isdigitW(*p)) {
273 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
279 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
280 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
283 if(*p=='-' || *p=='+')
289 fpcontrol = _control87(0, 0);
290 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
291 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
294 ret = (double)sign*d*pow(10, exp);
296 ret = (double)sign*d/pow(10, -exp);
298 _control87(fpcontrol, 0xffffffff);
300 if((d && ret==0.0) || isinf(ret))
301 *MSVCRT__errno() = MSVCRT_ERANGE;
304 *end = (MSVCRT_wchar_t*)p;
309 /*********************************************************************
310 * wcsrtombs_l (INTERNAL)
312 static MSVCRT_size_t CDECL MSVCRT_wcsrtombs_l(char *mbstr, const MSVCRT_wchar_t **wcstr,
313 MSVCRT_size_t count, MSVCRT__locale_t locale)
315 MSVCRT_pthreadlocinfo locinfo;
316 char default_char = '\0';
317 MSVCRT_size_t tmp = 0;
321 locinfo = get_locinfo();
323 locinfo = locale->locinfo;
326 return WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
327 *wcstr, -1, NULL, 0, &default_char, &used_default)-1;
331 MSVCRT_size_t i, size;
333 size = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
334 *wcstr, 1, buf, 3, &default_char, &used_default);
340 for(i=0; i<size; i++)
341 mbstr[tmp++] = buf[i];
352 /*********************************************************************
353 * _wcstombs_l (MSVCRT.@)
355 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
356 MSVCRT_size_t count, MSVCRT__locale_t locale)
358 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, locale);
361 /*********************************************************************
362 * wcstombs (MSVCRT.@)
364 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
367 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, NULL);
370 /*********************************************************************
371 * wcsrtombs (MSVCRT.@)
373 MSVCRT_size_t CDECL MSVCRT_wcsrtombs(char *mbstr, const MSVCRT_wchar_t **wcstr,
374 MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
379 return MSVCRT_wcsrtombs_l(mbstr, wcstr, count, NULL);
382 /*********************************************************************
383 * MSVCRT_wcsrtombs_s_l (INTERNAL)
385 static int MSVCRT_wcsrtombs_s_l(MSVCRT_size_t *ret, char *mbstr,
386 MSVCRT_size_t size, const MSVCRT_wchar_t **wcstr,
387 MSVCRT_size_t count, MSVCRT__locale_t locale)
391 if(!mbstr && !size && wcstr) {
392 conv = MSVCRT_wcsrtombs_l(NULL, wcstr, 0, locale);
398 if (!MSVCRT_CHECK_PMT(wcstr != NULL) || !MSVCRT_CHECK_PMT(*wcstr != NULL)
399 || !MSVCRT_CHECK_PMT(mbstr != NULL)) {
402 *MSVCRT__errno() = MSVCRT_EINVAL;
403 return MSVCRT_EINVAL;
406 if(count==MSVCRT__TRUNCATE || size<count)
411 conv = MSVCRT_wcsrtombs_l(mbstr, wcstr, conv, locale);
413 mbstr[conv++] = '\0';
414 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
415 mbstr[conv-1] = '\0';
417 MSVCRT_INVALID_PMT("mbstr[size] is too small");
420 *MSVCRT__errno() = MSVCRT_ERANGE;
421 return MSVCRT_ERANGE;
429 /*********************************************************************
430 * _wcstombs_s_l (MSVCRT.@)
432 int CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
433 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
434 MSVCRT_size_t count, MSVCRT__locale_t locale)
436 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr,count, locale);
439 /*********************************************************************
440 * wcstombs_s (MSVCRT.@)
442 int CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
443 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
445 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr, count, NULL);
448 /*********************************************************************
449 * wcsrtombs_s (MSVCRT.@)
451 int CDECL MSVCRT_wcsrtombs_s(MSVCRT_size_t *ret, char *mbstr, MSVCRT_size_t size,
452 const MSVCRT_wchar_t **wcstr, MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
457 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, wcstr, count, NULL);
460 /*********************************************************************
463 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
465 return MSVCRT__wcstod_l(lpszStr, end, NULL);
468 /*********************************************************************
471 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
473 return MSVCRT__wcstod_l(str, NULL, NULL);
476 /*********************************************************************
479 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
481 return MSVCRT__wcstod_l(str, NULL, locale);
484 /*********************************************************************
485 * arg_clbk_valist (INTERNAL)
487 printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valist)
492 ret.get_longlong = va_arg(*valist, LONGLONG);
493 else if(type == VT_INT)
494 ret.get_int = va_arg(*valist, int);
495 else if(type == VT_R8)
496 ret.get_double = va_arg(*valist, double);
497 else if(type == VT_PTR)
498 ret.get_ptr = va_arg(*valist, void*);
500 ERR("Incorrect type\n");
507 /*********************************************************************
508 * arg_clbk_positional (INTERNAL)
510 static printf_arg arg_clbk_positional(void *ctx, int pos, int type, __ms_va_list *valist)
512 printf_arg *args = ctx;
516 /*********************************************************************
517 * _vsnprintf (MSVCRT.@)
519 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
520 const char *format, __ms_va_list valist )
522 static const char nullbyte = '\0';
523 struct _str_ctx_a ctx = {len, str};
526 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, FALSE, FALSE,
527 arg_clbk_valist, NULL, &valist);
528 puts_clbk_str_a(&ctx, 1, &nullbyte);
532 /*********************************************************************
533 * _vsnprintf_l (MSVCRT.@)
535 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
536 MSVCRT__locale_t locale, __ms_va_list valist )
538 static const char nullbyte = '\0';
539 struct _str_ctx_a ctx = {len, str};
542 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, FALSE,
543 arg_clbk_valist, NULL, &valist);
544 puts_clbk_str_a(&ctx, 1, &nullbyte);
548 /*********************************************************************
549 * _vsnprintf_s_l (MSVCRT.@)
551 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
552 MSVCRT_size_t count, const char *format,
553 MSVCRT__locale_t locale, __ms_va_list valist )
555 static const char nullbyte = '\0';
556 struct _str_ctx_a ctx;
559 if(sizeOfBuffer<count+1 || count==-1)
566 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, TRUE,
567 arg_clbk_valist, NULL, &valist);
568 puts_clbk_str_a(&ctx, 1, &nullbyte);
570 if(ret<0 || ret==len) {
571 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
572 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small");
573 *MSVCRT__errno() = MSVCRT_ERANGE;
574 memset(str, 0, sizeOfBuffer);
584 /*********************************************************************
585 * _vsnprintf_s (MSVCRT.@)
587 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
588 MSVCRT_size_t count, const char *format, __ms_va_list valist )
590 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
593 /*********************************************************************
594 * vsprintf (MSVCRT.@)
596 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
598 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
601 /*********************************************************************
602 * vsprintf_s (MSVCRT.@)
604 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
606 return MSVCRT_vsnprintf(str, num, format, valist);
609 /*********************************************************************
610 * _vscprintf (MSVCRT.@)
612 int CDECL _vscprintf( const char *format, __ms_va_list valist )
614 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
617 /*********************************************************************
618 * _snprintf (MSVCRT.@)
620 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
624 __ms_va_start(valist, format);
625 retval = MSVCRT_vsnprintf(str, len, format, valist);
630 /*********************************************************************
631 * _snprintf_s (MSVCRT.@)
633 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
634 const char *format, ...)
638 __ms_va_start(valist, format);
639 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
644 /*********************************************************************
645 * _scprintf (MSVCRT.@)
647 int CDECL MSVCRT__scprintf(const char *format, ...)
651 __ms_va_start(valist, format);
652 retval = _vscprintf(format, valist);
657 /*********************************************************************
658 * _vsnwprintf (MSVCRT.@)
660 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
661 const MSVCRT_wchar_t *format, __ms_va_list valist)
663 static const MSVCRT_wchar_t nullbyte = '\0';
664 struct _str_ctx_w ctx = {len, str};
667 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, FALSE, FALSE,
668 arg_clbk_valist, NULL, &valist);
669 puts_clbk_str_w(&ctx, 1, &nullbyte);
673 /*********************************************************************
674 * _vsnwprintf_l (MSVCRT.@)
676 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
677 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
680 static const MSVCRT_wchar_t nullbyte = '\0';
681 struct _str_ctx_w ctx = {len, str};
684 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, FALSE,
685 arg_clbk_valist, NULL, &valist);
686 puts_clbk_str_w(&ctx, 1, &nullbyte);
690 /*********************************************************************
691 * _vsnwprintf_s_l (MSVCRT.@)
693 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
694 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
695 MSVCRT__locale_t locale, __ms_va_list valist)
697 static const MSVCRT_wchar_t nullbyte = '\0';
698 struct _str_ctx_w ctx;
702 if(count!=-1 && len>count+1)
707 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, TRUE,
708 arg_clbk_valist, NULL, &valist);
709 puts_clbk_str_w(&ctx, 1, &nullbyte);
711 if(ret<0 || ret==len) {
712 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
713 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small");
714 *MSVCRT__errno() = MSVCRT_ERANGE;
715 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
725 /*********************************************************************
726 * _vsnwprintf_s (MSVCRT.@)
728 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
729 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
731 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
732 format, NULL, valist);
735 /*********************************************************************
736 * _snwprintf (MSVCRT.@)
738 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
742 __ms_va_start(valist, format);
743 retval = MSVCRT_vsnwprintf(str, len, format, valist);
748 /*********************************************************************
749 * _snwprintf_s (MSVCRT.@)
751 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
752 const MSVCRT_wchar_t *format, ...)
756 __ms_va_start(valist, format);
757 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
762 /*********************************************************************
765 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
770 __ms_va_start( ap, format );
771 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
776 /*********************************************************************
777 * sprintf_s (MSVCRT.@)
779 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
784 __ms_va_start( ap, format );
785 r = MSVCRT_vsnprintf( str, num, format, ap );
790 /*********************************************************************
791 * _scwprintf (MSVCRT.@)
793 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
798 __ms_va_start( ap, format );
799 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
804 /*********************************************************************
805 * swprintf (MSVCRT.@)
807 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
812 __ms_va_start( ap, format );
813 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
818 /*********************************************************************
819 * swprintf_s (MSVCRT.@)
821 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
822 const MSVCRT_wchar_t *format, ... )
827 __ms_va_start(ap, format);
828 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
834 /*********************************************************************
835 * vswprintf (MSVCRT.@)
837 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
839 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
842 /*********************************************************************
843 * _vscwprintf (MSVCRT.@)
845 int CDECL _vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
847 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
850 /*********************************************************************
851 * vswprintf_s (MSVCRT.@)
853 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
854 const MSVCRT_wchar_t* format, __ms_va_list args)
856 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
859 /*********************************************************************
860 * _vswprintf_s_l (MSVCRT.@)
862 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
863 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
865 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
866 format, locale, args );
869 /*********************************************************************
870 * _vsprintf_p_l (MSVCRT.@)
872 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
873 MSVCRT__locale_t locale, __ms_va_list args)
875 static const char nullbyte = '\0';
876 printf_arg args_ctx[MSVCRT__ARGMAX+1];
877 struct _str_ctx_a puts_ctx = {length, buffer};
880 memset(args_ctx, 0, sizeof(args_ctx));
882 ret = create_positional_ctx_a(args_ctx, format, args);
884 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
885 *MSVCRT__errno() = MSVCRT_EINVAL;
888 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, FALSE, TRUE,
889 arg_clbk_valist, NULL, &args);
891 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, TRUE, TRUE,
892 arg_clbk_positional, args_ctx, NULL);
894 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
898 /*********************************************************************
899 * _vsprintf_p (MSVCRT.@)
901 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
902 const char *format, __ms_va_list args)
904 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
907 /*********************************************************************
908 * _sprintf_p_l (MSVCRT.@)
910 int CDECL MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
911 const char *format, MSVCRT__locale_t locale, ...)
916 __ms_va_start(valist, locale);
917 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
923 /*********************************************************************
924 * _vswprintf_p_l (MSVCRT.@)
926 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
927 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
929 static const MSVCRT_wchar_t nullbyte = '\0';
930 printf_arg args_ctx[MSVCRT__ARGMAX+1];
931 struct _str_ctx_w puts_ctx = {length, buffer};
934 memset(args_ctx, 0, sizeof(args_ctx));
936 ret = create_positional_ctx_w(args_ctx, format, args);
938 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
939 *MSVCRT__errno() = MSVCRT_EINVAL;
942 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, TRUE, TRUE,
943 arg_clbk_valist, NULL, &args);
945 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, TRUE, TRUE,
946 arg_clbk_positional, args_ctx, NULL);
948 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
952 /*********************************************************************
953 * _swprintf_p_l (MSVCRT.@)
955 int CDECL MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
956 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
961 __ms_va_start(valist, locale);
962 r = MSVCRT_vswprintf_p_l(buffer, length, format, locale, valist);
968 /*********************************************************************
971 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
973 /* FIXME: handle collates */
974 return strcmpW( str1, str2 );
977 /*********************************************************************
980 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
982 const MSVCRT_wchar_t* p;
985 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
991 /*********************************************************************
992 * wcstok_s (MSVCRT.@)
994 MSVCRT_wchar_t * CDECL wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
995 MSVCRT_wchar_t **next_token )
999 if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(next_token != NULL) ||
1000 !MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL))
1002 *MSVCRT__errno() = MSVCRT_EINVAL;
1005 if (!str) str = *next_token;
1007 while (*str && strchrW( delim, *str )) str++;
1008 if (!*str) return NULL;
1010 while (*str && !strchrW( delim, *str )) str++;
1011 if (*str) *str++ = 0;
1016 /*********************************************************************
1019 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1021 return wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1024 /*********************************************************************
1027 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1032 if(WideCharToMultiByte( get_locinfo()->lc_codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1037 /*********************************************************************
1040 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1042 return WideCharToMultiByte( get_locinfo()->lc_codepage, 0, &ch, 1, dst, 6, NULL, NULL );
1045 /*********************************************************************
1046 * iswalnum (MSVCRT.@)
1048 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1050 return isalnumW( wc );
1053 /*********************************************************************
1054 * iswalpha (MSVCRT.@)
1056 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1058 return isalphaW( wc );
1061 /*********************************************************************
1062 * iswalpha_l (MSVCRT.@)
1064 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1066 return isalphaW( wc );
1069 /*********************************************************************
1070 * iswcntrl (MSVCRT.@)
1072 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1074 return iscntrlW( wc );
1077 /*********************************************************************
1078 * iswdigit (MSVCRT.@)
1080 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1082 return isdigitW( wc );
1085 /*********************************************************************
1086 * iswgraph (MSVCRT.@)
1088 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1090 return isgraphW( wc );
1093 /*********************************************************************
1094 * iswlower (MSVCRT.@)
1096 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1098 return islowerW( wc );
1101 /*********************************************************************
1102 * iswprint (MSVCRT.@)
1104 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1106 return isprintW( wc );
1109 /*********************************************************************
1110 * iswpunct (MSVCRT.@)
1112 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1114 return ispunctW( wc );
1117 /*********************************************************************
1118 * iswspace (MSVCRT.@)
1120 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1122 return isspaceW( wc );
1125 /*********************************************************************
1126 * iswupper (MSVCRT.@)
1128 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1130 return isupperW( wc );
1133 /*********************************************************************
1134 * iswxdigit (MSVCRT.@)
1136 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1138 return isxdigitW( wc );
1141 /*********************************************************************
1142 * wcscpy_s (MSVCRT.@)
1144 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1146 MSVCRT_size_t size = 0;
1148 if(!wcDest || !numElement)
1149 return MSVCRT_EINVAL;
1155 return MSVCRT_EINVAL;
1158 size = strlenW(wcSrc) + 1;
1160 if(size > numElement)
1162 return MSVCRT_ERANGE;
1165 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1170 /******************************************************************
1171 * wcsncpy_s (MSVCRT.@)
1173 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1174 MSVCRT_size_t count )
1176 MSVCRT_size_t size = 0;
1178 if (!wcDest || !numElement)
1179 return MSVCRT_EINVAL;
1185 return MSVCRT_EINVAL;
1188 size = min(strlenW(wcSrc), count);
1190 if (size >= numElement)
1192 return MSVCRT_ERANGE;
1195 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1196 wcDest[size] = '\0';
1201 /******************************************************************
1202 * wcscat_s (MSVCRT.@)
1205 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1207 MSVCRT_wchar_t* ptr = dst;
1209 if (!dst || elem == 0) return MSVCRT_EINVAL;
1213 return MSVCRT_EINVAL;
1216 /* seek to end of dst string (or elem if no end of string is found */
1217 while (ptr < dst + elem && *ptr != '\0') ptr++;
1218 while (ptr < dst + elem)
1220 if ((*ptr++ = *src++) == '\0') return 0;
1222 /* not enough space */
1224 return MSVCRT_ERANGE;
1227 /*********************************************************************
1228 * wcsncat_s (MSVCRT.@)
1231 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1232 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1234 MSVCRT_size_t srclen;
1235 MSVCRT_wchar_t dststart;
1238 if (!MSVCRT_CHECK_PMT(dst != NULL) || !MSVCRT_CHECK_PMT(elem > 0))
1240 *MSVCRT__errno() = MSVCRT_EINVAL;
1241 return MSVCRT_EINVAL;
1243 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0))
1244 return MSVCRT_EINVAL;
1248 for (dststart = 0; dststart < elem; dststart++)
1250 if (dst[dststart] == '\0')
1253 if (dststart == elem)
1255 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n");
1256 return MSVCRT_EINVAL;
1259 if (count == MSVCRT__TRUNCATE)
1261 srclen = strlenW(src);
1262 if (srclen >= (elem - dststart))
1264 srclen = elem - dststart - 1;
1265 ret = MSVCRT_STRUNCATE;
1269 srclen = min(strlenW(src), count);
1270 if (srclen < (elem - dststart))
1272 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1273 dst[dststart+srclen] = '\0';
1276 MSVCRT_INVALID_PMT("dst[elem] is too small");
1278 return MSVCRT_ERANGE;
1281 /*********************************************************************
1282 * _wcstoi64_l (MSVCRT.@)
1284 * FIXME: locale parameter is ignored
1286 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1287 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1289 BOOL negative = FALSE;
1292 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1294 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
1295 !MSVCRT_CHECK_PMT(base <= 36)) {
1296 *MSVCRT__errno() = MSVCRT_EINVAL;
1300 while(isspaceW(*nptr)) nptr++;
1305 } else if(*nptr == '+')
1308 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1321 char cur = tolowerW(*nptr);
1329 if(cur<'a' || cur>='a'+base-10)
1339 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1340 ret = MSVCRT_I64_MAX;
1341 *MSVCRT__errno() = MSVCRT_ERANGE;
1342 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1343 ret = MSVCRT_I64_MIN;
1344 *MSVCRT__errno() = MSVCRT_ERANGE;
1350 *endptr = (MSVCRT_wchar_t*)nptr;
1355 /*********************************************************************
1356 * _wcstoi64 (MSVCRT.@)
1358 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1359 MSVCRT_wchar_t **endptr, int base)
1361 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1364 /*********************************************************************
1365 * _wcstoui64_l (MSVCRT.@)
1367 * FIXME: locale parameter is ignored
1369 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1370 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1372 BOOL negative = FALSE;
1373 unsigned __int64 ret = 0;
1375 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1377 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
1378 !MSVCRT_CHECK_PMT(base <= 36)) {
1379 *MSVCRT__errno() = MSVCRT_EINVAL;
1383 while(isspaceW(*nptr)) nptr++;
1388 } else if(*nptr == '+')
1391 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1404 char cur = tolowerW(*nptr);
1412 if(cur<'a' || cur>='a'+base-10)
1419 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1420 ret = MSVCRT_UI64_MAX;
1421 *MSVCRT__errno() = MSVCRT_ERANGE;
1427 *endptr = (MSVCRT_wchar_t*)nptr;
1429 return negative ? -ret : ret;
1432 /*********************************************************************
1433 * _wcstoui64 (MSVCRT.@)
1435 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1436 MSVCRT_wchar_t **endptr, int base)
1438 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
1441 /******************************************************************
1442 * wcsnlen (MSVCRT.@)
1444 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
1448 for (i = 0; i < maxlen; i++)
1453 /*********************************************************************
1454 * _towupper_l (MSVCRT.@)
1456 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
1461 /*********************************************************************
1462 * _towlower_l (MSVCRT.@)
1464 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)