kernel32: Add GetConsoleFontInfo stub.
[wine.git] / dlls / msvcrt / wcs.c
blobc6091a82b941e08c5eb6336d6dd0c985445faf44
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 /*********************************************************************
44 * _get_printf_count_output (MSVCR80.@)
46 int CDECL MSVCRT__get_printf_count_output( void )
48 return n_format_enabled ? 1 : 0;
51 /*********************************************************************
52 * _set_printf_count_output (MSVCR80.@)
54 int CDECL MSVCRT__set_printf_count_output( int enable )
56 BOOL old = n_format_enabled;
57 n_format_enabled = enable != 0;
58 return old ? 1 : 0;
61 /*********************************************************************
62 * _wcsdup (MSVCRT.@)
64 MSVCRT_wchar_t* CDECL MSVCRT__wcsdup( const MSVCRT_wchar_t* str )
66 MSVCRT_wchar_t* ret = NULL;
67 if (str)
69 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
70 ret = MSVCRT_malloc( size );
71 if (ret) memcpy( ret, str, size );
73 return ret;
76 INT CDECL MSVCRT__wcsicmp_l(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, MSVCRT__locale_t locale)
78 if(!MSVCRT_CHECK_PMT(str1 != NULL) || !MSVCRT_CHECK_PMT(str2 != NULL))
79 return MSVCRT__NLSCMPERROR;
81 return strcmpiW(str1, str2);
84 /*********************************************************************
85 * _wcsicmp (MSVCRT.@)
87 INT CDECL MSVCRT__wcsicmp( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
89 return strcmpiW( str1, str2 );
92 /*********************************************************************
93 * _wcsnicmp_l (MSVCRT.@)
95 INT CDECL MSVCRT__wcsnicmp_l(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, INT n, MSVCRT__locale_t locale)
97 return strncmpiW(str1, str2, n);
100 /*********************************************************************
101 * _wcsnicmp (MSVCRT.@)
103 INT CDECL MSVCRT__wcsnicmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, INT n)
105 return MSVCRT__wcsnicmp_l(str1, str2, n, NULL);
108 /*********************************************************************
109 * _wcsicoll_l (MSVCRT.@)
111 int CDECL MSVCRT__wcsicoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
113 MSVCRT_pthreadlocinfo locinfo;
115 if(!locale)
116 locinfo = get_locinfo();
117 else
118 locinfo = locale->locinfo;
120 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
121 return strcmpiW(str1, str2);
122 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
123 str1, -1, str2, -1)-CSTR_EQUAL;
126 /*********************************************************************
127 * _wcsicoll (MSVCRT.@)
129 INT CDECL MSVCRT__wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
131 return MSVCRT__wcsicoll_l(str1, str2, NULL);
134 /*********************************************************************
135 * _wcsnicoll_l (MSVCRT.@)
137 int CDECL MSVCRT__wcsnicoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2,
138 MSVCRT_size_t count, MSVCRT__locale_t locale)
140 MSVCRT_pthreadlocinfo locinfo;
142 if(!locale)
143 locinfo = get_locinfo();
144 else
145 locinfo = locale->locinfo;
147 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
148 return strncmpiW(str1, str2, count);
149 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
150 str1, count, str2, count)-CSTR_EQUAL;
153 /*********************************************************************
154 * _wcsnicoll (MSVCRT.@)
156 INT CDECL MSVCRT__wcsnicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count )
158 return MSVCRT__wcsnicoll_l(str1, str2, count, NULL);
161 /*********************************************************************
162 * _wcsnset (MSVCRT.@)
164 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
166 MSVCRT_wchar_t* ret = str;
167 while ((n-- > 0) && *str) *str++ = c;
168 return ret;
171 /*********************************************************************
172 * _wcsrev (MSVCRT.@)
174 MSVCRT_wchar_t* CDECL MSVCRT__wcsrev( MSVCRT_wchar_t* str )
176 MSVCRT_wchar_t* ret = str;
177 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
178 while (end > str)
180 MSVCRT_wchar_t t = *end;
181 *end-- = *str;
182 *str++ = t;
184 return ret;
187 /*********************************************************************
188 * _wcsset_s (MSVCRT.@)
190 int CDECL MSVCRT__wcsset_s( MSVCRT_wchar_t *str, MSVCRT_size_t n, MSVCRT_wchar_t c )
192 MSVCRT_wchar_t *p = str;
194 if(!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
195 if(!MSVCRT_CHECK_PMT(n)) return MSVCRT_EINVAL;
197 while(*p && --n) *p++ = c;
198 if(!n) {
199 str[0] = 0;
200 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
201 *MSVCRT__errno() = MSVCRT_EINVAL;
202 return MSVCRT_EINVAL;
204 return 0;
207 /*********************************************************************
208 * _wcsset (MSVCRT.@)
210 MSVCRT_wchar_t* CDECL MSVCRT__wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
212 MSVCRT_wchar_t* ret = str;
213 while (*str) *str++ = c;
214 return ret;
217 /******************************************************************
218 * _wcsupr_s_l (MSVCRT.@)
220 int CDECL MSVCRT__wcsupr_s_l( MSVCRT_wchar_t* str, MSVCRT_size_t n,
221 MSVCRT__locale_t locale )
223 MSVCRT_wchar_t* ptr = str;
225 if (!str || !n)
227 if (str) *str = '\0';
228 *MSVCRT__errno() = MSVCRT_EINVAL;
229 return MSVCRT_EINVAL;
232 while (n--)
234 if (!*ptr) return 0;
235 /* FIXME: add locale support */
236 *ptr = toupperW(*ptr);
237 ptr++;
240 /* MSDN claims that the function should return and set errno to
241 * ERANGE, which doesn't seem to be true based on the tests. */
242 *str = '\0';
243 *MSVCRT__errno() = MSVCRT_EINVAL;
244 return MSVCRT_EINVAL;
247 /******************************************************************
248 * _wcsupr_s (MSVCRT.@)
251 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
253 return MSVCRT__wcsupr_s_l( str, n, NULL );
256 /******************************************************************
257 * _wcsupr_l (MSVCRT.@)
259 MSVCRT_wchar_t* CDECL MSVCRT__wcsupr_l( MSVCRT_wchar_t *str, MSVCRT__locale_t locale )
261 MSVCRT__wcsupr_s_l( str, -1, locale);
262 return str;
265 /******************************************************************
266 * _wcslwr_s_l (MSVCRT.@)
268 int CDECL MSVCRT__wcslwr_s_l( MSVCRT_wchar_t* str, MSVCRT_size_t n, MSVCRT__locale_t locale )
270 MSVCRT_wchar_t* ptr = str;
272 if (!str || !n)
274 if (str) *str = '\0';
275 *MSVCRT__errno() = MSVCRT_EINVAL;
276 return MSVCRT_EINVAL;
279 while (n--)
281 if (!*ptr) return 0;
282 *ptr = tolowerW(*ptr);
283 ptr++;
286 /* MSDN claims that the function should return and set errno to
287 * ERANGE, which doesn't seem to be true based on the tests. */
288 *str = '\0';
289 *MSVCRT__errno() = MSVCRT_EINVAL;
290 return MSVCRT_EINVAL;
293 /******************************************************************
294 * _wcslwr_s (MSVCRT.@)
296 int CDECL MSVCRT__wcslwr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
298 return MSVCRT__wcslwr_s_l(str, n, NULL);
301 /******************************************************************
302 * _wcslwr_l (MSVCRT.@)
304 MSVCRT_wchar_t* CDECL MSVCRT__wcslwr_l( MSVCRT_wchar_t* str, MSVCRT__locale_t locale )
306 MSVCRT__wcslwr_s_l(str, -1, locale);
307 return str;
310 /******************************************************************
311 * _wcslwr (MSVCRT.@)
313 MSVCRT_wchar_t* CDECL MSVCRT__wcslwr( MSVCRT_wchar_t* str )
315 MSVCRT__wcslwr_s_l(str, -1, NULL);
316 return str;
319 /*********************************************************************
320 * _wcsncoll_l (MSVCRT.@)
322 int CDECL MSVCRT__wcsncoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2,
323 MSVCRT_size_t count, MSVCRT__locale_t locale)
325 MSVCRT_pthreadlocinfo locinfo;
327 if(!locale)
328 locinfo = get_locinfo();
329 else
330 locinfo = locale->locinfo;
332 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
333 return strncmpW(str1, str2, count);
334 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, count, str2, count)-CSTR_EQUAL;
337 /*********************************************************************
338 * _wcsncoll (MSVCRT.@)
340 int CDECL MSVCRT__wcsncoll(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count)
342 return MSVCRT__wcsncoll_l(str1, str2, count, NULL);
345 /*********************************************************************
346 * _wcstod_l (MSVCRT.@)
348 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
349 MSVCRT__locale_t locale)
351 MSVCRT_pthreadlocinfo locinfo;
352 unsigned __int64 d=0, hlp;
353 unsigned fpcontrol;
354 int exp=0, sign=1;
355 const MSVCRT_wchar_t *p;
356 double ret;
357 long double lret=1, expcnt = 10;
358 BOOL found_digit = FALSE, negexp;
360 if (!MSVCRT_CHECK_PMT(str != NULL)) return 0;
362 if(!locale)
363 locinfo = get_locinfo();
364 else
365 locinfo = locale->locinfo;
367 p = str;
368 while(isspaceW(*p))
369 p++;
371 if(*p == '-') {
372 sign = -1;
373 p++;
374 } else if(*p == '+')
375 p++;
377 while(*p>='0' && *p<='9') {
378 found_digit = TRUE;
379 hlp = d*10+*(p++)-'0';
380 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
381 exp++;
382 break;
383 } else
384 d = hlp;
386 while(*p>='0' && *p<='9') {
387 exp++;
388 p++;
390 if(*p == *locinfo->lconv->decimal_point)
391 p++;
393 while(*p>='0' && *p<='9') {
394 found_digit = TRUE;
395 hlp = d*10+*(p++)-'0';
396 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
397 break;
399 d = hlp;
400 exp--;
402 while(*p>='0' && *p<='9')
403 p++;
405 if(!found_digit) {
406 if(end)
407 *end = (MSVCRT_wchar_t*)str;
408 return 0.0;
411 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
412 int e=0, s=1;
414 p++;
415 if(*p == '-') {
416 s = -1;
417 p++;
418 } else if(*p == '+')
419 p++;
421 if(*p>='0' && *p<='9') {
422 while(*p>='0' && *p<='9') {
423 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
424 e = INT_MAX;
425 p++;
427 e *= s;
429 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
430 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
431 else exp += e;
432 } else {
433 if(*p=='-' || *p=='+')
434 p--;
435 p--;
439 fpcontrol = _control87(0, 0);
440 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
441 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
443 negexp = (exp < 0);
444 if(negexp)
445 exp = -exp;
446 while(exp) {
447 if(exp & 1)
448 lret *= expcnt;
449 exp /= 2;
450 expcnt = expcnt*expcnt;
452 ret = (long double)sign * (negexp ? d/lret : d*lret);
454 _control87(fpcontrol, 0xffffffff);
456 if((d && ret==0.0) || isinf(ret))
457 *MSVCRT__errno() = MSVCRT_ERANGE;
459 if(end)
460 *end = (MSVCRT_wchar_t*)p;
462 return ret;
465 /*********************************************************************
466 * wcsrtombs_l (INTERNAL)
468 static MSVCRT_size_t MSVCRT_wcsrtombs_l(char *mbstr, const MSVCRT_wchar_t **wcstr,
469 MSVCRT_size_t count, MSVCRT__locale_t locale)
471 MSVCRT_pthreadlocinfo locinfo;
472 MSVCRT_size_t tmp = 0;
473 BOOL used_default;
475 if(!locale)
476 locinfo = get_locinfo();
477 else
478 locinfo = locale->locinfo;
480 if(!mbstr) {
481 tmp = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
482 *wcstr, -1, NULL, 0, NULL, &used_default)-1;
483 if(!tmp || used_default) {
484 *MSVCRT__errno() = MSVCRT_EILSEQ;
485 return -1;
487 return tmp;
490 while(**wcstr) {
491 char buf[3];
492 MSVCRT_size_t i, size;
494 size = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
495 *wcstr, 1, buf, 3, NULL, &used_default);
496 if(!size || used_default) {
497 *MSVCRT__errno() = MSVCRT_EILSEQ;
498 return -1;
500 if(tmp+size > count)
501 return tmp;
503 for(i=0; i<size; i++)
504 mbstr[tmp++] = buf[i];
505 (*wcstr)++;
508 if(tmp < count) {
509 mbstr[tmp] = '\0';
510 *wcstr = NULL;
512 return tmp;
515 /*********************************************************************
516 * _wcstombs_l (MSVCRT.@)
518 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
519 MSVCRT_size_t count, MSVCRT__locale_t locale)
521 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, locale);
524 /*********************************************************************
525 * wcstombs (MSVCRT.@)
527 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
528 MSVCRT_size_t count)
530 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, NULL);
533 /*********************************************************************
534 * wcsrtombs (MSVCRT.@)
536 MSVCRT_size_t CDECL MSVCRT_wcsrtombs(char *mbstr, const MSVCRT_wchar_t **wcstr,
537 MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
539 if(mbstate)
540 *mbstate = 0;
542 return MSVCRT_wcsrtombs_l(mbstr, wcstr, count, NULL);
545 /*********************************************************************
546 * MSVCRT_wcsrtombs_s_l (INTERNAL)
548 static int MSVCRT_wcsrtombs_s_l(MSVCRT_size_t *ret, char *mbstr,
549 MSVCRT_size_t size, const MSVCRT_wchar_t **wcstr,
550 MSVCRT_size_t count, MSVCRT__locale_t locale)
552 MSVCRT_size_t conv;
554 if(!mbstr && !size && wcstr) {
555 conv = MSVCRT_wcsrtombs_l(NULL, wcstr, 0, locale);
556 if(conv == -1)
557 return *MSVCRT__errno();
558 if(ret)
559 *ret = conv+1;
560 return 0;
563 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) return MSVCRT_EINVAL;
564 if (size) mbstr[0] = '\0';
565 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
566 if (!MSVCRT_CHECK_PMT(*wcstr != NULL)) return MSVCRT_EINVAL;
568 if(count==MSVCRT__TRUNCATE || size<count)
569 conv = size;
570 else
571 conv = count;
573 conv = MSVCRT_wcsrtombs_l(mbstr, wcstr, conv, locale);
574 if(conv == -1) {
575 if(size)
576 mbstr[0] = '\0';
577 return *MSVCRT__errno();
578 }else if(conv < size)
579 mbstr[conv++] = '\0';
580 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
581 mbstr[conv-1] = '\0';
582 else {
583 MSVCRT_INVALID_PMT("mbstr[size] is too small", MSVCRT_ERANGE);
584 if(size)
585 mbstr[0] = '\0';
586 return MSVCRT_ERANGE;
589 if(ret)
590 *ret = conv;
591 return 0;
594 /*********************************************************************
595 * _wcstombs_s_l (MSVCRT.@)
597 int CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
598 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
599 MSVCRT_size_t count, MSVCRT__locale_t locale)
601 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr,count, locale);
604 /*********************************************************************
605 * wcstombs_s (MSVCRT.@)
607 int CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
608 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
610 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr, count, NULL);
613 /*********************************************************************
614 * wcsrtombs_s (MSVCRT.@)
616 int CDECL MSVCRT_wcsrtombs_s(MSVCRT_size_t *ret, char *mbstr, MSVCRT_size_t size,
617 const MSVCRT_wchar_t **wcstr, MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
619 if(mbstate)
620 *mbstate = 0;
622 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, wcstr, count, NULL);
625 /*********************************************************************
626 * wcstod (MSVCRT.@)
628 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
630 return MSVCRT__wcstod_l(lpszStr, end, NULL);
633 /*********************************************************************
634 * _wtof (MSVCRT.@)
636 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
638 return MSVCRT__wcstod_l(str, NULL, NULL);
641 /*********************************************************************
642 * _wtof_l (MSVCRT.@)
644 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
646 return MSVCRT__wcstod_l(str, NULL, locale);
649 /*********************************************************************
650 * _wcstof_l (MSVCR120.@)
652 float CDECL MSVCRT__wcstof_l( const MSVCRT_wchar_t *str, MSVCRT_wchar_t **end, MSVCRT__locale_t locale )
654 return MSVCRT__wcstod_l(str, end, locale);
657 /*********************************************************************
658 * wcstof (MSVCR120.@)
660 float CDECL MSVCRT_wcstof( const MSVCRT_wchar_t *str, MSVCRT_wchar_t **end )
662 return MSVCRT__wcstof_l(str, end, NULL);
665 /*********************************************************************
666 * arg_clbk_valist (INTERNAL)
668 printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valist)
670 printf_arg ret;
672 if(type == VT_I8)
673 ret.get_longlong = va_arg(*valist, LONGLONG);
674 else if(type == VT_INT)
675 ret.get_int = va_arg(*valist, int);
676 else if(type == VT_R8)
677 ret.get_double = va_arg(*valist, double);
678 else if(type == VT_PTR)
679 ret.get_ptr = va_arg(*valist, void*);
680 else {
681 ERR("Incorrect type\n");
682 ret.get_int = 0;
685 return ret;
688 /*********************************************************************
689 * arg_clbk_positional (INTERNAL)
691 static printf_arg arg_clbk_positional(void *ctx, int pos, int type, __ms_va_list *valist)
693 printf_arg *args = ctx;
694 return args[pos];
697 /*********************************************************************
698 * _vsnprintf (MSVCRT.@)
700 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
701 const char *format, __ms_va_list valist )
703 static const char nullbyte = '\0';
704 struct _str_ctx_a ctx = {len, str};
705 int ret;
707 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, 0,
708 arg_clbk_valist, NULL, &valist);
709 puts_clbk_str_a(&ctx, 1, &nullbyte);
710 return ret;
713 static int puts_clbk_str_c99_a(void *ctx, int len, const char *str)
715 struct _str_ctx_a *out = ctx;
717 if(!out->buf)
718 return len;
720 if(out->len < len) {
721 memcpy(out->buf, str, out->len);
722 out->buf += out->len;
723 out->len = 0;
724 return len;
727 memcpy(out->buf, str, len);
728 out->buf += len;
729 out->len -= len;
730 return len;
733 /*********************************************************************
734 * __stdio_common_vsprintf (MSVCRT.@)
736 int CDECL MSVCRT__stdio_common_vsprintf( unsigned __int64 options, char *str, MSVCRT_size_t len, const char *format,
737 MSVCRT__locale_t locale, __ms_va_list valist )
739 static const char nullbyte = '\0';
740 struct _str_ctx_a ctx = {len, str};
741 int ret;
743 if (options & ~UCRTBASE_PRINTF_MASK)
744 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
745 ret = pf_printf_a(puts_clbk_str_c99_a,
746 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
747 puts_clbk_str_a(&ctx, 1, &nullbyte);
749 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
750 return ret>len ? -1 : ret;
751 if(ret>=len) {
752 if(len) str[len-1] = 0;
753 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
755 return ret;
758 /*********************************************************************
759 * _vsnprintf_l (MSVCRT.@)
761 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
762 MSVCRT__locale_t locale, __ms_va_list valist )
764 static const char nullbyte = '\0';
765 struct _str_ctx_a ctx = {len, str};
766 int ret;
768 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, 0,
769 arg_clbk_valist, NULL, &valist);
770 puts_clbk_str_a(&ctx, 1, &nullbyte);
771 return ret;
774 /*********************************************************************
775 * _vsprintf_l (MSVCRT.@)
777 int CDECL MSVCRT_vsprintf_l( char *str, const char *format,
778 MSVCRT__locale_t locale, __ms_va_list valist )
780 return MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
783 /*********************************************************************
784 * _sprintf_l (MSVCRT.@)
786 int CDECL MSVCRT_sprintf_l(char *str, const char *format,
787 MSVCRT__locale_t locale, ...)
789 int retval;
790 __ms_va_list valist;
791 __ms_va_start(valist, locale);
792 retval = MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
793 __ms_va_end(valist);
794 return retval;
797 static int CDECL MSVCRT_vsnprintf_s_l_opt( char *str, MSVCRT_size_t sizeOfBuffer,
798 MSVCRT_size_t count, const char *format, DWORD options,
799 MSVCRT__locale_t locale, __ms_va_list valist )
801 static const char nullbyte = '\0';
802 struct _str_ctx_a ctx;
803 int len, ret;
805 if(sizeOfBuffer<count+1 || count==-1)
806 len = sizeOfBuffer;
807 else
808 len = count+1;
810 ctx.len = len;
811 ctx.buf = str;
812 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
813 arg_clbk_valist, NULL, &valist);
814 puts_clbk_str_a(&ctx, 1, &nullbyte);
816 if(ret<0 || ret==len) {
817 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
818 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
819 memset(str, 0, sizeOfBuffer);
820 } else
821 str[len-1] = '\0';
823 return -1;
826 return ret;
829 /*********************************************************************
830 * _vsnprintf_s_l (MSVCRT.@)
832 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
833 MSVCRT_size_t count, const char *format,
834 MSVCRT__locale_t locale, __ms_va_list valist )
836 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, 0, locale, valist);
839 /*********************************************************************
840 * _vsprintf_s_l (MSVCRT.@)
842 int CDECL MSVCRT_vsprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
843 MSVCRT__locale_t locale, __ms_va_list valist )
845 return MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
848 /*********************************************************************
849 * _sprintf_s_l (MSVCRT.@)
851 int CDECL MSVCRT_sprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
852 MSVCRT__locale_t locale, ...)
854 int retval;
855 __ms_va_list valist;
856 __ms_va_start(valist, locale);
857 retval = MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
858 __ms_va_end(valist);
859 return retval;
862 /*********************************************************************
863 * _vsnprintf_s (MSVCRT.@)
865 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
866 MSVCRT_size_t count, const char *format, __ms_va_list valist )
868 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
871 /*********************************************************************
872 * __stdio_common_vsnprintf_s (MSVCRT.@)
874 int CDECL MSVCRT__stdio_common_vsnprintf_s( unsigned __int64 options,
875 char *str, MSVCRT_size_t sizeOfBuffer, MSVCRT_size_t count,
876 const char *format, MSVCRT__locale_t locale, __ms_va_list valist )
878 if (options & ~UCRTBASE_PRINTF_MASK)
879 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
880 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
883 /*********************************************************************
884 * __stdio_common_vsprintf_s (MSVCRT.@)
886 int CDECL MSVCRT__stdio_common_vsprintf_s( unsigned __int64 options,
887 char *str, MSVCRT_size_t count, const char *format,
888 MSVCRT__locale_t locale, __ms_va_list valist )
890 if (options & ~UCRTBASE_PRINTF_MASK)
891 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
892 return MSVCRT_vsnprintf_s_l_opt(str, INT_MAX, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
895 /*********************************************************************
896 * vsprintf (MSVCRT.@)
898 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
900 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
903 /*********************************************************************
904 * vsprintf_s (MSVCRT.@)
906 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
908 return MSVCRT_vsnprintf(str, num, format, valist);
911 /*********************************************************************
912 * _vscprintf (MSVCRT.@)
914 int CDECL MSVCRT__vscprintf( const char *format, __ms_va_list valist )
916 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
919 /*********************************************************************
920 * _vscprintf_p_l (MSVCRT.@)
922 int CDECL MSVCRT__vscprintf_p_l(const char *format,
923 MSVCRT__locale_t locale, __ms_va_list args)
925 printf_arg args_ctx[MSVCRT__ARGMAX+1];
926 struct _str_ctx_a puts_ctx = {INT_MAX, NULL};
927 int ret;
929 memset(args_ctx, 0, sizeof(args_ctx));
931 ret = create_positional_ctx_a(args_ctx, format, args);
932 if(ret < 0) {
933 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
934 *MSVCRT__errno() = MSVCRT_EINVAL;
935 return ret;
936 } else if(ret == 0) {
937 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
938 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
939 arg_clbk_valist, NULL, &args);
940 } else {
941 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
942 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
943 arg_clbk_positional, args_ctx, NULL);
946 return ret;
949 /*********************************************************************
950 * _vscprintf_p (MSVCR80.@)
952 int CDECL MSVCRT__vscprintf_p(const char *format, __ms_va_list argptr)
954 return MSVCRT__vscprintf_p_l(format, NULL, argptr);
957 /*********************************************************************
958 * _snprintf (MSVCRT.@)
960 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
962 int retval;
963 __ms_va_list valist;
964 __ms_va_start(valist, format);
965 retval = MSVCRT_vsnprintf(str, len, format, valist);
966 __ms_va_end(valist);
967 return retval;
970 /*********************************************************************
971 * _snprintf_l (MSVCRT.@)
973 int CDECL MSVCRT__snprintf_l(char *str, unsigned int count, MSVCRT__locale_t locale,
974 const char *format, ...)
976 int retval;
977 __ms_va_list valist;
978 __ms_va_start(valist, format);
979 retval = MSVCRT_vsnprintf_l(str, count, format, locale, valist);
980 __ms_va_end(valist);
981 return retval;
984 /*********************************************************************
985 * _snprintf_s (MSVCRT.@)
987 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
988 const char *format, ...)
990 int retval;
991 __ms_va_list valist;
992 __ms_va_start(valist, format);
993 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
994 __ms_va_end(valist);
995 return retval;
998 /*********************************************************************
999 * _scprintf (MSVCRT.@)
1001 int CDECL MSVCRT__scprintf(const char *format, ...)
1003 int retval;
1004 __ms_va_list valist;
1005 __ms_va_start(valist, format);
1006 retval = MSVCRT__vscprintf(format, valist);
1007 __ms_va_end(valist);
1008 return retval;
1011 /*********************************************************************
1012 * _vsnwprintf (MSVCRT.@)
1014 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1015 const MSVCRT_wchar_t *format, __ms_va_list valist)
1017 static const MSVCRT_wchar_t nullbyte = '\0';
1018 struct _str_ctx_w ctx = {len, str};
1019 int ret;
1021 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, 0,
1022 arg_clbk_valist, NULL, &valist);
1023 puts_clbk_str_w(&ctx, 1, &nullbyte);
1024 return ret;
1027 /*********************************************************************
1028 * _vsnwprintf_l (MSVCRT.@)
1030 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1031 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1032 __ms_va_list valist)
1034 static const MSVCRT_wchar_t nullbyte = '\0';
1035 struct _str_ctx_w ctx = {len, str};
1036 int ret;
1038 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, 0,
1039 arg_clbk_valist, NULL, &valist);
1040 puts_clbk_str_w(&ctx, 1, &nullbyte);
1041 return ret;
1044 /*********************************************************************
1045 * _vswprintf_p_l (MSVCRT.@)
1047 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1048 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
1050 static const MSVCRT_wchar_t nullbyte = '\0';
1051 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1052 struct _str_ctx_w puts_ctx = {length, buffer};
1053 int ret;
1055 memset(args_ctx, 0, sizeof(args_ctx));
1057 ret = create_positional_ctx_w(args_ctx, format, args);
1058 if(ret < 0) {
1059 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1060 *MSVCRT__errno() = MSVCRT_EINVAL;
1061 return ret;
1062 } else if(ret == 0)
1063 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1064 arg_clbk_valist, NULL, &args);
1065 else
1066 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale,
1067 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1068 arg_clbk_positional, args_ctx, NULL);
1070 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
1071 return ret;
1074 /*********************************************************************
1075 * _vswprintf_p (MSVCR100.@)
1077 int CDECL MSVCRT__vswprintf_p(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1078 const MSVCRT_wchar_t *format, __ms_va_list args)
1080 return MSVCRT_vswprintf_p_l(buffer, length, format, NULL, args);
1083 /*********************************************************************
1084 * _vsnwprintf_s_l (MSVCRT.@)
1086 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1087 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1088 MSVCRT__locale_t locale, __ms_va_list valist)
1090 static const MSVCRT_wchar_t nullbyte = '\0';
1091 struct _str_ctx_w ctx;
1092 int len, ret;
1094 len = sizeOfBuffer;
1095 if(count!=-1 && len>count+1)
1096 len = count+1;
1098 ctx.len = len;
1099 ctx.buf = str;
1100 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1101 arg_clbk_valist, NULL, &valist);
1102 puts_clbk_str_w(&ctx, 1, &nullbyte);
1104 if(ret<0 || ret==len) {
1105 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1106 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
1107 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
1108 } else
1109 str[len-1] = '\0';
1111 return -1;
1114 return ret;
1117 /*********************************************************************
1118 * _vsnwprintf_s (MSVCRT.@)
1120 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1121 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1123 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1124 format, NULL, valist);
1127 /*********************************************************************
1128 * _snwprintf (MSVCRT.@)
1130 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1132 int retval;
1133 __ms_va_list valist;
1134 __ms_va_start(valist, format);
1135 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1136 __ms_va_end(valist);
1137 return retval;
1140 /*********************************************************************
1141 * _snwprintf_l (MSVCRT.@)
1143 int CDECL MSVCRT__snwprintf_l( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format,
1144 MSVCRT__locale_t locale, ...)
1146 int retval;
1147 __ms_va_list valist;
1148 __ms_va_start(valist, locale);
1149 retval = MSVCRT_vsnwprintf_l(str, len, format, locale, valist);
1150 __ms_va_end(valist);
1151 return retval;
1154 /*********************************************************************
1155 * _snwprintf_s (MSVCRT.@)
1157 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1158 const MSVCRT_wchar_t *format, ...)
1160 int retval;
1161 __ms_va_list valist;
1162 __ms_va_start(valist, format);
1163 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1164 __ms_va_end(valist);
1165 return retval;
1168 /*********************************************************************
1169 * _snwprintf_s_l (MSVCRT.@)
1171 int CDECL MSVCRT__snwprintf_s_l( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1172 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1174 int retval;
1175 __ms_va_list valist;
1176 __ms_va_start(valist, locale);
1177 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, locale, valist);
1178 __ms_va_end(valist);
1179 return retval;
1182 static int puts_clbk_str_c99_w(void *ctx, int len, const MSVCRT_wchar_t *str)
1184 struct _str_ctx_w *out = ctx;
1186 if(!out->buf)
1187 return len;
1189 if(out->len < len) {
1190 memcpy(out->buf, str, out->len*sizeof(MSVCRT_wchar_t));
1191 out->buf += out->len;
1192 out->len = 0;
1193 return len;
1196 memcpy(out->buf, str, len*sizeof(MSVCRT_wchar_t));
1197 out->buf += len;
1198 out->len -= len;
1199 return len;
1202 /*********************************************************************
1203 * __stdio_common_vswprintf (MSVCRT.@)
1205 int CDECL MSVCRT__stdio_common_vswprintf( unsigned __int64 options,
1206 MSVCRT_wchar_t *str, MSVCRT_size_t len, const MSVCRT_wchar_t *format,
1207 MSVCRT__locale_t locale, __ms_va_list valist )
1209 static const MSVCRT_wchar_t nullbyte = '\0';
1210 struct _str_ctx_w ctx = {len, str};
1211 int ret;
1213 if (options & ~UCRTBASE_PRINTF_MASK)
1214 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1215 ret = pf_printf_w(puts_clbk_str_c99_w,
1216 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
1217 puts_clbk_str_w(&ctx, 1, &nullbyte);
1219 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
1220 return ret>len ? -1 : ret;
1221 if(ret>=len) {
1222 if(len) str[len-1] = 0;
1223 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
1225 return ret;
1228 /*********************************************************************
1229 * sprintf (MSVCRT.@)
1231 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1233 __ms_va_list ap;
1234 int r;
1236 __ms_va_start( ap, format );
1237 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1238 __ms_va_end( ap );
1239 return r;
1242 /*********************************************************************
1243 * sprintf_s (MSVCRT.@)
1245 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1247 __ms_va_list ap;
1248 int r;
1250 __ms_va_start( ap, format );
1251 r = MSVCRT_vsnprintf( str, num, format, ap );
1252 __ms_va_end( ap );
1253 return r;
1256 /*********************************************************************
1257 * _scwprintf (MSVCRT.@)
1259 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1261 __ms_va_list ap;
1262 int r;
1264 __ms_va_start( ap, format );
1265 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1266 __ms_va_end( ap );
1267 return r;
1270 /*********************************************************************
1271 * swprintf (MSVCRT.@)
1273 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1275 __ms_va_list ap;
1276 int r;
1278 __ms_va_start( ap, format );
1279 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1280 __ms_va_end( ap );
1281 return r;
1284 /*********************************************************************
1285 * swprintf_s (MSVCRT.@)
1287 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1288 const MSVCRT_wchar_t *format, ... )
1290 __ms_va_list ap;
1291 int r;
1293 __ms_va_start(ap, format);
1294 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1295 __ms_va_end(ap);
1297 return r;
1300 /*********************************************************************
1301 * _swprintf_s_l (MSVCRT.@)
1303 int CDECL MSVCRT__swprintf_s_l(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1304 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1306 __ms_va_list ap;
1307 int r;
1309 __ms_va_start(ap, locale);
1310 r = MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX, format, locale, ap);
1311 __ms_va_end(ap);
1313 return r;
1316 /*********************************************************************
1317 * _vswprintf (MSVCRT.@)
1319 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1321 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1324 /*********************************************************************
1325 * _vswprintf (MSVCRT.@)
1327 int CDECL MSVCRT_vswprintf_l( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format,
1328 MSVCRT__locale_t locale, __ms_va_list args )
1330 return MSVCRT_vsnwprintf_l( str, INT_MAX, format, locale, args );
1333 /*********************************************************************
1334 * _vscwprintf (MSVCRT.@)
1336 int CDECL MSVCRT__vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1338 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1341 /*********************************************************************
1342 * _vscwprintf_l (MSVCRT.@)
1344 int CDECL MSVCRT__vscwprintf_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1346 return MSVCRT_vsnwprintf_l( NULL, INT_MAX, format, locale, args );
1349 /*********************************************************************
1350 * _vscwprintf_p_l (MSVCRT.@)
1352 int CDECL MSVCRT__vscwprintf_p_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1354 return MSVCRT_vswprintf_p_l( NULL, INT_MAX, format, locale, args );
1357 /*********************************************************************
1358 * _vscwprintf_p (MSVCR100.@)
1360 int CDECL MSVCRT__vscwprintf_p(const MSVCRT_wchar_t *format, __ms_va_list args)
1362 return MSVCRT_vswprintf_p_l(NULL, INT_MAX, format, NULL, args);
1365 /*********************************************************************
1366 * vswprintf_s (MSVCRT.@)
1368 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1369 const MSVCRT_wchar_t* format, __ms_va_list args)
1371 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1374 /*********************************************************************
1375 * _vswprintf_s_l (MSVCRT.@)
1377 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1378 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1380 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1381 format, locale, args );
1384 /*********************************************************************
1385 * _vsprintf_p_l (MSVCRT.@)
1387 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
1388 MSVCRT__locale_t locale, __ms_va_list args)
1390 static const char nullbyte = '\0';
1391 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1392 struct _str_ctx_a puts_ctx = {length, buffer};
1393 int ret;
1395 memset(args_ctx, 0, sizeof(args_ctx));
1397 ret = create_positional_ctx_a(args_ctx, format, args);
1398 if(ret < 0) {
1399 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1400 *MSVCRT__errno() = MSVCRT_EINVAL;
1401 return ret;
1402 } else if(ret == 0)
1403 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1404 arg_clbk_valist, NULL, &args);
1405 else
1406 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1407 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1408 arg_clbk_positional, args_ctx, NULL);
1410 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
1411 return ret;
1414 /*********************************************************************
1415 * _vsprintf_p (MSVCRT.@)
1417 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
1418 const char *format, __ms_va_list args)
1420 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
1423 /*********************************************************************
1424 * _sprintf_p_l (MSVCRT.@)
1426 int CDECL MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
1427 const char *format, MSVCRT__locale_t locale, ...)
1429 __ms_va_list valist;
1430 int r;
1432 __ms_va_start(valist, locale);
1433 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
1434 __ms_va_end(valist);
1436 return r;
1439 /*********************************************************************
1440 * __swprintf_l (MSVCRT.@)
1442 int CDECL MSVCRT___swprintf_l( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
1443 MSVCRT__locale_t locale, ...)
1445 int retval;
1446 __ms_va_list valist;
1447 __ms_va_start(valist, locale);
1448 retval = MSVCRT_vswprintf_l(str, format, locale, valist);
1449 __ms_va_end(valist);
1450 return retval;
1453 /*********************************************************************
1454 * _sprintf_p (MSVCR100.@)
1456 int CDECL MSVCRT__sprintf_p(char *buffer, MSVCRT_size_t length, const char *format, ...)
1458 __ms_va_list valist;
1459 int r;
1461 __ms_va_start(valist, format);
1462 r = MSVCRT_vsprintf_p_l(buffer, length, format, NULL, valist);
1463 __ms_va_end(valist);
1465 return r;
1468 /*********************************************************************
1469 * _swprintf_p_l (MSVCRT.@)
1471 int CDECL MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1472 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
1474 __ms_va_list valist;
1475 int r;
1477 __ms_va_start(valist, locale);
1478 r = MSVCRT_vswprintf_p_l(buffer, length, format, locale, valist);
1479 __ms_va_end(valist);
1481 return r;
1484 /*********************************************************************
1485 * _wcscoll_l (MSVCRT.@)
1487 int CDECL MSVCRT__wcscoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
1489 MSVCRT_pthreadlocinfo locinfo;
1491 if(!locale)
1492 locinfo = get_locinfo();
1493 else
1494 locinfo = locale->locinfo;
1496 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
1497 return strcmpW(str1, str2);
1498 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
1501 /*********************************************************************
1502 * wcscoll (MSVCRT.@)
1504 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1506 return MSVCRT__wcscoll_l(str1, str2, NULL);
1509 /*********************************************************************
1510 * wcspbrk (MSVCRT.@)
1512 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1514 const MSVCRT_wchar_t* p;
1516 while (*str)
1518 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1519 str++;
1521 return NULL;
1524 /*********************************************************************
1525 * wcstok_s (MSVCRT.@)
1527 MSVCRT_wchar_t * CDECL MSVCRT_wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1528 MSVCRT_wchar_t **next_token )
1530 MSVCRT_wchar_t *ret;
1532 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1533 if (!MSVCRT_CHECK_PMT(next_token != NULL)) return NULL;
1534 if (!MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) return NULL;
1536 if (!str) str = *next_token;
1538 while (*str && strchrW( delim, *str )) str++;
1539 if (!*str) return NULL;
1540 ret = str++;
1541 while (*str && !strchrW( delim, *str )) str++;
1542 if (*str) *str++ = 0;
1543 *next_token = str;
1544 return ret;
1547 /*********************************************************************
1548 * wcstok (MSVCRT.@)
1550 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1552 return MSVCRT_wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1555 /*********************************************************************
1556 * _wctomb_s_l (MSVCRT.@)
1558 int CDECL MSVCRT__wctomb_s_l(int *len, char *mbchar, MSVCRT_size_t size,
1559 MSVCRT_wchar_t wch, MSVCRT__locale_t locale)
1561 MSVCRT_pthreadlocinfo locinfo;
1562 BOOL error;
1563 int mblen;
1565 if(!mbchar && size>0) {
1566 if(len)
1567 *len = 0;
1568 return 0;
1571 if(len)
1572 *len = -1;
1574 if(!MSVCRT_CHECK_PMT(size <= INT_MAX))
1575 return MSVCRT_EINVAL;
1577 if(!locale)
1578 locinfo = get_locinfo();
1579 else
1580 locinfo = locale->locinfo;
1582 if(!locinfo->lc_codepage) {
1583 if(wch > 0xff) {
1584 if(mbchar && size>0)
1585 memset(mbchar, 0, size);
1586 *MSVCRT__errno() = MSVCRT_EILSEQ;
1587 return MSVCRT_EILSEQ;
1590 if(!MSVCRT_CHECK_PMT_ERR(size >= 1, MSVCRT_ERANGE))
1591 return MSVCRT_ERANGE;
1593 *mbchar = wch;
1594 if(len)
1595 *len = 1;
1596 return 0;
1599 mblen = WideCharToMultiByte(locinfo->lc_codepage, 0, &wch, 1, mbchar, size, NULL, &error);
1600 if(!mblen || error) {
1601 if(!mblen && GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1602 if(mbchar && size>0)
1603 memset(mbchar, 0, size);
1605 MSVCRT_INVALID_PMT("insufficient buffer size", MSVCRT_ERANGE);
1606 return MSVCRT_ERANGE;
1609 *MSVCRT__errno() = MSVCRT_EILSEQ;
1610 return MSVCRT_EILSEQ;
1613 if(len)
1614 *len = mblen;
1615 return 0;
1618 /*********************************************************************
1619 * wctomb_s (MSVCRT.@)
1621 int CDECL MSVCRT_wctomb_s(int *len, char *mbchar, MSVCRT_size_t size, MSVCRT_wchar_t wch)
1623 return MSVCRT__wctomb_s_l(len, mbchar, size, wch, NULL);
1626 /*********************************************************************
1627 * _wctomb_l (MSVCRT.@)
1629 int CDECL MSVCRT__wctomb_l(char *dst, MSVCRT_wchar_t ch, MSVCRT__locale_t locale)
1631 int len;
1633 MSVCRT__wctomb_s_l(&len, dst, dst ? MSVCRT_MB_LEN_MAX : 0, ch, locale);
1634 return len;
1637 /*********************************************************************
1638 * wctomb (MSVCRT.@)
1640 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1642 return MSVCRT__wctomb_l(dst, ch, NULL);
1645 /*********************************************************************
1646 * wctob (MSVCRT.@)
1648 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1650 char out;
1651 BOOL error;
1652 UINT codepage = get_locinfo()->lc_codepage;
1654 if(!codepage) {
1655 if (wchar < 0xff)
1656 return (signed char)wchar;
1657 else
1658 return MSVCRT_EOF;
1659 } else if(WideCharToMultiByte( codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1660 return (INT)out;
1661 return MSVCRT_EOF;
1664 /*********************************************************************
1665 * wcrtomb (MSVCRT.@)
1667 MSVCRT_size_t CDECL MSVCRT_wcrtomb( char *dst, MSVCRT_wchar_t ch, MSVCRT_mbstate_t *s)
1669 if(s)
1670 *s = 0;
1671 return MSVCRT_wctomb(dst, ch);
1674 /*********************************************************************
1675 * iswalnum (MSVCRT.@)
1677 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1679 return isalnumW( wc );
1682 /*********************************************************************
1683 * iswalpha (MSVCRT.@)
1685 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1687 return isalphaW( wc );
1690 /*********************************************************************
1691 * iswalpha_l (MSVCRT.@)
1693 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1695 return isalphaW( wc );
1698 /*********************************************************************
1699 * iswcntrl (MSVCRT.@)
1701 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1703 return iscntrlW( wc );
1706 /*********************************************************************
1707 * iswdigit (MSVCRT.@)
1709 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1711 return isdigitW( wc );
1714 /*********************************************************************
1715 * _iswdigit_l (MSVCRT.@)
1717 INT CDECL MSVCRT__iswdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1719 return isdigitW( wc );
1722 /*********************************************************************
1723 * iswgraph (MSVCRT.@)
1725 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1727 return isgraphW( wc );
1730 /*********************************************************************
1731 * iswlower (MSVCRT.@)
1733 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1735 return islowerW( wc );
1738 /*********************************************************************
1739 * iswprint (MSVCRT.@)
1741 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1743 return isprintW( wc );
1746 /*********************************************************************
1747 * _iswpunct_l (MSVCRT.@)
1749 INT CDECL MSVCRT__iswpunct_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1751 return ispunctW( wc );
1754 /*********************************************************************
1755 * iswpunct (MSVCRT.@)
1757 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1759 return ispunctW( wc );
1762 /*********************************************************************
1763 * _iswspace_l (MSVCRT.@)
1765 INT CDECL MSVCRT__iswspace_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1767 return isspaceW( wc );
1770 /*********************************************************************
1771 * iswspace (MSVCRT.@)
1773 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1775 return isspaceW( wc );
1778 /*********************************************************************
1779 * iswupper (MSVCRT.@)
1781 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1783 return isupperW( wc );
1786 /*********************************************************************
1787 * iswxdigit (MSVCRT.@)
1789 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1791 return isxdigitW( wc );
1794 /*********************************************************************
1795 * wcscpy_s (MSVCRT.@)
1797 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1799 MSVCRT_size_t size = 0;
1801 if(!MSVCRT_CHECK_PMT(wcDest)) return MSVCRT_EINVAL;
1802 if(!MSVCRT_CHECK_PMT(numElement)) return MSVCRT_EINVAL;
1804 wcDest[0] = 0;
1806 if(!MSVCRT_CHECK_PMT(wcSrc)) return MSVCRT_EINVAL;
1808 size = strlenW(wcSrc) + 1;
1810 if(!MSVCRT_CHECK_PMT_ERR(size <= numElement, MSVCRT_ERANGE))
1811 return MSVCRT_ERANGE;
1813 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1815 return 0;
1818 /******************************************************************
1819 * wcsncpy (MSVCRT.@)
1821 MSVCRT_wchar_t* __cdecl MSVCRT_wcsncpy( MSVCRT_wchar_t* s1,
1822 const MSVCRT_wchar_t *s2, MSVCRT_size_t n )
1824 MSVCRT_size_t i;
1826 for(i=0; i<n; i++)
1827 if(!(s1[i] = s2[i])) break;
1828 for(; i<n; i++)
1829 s1[i] = 0;
1830 return s1;
1833 /******************************************************************
1834 * wcsncpy_s (MSVCRT.@)
1836 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1837 MSVCRT_size_t count )
1839 WCHAR *p = wcDest;
1840 BOOL truncate = (count == MSVCRT__TRUNCATE);
1842 if(!wcDest && !numElement && !count)
1843 return 0;
1845 if (!wcDest || !numElement)
1846 return MSVCRT_EINVAL;
1848 if (!wcSrc)
1850 *wcDest = 0;
1851 return count ? MSVCRT_EINVAL : 0;
1854 while (numElement && count && *wcSrc)
1856 *p++ = *wcSrc++;
1857 numElement--;
1858 count--;
1860 if (!numElement && truncate)
1862 *(p-1) = 0;
1863 return MSVCRT_STRUNCATE;
1865 else if (!numElement)
1867 *wcDest = 0;
1868 return MSVCRT_ERANGE;
1871 *p = 0;
1872 return 0;
1875 /******************************************************************
1876 * wcscat_s (MSVCRT.@)
1879 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1881 MSVCRT_wchar_t* ptr = dst;
1883 if (!dst || elem == 0) return MSVCRT_EINVAL;
1884 if (!src)
1886 dst[0] = '\0';
1887 return MSVCRT_EINVAL;
1890 /* seek to end of dst string (or elem if no end of string is found */
1891 while (ptr < dst + elem && *ptr != '\0') ptr++;
1892 while (ptr < dst + elem)
1894 if ((*ptr++ = *src++) == '\0') return 0;
1896 /* not enough space */
1897 dst[0] = '\0';
1898 return MSVCRT_ERANGE;
1901 /*********************************************************************
1902 * wcsncat_s (MSVCRT.@)
1905 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1906 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1908 MSVCRT_size_t srclen;
1909 MSVCRT_wchar_t dststart;
1910 INT ret = 0;
1912 if (!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
1913 if (!MSVCRT_CHECK_PMT(elem > 0)) return MSVCRT_EINVAL;
1914 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return MSVCRT_EINVAL;
1916 if (count == 0)
1917 return 0;
1919 for (dststart = 0; dststart < elem; dststart++)
1921 if (dst[dststart] == '\0')
1922 break;
1924 if (dststart == elem)
1926 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", MSVCRT_EINVAL);
1927 return MSVCRT_EINVAL;
1930 if (count == MSVCRT__TRUNCATE)
1932 srclen = strlenW(src);
1933 if (srclen >= (elem - dststart))
1935 srclen = elem - dststart - 1;
1936 ret = MSVCRT_STRUNCATE;
1939 else
1940 srclen = min(strlenW(src), count);
1941 if (srclen < (elem - dststart))
1943 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1944 dst[dststart+srclen] = '\0';
1945 return ret;
1947 MSVCRT_INVALID_PMT("dst[elem] is too small", MSVCRT_ERANGE);
1948 dst[0] = '\0';
1949 return MSVCRT_ERANGE;
1952 /*********************************************************************
1953 * _wcstoi64_l (MSVCRT.@)
1955 * FIXME: locale parameter is ignored
1957 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1958 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1960 BOOL negative = FALSE;
1961 __int64 ret = 0;
1963 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1965 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1966 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1967 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1969 while(isspaceW(*nptr)) nptr++;
1971 if(*nptr == '-') {
1972 negative = TRUE;
1973 nptr++;
1974 } else if(*nptr == '+')
1975 nptr++;
1977 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1978 base = 16;
1979 nptr += 2;
1982 if(base == 0) {
1983 if(*nptr=='0')
1984 base = 8;
1985 else
1986 base = 10;
1989 while(*nptr) {
1990 MSVCRT_wchar_t cur = tolowerW(*nptr);
1991 int v;
1993 if(cur>='0' && cur<='9') {
1994 if(cur >= '0'+base)
1995 break;
1996 v = cur-'0';
1997 } else {
1998 if(cur<'a' || cur>='a'+base-10)
1999 break;
2000 v = cur-'a'+10;
2003 if(negative)
2004 v = -v;
2006 nptr++;
2008 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
2009 ret = MSVCRT_I64_MAX;
2010 *MSVCRT__errno() = MSVCRT_ERANGE;
2011 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
2012 ret = MSVCRT_I64_MIN;
2013 *MSVCRT__errno() = MSVCRT_ERANGE;
2014 } else
2015 ret = ret*base + v;
2018 if(endptr)
2019 *endptr = (MSVCRT_wchar_t*)nptr;
2021 return ret;
2024 /*********************************************************************
2025 * _wcstoi64 (MSVCRT.@)
2027 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
2028 MSVCRT_wchar_t **endptr, int base)
2030 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
2033 /*********************************************************************
2034 * _wcstol_l (MSVCRT.@)
2036 MSVCRT_long CDECL MSVCRT__wcstol_l(const MSVCRT_wchar_t *s,
2037 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2039 __int64 ret = MSVCRT__wcstoi64_l(s, end, base, locale);
2041 if(ret > MSVCRT_LONG_MAX) {
2042 ret = MSVCRT_LONG_MAX;
2043 *MSVCRT__errno() = MSVCRT_ERANGE;
2044 }else if(ret < MSVCRT_LONG_MIN) {
2045 ret = MSVCRT_LONG_MIN;
2046 *MSVCRT__errno() = MSVCRT_ERANGE;
2048 return ret;
2051 /*********************************************************************
2052 * _wtoi_l (MSVCRT.@)
2054 int __cdecl MSVCRT__wtoi_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2056 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2058 if(ret > INT_MAX) {
2059 ret = INT_MAX;
2060 *MSVCRT__errno() = MSVCRT_ERANGE;
2061 } else if(ret < INT_MIN) {
2062 ret = INT_MIN;
2063 *MSVCRT__errno() = MSVCRT_ERANGE;
2065 return ret;
2068 /*********************************************************************
2069 * _wtoi (MSVCRT.@)
2071 int __cdecl MSVCRT__wtoi(const MSVCRT_wchar_t *str)
2073 return MSVCRT__wtoi_l(str, NULL);
2076 /*********************************************************************
2077 * _wtol_l (MSVCRT.@)
2079 MSVCRT_long __cdecl MSVCRT__wtol_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2081 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2083 if(ret > MSVCRT_LONG_MAX) {
2084 ret = MSVCRT_LONG_MAX;
2085 *MSVCRT__errno() = MSVCRT_ERANGE;
2086 } else if(ret < MSVCRT_LONG_MIN) {
2087 ret = MSVCRT_LONG_MIN;
2088 *MSVCRT__errno() = MSVCRT_ERANGE;
2090 return ret;
2093 /*********************************************************************
2094 * _wtol (MSVCRT.@)
2096 MSVCRT_long __cdecl MSVCRT__wtol(const MSVCRT_wchar_t *str)
2098 return MSVCRT__wtol_l(str, NULL);
2101 /*********************************************************************
2102 * _wtoll_l (MSVCR120.@)
2104 MSVCRT_longlong __cdecl MSVCRT__wtoll_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2106 return MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2109 /*********************************************************************
2110 * _wtoll (MSVCR120.@)
2112 MSVCRT_longlong __cdecl MSVCRT__wtoll(const MSVCRT_wchar_t *str)
2114 return MSVCRT__wtoll_l(str, NULL);
2117 /*********************************************************************
2118 * _wcstoui64_l (MSVCRT.@)
2120 * FIXME: locale parameter is ignored
2122 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
2123 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
2125 BOOL negative = FALSE;
2126 unsigned __int64 ret = 0;
2128 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
2130 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
2131 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
2132 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
2134 while(isspaceW(*nptr)) nptr++;
2136 if(*nptr == '-') {
2137 negative = TRUE;
2138 nptr++;
2139 } else if(*nptr == '+')
2140 nptr++;
2142 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
2143 base = 16;
2144 nptr += 2;
2147 if(base == 0) {
2148 if(*nptr=='0')
2149 base = 8;
2150 else
2151 base = 10;
2154 while(*nptr) {
2155 MSVCRT_wchar_t cur = tolowerW(*nptr);
2156 int v;
2158 if(cur>='0' && cur<='9') {
2159 if(cur >= '0'+base)
2160 break;
2161 v = *nptr-'0';
2162 } else {
2163 if(cur<'a' || cur>='a'+base-10)
2164 break;
2165 v = cur-'a'+10;
2168 nptr++;
2170 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
2171 ret = MSVCRT_UI64_MAX;
2172 *MSVCRT__errno() = MSVCRT_ERANGE;
2173 } else
2174 ret = ret*base + v;
2177 if(endptr)
2178 *endptr = (MSVCRT_wchar_t*)nptr;
2180 return negative ? -ret : ret;
2183 /*********************************************************************
2184 * _wcstoui64 (MSVCRT.@)
2186 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
2187 MSVCRT_wchar_t **endptr, int base)
2189 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
2192 /*********************************************************************
2193 * _wcstoul_l (MSVCRT.@)
2195 MSVCRT_ulong __cdecl MSVCRT__wcstoul_l(const MSVCRT_wchar_t *s,
2196 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2198 __int64 ret = MSVCRT__wcstoui64_l(s, end, base, locale);
2200 if(ret > MSVCRT_ULONG_MAX) {
2201 ret = MSVCRT_ULONG_MAX;
2202 *MSVCRT__errno() = MSVCRT_ERANGE;
2204 return ret;
2207 /*********************************************************************
2208 * wcstoul (MSVCRT.@)
2210 MSVCRT_ulong __cdecl MSVCRT_wcstoul(const MSVCRT_wchar_t *s, MSVCRT_wchar_t **end, int base)
2212 return MSVCRT__wcstoul_l(s, end, base, NULL);
2215 /******************************************************************
2216 * wcsnlen (MSVCRT.@)
2218 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
2220 MSVCRT_size_t i;
2222 for (i = 0; i < maxlen; i++)
2223 if (!s[i]) break;
2224 return i;
2227 /*********************************************************************
2228 * _towupper_l (MSVCRT.@)
2230 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2232 return toupperW(c);
2235 /*********************************************************************
2236 * towupper (MSVCRT.@)
2238 int CDECL MSVCRT_towupper(MSVCRT_wint_t c)
2240 return MSVCRT__towupper_l(c, NULL);
2243 /*********************************************************************
2244 * _towlower_l (MSVCRT.@)
2246 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2248 return tolowerW(c);
2251 /*********************************************************************
2252 * towlower (MSVCRT.@)
2254 int CDECL MSVCRT_towlower(MSVCRT_wint_t c)
2256 return MSVCRT__towlower_l(c, NULL);
2259 /*********************************************************************
2260 * wcschr (MSVCRT.@)
2262 MSVCRT_wchar_t* CDECL MSVCRT_wcschr(const MSVCRT_wchar_t *str, MSVCRT_wchar_t ch)
2264 return strchrW(str, ch);
2267 /***********************************************************************
2268 * wcslen (MSVCRT.@)
2270 int CDECL MSVCRT_wcslen(const MSVCRT_wchar_t *str)
2272 return strlenW(str);
2275 /*********************************************************************
2276 * wcsstr (MSVCRT.@)
2278 MSVCRT_wchar_t* CDECL MSVCRT_wcsstr(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *sub)
2280 return strstrW(str, sub);
2283 /*********************************************************************
2284 * _wtoi64_l (MSVCRT.@)
2286 __int64 CDECL MSVCRT__wtoi64_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2288 ULONGLONG RunningTotal = 0;
2289 BOOL bMinus = FALSE;
2291 while (isspaceW(*str)) {
2292 str++;
2293 } /* while */
2295 if (*str == '+') {
2296 str++;
2297 } else if (*str == '-') {
2298 bMinus = TRUE;
2299 str++;
2300 } /* if */
2302 while (*str >= '0' && *str <= '9') {
2303 RunningTotal = RunningTotal * 10 + *str - '0';
2304 str++;
2305 } /* while */
2307 return bMinus ? -RunningTotal : RunningTotal;
2310 /*********************************************************************
2311 * _wtoi64 (MSVCRT.@)
2313 __int64 CDECL MSVCRT__wtoi64(const MSVCRT_wchar_t *str)
2315 return MSVCRT__wtoi64_l(str, NULL);
2318 /*********************************************************************
2319 * wcsncmp (MSVCRT.@)
2321 int CDECL MSVCRT_wcsncmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, int n)
2323 return strncmpW(str1, str2, n);
2326 /*********************************************************************
2327 * _wcsxfrm_l (MSVCRT.@)
2329 MSVCRT_size_t CDECL MSVCRT__wcsxfrm_l(MSVCRT_wchar_t *dest, const MSVCRT_wchar_t *src,
2330 MSVCRT_size_t len, MSVCRT__locale_t locale)
2332 MSVCRT_pthreadlocinfo locinfo;
2333 int i, ret;
2335 if(!MSVCRT_CHECK_PMT(src)) return INT_MAX;
2336 if(!MSVCRT_CHECK_PMT(dest || !len)) return INT_MAX;
2338 if(len > INT_MAX) {
2339 FIXME("len > INT_MAX not supported\n");
2340 len = INT_MAX;
2343 if(!locale)
2344 locinfo = get_locinfo();
2345 else
2346 locinfo = locale->locinfo;
2348 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE]) {
2349 MSVCRT_wcsncpy(dest, src, len);
2350 return strlenW(src);
2353 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2354 LCMAP_SORTKEY, src, -1, NULL, 0);
2355 if(!ret) {
2356 if(len) dest[0] = 0;
2357 *MSVCRT__errno() = MSVCRT_EILSEQ;
2358 return INT_MAX;
2360 if(!len) return ret-1;
2362 if(ret > len) {
2363 dest[0] = 0;
2364 *MSVCRT__errno() = MSVCRT_ERANGE;
2365 return ret-1;
2368 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2369 LCMAP_SORTKEY, src, -1, dest, len) - 1;
2370 for(i=ret; i>=0; i--)
2371 dest[i] = ((unsigned char*)dest)[i];
2372 return ret;
2375 /*********************************************************************
2376 * wcsxfrm (MSVCRT.@)
2378 MSVCRT_size_t CDECL MSVCRT_wcsxfrm(MSVCRT_wchar_t *dest,
2379 const MSVCRT_wchar_t *src, MSVCRT_size_t len)
2381 return MSVCRT__wcsxfrm_l(dest, src, len, NULL);