msvcrt: _towupper_l only uppercase a-z in C locale.
[wine.git] / dlls / msvcrt / wcs.c
blob28e82ec30300456f71141f7fb230d581a50c069a
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <limits.h>
25 #include <stdio.h>
26 #include <math.h>
27 #include <assert.h>
28 #include "msvcrt.h"
29 #include "winnls.h"
30 #include "wtypes.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
36 static BOOL n_format_enabled = TRUE;
38 #include "printf.h"
39 #define PRINTF_WIDE
40 #include "printf.h"
41 #undef PRINTF_WIDE
43 #if _MSVCR_VER>=80
45 /*********************************************************************
46 * _get_printf_count_output (MSVCR80.@)
48 int CDECL MSVCRT__get_printf_count_output( void )
50 return n_format_enabled ? 1 : 0;
53 /*********************************************************************
54 * _set_printf_count_output (MSVCR80.@)
56 int CDECL MSVCRT__set_printf_count_output( int enable )
58 BOOL old = n_format_enabled;
59 n_format_enabled = enable != 0;
60 return old ? 1 : 0;
63 #endif /* _MSVCR_VER>=80 */
65 /*********************************************************************
66 * _wcsdup (MSVCRT.@)
68 MSVCRT_wchar_t* CDECL MSVCRT__wcsdup( const MSVCRT_wchar_t* str )
70 MSVCRT_wchar_t* ret = NULL;
71 if (str)
73 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
74 ret = MSVCRT_malloc( size );
75 if (ret) memcpy( ret, str, size );
77 return ret;
80 INT CDECL MSVCRT__wcsicmp_l(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, MSVCRT__locale_t locale)
82 if(!MSVCRT_CHECK_PMT(str1 != NULL) || !MSVCRT_CHECK_PMT(str2 != NULL))
83 return MSVCRT__NLSCMPERROR;
85 return strcmpiW(str1, str2);
88 /*********************************************************************
89 * _wcsicmp (MSVCRT.@)
91 INT CDECL MSVCRT__wcsicmp( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
93 return strcmpiW( str1, str2 );
96 /*********************************************************************
97 * _wcsnicmp_l (MSVCRT.@)
99 INT CDECL MSVCRT__wcsnicmp_l(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, INT n, MSVCRT__locale_t locale)
101 return strncmpiW(str1, str2, n);
104 /*********************************************************************
105 * _wcsnicmp (MSVCRT.@)
107 INT CDECL MSVCRT__wcsnicmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, INT n)
109 return MSVCRT__wcsnicmp_l(str1, str2, n, NULL);
112 /*********************************************************************
113 * _wcsicoll_l (MSVCRT.@)
115 int CDECL MSVCRT__wcsicoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
117 MSVCRT_pthreadlocinfo locinfo;
119 if(!locale)
120 locinfo = get_locinfo();
121 else
122 locinfo = locale->locinfo;
124 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
125 return strcmpiW(str1, str2);
126 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
127 str1, -1, str2, -1)-CSTR_EQUAL;
130 /*********************************************************************
131 * _wcsicoll (MSVCRT.@)
133 INT CDECL MSVCRT__wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
135 return MSVCRT__wcsicoll_l(str1, str2, NULL);
138 /*********************************************************************
139 * _wcsnicoll_l (MSVCRT.@)
141 int CDECL MSVCRT__wcsnicoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2,
142 MSVCRT_size_t count, MSVCRT__locale_t locale)
144 MSVCRT_pthreadlocinfo locinfo;
146 if(!locale)
147 locinfo = get_locinfo();
148 else
149 locinfo = locale->locinfo;
151 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
152 return strncmpiW(str1, str2, count);
153 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
154 str1, MSVCRT_wcsnlen(str1, count),
155 str2, MSVCRT_wcsnlen(str2, count))-CSTR_EQUAL;
158 /*********************************************************************
159 * _wcsnicoll (MSVCRT.@)
161 INT CDECL MSVCRT__wcsnicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count )
163 return MSVCRT__wcsnicoll_l(str1, str2, count, NULL);
166 /*********************************************************************
167 * _wcsnset (MSVCRT.@)
169 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
171 MSVCRT_wchar_t* ret = str;
172 while ((n-- > 0) && *str) *str++ = c;
173 return ret;
176 /*********************************************************************
177 * _wcsrev (MSVCRT.@)
179 MSVCRT_wchar_t* CDECL MSVCRT__wcsrev( MSVCRT_wchar_t* str )
181 MSVCRT_wchar_t* ret = str;
182 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
183 while (end > str)
185 MSVCRT_wchar_t t = *end;
186 *end-- = *str;
187 *str++ = t;
189 return ret;
192 /*********************************************************************
193 * _wcsset_s (MSVCRT.@)
195 int CDECL MSVCRT__wcsset_s( MSVCRT_wchar_t *str, MSVCRT_size_t n, MSVCRT_wchar_t c )
197 MSVCRT_wchar_t *p = str;
199 if(!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
200 if(!MSVCRT_CHECK_PMT(n)) return MSVCRT_EINVAL;
202 while(*p && --n) *p++ = c;
203 if(!n) {
204 str[0] = 0;
205 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
206 *MSVCRT__errno() = MSVCRT_EINVAL;
207 return MSVCRT_EINVAL;
209 return 0;
212 /*********************************************************************
213 * _wcsset (MSVCRT.@)
215 MSVCRT_wchar_t* CDECL MSVCRT__wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
217 MSVCRT_wchar_t* ret = str;
218 while (*str) *str++ = c;
219 return ret;
222 /******************************************************************
223 * _wcsupr_s_l (MSVCRT.@)
225 int CDECL MSVCRT__wcsupr_s_l( MSVCRT_wchar_t* str, MSVCRT_size_t n,
226 MSVCRT__locale_t locale )
228 MSVCRT_wchar_t* ptr = str;
230 if (!str || !n)
232 if (str) *str = '\0';
233 *MSVCRT__errno() = MSVCRT_EINVAL;
234 return MSVCRT_EINVAL;
237 while (n--)
239 if (!*ptr) return 0;
240 /* FIXME: add locale support */
241 *ptr = toupperW(*ptr);
242 ptr++;
245 /* MSDN claims that the function should return and set errno to
246 * ERANGE, which doesn't seem to be true based on the tests. */
247 *str = '\0';
248 *MSVCRT__errno() = MSVCRT_EINVAL;
249 return MSVCRT_EINVAL;
252 /******************************************************************
253 * _wcsupr_s (MSVCRT.@)
256 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
258 return MSVCRT__wcsupr_s_l( str, n, NULL );
261 /******************************************************************
262 * _wcsupr_l (MSVCRT.@)
264 MSVCRT_wchar_t* CDECL MSVCRT__wcsupr_l( MSVCRT_wchar_t *str, MSVCRT__locale_t locale )
266 MSVCRT__wcsupr_s_l( str, -1, locale);
267 return str;
270 /******************************************************************
271 * _wcsupr (MSVCRT.@)
273 MSVCRT_wchar_t* CDECL MSVCRT__wcsupr( MSVCRT_wchar_t *str )
275 return MSVCRT__wcsupr_l(str, NULL);
278 /******************************************************************
279 * _wcslwr_s_l (MSVCRT.@)
281 int CDECL MSVCRT__wcslwr_s_l( MSVCRT_wchar_t* str, MSVCRT_size_t n, MSVCRT__locale_t locale )
283 MSVCRT_wchar_t* ptr = str;
285 if (!str || !n)
287 if (str) *str = '\0';
288 *MSVCRT__errno() = MSVCRT_EINVAL;
289 return MSVCRT_EINVAL;
292 while (n--)
294 if (!*ptr) return 0;
295 *ptr = tolowerW(*ptr);
296 ptr++;
299 /* MSDN claims that the function should return and set errno to
300 * ERANGE, which doesn't seem to be true based on the tests. */
301 *str = '\0';
302 *MSVCRT__errno() = MSVCRT_EINVAL;
303 return MSVCRT_EINVAL;
306 /******************************************************************
307 * _wcslwr_s (MSVCRT.@)
309 int CDECL MSVCRT__wcslwr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
311 return MSVCRT__wcslwr_s_l(str, n, NULL);
314 /******************************************************************
315 * _wcslwr_l (MSVCRT.@)
317 MSVCRT_wchar_t* CDECL MSVCRT__wcslwr_l( MSVCRT_wchar_t* str, MSVCRT__locale_t locale )
319 MSVCRT__wcslwr_s_l(str, -1, locale);
320 return str;
323 /******************************************************************
324 * _wcslwr (MSVCRT.@)
326 MSVCRT_wchar_t* CDECL MSVCRT__wcslwr( MSVCRT_wchar_t* str )
328 MSVCRT__wcslwr_s_l(str, -1, NULL);
329 return str;
332 /*********************************************************************
333 * _wcsncoll_l (MSVCRT.@)
335 int CDECL MSVCRT__wcsncoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2,
336 MSVCRT_size_t count, MSVCRT__locale_t locale)
338 MSVCRT_pthreadlocinfo locinfo;
340 if(!locale)
341 locinfo = get_locinfo();
342 else
343 locinfo = locale->locinfo;
345 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
346 return strncmpW(str1, str2, count);
347 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0,
348 str1, MSVCRT_wcsnlen(str1, count),
349 str2, MSVCRT_wcsnlen(str2, count))-CSTR_EQUAL;
352 /*********************************************************************
353 * _wcsncoll (MSVCRT.@)
355 int CDECL MSVCRT__wcsncoll(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count)
357 return MSVCRT__wcsncoll_l(str1, str2, count, NULL);
360 /*********************************************************************
361 * _wcstod_l (MSVCRT.@)
363 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
364 MSVCRT__locale_t locale)
366 MSVCRT_pthreadlocinfo locinfo;
367 unsigned __int64 d=0, hlp;
368 unsigned fpcontrol;
369 int exp=0, sign=1;
370 const MSVCRT_wchar_t *p;
371 double ret;
372 long double lret=1, expcnt = 10;
373 BOOL found_digit = FALSE, negexp;
375 if (!MSVCRT_CHECK_PMT(str != NULL)) return 0;
377 if(!locale)
378 locinfo = get_locinfo();
379 else
380 locinfo = locale->locinfo;
382 p = str;
383 while(isspaceW(*p))
384 p++;
386 if(*p == '-') {
387 sign = -1;
388 p++;
389 } else if(*p == '+')
390 p++;
392 while(*p>='0' && *p<='9') {
393 found_digit = TRUE;
394 hlp = d*10+*(p++)-'0';
395 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
396 exp++;
397 break;
398 } else
399 d = hlp;
401 while(*p>='0' && *p<='9') {
402 exp++;
403 p++;
405 if(*p == *locinfo->lconv->decimal_point)
406 p++;
408 while(*p>='0' && *p<='9') {
409 found_digit = TRUE;
410 hlp = d*10+*(p++)-'0';
411 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
412 break;
414 d = hlp;
415 exp--;
417 while(*p>='0' && *p<='9')
418 p++;
420 if(!found_digit) {
421 if(end)
422 *end = (MSVCRT_wchar_t*)str;
423 return 0.0;
426 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
427 int e=0, s=1;
429 p++;
430 if(*p == '-') {
431 s = -1;
432 p++;
433 } else if(*p == '+')
434 p++;
436 if(*p>='0' && *p<='9') {
437 while(*p>='0' && *p<='9') {
438 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
439 e = INT_MAX;
440 p++;
442 e *= s;
444 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
445 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
446 else exp += e;
447 } else {
448 if(*p=='-' || *p=='+')
449 p--;
450 p--;
454 fpcontrol = _control87(0, 0);
455 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
456 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
458 negexp = (exp < 0);
459 if(negexp)
460 exp = -exp;
461 while(exp) {
462 if(exp & 1)
463 lret *= expcnt;
464 exp /= 2;
465 expcnt = expcnt*expcnt;
467 ret = (long double)sign * (negexp ? d/lret : d*lret);
469 _control87(fpcontrol, 0xffffffff);
471 if((d && ret==0.0) || isinf(ret))
472 *MSVCRT__errno() = MSVCRT_ERANGE;
474 if(end)
475 *end = (MSVCRT_wchar_t*)p;
477 return ret;
480 /*********************************************************************
481 * wcsrtombs_l (INTERNAL)
483 static MSVCRT_size_t MSVCRT_wcsrtombs_l(char *mbstr, const MSVCRT_wchar_t **wcstr,
484 MSVCRT_size_t count, MSVCRT__locale_t locale)
486 MSVCRT_pthreadlocinfo locinfo;
487 MSVCRT_size_t tmp = 0;
488 BOOL used_default;
490 if(!locale)
491 locinfo = get_locinfo();
492 else
493 locinfo = locale->locinfo;
495 if(!locinfo->lc_codepage) {
496 MSVCRT_size_t i;
498 if(!mbstr)
499 return strlenW(*wcstr);
501 for(i=0; i<count; i++) {
502 if((*wcstr)[i] > 255) {
503 *MSVCRT__errno() = MSVCRT_EILSEQ;
504 return -1;
507 mbstr[i] = (*wcstr)[i];
508 if(!(*wcstr)[i]) break;
510 return i;
513 if(!mbstr) {
514 tmp = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
515 *wcstr, -1, NULL, 0, NULL, &used_default)-1;
516 if(!tmp || used_default) {
517 *MSVCRT__errno() = MSVCRT_EILSEQ;
518 return -1;
520 return tmp;
523 while(**wcstr) {
524 char buf[3];
525 MSVCRT_size_t i, size;
527 size = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
528 *wcstr, 1, buf, 3, NULL, &used_default);
529 if(!size || used_default) {
530 *MSVCRT__errno() = MSVCRT_EILSEQ;
531 return -1;
533 if(tmp+size > count)
534 return tmp;
536 for(i=0; i<size; i++)
537 mbstr[tmp++] = buf[i];
538 (*wcstr)++;
541 if(tmp < count) {
542 mbstr[tmp] = '\0';
543 *wcstr = NULL;
545 return tmp;
548 /*********************************************************************
549 * _wcstombs_l (MSVCRT.@)
551 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
552 MSVCRT_size_t count, MSVCRT__locale_t locale)
554 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, locale);
557 /*********************************************************************
558 * wcstombs (MSVCRT.@)
560 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
561 MSVCRT_size_t count)
563 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, NULL);
566 /*********************************************************************
567 * wcsrtombs (MSVCRT.@)
569 MSVCRT_size_t CDECL MSVCRT_wcsrtombs(char *mbstr, const MSVCRT_wchar_t **wcstr,
570 MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
572 if(mbstate)
573 *mbstate = 0;
575 return MSVCRT_wcsrtombs_l(mbstr, wcstr, count, NULL);
578 /*********************************************************************
579 * MSVCRT_wcsrtombs_s_l (INTERNAL)
581 static int MSVCRT_wcsrtombs_s_l(MSVCRT_size_t *ret, char *mbstr,
582 MSVCRT_size_t size, const MSVCRT_wchar_t **wcstr,
583 MSVCRT_size_t count, MSVCRT__locale_t locale)
585 MSVCRT_size_t conv;
587 if(!mbstr && !size && wcstr) {
588 conv = MSVCRT_wcsrtombs_l(NULL, wcstr, 0, locale);
589 if(conv == -1)
590 return *MSVCRT__errno();
591 if(ret)
592 *ret = conv+1;
593 return 0;
596 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) return MSVCRT_EINVAL;
597 if (size) mbstr[0] = '\0';
598 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
599 if (!MSVCRT_CHECK_PMT(*wcstr != NULL)) return MSVCRT_EINVAL;
601 if(count==MSVCRT__TRUNCATE || size<count)
602 conv = size;
603 else
604 conv = count;
606 conv = MSVCRT_wcsrtombs_l(mbstr, wcstr, conv, locale);
607 if(conv == -1) {
608 if(size)
609 mbstr[0] = '\0';
610 return *MSVCRT__errno();
611 }else if(conv < size)
612 mbstr[conv++] = '\0';
613 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
614 mbstr[conv-1] = '\0';
615 else {
616 MSVCRT_INVALID_PMT("mbstr[size] is too small", MSVCRT_ERANGE);
617 if(size)
618 mbstr[0] = '\0';
619 return MSVCRT_ERANGE;
622 if(ret)
623 *ret = conv;
624 return 0;
627 /*********************************************************************
628 * _wcstombs_s_l (MSVCRT.@)
630 int CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
631 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
632 MSVCRT_size_t count, MSVCRT__locale_t locale)
634 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr,count, locale);
637 /*********************************************************************
638 * wcstombs_s (MSVCRT.@)
640 int CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
641 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
643 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr, count, NULL);
646 /*********************************************************************
647 * wcsrtombs_s (MSVCRT.@)
649 int CDECL MSVCRT_wcsrtombs_s(MSVCRT_size_t *ret, char *mbstr, MSVCRT_size_t size,
650 const MSVCRT_wchar_t **wcstr, MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
652 if(mbstate)
653 *mbstate = 0;
655 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, wcstr, count, NULL);
658 /*********************************************************************
659 * wcstod (MSVCRT.@)
661 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
663 return MSVCRT__wcstod_l(lpszStr, end, NULL);
666 /*********************************************************************
667 * _wtof (MSVCRT.@)
669 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
671 return MSVCRT__wcstod_l(str, NULL, NULL);
674 /*********************************************************************
675 * _wtof_l (MSVCRT.@)
677 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
679 return MSVCRT__wcstod_l(str, NULL, locale);
682 #if _MSVCR_VER>=120
684 /*********************************************************************
685 * _wcstof_l (MSVCR120.@)
687 float CDECL MSVCRT__wcstof_l( const MSVCRT_wchar_t *str, MSVCRT_wchar_t **end, MSVCRT__locale_t locale )
689 return MSVCRT__wcstod_l(str, end, locale);
692 /*********************************************************************
693 * wcstof (MSVCR120.@)
695 float CDECL MSVCRT_wcstof( const MSVCRT_wchar_t *str, MSVCRT_wchar_t **end )
697 return MSVCRT__wcstof_l(str, end, NULL);
700 #endif /* _MSVCR_VER>=120 */
702 /*********************************************************************
703 * arg_clbk_valist (INTERNAL)
705 printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valist)
707 printf_arg ret;
709 if(type == VT_I8)
710 ret.get_longlong = va_arg(*valist, LONGLONG);
711 else if(type == VT_INT)
712 ret.get_int = va_arg(*valist, int);
713 else if(type == VT_R8)
714 ret.get_double = va_arg(*valist, double);
715 else if(type == VT_PTR)
716 ret.get_ptr = va_arg(*valist, void*);
717 else {
718 ERR("Incorrect type\n");
719 ret.get_int = 0;
722 return ret;
725 /*********************************************************************
726 * arg_clbk_positional (INTERNAL)
728 printf_arg arg_clbk_positional(void *ctx, int pos, int type, __ms_va_list *valist)
730 printf_arg *args = ctx;
731 return args[pos];
734 /*********************************************************************
735 * _vsnprintf (MSVCRT.@)
737 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
738 const char *format, __ms_va_list valist )
740 static const char nullbyte = '\0';
741 struct _str_ctx_a ctx = {len, str};
742 int ret;
744 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, 0,
745 arg_clbk_valist, NULL, &valist);
746 puts_clbk_str_a(&ctx, 1, &nullbyte);
747 return ret;
750 #if _MSVCR_VER>=140
752 static int puts_clbk_str_c99_a(void *ctx, int len, const char *str)
754 struct _str_ctx_a *out = ctx;
756 if(!out->buf)
757 return len;
759 if(out->len < len) {
760 memcpy(out->buf, str, out->len);
761 out->buf += out->len;
762 out->len = 0;
763 return len;
766 memcpy(out->buf, str, len);
767 out->buf += len;
768 out->len -= len;
769 return len;
772 /*********************************************************************
773 * __stdio_common_vsprintf (UCRTBASE.@)
775 int CDECL MSVCRT__stdio_common_vsprintf( unsigned __int64 options, char *str, MSVCRT_size_t len, const char *format,
776 MSVCRT__locale_t locale, __ms_va_list valist )
778 static const char nullbyte = '\0';
779 struct _str_ctx_a ctx = {len, str};
780 int ret;
782 if (options & ~UCRTBASE_PRINTF_MASK)
783 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
784 ret = pf_printf_a(puts_clbk_str_c99_a,
785 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
786 puts_clbk_str_a(&ctx, 1, &nullbyte);
788 if(!str)
789 return ret;
790 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
791 return ret>len ? -1 : ret;
792 if(ret>=len) {
793 if(len) str[len-1] = 0;
794 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
796 return ret;
799 #endif /* _MSVCR_VER>=140 */
801 /*********************************************************************
802 * _vsnprintf_l (MSVCRT.@)
804 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
805 MSVCRT__locale_t locale, __ms_va_list valist )
807 static const char nullbyte = '\0';
808 struct _str_ctx_a ctx = {len, str};
809 int ret;
811 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, 0,
812 arg_clbk_valist, NULL, &valist);
813 puts_clbk_str_a(&ctx, 1, &nullbyte);
814 return ret;
817 /*********************************************************************
818 * _vsprintf_l (MSVCRT.@)
820 int CDECL MSVCRT_vsprintf_l( char *str, const char *format,
821 MSVCRT__locale_t locale, __ms_va_list valist )
823 return MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
826 /*********************************************************************
827 * _sprintf_l (MSVCRT.@)
829 int WINAPIV MSVCRT_sprintf_l(char *str, const char *format,
830 MSVCRT__locale_t locale, ...)
832 int retval;
833 __ms_va_list valist;
834 __ms_va_start(valist, locale);
835 retval = MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
836 __ms_va_end(valist);
837 return retval;
840 static int CDECL MSVCRT_vsnprintf_s_l_opt( char *str, MSVCRT_size_t sizeOfBuffer,
841 MSVCRT_size_t count, const char *format, DWORD options,
842 MSVCRT__locale_t locale, __ms_va_list valist )
844 static const char nullbyte = '\0';
845 struct _str_ctx_a ctx;
846 int len, ret;
848 if(sizeOfBuffer<count+1 || count==-1)
849 len = sizeOfBuffer;
850 else
851 len = count+1;
853 ctx.len = len;
854 ctx.buf = str;
855 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
856 arg_clbk_valist, NULL, &valist);
857 puts_clbk_str_a(&ctx, 1, &nullbyte);
859 if(ret<0 || ret==len) {
860 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
861 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
862 memset(str, 0, sizeOfBuffer);
863 } else
864 str[len-1] = '\0';
866 return -1;
869 return ret;
872 static int MSVCRT_vsnwprintf_s_l_opt( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
873 MSVCRT_size_t count, const MSVCRT_wchar_t *format, DWORD options,
874 MSVCRT__locale_t locale, __ms_va_list valist)
876 static const MSVCRT_wchar_t nullbyte = '\0';
877 struct _str_ctx_w ctx;
878 int len, ret;
880 len = sizeOfBuffer;
881 if(count!=-1 && len>count+1)
882 len = count+1;
884 ctx.len = len;
885 ctx.buf = str;
886 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
887 arg_clbk_valist, NULL, &valist);
888 puts_clbk_str_w(&ctx, 1, &nullbyte);
890 if(ret<0 || ret==len) {
891 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
892 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
893 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
894 } else
895 str[len-1] = '\0';
897 return -1;
900 return ret;
903 /*********************************************************************
904 * _vsnprintf_s_l (MSVCRT.@)
906 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
907 MSVCRT_size_t count, const char *format,
908 MSVCRT__locale_t locale, __ms_va_list valist )
910 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, 0, locale, valist);
913 /*********************************************************************
914 * _vsprintf_s_l (MSVCRT.@)
916 int CDECL MSVCRT_vsprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
917 MSVCRT__locale_t locale, __ms_va_list valist )
919 return MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
922 /*********************************************************************
923 * _sprintf_s_l (MSVCRT.@)
925 int WINAPIV MSVCRT_sprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
926 MSVCRT__locale_t locale, ...)
928 int retval;
929 __ms_va_list valist;
930 __ms_va_start(valist, locale);
931 retval = MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
932 __ms_va_end(valist);
933 return retval;
936 /*********************************************************************
937 * _vsnprintf_s (MSVCRT.@)
939 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
940 MSVCRT_size_t count, const char *format, __ms_va_list valist )
942 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
945 #if _MSVCR_VER>=140
947 /*********************************************************************
948 * __stdio_common_vsnprintf_s (UCRTBASE.@)
950 int CDECL MSVCRT__stdio_common_vsnprintf_s( unsigned __int64 options,
951 char *str, MSVCRT_size_t sizeOfBuffer, MSVCRT_size_t count,
952 const char *format, MSVCRT__locale_t locale, __ms_va_list valist )
954 if (options & ~UCRTBASE_PRINTF_MASK)
955 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
956 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
959 /*********************************************************************
960 * __stdio_common_vsnwprintf_s (UCRTBASE.@)
962 int CDECL MSVCRT__stdio_common_vsnwprintf_s( unsigned __int64 options,
963 MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer, MSVCRT_size_t count,
964 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list valist )
966 if (options & ~UCRTBASE_PRINTF_MASK)
967 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
968 return MSVCRT_vsnwprintf_s_l_opt(str, sizeOfBuffer, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
971 /*********************************************************************
972 * __stdio_common_vswprintf_s (UCRTBASE.@)
974 int CDECL MSVCRT__stdio_common_vswprintf_s( unsigned __int64 options,
975 MSVCRT_wchar_t *str, MSVCRT_size_t count, const MSVCRT_wchar_t *format,
976 MSVCRT__locale_t locale, __ms_va_list valist )
978 return MSVCRT__stdio_common_vsnwprintf_s(options, str, INT_MAX, count, format, locale, valist);
981 /*********************************************************************
982 * __stdio_common_vsprintf_s (UCRTBASE.@)
984 int CDECL MSVCRT__stdio_common_vsprintf_s( unsigned __int64 options,
985 char *str, MSVCRT_size_t count, const char *format,
986 MSVCRT__locale_t locale, __ms_va_list valist )
988 if (options & ~UCRTBASE_PRINTF_MASK)
989 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
990 return MSVCRT_vsnprintf_s_l_opt(str, INT_MAX, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
993 #endif /* _MSVCR_VER>=140 */
995 /*********************************************************************
996 * vsprintf (MSVCRT.@)
998 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
1000 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
1003 /*********************************************************************
1004 * vsprintf_s (MSVCRT.@)
1006 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
1008 return MSVCRT_vsnprintf(str, num, format, valist);
1011 /*********************************************************************
1012 * _vscprintf (MSVCRT.@)
1014 int CDECL MSVCRT__vscprintf( const char *format, __ms_va_list valist )
1016 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
1019 /*********************************************************************
1020 * _vscprintf_l (MSVCRT.@)
1022 int CDECL MSVCRT__vscprintf_l(const char *format,
1023 MSVCRT__locale_t locale, __ms_va_list valist)
1025 return MSVCRT_vsnprintf_l(NULL, INT_MAX, format, locale, valist);
1028 /*********************************************************************
1029 * _vscprintf_p_l (MSVCRT.@)
1031 int CDECL MSVCRT__vscprintf_p_l(const char *format,
1032 MSVCRT__locale_t locale, __ms_va_list args)
1034 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1035 struct _str_ctx_a puts_ctx = {INT_MAX, NULL};
1036 int ret;
1038 memset(args_ctx, 0, sizeof(args_ctx));
1040 ret = create_positional_ctx_a(args_ctx, format, args);
1041 if(ret < 0) {
1042 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1043 *MSVCRT__errno() = MSVCRT_EINVAL;
1044 return ret;
1045 } else if(ret == 0) {
1046 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1047 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1048 arg_clbk_valist, NULL, &args);
1049 } else {
1050 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1051 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1052 arg_clbk_positional, args_ctx, NULL);
1055 return ret;
1058 /*********************************************************************
1059 * _vscprintf_p (MSVCR80.@)
1061 int CDECL MSVCRT__vscprintf_p(const char *format, __ms_va_list argptr)
1063 return MSVCRT__vscprintf_p_l(format, NULL, argptr);
1066 /*********************************************************************
1067 * _snprintf (MSVCRT.@)
1069 int WINAPIV MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
1071 int retval;
1072 __ms_va_list valist;
1073 __ms_va_start(valist, format);
1074 retval = MSVCRT_vsnprintf(str, len, format, valist);
1075 __ms_va_end(valist);
1076 return retval;
1079 /*********************************************************************
1080 * _snprintf_l (MSVCRT.@)
1082 int WINAPIV MSVCRT__snprintf_l(char *str, unsigned int count, MSVCRT__locale_t locale,
1083 const char *format, ...)
1085 int retval;
1086 __ms_va_list valist;
1087 __ms_va_start(valist, format);
1088 retval = MSVCRT_vsnprintf_l(str, count, format, locale, valist);
1089 __ms_va_end(valist);
1090 return retval;
1093 /*********************************************************************
1094 * _snprintf_s (MSVCRT.@)
1096 int WINAPIV MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
1097 const char *format, ...)
1099 int retval;
1100 __ms_va_list valist;
1101 __ms_va_start(valist, format);
1102 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
1103 __ms_va_end(valist);
1104 return retval;
1107 /*********************************************************************
1108 * _scprintf (MSVCRT.@)
1110 int WINAPIV MSVCRT__scprintf(const char *format, ...)
1112 int retval;
1113 __ms_va_list valist;
1114 __ms_va_start(valist, format);
1115 retval = MSVCRT__vscprintf(format, valist);
1116 __ms_va_end(valist);
1117 return retval;
1120 /*********************************************************************
1121 * _vsnwprintf (MSVCRT.@)
1123 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1124 const MSVCRT_wchar_t *format, __ms_va_list valist)
1126 static const MSVCRT_wchar_t nullbyte = '\0';
1127 struct _str_ctx_w ctx = {len, str};
1128 int ret;
1130 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, 0,
1131 arg_clbk_valist, NULL, &valist);
1132 puts_clbk_str_w(&ctx, 1, &nullbyte);
1133 return ret;
1136 /*********************************************************************
1137 * _vsnwprintf_l (MSVCRT.@)
1139 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1140 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1141 __ms_va_list valist)
1143 static const MSVCRT_wchar_t nullbyte = '\0';
1144 struct _str_ctx_w ctx = {len, str};
1145 int ret;
1147 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, 0,
1148 arg_clbk_valist, NULL, &valist);
1149 puts_clbk_str_w(&ctx, 1, &nullbyte);
1150 return ret;
1153 static int MSVCRT_vswprintf_p_l_opt(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1154 const MSVCRT_wchar_t *format, DWORD options, MSVCRT__locale_t locale, __ms_va_list args)
1156 static const MSVCRT_wchar_t nullbyte = '\0';
1157 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1158 struct _str_ctx_w puts_ctx = {length, buffer};
1159 int ret;
1161 memset(args_ctx, 0, sizeof(args_ctx));
1163 ret = create_positional_ctx_w(args_ctx, format, args);
1164 if(ret < 0) {
1165 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1166 *MSVCRT__errno() = MSVCRT_EINVAL;
1167 return ret;
1168 } else if(ret == 0)
1169 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
1170 arg_clbk_valist, NULL, &args);
1171 else
1172 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale,
1173 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
1174 arg_clbk_positional, args_ctx, NULL);
1176 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
1177 return ret;
1180 /*********************************************************************
1181 * _vswprintf_p_l (MSVCRT.@)
1183 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1184 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
1186 return MSVCRT_vswprintf_p_l_opt(buffer, length, format, 0, locale, args);
1189 #if _MSVCR_VER>=80
1190 /*********************************************************************
1191 * _vswprintf_p (MSVCR80.@)
1193 int CDECL MSVCRT__vswprintf_p(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1194 const MSVCRT_wchar_t *format, __ms_va_list args)
1196 return MSVCRT_vswprintf_p_l_opt(buffer, length, format, 0, NULL, args);
1198 #endif
1200 #if _MSVCR_VER>=140
1201 /*********************************************************************
1202 * __stdio_common_vswprintf_p (UCRTBASE.@)
1204 int CDECL MSVCRT__stdio_common_vswprintf_p( unsigned __int64 options,
1205 MSVCRT_wchar_t *str, MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1206 MSVCRT__locale_t locale, __ms_va_list valist )
1208 if (options & ~UCRTBASE_PRINTF_MASK)
1209 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1210 return MSVCRT_vswprintf_p_l_opt(str, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
1212 #endif
1214 /*********************************************************************
1215 * _vsnwprintf_s_l (MSVCRT.@)
1217 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1218 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1219 MSVCRT__locale_t locale, __ms_va_list valist)
1221 return MSVCRT_vsnwprintf_s_l_opt(str, sizeOfBuffer, count, format, 0, locale, valist);
1224 /*********************************************************************
1225 * _vsnwprintf_s (MSVCRT.@)
1227 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1228 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1230 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1231 format, NULL, valist);
1234 /*********************************************************************
1235 * _snwprintf (MSVCRT.@)
1237 int WINAPIV MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1239 int retval;
1240 __ms_va_list valist;
1241 __ms_va_start(valist, format);
1242 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1243 __ms_va_end(valist);
1244 return retval;
1247 /*********************************************************************
1248 * _snwprintf_l (MSVCRT.@)
1250 int WINAPIV MSVCRT__snwprintf_l( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format,
1251 MSVCRT__locale_t locale, ...)
1253 int retval;
1254 __ms_va_list valist;
1255 __ms_va_start(valist, locale);
1256 retval = MSVCRT_vsnwprintf_l(str, len, format, locale, valist);
1257 __ms_va_end(valist);
1258 return retval;
1261 /*********************************************************************
1262 * _snwprintf_s (MSVCRT.@)
1264 int WINAPIV MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1265 const MSVCRT_wchar_t *format, ...)
1267 int retval;
1268 __ms_va_list valist;
1269 __ms_va_start(valist, format);
1270 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1271 __ms_va_end(valist);
1272 return retval;
1275 /*********************************************************************
1276 * _snwprintf_s_l (MSVCRT.@)
1278 int WINAPIV MSVCRT__snwprintf_s_l( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1279 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1281 int retval;
1282 __ms_va_list valist;
1283 __ms_va_start(valist, locale);
1284 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, locale, valist);
1285 __ms_va_end(valist);
1286 return retval;
1289 #if _MSVCR_VER>=140
1291 static int puts_clbk_str_c99_w(void *ctx, int len, const MSVCRT_wchar_t *str)
1293 struct _str_ctx_w *out = ctx;
1295 if(!out->buf)
1296 return len;
1298 if(out->len < len) {
1299 memcpy(out->buf, str, out->len*sizeof(MSVCRT_wchar_t));
1300 out->buf += out->len;
1301 out->len = 0;
1302 return len;
1305 memcpy(out->buf, str, len*sizeof(MSVCRT_wchar_t));
1306 out->buf += len;
1307 out->len -= len;
1308 return len;
1311 /*********************************************************************
1312 * __stdio_common_vswprintf (UCRTBASE.@)
1314 int CDECL MSVCRT__stdio_common_vswprintf( unsigned __int64 options,
1315 MSVCRT_wchar_t *str, MSVCRT_size_t len, const MSVCRT_wchar_t *format,
1316 MSVCRT__locale_t locale, __ms_va_list valist )
1318 static const MSVCRT_wchar_t nullbyte = '\0';
1319 struct _str_ctx_w ctx = {len, str};
1320 int ret;
1322 if (options & ~UCRTBASE_PRINTF_MASK)
1323 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1324 ret = pf_printf_w(puts_clbk_str_c99_w,
1325 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
1326 puts_clbk_str_w(&ctx, 1, &nullbyte);
1328 if(!str)
1329 return ret;
1330 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
1331 return ret>len ? -1 : ret;
1332 if(ret>=len) {
1333 if(len) str[len-1] = 0;
1334 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
1336 return ret;
1339 #endif /* _MSVCR_VER>=140 */
1341 /*********************************************************************
1342 * sprintf (MSVCRT.@)
1344 int WINAPIV MSVCRT_sprintf( char *str, const char *format, ... )
1346 __ms_va_list ap;
1347 int r;
1349 __ms_va_start( ap, format );
1350 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1351 __ms_va_end( ap );
1352 return r;
1355 /*********************************************************************
1356 * sprintf_s (MSVCRT.@)
1358 int WINAPIV MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1360 __ms_va_list ap;
1361 int r;
1363 __ms_va_start( ap, format );
1364 r = MSVCRT_vsnprintf( str, num, format, ap );
1365 __ms_va_end( ap );
1366 return r;
1369 /*********************************************************************
1370 * _scwprintf (MSVCRT.@)
1372 int WINAPIV MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1374 __ms_va_list ap;
1375 int r;
1377 __ms_va_start( ap, format );
1378 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1379 __ms_va_end( ap );
1380 return r;
1383 /*********************************************************************
1384 * swprintf (MSVCRT.@)
1386 int WINAPIV MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1388 __ms_va_list ap;
1389 int r;
1391 __ms_va_start( ap, format );
1392 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1393 __ms_va_end( ap );
1394 return r;
1397 /*********************************************************************
1398 * swprintf_s (MSVCRT.@)
1400 int WINAPIV MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1401 const MSVCRT_wchar_t *format, ... )
1403 __ms_va_list ap;
1404 int r;
1406 __ms_va_start(ap, format);
1407 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1408 __ms_va_end(ap);
1410 return r;
1413 /*********************************************************************
1414 * _swprintf_s_l (MSVCRT.@)
1416 int WINAPIV MSVCRT__swprintf_s_l(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1417 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1419 __ms_va_list ap;
1420 int r;
1422 __ms_va_start(ap, locale);
1423 r = MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX, format, locale, ap);
1424 __ms_va_end(ap);
1426 return r;
1429 /*********************************************************************
1430 * _vswprintf (MSVCRT.@)
1432 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1434 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1437 /*********************************************************************
1438 * _vswprintf (MSVCRT.@)
1440 int CDECL MSVCRT_vswprintf_l( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format,
1441 MSVCRT__locale_t locale, __ms_va_list args )
1443 return MSVCRT_vsnwprintf_l( str, INT_MAX, format, locale, args );
1446 /*********************************************************************
1447 * _vscwprintf (MSVCRT.@)
1449 int CDECL MSVCRT__vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1451 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1454 /*********************************************************************
1455 * _vscwprintf_l (MSVCRT.@)
1457 int CDECL MSVCRT__vscwprintf_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1459 return MSVCRT_vsnwprintf_l( NULL, INT_MAX, format, locale, args );
1462 /*********************************************************************
1463 * _vscwprintf_p_l (MSVCRT.@)
1465 int CDECL MSVCRT__vscwprintf_p_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1467 return MSVCRT_vswprintf_p_l_opt( NULL, INT_MAX, format, 0, locale, args );
1470 #if _MSVCR_VER>=80
1471 /*********************************************************************
1472 * _vscwprintf_p (MSVCR80.@)
1474 int CDECL MSVCRT__vscwprintf_p(const MSVCRT_wchar_t *format, __ms_va_list args)
1476 return MSVCRT_vswprintf_p_l_opt(NULL, INT_MAX, format, 0, NULL, args);
1478 #endif
1480 /*********************************************************************
1481 * vswprintf_s (MSVCRT.@)
1483 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1484 const MSVCRT_wchar_t* format, __ms_va_list args)
1486 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1489 /*********************************************************************
1490 * _vswprintf_s_l (MSVCRT.@)
1492 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1493 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1495 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1496 format, locale, args );
1499 static int MSVCRT_vsprintf_p_l_opt(char *buffer, MSVCRT_size_t length, const char *format,
1500 DWORD options, MSVCRT__locale_t locale, __ms_va_list args)
1502 static const char nullbyte = '\0';
1503 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1504 struct _str_ctx_a puts_ctx = {length, buffer};
1505 int ret;
1507 memset(args_ctx, 0, sizeof(args_ctx));
1509 ret = create_positional_ctx_a(args_ctx, format, args);
1510 if(ret < 0) {
1511 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1512 *MSVCRT__errno() = MSVCRT_EINVAL;
1513 return ret;
1514 } else if(ret == 0)
1515 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1516 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options, arg_clbk_valist, NULL, &args);
1517 else
1518 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1519 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
1520 arg_clbk_positional, args_ctx, NULL);
1522 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
1523 return ret;
1526 /*********************************************************************
1527 * _vsprintf_p_l (MSVCRT.@)
1529 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
1530 MSVCRT__locale_t locale, __ms_va_list args)
1532 return MSVCRT_vsprintf_p_l_opt(buffer, length, format, 0, locale, args);
1535 /*********************************************************************
1536 * _vsprintf_p (MSVCRT.@)
1538 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
1539 const char *format, __ms_va_list args)
1541 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
1544 #if _MSVCR_VER>=140
1545 /*********************************************************************
1546 * __stdio_common_vsprintf_p (UCRTBASE.@)
1548 int CDECL MSVCRT__stdio_common_vsprintf_p(unsigned __int64 options, char *buffer, MSVCRT_size_t length,
1549 const char *format, MSVCRT__locale_t locale, __ms_va_list args)
1551 if (options & ~UCRTBASE_PRINTF_MASK)
1552 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1553 return MSVCRT_vsprintf_p_l_opt(buffer, length, format, options & UCRTBASE_PRINTF_MASK, locale, args);
1555 #endif
1557 /*********************************************************************
1558 * _sprintf_p_l (MSVCRT.@)
1560 int WINAPIV MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
1561 const char *format, MSVCRT__locale_t locale, ...)
1563 __ms_va_list valist;
1564 int r;
1566 __ms_va_start(valist, locale);
1567 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
1568 __ms_va_end(valist);
1570 return r;
1573 /*********************************************************************
1574 * __swprintf_l (MSVCRT.@)
1576 int WINAPIV MSVCRT___swprintf_l( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
1577 MSVCRT__locale_t locale, ...)
1579 int retval;
1580 __ms_va_list valist;
1581 __ms_va_start(valist, locale);
1582 retval = MSVCRT_vswprintf_l(str, format, locale, valist);
1583 __ms_va_end(valist);
1584 return retval;
1587 #if _MSVCR_VER>=80
1588 /*********************************************************************
1589 * _sprintf_p (MSVCR80.@)
1591 int WINAPIV MSVCRT__sprintf_p(char *buffer, MSVCRT_size_t length, const char *format, ...)
1593 __ms_va_list valist;
1594 int r;
1596 __ms_va_start(valist, format);
1597 r = MSVCRT_vsprintf_p_l(buffer, length, format, NULL, valist);
1598 __ms_va_end(valist);
1600 return r;
1602 #endif
1604 /*********************************************************************
1605 * _swprintf_p_l (MSVCRT.@)
1607 int WINAPIV MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1608 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
1610 __ms_va_list valist;
1611 int r;
1613 __ms_va_start(valist, locale);
1614 r = MSVCRT_vswprintf_p_l_opt(buffer, length, format, 0, locale, valist);
1615 __ms_va_end(valist);
1617 return r;
1620 /*********************************************************************
1621 * _wcscoll_l (MSVCRT.@)
1623 int CDECL MSVCRT__wcscoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
1625 MSVCRT_pthreadlocinfo locinfo;
1627 if(!locale)
1628 locinfo = get_locinfo();
1629 else
1630 locinfo = locale->locinfo;
1632 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
1633 return strcmpW(str1, str2);
1634 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
1637 /*********************************************************************
1638 * wcscoll (MSVCRT.@)
1640 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1642 return MSVCRT__wcscoll_l(str1, str2, NULL);
1645 /*********************************************************************
1646 * wcspbrk (MSVCRT.@)
1648 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1650 const MSVCRT_wchar_t* p;
1652 while (*str)
1654 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1655 str++;
1657 return NULL;
1660 /*********************************************************************
1661 * wcstok_s (MSVCRT.@)
1663 MSVCRT_wchar_t * CDECL MSVCRT_wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1664 MSVCRT_wchar_t **next_token )
1666 MSVCRT_wchar_t *ret;
1668 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1669 if (!MSVCRT_CHECK_PMT(next_token != NULL)) return NULL;
1670 if (!MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) return NULL;
1672 if (!str) str = *next_token;
1674 while (*str && strchrW( delim, *str )) str++;
1675 if (!*str) return NULL;
1676 ret = str++;
1677 while (*str && !strchrW( delim, *str )) str++;
1678 if (*str) *str++ = 0;
1679 *next_token = str;
1680 return ret;
1683 /*********************************************************************
1684 * wcstok (MSVCRT.@)
1686 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1688 return MSVCRT_wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1691 /*********************************************************************
1692 * _wctomb_s_l (MSVCRT.@)
1694 int CDECL MSVCRT__wctomb_s_l(int *len, char *mbchar, MSVCRT_size_t size,
1695 MSVCRT_wchar_t wch, MSVCRT__locale_t locale)
1697 MSVCRT_pthreadlocinfo locinfo;
1698 BOOL error;
1699 int mblen;
1701 if(!mbchar && size>0) {
1702 if(len)
1703 *len = 0;
1704 return 0;
1707 if(len)
1708 *len = -1;
1710 if(!MSVCRT_CHECK_PMT(size <= INT_MAX))
1711 return MSVCRT_EINVAL;
1713 if(!locale)
1714 locinfo = get_locinfo();
1715 else
1716 locinfo = locale->locinfo;
1718 if(!locinfo->lc_codepage) {
1719 if(wch > 0xff) {
1720 if(mbchar && size>0)
1721 memset(mbchar, 0, size);
1722 *MSVCRT__errno() = MSVCRT_EILSEQ;
1723 return MSVCRT_EILSEQ;
1726 if(!MSVCRT_CHECK_PMT_ERR(size >= 1, MSVCRT_ERANGE))
1727 return MSVCRT_ERANGE;
1729 *mbchar = wch;
1730 if(len)
1731 *len = 1;
1732 return 0;
1735 mblen = WideCharToMultiByte(locinfo->lc_codepage, 0, &wch, 1, mbchar, size, NULL, &error);
1736 if(!mblen || error) {
1737 if(!mblen && GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1738 if(mbchar && size>0)
1739 memset(mbchar, 0, size);
1741 MSVCRT_INVALID_PMT("insufficient buffer size", MSVCRT_ERANGE);
1742 return MSVCRT_ERANGE;
1745 *MSVCRT__errno() = MSVCRT_EILSEQ;
1746 return MSVCRT_EILSEQ;
1749 if(len)
1750 *len = mblen;
1751 return 0;
1754 /*********************************************************************
1755 * wctomb_s (MSVCRT.@)
1757 int CDECL MSVCRT_wctomb_s(int *len, char *mbchar, MSVCRT_size_t size, MSVCRT_wchar_t wch)
1759 return MSVCRT__wctomb_s_l(len, mbchar, size, wch, NULL);
1762 /*********************************************************************
1763 * _wctomb_l (MSVCRT.@)
1765 int CDECL MSVCRT__wctomb_l(char *dst, MSVCRT_wchar_t ch, MSVCRT__locale_t locale)
1767 int len;
1769 MSVCRT__wctomb_s_l(&len, dst, dst ? MSVCRT_MB_LEN_MAX : 0, ch, locale);
1770 return len;
1773 /*********************************************************************
1774 * wctomb (MSVCRT.@)
1776 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1778 return MSVCRT__wctomb_l(dst, ch, NULL);
1781 /*********************************************************************
1782 * wctob (MSVCRT.@)
1784 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1786 char out;
1787 BOOL error;
1788 UINT codepage = get_locinfo()->lc_codepage;
1790 if(!codepage) {
1791 if (wchar < 0xff)
1792 return (signed char)wchar;
1793 else
1794 return MSVCRT_EOF;
1795 } else if(WideCharToMultiByte( codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1796 return (INT)out;
1797 return MSVCRT_EOF;
1800 /*********************************************************************
1801 * wcrtomb (MSVCRT.@)
1803 MSVCRT_size_t CDECL MSVCRT_wcrtomb( char *dst, MSVCRT_wchar_t ch, MSVCRT_mbstate_t *s)
1805 if(s)
1806 *s = 0;
1807 return MSVCRT_wctomb(dst, ch);
1810 /*********************************************************************
1811 * iswalnum (MSVCRT.@)
1813 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1815 return isalnumW( wc );
1818 /*********************************************************************
1819 * _iswalnum_l (MSVCRT.@)
1821 int CDECL MSVCRT__iswalnum_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1823 return isalnumW( wc );
1826 /*********************************************************************
1827 * iswalpha (MSVCRT.@)
1829 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1831 return isalphaW( wc );
1834 /*********************************************************************
1835 * iswalpha_l (MSVCRT.@)
1837 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1839 return isalphaW( wc );
1842 /*********************************************************************
1843 * iswcntrl (MSVCRT.@)
1845 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1847 return iscntrlW( wc );
1850 /*********************************************************************
1851 * _iswcntrl_l (MSVCRT.@)
1853 int CDECL MSVCRT__iswcntrl_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1855 return iscntrlW( wc );
1858 /*********************************************************************
1859 * iswdigit (MSVCRT.@)
1861 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1863 return isdigitW( wc );
1866 /*********************************************************************
1867 * _iswdigit_l (MSVCRT.@)
1869 INT CDECL MSVCRT__iswdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1871 return isdigitW( wc );
1874 /*********************************************************************
1875 * iswgraph (MSVCRT.@)
1877 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1879 return isgraphW( wc );
1882 /*********************************************************************
1883 * _iswgraph_l (MSVCRT.@)
1885 int CDECL MSVCRT__iswgraph_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1887 return isgraphW( wc );
1890 /*********************************************************************
1891 * iswlower (MSVCRT.@)
1893 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1895 return islowerW( wc );
1898 /*********************************************************************
1899 * _iswlower_l (MSVCRT.@)
1901 int CDECL MSVCRT__iswlower_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1903 return islowerW( wc );
1906 /*********************************************************************
1907 * iswprint (MSVCRT.@)
1909 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1911 return isprintW( wc );
1914 /*********************************************************************
1915 * _iswprint_l (MSVCRT.@)
1917 int CDECL MSVCRT__iswprint_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1919 return isprintW( wc );
1922 /*********************************************************************
1923 * _iswpunct_l (MSVCRT.@)
1925 INT CDECL MSVCRT__iswpunct_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1927 return ispunctW( wc );
1930 /*********************************************************************
1931 * iswpunct (MSVCRT.@)
1933 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1935 return ispunctW( wc );
1938 /*********************************************************************
1939 * _iswspace_l (MSVCRT.@)
1941 INT CDECL MSVCRT__iswspace_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1943 return isspaceW( wc );
1946 /*********************************************************************
1947 * iswspace (MSVCRT.@)
1949 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1951 return isspaceW( wc );
1954 /*********************************************************************
1955 * iswupper (MSVCRT.@)
1957 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1959 return isupperW( wc );
1962 /*********************************************************************
1963 * _iswupper_l (MSVCRT.@)
1965 int CDECL MSVCRT__iswupper_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1967 return isupperW( wc );
1970 /*********************************************************************
1971 * iswxdigit (MSVCRT.@)
1973 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1975 return isxdigitW( wc );
1978 /*********************************************************************
1979 * _iswxdigit_l (MSVCRT.@)
1981 int CDECL MSVCRT__iswxdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1983 return isxdigitW( wc );
1986 /*********************************************************************
1987 * _iswctype_l (MSVCRT.@)
1989 INT CDECL MSVCRT__iswctype_l( MSVCRT_wchar_t wc, MSVCRT_wctype_t type, MSVCRT__locale_t locale )
1991 return (get_char_typeW(wc) & 0xffff) & type;
1994 /*********************************************************************
1995 * _iswblank_l (MSVCRT.@)
1997 INT CDECL MSVCRT__iswblank_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1999 return wc == '\t' || MSVCRT__iswctype_l( wc, MSVCRT__BLANK, locale );
2002 /*********************************************************************
2003 * iswblank (MSVCRT.@)
2005 INT CDECL MSVCRT_iswblank( MSVCRT_wchar_t wc )
2007 return wc == '\t' || MSVCRT__iswctype_l( wc, MSVCRT__BLANK, NULL );
2010 /*********************************************************************
2011 * wcscpy_s (MSVCRT.@)
2013 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
2015 MSVCRT_size_t size = 0;
2017 if(!MSVCRT_CHECK_PMT(wcDest)) return MSVCRT_EINVAL;
2018 if(!MSVCRT_CHECK_PMT(numElement)) return MSVCRT_EINVAL;
2020 wcDest[0] = 0;
2022 if(!MSVCRT_CHECK_PMT(wcSrc)) return MSVCRT_EINVAL;
2024 size = strlenW(wcSrc) + 1;
2026 if(!MSVCRT_CHECK_PMT_ERR(size <= numElement, MSVCRT_ERANGE))
2027 return MSVCRT_ERANGE;
2029 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
2031 return 0;
2034 /******************************************************************
2035 * wcsncpy (MSVCRT.@)
2037 MSVCRT_wchar_t* __cdecl MSVCRT_wcsncpy( MSVCRT_wchar_t* s1,
2038 const MSVCRT_wchar_t *s2, MSVCRT_size_t n )
2040 MSVCRT_size_t i;
2042 for(i=0; i<n; i++)
2043 if(!(s1[i] = s2[i])) break;
2044 for(; i<n; i++)
2045 s1[i] = 0;
2046 return s1;
2049 /******************************************************************
2050 * wcsncpy_s (MSVCRT.@)
2052 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
2053 MSVCRT_size_t count )
2055 WCHAR *p = wcDest;
2056 BOOL truncate = (count == MSVCRT__TRUNCATE);
2058 if(!wcDest && !numElement && !count)
2059 return 0;
2061 if (!wcDest || !numElement)
2062 return MSVCRT_EINVAL;
2064 if (!wcSrc)
2066 *wcDest = 0;
2067 return count ? MSVCRT_EINVAL : 0;
2070 while (numElement && count && *wcSrc)
2072 *p++ = *wcSrc++;
2073 numElement--;
2074 count--;
2076 if (!numElement && truncate)
2078 *(p-1) = 0;
2079 return MSVCRT_STRUNCATE;
2081 else if (!numElement)
2083 *wcDest = 0;
2084 return MSVCRT_ERANGE;
2087 *p = 0;
2088 return 0;
2091 /******************************************************************
2092 * wcscat_s (MSVCRT.@)
2095 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
2097 MSVCRT_wchar_t* ptr = dst;
2099 if (!dst || elem == 0) return MSVCRT_EINVAL;
2100 if (!src)
2102 dst[0] = '\0';
2103 return MSVCRT_EINVAL;
2106 /* seek to end of dst string (or elem if no end of string is found */
2107 while (ptr < dst + elem && *ptr != '\0') ptr++;
2108 while (ptr < dst + elem)
2110 if ((*ptr++ = *src++) == '\0') return 0;
2112 /* not enough space */
2113 dst[0] = '\0';
2114 return MSVCRT_ERANGE;
2117 /*********************************************************************
2118 * wcsncat_s (MSVCRT.@)
2121 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
2122 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
2124 MSVCRT_size_t srclen;
2125 MSVCRT_wchar_t dststart;
2126 INT ret = 0;
2128 if (!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
2129 if (!MSVCRT_CHECK_PMT(elem > 0)) return MSVCRT_EINVAL;
2130 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return MSVCRT_EINVAL;
2132 if (count == 0)
2133 return 0;
2135 for (dststart = 0; dststart < elem; dststart++)
2137 if (dst[dststart] == '\0')
2138 break;
2140 if (dststart == elem)
2142 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", MSVCRT_EINVAL);
2143 return MSVCRT_EINVAL;
2146 if (count == MSVCRT__TRUNCATE)
2148 srclen = strlenW(src);
2149 if (srclen >= (elem - dststart))
2151 srclen = elem - dststart - 1;
2152 ret = MSVCRT_STRUNCATE;
2155 else
2156 srclen = min(strlenW(src), count);
2157 if (srclen < (elem - dststart))
2159 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
2160 dst[dststart+srclen] = '\0';
2161 return ret;
2163 MSVCRT_INVALID_PMT("dst[elem] is too small", MSVCRT_ERANGE);
2164 dst[0] = '\0';
2165 return MSVCRT_ERANGE;
2168 /*********************************************************************
2169 * wctoint (INTERNAL)
2171 static int wctoint(WCHAR c, int base)
2173 int v = -1;
2174 if ('0' <= c && c <= '9')
2175 v = c - '0';
2176 else if ('A' <= c && c <= 'Z')
2177 v = c - 'A' + 10;
2178 else if ('a' <= c && c <= 'z')
2179 v = c - 'a' + 10;
2180 else {
2181 /* NOTE: wine_fold_string(MAP_FOLDDIGITS) supports too many things. */
2182 /* Unicode points that contain digits 0-9; keep this sorted! */
2183 static const WCHAR zeros[] = {
2184 0x660, 0x6f0, 0x966, 0x9e6, 0xa66, 0xae6, 0xb66, 0xc66, 0xce6,
2185 0xd66, 0xe50, 0xed0, 0xf20, 0x1040, 0x17e0, 0x1810, 0xff10
2187 int i;
2188 for (i = 0; i < ARRAY_SIZE(zeros) && c >= zeros[i]; ++i) {
2189 if (zeros[i] <= c && c <= zeros[i] + 9) {
2190 v = c - zeros[i];
2191 break;
2195 return v < base ? v : -1;
2198 /*********************************************************************
2199 * _wcstoi64_l (MSVCRT.@)
2201 * FIXME: locale parameter is ignored
2203 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
2204 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
2206 BOOL negative = FALSE, empty = TRUE;
2207 __int64 ret = 0;
2209 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
2211 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
2212 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
2213 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
2215 if(endptr)
2216 *endptr = (MSVCRT_wchar_t*)nptr;
2218 while(isspaceW(*nptr)) nptr++;
2220 if(*nptr == '-') {
2221 negative = TRUE;
2222 nptr++;
2223 } else if(*nptr == '+')
2224 nptr++;
2226 if((base==0 || base==16) && wctoint(*nptr, 1)==0 && tolowerW(*(nptr+1))=='x') {
2227 base = 16;
2228 nptr += 2;
2231 if(base == 0) {
2232 if(wctoint(*nptr, 1)==0)
2233 base = 8;
2234 else
2235 base = 10;
2238 while(*nptr) {
2239 int v = wctoint(*nptr, base);
2240 if(v<0)
2241 break;
2243 if(negative)
2244 v = -v;
2246 nptr++;
2247 empty = FALSE;
2249 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
2250 ret = MSVCRT_I64_MAX;
2251 *MSVCRT__errno() = MSVCRT_ERANGE;
2252 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
2253 ret = MSVCRT_I64_MIN;
2254 *MSVCRT__errno() = MSVCRT_ERANGE;
2255 } else
2256 ret = ret*base + v;
2259 if(endptr && !empty)
2260 *endptr = (MSVCRT_wchar_t*)nptr;
2262 return ret;
2265 /*********************************************************************
2266 * _wcstoi64 (MSVCRT.@)
2268 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
2269 MSVCRT_wchar_t **endptr, int base)
2271 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
2274 /*********************************************************************
2275 * _wcstol_l (MSVCRT.@)
2277 MSVCRT_long CDECL MSVCRT__wcstol_l(const MSVCRT_wchar_t *s,
2278 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2280 __int64 ret = MSVCRT__wcstoi64_l(s, end, base, locale);
2282 if(ret > MSVCRT_LONG_MAX) {
2283 ret = MSVCRT_LONG_MAX;
2284 *MSVCRT__errno() = MSVCRT_ERANGE;
2285 }else if(ret < MSVCRT_LONG_MIN) {
2286 ret = MSVCRT_LONG_MIN;
2287 *MSVCRT__errno() = MSVCRT_ERANGE;
2289 return ret;
2292 /*********************************************************************
2293 * wcstol (MSVCRT.@)
2295 MSVCRT_long CDECL MSVCRT_wcstol(const MSVCRT_wchar_t *s,
2296 MSVCRT_wchar_t **end, int base)
2298 return MSVCRT__wcstol_l(s, end, base, NULL);
2301 /*********************************************************************
2302 * _wtoi_l (MSVCRT.@)
2304 int __cdecl MSVCRT__wtoi_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2306 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2308 if(ret > INT_MAX) {
2309 ret = INT_MAX;
2310 *MSVCRT__errno() = MSVCRT_ERANGE;
2311 } else if(ret < INT_MIN) {
2312 ret = INT_MIN;
2313 *MSVCRT__errno() = MSVCRT_ERANGE;
2315 return ret;
2318 /*********************************************************************
2319 * _wtoi (MSVCRT.@)
2321 int __cdecl MSVCRT__wtoi(const MSVCRT_wchar_t *str)
2323 return MSVCRT__wtoi_l(str, NULL);
2326 /*********************************************************************
2327 * _wtol_l (MSVCRT.@)
2329 MSVCRT_long __cdecl MSVCRT__wtol_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2331 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2333 if(ret > MSVCRT_LONG_MAX) {
2334 ret = MSVCRT_LONG_MAX;
2335 *MSVCRT__errno() = MSVCRT_ERANGE;
2336 } else if(ret < MSVCRT_LONG_MIN) {
2337 ret = MSVCRT_LONG_MIN;
2338 *MSVCRT__errno() = MSVCRT_ERANGE;
2340 return ret;
2343 /*********************************************************************
2344 * _wtol (MSVCRT.@)
2346 MSVCRT_long __cdecl MSVCRT__wtol(const MSVCRT_wchar_t *str)
2348 return MSVCRT__wtol_l(str, NULL);
2351 #if _MSVCR_VER>=120
2353 /*********************************************************************
2354 * _wtoll_l (MSVCR120.@)
2356 MSVCRT_longlong __cdecl MSVCRT__wtoll_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2358 return MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2361 /*********************************************************************
2362 * _wtoll (MSVCR120.@)
2364 MSVCRT_longlong __cdecl MSVCRT__wtoll(const MSVCRT_wchar_t *str)
2366 return MSVCRT__wtoll_l(str, NULL);
2369 #endif /* _MSVCR_VER>=120 */
2371 /*********************************************************************
2372 * _wcstoui64_l (MSVCRT.@)
2374 * FIXME: locale parameter is ignored
2376 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
2377 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
2379 BOOL negative = FALSE, empty = TRUE;
2380 unsigned __int64 ret = 0;
2382 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
2384 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
2385 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
2386 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
2388 if(endptr)
2389 *endptr = (MSVCRT_wchar_t*)nptr;
2391 while(isspaceW(*nptr)) nptr++;
2393 if(*nptr == '-') {
2394 negative = TRUE;
2395 nptr++;
2396 } else if(*nptr == '+')
2397 nptr++;
2399 if((base==0 || base==16) && wctoint(*nptr, 1)==0 && tolowerW(*(nptr+1))=='x') {
2400 base = 16;
2401 nptr += 2;
2404 if(base == 0) {
2405 if(wctoint(*nptr, 1)==0)
2406 base = 8;
2407 else
2408 base = 10;
2411 while(*nptr) {
2412 int v = wctoint(*nptr, base);
2413 if(v<0)
2414 break;
2416 nptr++;
2417 empty = FALSE;
2419 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
2420 ret = MSVCRT_UI64_MAX;
2421 *MSVCRT__errno() = MSVCRT_ERANGE;
2422 } else
2423 ret = ret*base + v;
2426 if(endptr && !empty)
2427 *endptr = (MSVCRT_wchar_t*)nptr;
2429 return negative ? -ret : ret;
2432 /*********************************************************************
2433 * _wcstoui64 (MSVCRT.@)
2435 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
2436 MSVCRT_wchar_t **endptr, int base)
2438 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
2441 /*********************************************************************
2442 * _wcstoul_l (MSVCRT.@)
2444 MSVCRT_ulong __cdecl MSVCRT__wcstoul_l(const MSVCRT_wchar_t *s,
2445 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2447 __int64 ret = MSVCRT__wcstoui64_l(s, end, base, locale);
2449 if(ret > MSVCRT_ULONG_MAX) {
2450 ret = MSVCRT_ULONG_MAX;
2451 *MSVCRT__errno() = MSVCRT_ERANGE;
2453 return ret;
2456 /*********************************************************************
2457 * wcstoul (MSVCRT.@)
2459 MSVCRT_ulong __cdecl MSVCRT_wcstoul(const MSVCRT_wchar_t *s, MSVCRT_wchar_t **end, int base)
2461 return MSVCRT__wcstoul_l(s, end, base, NULL);
2464 /******************************************************************
2465 * wcsnlen (MSVCRT.@)
2467 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
2469 MSVCRT_size_t i;
2471 for (i = 0; i < maxlen; i++)
2472 if (!s[i]) break;
2473 return i;
2476 /*********************************************************************
2477 * _towupper_l (MSVCRT.@)
2479 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2481 MSVCRT_pthreadlocinfo locinfo;
2483 if(!locale)
2484 locinfo = get_locinfo();
2485 else
2486 locinfo = locale->locinfo;
2488 if(!locinfo->lc_handle[MSVCRT_LC_CTYPE]) {
2489 if(c >= 'a' && c <= 'z')
2490 return c + 'A' - 'a';
2491 return c;
2494 return toupperW(c);
2497 /*********************************************************************
2498 * towupper (MSVCRT.@)
2500 int CDECL MSVCRT_towupper(MSVCRT_wint_t c)
2502 return MSVCRT__towupper_l(c, NULL);
2505 /*********************************************************************
2506 * _towlower_l (MSVCRT.@)
2508 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2510 MSVCRT_pthreadlocinfo locinfo;
2512 if(!locale)
2513 locinfo = get_locinfo();
2514 else
2515 locinfo = locale->locinfo;
2517 if(!locinfo->lc_handle[MSVCRT_LC_CTYPE]) {
2518 if(c >= 'A' && c <= 'Z')
2519 return c + 'a' - 'A';
2520 return c;
2523 return tolowerW(c);
2526 /*********************************************************************
2527 * towlower (MSVCRT.@)
2529 int CDECL MSVCRT_towlower(MSVCRT_wint_t c)
2531 return MSVCRT__towlower_l(c, NULL);
2534 /*********************************************************************
2535 * wcschr (MSVCRT.@)
2537 MSVCRT_wchar_t* CDECL MSVCRT_wcschr(const MSVCRT_wchar_t *str, MSVCRT_wchar_t ch)
2539 return strchrW(str, ch);
2542 /*********************************************************************
2543 * wcsrchr (MSVCRT.@)
2545 MSVCRT_wchar_t* CDECL MSVCRT_wcsrchr(const MSVCRT_wchar_t *str, MSVCRT_wchar_t ch)
2547 return strrchrW(str, ch);
2550 /***********************************************************************
2551 * wcslen (MSVCRT.@)
2553 int CDECL MSVCRT_wcslen(const MSVCRT_wchar_t *str)
2555 return strlenW(str);
2558 /*********************************************************************
2559 * wcsstr (MSVCRT.@)
2561 MSVCRT_wchar_t* CDECL MSVCRT_wcsstr(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *sub)
2563 return strstrW(str, sub);
2566 /*********************************************************************
2567 * _wtoi64_l (MSVCRT.@)
2569 __int64 CDECL MSVCRT__wtoi64_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2571 ULONGLONG RunningTotal = 0;
2572 BOOL bMinus = FALSE;
2574 while (isspaceW(*str)) {
2575 str++;
2576 } /* while */
2578 if (*str == '+') {
2579 str++;
2580 } else if (*str == '-') {
2581 bMinus = TRUE;
2582 str++;
2583 } /* if */
2585 while (*str >= '0' && *str <= '9') {
2586 RunningTotal = RunningTotal * 10 + *str - '0';
2587 str++;
2588 } /* while */
2590 return bMinus ? -RunningTotal : RunningTotal;
2593 /*********************************************************************
2594 * _wtoi64 (MSVCRT.@)
2596 __int64 CDECL MSVCRT__wtoi64(const MSVCRT_wchar_t *str)
2598 return MSVCRT__wtoi64_l(str, NULL);
2601 /*********************************************************************
2602 * wcsncmp (MSVCRT.@)
2604 int CDECL MSVCRT_wcsncmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, int n)
2606 return strncmpW(str1, str2, n);
2609 /*********************************************************************
2610 * _wcsxfrm_l (MSVCRT.@)
2612 MSVCRT_size_t CDECL MSVCRT__wcsxfrm_l(MSVCRT_wchar_t *dest, const MSVCRT_wchar_t *src,
2613 MSVCRT_size_t len, MSVCRT__locale_t locale)
2615 MSVCRT_pthreadlocinfo locinfo;
2616 int i, ret;
2618 if(!MSVCRT_CHECK_PMT(src)) return INT_MAX;
2619 if(!MSVCRT_CHECK_PMT(dest || !len)) return INT_MAX;
2621 if(len > INT_MAX) {
2622 FIXME("len > INT_MAX not supported\n");
2623 len = INT_MAX;
2626 if(!locale)
2627 locinfo = get_locinfo();
2628 else
2629 locinfo = locale->locinfo;
2631 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE]) {
2632 MSVCRT_wcsncpy(dest, src, len);
2633 return strlenW(src);
2636 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2637 LCMAP_SORTKEY, src, -1, NULL, 0);
2638 if(!ret) {
2639 if(len) dest[0] = 0;
2640 *MSVCRT__errno() = MSVCRT_EILSEQ;
2641 return INT_MAX;
2643 if(!len) return ret-1;
2645 if(ret > len) {
2646 dest[0] = 0;
2647 *MSVCRT__errno() = MSVCRT_ERANGE;
2648 return ret-1;
2651 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2652 LCMAP_SORTKEY, src, -1, dest, len) - 1;
2653 for(i=ret; i>=0; i--)
2654 dest[i] = ((unsigned char*)dest)[i];
2655 return ret;
2658 /*********************************************************************
2659 * wcsxfrm (MSVCRT.@)
2661 MSVCRT_size_t CDECL MSVCRT_wcsxfrm(MSVCRT_wchar_t *dest,
2662 const MSVCRT_wchar_t *src, MSVCRT_size_t len)
2664 return MSVCRT__wcsxfrm_l(dest, src, len, NULL);
2667 /*********************************************************************
2668 * wcscmp (MSVCRT.@)
2670 int CDECL MSVCRT_wcscmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2)
2672 return strcmpW(str1, str2);