gdi32: Allocate smaller buffer in delete_external_font_keys helper.
[wine.git] / dlls / msvcrt / wcs.c
bloba3429eaff5182b5e5b56c07a449abe12945f771e
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(!locinfo->lc_codepage) {
481 MSVCRT_size_t i;
483 if(!mbstr)
484 return strlenW(*wcstr);
486 for(i=0; i<count; i++) {
487 if((*wcstr)[i] > 255) {
488 *MSVCRT__errno() = MSVCRT_EILSEQ;
489 return -1;
492 mbstr[i] = (*wcstr)[i];
493 if(!(*wcstr)[i]) break;
495 return i;
498 if(!mbstr) {
499 tmp = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
500 *wcstr, -1, NULL, 0, NULL, &used_default)-1;
501 if(!tmp || used_default) {
502 *MSVCRT__errno() = MSVCRT_EILSEQ;
503 return -1;
505 return tmp;
508 while(**wcstr) {
509 char buf[3];
510 MSVCRT_size_t i, size;
512 size = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
513 *wcstr, 1, buf, 3, NULL, &used_default);
514 if(!size || used_default) {
515 *MSVCRT__errno() = MSVCRT_EILSEQ;
516 return -1;
518 if(tmp+size > count)
519 return tmp;
521 for(i=0; i<size; i++)
522 mbstr[tmp++] = buf[i];
523 (*wcstr)++;
526 if(tmp < count) {
527 mbstr[tmp] = '\0';
528 *wcstr = NULL;
530 return tmp;
533 /*********************************************************************
534 * _wcstombs_l (MSVCRT.@)
536 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
537 MSVCRT_size_t count, MSVCRT__locale_t locale)
539 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, locale);
542 /*********************************************************************
543 * wcstombs (MSVCRT.@)
545 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
546 MSVCRT_size_t count)
548 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, NULL);
551 /*********************************************************************
552 * wcsrtombs (MSVCRT.@)
554 MSVCRT_size_t CDECL MSVCRT_wcsrtombs(char *mbstr, const MSVCRT_wchar_t **wcstr,
555 MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
557 if(mbstate)
558 *mbstate = 0;
560 return MSVCRT_wcsrtombs_l(mbstr, wcstr, count, NULL);
563 /*********************************************************************
564 * MSVCRT_wcsrtombs_s_l (INTERNAL)
566 static int MSVCRT_wcsrtombs_s_l(MSVCRT_size_t *ret, char *mbstr,
567 MSVCRT_size_t size, const MSVCRT_wchar_t **wcstr,
568 MSVCRT_size_t count, MSVCRT__locale_t locale)
570 MSVCRT_size_t conv;
572 if(!mbstr && !size && wcstr) {
573 conv = MSVCRT_wcsrtombs_l(NULL, wcstr, 0, locale);
574 if(conv == -1)
575 return *MSVCRT__errno();
576 if(ret)
577 *ret = conv+1;
578 return 0;
581 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) return MSVCRT_EINVAL;
582 if (size) mbstr[0] = '\0';
583 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
584 if (!MSVCRT_CHECK_PMT(*wcstr != NULL)) return MSVCRT_EINVAL;
586 if(count==MSVCRT__TRUNCATE || size<count)
587 conv = size;
588 else
589 conv = count;
591 conv = MSVCRT_wcsrtombs_l(mbstr, wcstr, conv, locale);
592 if(conv == -1) {
593 if(size)
594 mbstr[0] = '\0';
595 return *MSVCRT__errno();
596 }else if(conv < size)
597 mbstr[conv++] = '\0';
598 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
599 mbstr[conv-1] = '\0';
600 else {
601 MSVCRT_INVALID_PMT("mbstr[size] is too small", MSVCRT_ERANGE);
602 if(size)
603 mbstr[0] = '\0';
604 return MSVCRT_ERANGE;
607 if(ret)
608 *ret = conv;
609 return 0;
612 /*********************************************************************
613 * _wcstombs_s_l (MSVCRT.@)
615 int CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
616 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
617 MSVCRT_size_t count, MSVCRT__locale_t locale)
619 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr,count, locale);
622 /*********************************************************************
623 * wcstombs_s (MSVCRT.@)
625 int CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
626 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
628 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr, count, NULL);
631 /*********************************************************************
632 * wcsrtombs_s (MSVCRT.@)
634 int CDECL MSVCRT_wcsrtombs_s(MSVCRT_size_t *ret, char *mbstr, MSVCRT_size_t size,
635 const MSVCRT_wchar_t **wcstr, MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
637 if(mbstate)
638 *mbstate = 0;
640 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, wcstr, count, NULL);
643 /*********************************************************************
644 * wcstod (MSVCRT.@)
646 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
648 return MSVCRT__wcstod_l(lpszStr, end, NULL);
651 /*********************************************************************
652 * _wtof (MSVCRT.@)
654 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
656 return MSVCRT__wcstod_l(str, NULL, NULL);
659 /*********************************************************************
660 * _wtof_l (MSVCRT.@)
662 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
664 return MSVCRT__wcstod_l(str, NULL, locale);
667 /*********************************************************************
668 * _wcstof_l (MSVCR120.@)
670 float CDECL MSVCRT__wcstof_l( const MSVCRT_wchar_t *str, MSVCRT_wchar_t **end, MSVCRT__locale_t locale )
672 return MSVCRT__wcstod_l(str, end, locale);
675 /*********************************************************************
676 * wcstof (MSVCR120.@)
678 float CDECL MSVCRT_wcstof( const MSVCRT_wchar_t *str, MSVCRT_wchar_t **end )
680 return MSVCRT__wcstof_l(str, end, NULL);
683 /*********************************************************************
684 * arg_clbk_valist (INTERNAL)
686 printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valist)
688 printf_arg ret;
690 if(type == VT_I8)
691 ret.get_longlong = va_arg(*valist, LONGLONG);
692 else if(type == VT_INT)
693 ret.get_int = va_arg(*valist, int);
694 else if(type == VT_R8)
695 ret.get_double = va_arg(*valist, double);
696 else if(type == VT_PTR)
697 ret.get_ptr = va_arg(*valist, void*);
698 else {
699 ERR("Incorrect type\n");
700 ret.get_int = 0;
703 return ret;
706 /*********************************************************************
707 * arg_clbk_positional (INTERNAL)
709 static printf_arg arg_clbk_positional(void *ctx, int pos, int type, __ms_va_list *valist)
711 printf_arg *args = ctx;
712 return args[pos];
715 /*********************************************************************
716 * _vsnprintf (MSVCRT.@)
718 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
719 const char *format, __ms_va_list valist )
721 static const char nullbyte = '\0';
722 struct _str_ctx_a ctx = {len, str};
723 int ret;
725 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, 0,
726 arg_clbk_valist, NULL, &valist);
727 puts_clbk_str_a(&ctx, 1, &nullbyte);
728 return ret;
731 static int puts_clbk_str_c99_a(void *ctx, int len, const char *str)
733 struct _str_ctx_a *out = ctx;
735 if(!out->buf)
736 return len;
738 if(out->len < len) {
739 memcpy(out->buf, str, out->len);
740 out->buf += out->len;
741 out->len = 0;
742 return len;
745 memcpy(out->buf, str, len);
746 out->buf += len;
747 out->len -= len;
748 return len;
751 /*********************************************************************
752 * __stdio_common_vsprintf (MSVCRT.@)
754 int CDECL MSVCRT__stdio_common_vsprintf( unsigned __int64 options, char *str, MSVCRT_size_t len, const char *format,
755 MSVCRT__locale_t locale, __ms_va_list valist )
757 static const char nullbyte = '\0';
758 struct _str_ctx_a ctx = {len, str};
759 int ret;
761 if (options & ~UCRTBASE_PRINTF_MASK)
762 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
763 ret = pf_printf_a(puts_clbk_str_c99_a,
764 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
765 puts_clbk_str_a(&ctx, 1, &nullbyte);
767 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
768 return ret>len ? -1 : ret;
769 if(ret>=len) {
770 if(len) str[len-1] = 0;
771 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
773 return ret;
776 /*********************************************************************
777 * _vsnprintf_l (MSVCRT.@)
779 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
780 MSVCRT__locale_t locale, __ms_va_list valist )
782 static const char nullbyte = '\0';
783 struct _str_ctx_a ctx = {len, str};
784 int ret;
786 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, 0,
787 arg_clbk_valist, NULL, &valist);
788 puts_clbk_str_a(&ctx, 1, &nullbyte);
789 return ret;
792 /*********************************************************************
793 * _vsprintf_l (MSVCRT.@)
795 int CDECL MSVCRT_vsprintf_l( char *str, const char *format,
796 MSVCRT__locale_t locale, __ms_va_list valist )
798 return MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
801 /*********************************************************************
802 * _sprintf_l (MSVCRT.@)
804 int CDECL MSVCRT_sprintf_l(char *str, const char *format,
805 MSVCRT__locale_t locale, ...)
807 int retval;
808 __ms_va_list valist;
809 __ms_va_start(valist, locale);
810 retval = MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
811 __ms_va_end(valist);
812 return retval;
815 static int CDECL MSVCRT_vsnprintf_s_l_opt( char *str, MSVCRT_size_t sizeOfBuffer,
816 MSVCRT_size_t count, const char *format, DWORD options,
817 MSVCRT__locale_t locale, __ms_va_list valist )
819 static const char nullbyte = '\0';
820 struct _str_ctx_a ctx;
821 int len, ret;
823 if(sizeOfBuffer<count+1 || count==-1)
824 len = sizeOfBuffer;
825 else
826 len = count+1;
828 ctx.len = len;
829 ctx.buf = str;
830 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
831 arg_clbk_valist, NULL, &valist);
832 puts_clbk_str_a(&ctx, 1, &nullbyte);
834 if(ret<0 || ret==len) {
835 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
836 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
837 memset(str, 0, sizeOfBuffer);
838 } else
839 str[len-1] = '\0';
841 return -1;
844 return ret;
847 static int MSVCRT_vsnwprintf_s_l_opt( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
848 MSVCRT_size_t count, const MSVCRT_wchar_t *format, DWORD options,
849 MSVCRT__locale_t locale, __ms_va_list valist)
851 static const MSVCRT_wchar_t nullbyte = '\0';
852 struct _str_ctx_w ctx;
853 int len, ret;
855 len = sizeOfBuffer;
856 if(count!=-1 && len>count+1)
857 len = count+1;
859 ctx.len = len;
860 ctx.buf = str;
861 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
862 arg_clbk_valist, NULL, &valist);
863 puts_clbk_str_w(&ctx, 1, &nullbyte);
865 if(ret<0 || ret==len) {
866 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
867 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
868 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
869 } else
870 str[len-1] = '\0';
872 return -1;
875 return ret;
878 /*********************************************************************
879 * _vsnprintf_s_l (MSVCRT.@)
881 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
882 MSVCRT_size_t count, const char *format,
883 MSVCRT__locale_t locale, __ms_va_list valist )
885 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, 0, locale, valist);
888 /*********************************************************************
889 * _vsprintf_s_l (MSVCRT.@)
891 int CDECL MSVCRT_vsprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
892 MSVCRT__locale_t locale, __ms_va_list valist )
894 return MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
897 /*********************************************************************
898 * _sprintf_s_l (MSVCRT.@)
900 int CDECL MSVCRT_sprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
901 MSVCRT__locale_t locale, ...)
903 int retval;
904 __ms_va_list valist;
905 __ms_va_start(valist, locale);
906 retval = MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
907 __ms_va_end(valist);
908 return retval;
911 /*********************************************************************
912 * _vsnprintf_s (MSVCRT.@)
914 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
915 MSVCRT_size_t count, const char *format, __ms_va_list valist )
917 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
920 /*********************************************************************
921 * __stdio_common_vsnprintf_s (MSVCRT.@)
923 int CDECL MSVCRT__stdio_common_vsnprintf_s( unsigned __int64 options,
924 char *str, MSVCRT_size_t sizeOfBuffer, MSVCRT_size_t count,
925 const char *format, MSVCRT__locale_t locale, __ms_va_list valist )
927 if (options & ~UCRTBASE_PRINTF_MASK)
928 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
929 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
932 /*********************************************************************
933 * __stdio_common_vsnwprintf_s (MSVCRT.@)
935 int CDECL MSVCRT__stdio_common_vsnwprintf_s( unsigned __int64 options,
936 MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer, MSVCRT_size_t count,
937 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list valist )
939 if (options & ~UCRTBASE_PRINTF_MASK)
940 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
941 return MSVCRT_vsnwprintf_s_l_opt(str, sizeOfBuffer, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
944 /*********************************************************************
945 * __stdio_common_vswprintf_s (MSVCRT.@)
947 int CDECL MSVCRT__stdio_common_vswprintf_s( unsigned __int64 options,
948 MSVCRT_wchar_t *str, MSVCRT_size_t count, const MSVCRT_wchar_t *format,
949 MSVCRT__locale_t locale, __ms_va_list valist )
951 return MSVCRT__stdio_common_vsnwprintf_s(options, str, INT_MAX, count, format, locale, valist);
954 /*********************************************************************
955 * __stdio_common_vsprintf_s (MSVCRT.@)
957 int CDECL MSVCRT__stdio_common_vsprintf_s( unsigned __int64 options,
958 char *str, MSVCRT_size_t count, const char *format,
959 MSVCRT__locale_t locale, __ms_va_list valist )
961 if (options & ~UCRTBASE_PRINTF_MASK)
962 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
963 return MSVCRT_vsnprintf_s_l_opt(str, INT_MAX, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
966 /*********************************************************************
967 * vsprintf (MSVCRT.@)
969 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
971 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
974 /*********************************************************************
975 * vsprintf_s (MSVCRT.@)
977 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
979 return MSVCRT_vsnprintf(str, num, format, valist);
982 /*********************************************************************
983 * _vscprintf (MSVCRT.@)
985 int CDECL MSVCRT__vscprintf( const char *format, __ms_va_list valist )
987 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
990 /*********************************************************************
991 * _vscprintf_p_l (MSVCRT.@)
993 int CDECL MSVCRT__vscprintf_p_l(const char *format,
994 MSVCRT__locale_t locale, __ms_va_list args)
996 printf_arg args_ctx[MSVCRT__ARGMAX+1];
997 struct _str_ctx_a puts_ctx = {INT_MAX, NULL};
998 int ret;
1000 memset(args_ctx, 0, sizeof(args_ctx));
1002 ret = create_positional_ctx_a(args_ctx, format, args);
1003 if(ret < 0) {
1004 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1005 *MSVCRT__errno() = MSVCRT_EINVAL;
1006 return ret;
1007 } else if(ret == 0) {
1008 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1009 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1010 arg_clbk_valist, NULL, &args);
1011 } else {
1012 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1013 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1014 arg_clbk_positional, args_ctx, NULL);
1017 return ret;
1020 /*********************************************************************
1021 * _vscprintf_p (MSVCR80.@)
1023 int CDECL MSVCRT__vscprintf_p(const char *format, __ms_va_list argptr)
1025 return MSVCRT__vscprintf_p_l(format, NULL, argptr);
1028 /*********************************************************************
1029 * _snprintf (MSVCRT.@)
1031 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
1033 int retval;
1034 __ms_va_list valist;
1035 __ms_va_start(valist, format);
1036 retval = MSVCRT_vsnprintf(str, len, format, valist);
1037 __ms_va_end(valist);
1038 return retval;
1041 /*********************************************************************
1042 * _snprintf_l (MSVCRT.@)
1044 int CDECL MSVCRT__snprintf_l(char *str, unsigned int count, MSVCRT__locale_t locale,
1045 const char *format, ...)
1047 int retval;
1048 __ms_va_list valist;
1049 __ms_va_start(valist, format);
1050 retval = MSVCRT_vsnprintf_l(str, count, format, locale, valist);
1051 __ms_va_end(valist);
1052 return retval;
1055 /*********************************************************************
1056 * _snprintf_s (MSVCRT.@)
1058 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
1059 const char *format, ...)
1061 int retval;
1062 __ms_va_list valist;
1063 __ms_va_start(valist, format);
1064 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
1065 __ms_va_end(valist);
1066 return retval;
1069 /*********************************************************************
1070 * _scprintf (MSVCRT.@)
1072 int CDECL MSVCRT__scprintf(const char *format, ...)
1074 int retval;
1075 __ms_va_list valist;
1076 __ms_va_start(valist, format);
1077 retval = MSVCRT__vscprintf(format, valist);
1078 __ms_va_end(valist);
1079 return retval;
1082 /*********************************************************************
1083 * _vsnwprintf (MSVCRT.@)
1085 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1086 const MSVCRT_wchar_t *format, __ms_va_list valist)
1088 static const MSVCRT_wchar_t nullbyte = '\0';
1089 struct _str_ctx_w ctx = {len, str};
1090 int ret;
1092 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, 0,
1093 arg_clbk_valist, NULL, &valist);
1094 puts_clbk_str_w(&ctx, 1, &nullbyte);
1095 return ret;
1098 /*********************************************************************
1099 * _vsnwprintf_l (MSVCRT.@)
1101 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1102 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1103 __ms_va_list valist)
1105 static const MSVCRT_wchar_t nullbyte = '\0';
1106 struct _str_ctx_w ctx = {len, str};
1107 int ret;
1109 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, 0,
1110 arg_clbk_valist, NULL, &valist);
1111 puts_clbk_str_w(&ctx, 1, &nullbyte);
1112 return ret;
1115 static int MSVCRT_vswprintf_p_l_opt(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1116 const MSVCRT_wchar_t *format, DWORD options, MSVCRT__locale_t locale, __ms_va_list args)
1118 static const MSVCRT_wchar_t nullbyte = '\0';
1119 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1120 struct _str_ctx_w puts_ctx = {length, buffer};
1121 int ret;
1123 memset(args_ctx, 0, sizeof(args_ctx));
1125 ret = create_positional_ctx_w(args_ctx, format, args);
1126 if(ret < 0) {
1127 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1128 *MSVCRT__errno() = MSVCRT_EINVAL;
1129 return ret;
1130 } else if(ret == 0)
1131 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
1132 arg_clbk_valist, NULL, &args);
1133 else
1134 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale,
1135 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
1136 arg_clbk_positional, args_ctx, NULL);
1138 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
1139 return ret;
1142 /*********************************************************************
1143 * _vswprintf_p_l (MSVCRT.@)
1145 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1146 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
1148 return MSVCRT_vswprintf_p_l_opt(buffer, length, format, 0, locale, args);
1151 /*********************************************************************
1152 * _vswprintf_p (MSVCR100.@)
1154 int CDECL MSVCRT__vswprintf_p(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1155 const MSVCRT_wchar_t *format, __ms_va_list args)
1157 return MSVCRT_vswprintf_p_l_opt(buffer, length, format, 0, NULL, args);
1161 /*********************************************************************
1162 * __stdio_common_vswprintf_p (MSVCRT.@)
1164 int CDECL MSVCRT__stdio_common_vswprintf_p( unsigned __int64 options,
1165 MSVCRT_wchar_t *str, MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1166 MSVCRT__locale_t locale, __ms_va_list valist )
1168 if (options & ~UCRTBASE_PRINTF_MASK)
1169 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1170 return MSVCRT_vswprintf_p_l_opt(str, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
1173 /*********************************************************************
1174 * _vsnwprintf_s_l (MSVCRT.@)
1176 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1177 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1178 MSVCRT__locale_t locale, __ms_va_list valist)
1180 return MSVCRT_vsnwprintf_s_l_opt(str, sizeOfBuffer, count, format, 0, locale, valist);
1183 /*********************************************************************
1184 * _vsnwprintf_s (MSVCRT.@)
1186 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1187 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1189 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1190 format, NULL, valist);
1193 /*********************************************************************
1194 * _snwprintf (MSVCRT.@)
1196 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1198 int retval;
1199 __ms_va_list valist;
1200 __ms_va_start(valist, format);
1201 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1202 __ms_va_end(valist);
1203 return retval;
1206 /*********************************************************************
1207 * _snwprintf_l (MSVCRT.@)
1209 int CDECL MSVCRT__snwprintf_l( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format,
1210 MSVCRT__locale_t locale, ...)
1212 int retval;
1213 __ms_va_list valist;
1214 __ms_va_start(valist, locale);
1215 retval = MSVCRT_vsnwprintf_l(str, len, format, locale, valist);
1216 __ms_va_end(valist);
1217 return retval;
1220 /*********************************************************************
1221 * _snwprintf_s (MSVCRT.@)
1223 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1224 const MSVCRT_wchar_t *format, ...)
1226 int retval;
1227 __ms_va_list valist;
1228 __ms_va_start(valist, format);
1229 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1230 __ms_va_end(valist);
1231 return retval;
1234 /*********************************************************************
1235 * _snwprintf_s_l (MSVCRT.@)
1237 int CDECL MSVCRT__snwprintf_s_l( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1238 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1240 int retval;
1241 __ms_va_list valist;
1242 __ms_va_start(valist, locale);
1243 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, locale, valist);
1244 __ms_va_end(valist);
1245 return retval;
1248 static int puts_clbk_str_c99_w(void *ctx, int len, const MSVCRT_wchar_t *str)
1250 struct _str_ctx_w *out = ctx;
1252 if(!out->buf)
1253 return len;
1255 if(out->len < len) {
1256 memcpy(out->buf, str, out->len*sizeof(MSVCRT_wchar_t));
1257 out->buf += out->len;
1258 out->len = 0;
1259 return len;
1262 memcpy(out->buf, str, len*sizeof(MSVCRT_wchar_t));
1263 out->buf += len;
1264 out->len -= len;
1265 return len;
1268 /*********************************************************************
1269 * __stdio_common_vswprintf (MSVCRT.@)
1271 int CDECL MSVCRT__stdio_common_vswprintf( unsigned __int64 options,
1272 MSVCRT_wchar_t *str, MSVCRT_size_t len, const MSVCRT_wchar_t *format,
1273 MSVCRT__locale_t locale, __ms_va_list valist )
1275 static const MSVCRT_wchar_t nullbyte = '\0';
1276 struct _str_ctx_w ctx = {len, str};
1277 int ret;
1279 if (options & ~UCRTBASE_PRINTF_MASK)
1280 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1281 ret = pf_printf_w(puts_clbk_str_c99_w,
1282 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
1283 puts_clbk_str_w(&ctx, 1, &nullbyte);
1285 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
1286 return ret>len ? -1 : ret;
1287 if(ret>=len) {
1288 if(len) str[len-1] = 0;
1289 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
1291 return ret;
1294 /*********************************************************************
1295 * sprintf (MSVCRT.@)
1297 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1299 __ms_va_list ap;
1300 int r;
1302 __ms_va_start( ap, format );
1303 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1304 __ms_va_end( ap );
1305 return r;
1308 /*********************************************************************
1309 * sprintf_s (MSVCRT.@)
1311 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1313 __ms_va_list ap;
1314 int r;
1316 __ms_va_start( ap, format );
1317 r = MSVCRT_vsnprintf( str, num, format, ap );
1318 __ms_va_end( ap );
1319 return r;
1322 /*********************************************************************
1323 * _scwprintf (MSVCRT.@)
1325 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1327 __ms_va_list ap;
1328 int r;
1330 __ms_va_start( ap, format );
1331 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1332 __ms_va_end( ap );
1333 return r;
1336 /*********************************************************************
1337 * swprintf (MSVCRT.@)
1339 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1341 __ms_va_list ap;
1342 int r;
1344 __ms_va_start( ap, format );
1345 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1346 __ms_va_end( ap );
1347 return r;
1350 /*********************************************************************
1351 * swprintf_s (MSVCRT.@)
1353 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1354 const MSVCRT_wchar_t *format, ... )
1356 __ms_va_list ap;
1357 int r;
1359 __ms_va_start(ap, format);
1360 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1361 __ms_va_end(ap);
1363 return r;
1366 /*********************************************************************
1367 * _swprintf_s_l (MSVCRT.@)
1369 int CDECL MSVCRT__swprintf_s_l(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1370 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1372 __ms_va_list ap;
1373 int r;
1375 __ms_va_start(ap, locale);
1376 r = MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX, format, locale, ap);
1377 __ms_va_end(ap);
1379 return r;
1382 /*********************************************************************
1383 * _vswprintf (MSVCRT.@)
1385 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1387 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1390 /*********************************************************************
1391 * _vswprintf (MSVCRT.@)
1393 int CDECL MSVCRT_vswprintf_l( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format,
1394 MSVCRT__locale_t locale, __ms_va_list args )
1396 return MSVCRT_vsnwprintf_l( str, INT_MAX, format, locale, args );
1399 /*********************************************************************
1400 * _vscwprintf (MSVCRT.@)
1402 int CDECL MSVCRT__vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1404 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1407 /*********************************************************************
1408 * _vscwprintf_l (MSVCRT.@)
1410 int CDECL MSVCRT__vscwprintf_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1412 return MSVCRT_vsnwprintf_l( NULL, INT_MAX, format, locale, args );
1415 /*********************************************************************
1416 * _vscwprintf_p_l (MSVCRT.@)
1418 int CDECL MSVCRT__vscwprintf_p_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1420 return MSVCRT_vswprintf_p_l_opt( NULL, INT_MAX, format, 0, locale, args );
1423 /*********************************************************************
1424 * _vscwprintf_p (MSVCR100.@)
1426 int CDECL MSVCRT__vscwprintf_p(const MSVCRT_wchar_t *format, __ms_va_list args)
1428 return MSVCRT_vswprintf_p_l_opt(NULL, INT_MAX, format, 0, NULL, args);
1431 /*********************************************************************
1432 * vswprintf_s (MSVCRT.@)
1434 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1435 const MSVCRT_wchar_t* format, __ms_va_list args)
1437 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1440 /*********************************************************************
1441 * _vswprintf_s_l (MSVCRT.@)
1443 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1444 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1446 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1447 format, locale, args );
1450 /*********************************************************************
1451 * _vsprintf_p_l (MSVCRT.@)
1453 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
1454 MSVCRT__locale_t locale, __ms_va_list args)
1456 static const char nullbyte = '\0';
1457 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1458 struct _str_ctx_a puts_ctx = {length, buffer};
1459 int ret;
1461 memset(args_ctx, 0, sizeof(args_ctx));
1463 ret = create_positional_ctx_a(args_ctx, format, args);
1464 if(ret < 0) {
1465 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1466 *MSVCRT__errno() = MSVCRT_EINVAL;
1467 return ret;
1468 } else if(ret == 0)
1469 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1470 arg_clbk_valist, NULL, &args);
1471 else
1472 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1473 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1474 arg_clbk_positional, args_ctx, NULL);
1476 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
1477 return ret;
1480 /*********************************************************************
1481 * _vsprintf_p (MSVCRT.@)
1483 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
1484 const char *format, __ms_va_list args)
1486 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
1489 /*********************************************************************
1490 * _sprintf_p_l (MSVCRT.@)
1492 int CDECL MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
1493 const char *format, MSVCRT__locale_t locale, ...)
1495 __ms_va_list valist;
1496 int r;
1498 __ms_va_start(valist, locale);
1499 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
1500 __ms_va_end(valist);
1502 return r;
1505 /*********************************************************************
1506 * __swprintf_l (MSVCRT.@)
1508 int CDECL MSVCRT___swprintf_l( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
1509 MSVCRT__locale_t locale, ...)
1511 int retval;
1512 __ms_va_list valist;
1513 __ms_va_start(valist, locale);
1514 retval = MSVCRT_vswprintf_l(str, format, locale, valist);
1515 __ms_va_end(valist);
1516 return retval;
1519 /*********************************************************************
1520 * _sprintf_p (MSVCR100.@)
1522 int CDECL MSVCRT__sprintf_p(char *buffer, MSVCRT_size_t length, const char *format, ...)
1524 __ms_va_list valist;
1525 int r;
1527 __ms_va_start(valist, format);
1528 r = MSVCRT_vsprintf_p_l(buffer, length, format, NULL, valist);
1529 __ms_va_end(valist);
1531 return r;
1534 /*********************************************************************
1535 * _swprintf_p_l (MSVCRT.@)
1537 int CDECL MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1538 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
1540 __ms_va_list valist;
1541 int r;
1543 __ms_va_start(valist, locale);
1544 r = MSVCRT_vswprintf_p_l_opt(buffer, length, format, 0, locale, valist);
1545 __ms_va_end(valist);
1547 return r;
1550 /*********************************************************************
1551 * _wcscoll_l (MSVCRT.@)
1553 int CDECL MSVCRT__wcscoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
1555 MSVCRT_pthreadlocinfo locinfo;
1557 if(!locale)
1558 locinfo = get_locinfo();
1559 else
1560 locinfo = locale->locinfo;
1562 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
1563 return strcmpW(str1, str2);
1564 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
1567 /*********************************************************************
1568 * wcscoll (MSVCRT.@)
1570 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1572 return MSVCRT__wcscoll_l(str1, str2, NULL);
1575 /*********************************************************************
1576 * wcspbrk (MSVCRT.@)
1578 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1580 const MSVCRT_wchar_t* p;
1582 while (*str)
1584 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1585 str++;
1587 return NULL;
1590 /*********************************************************************
1591 * wcstok_s (MSVCRT.@)
1593 MSVCRT_wchar_t * CDECL MSVCRT_wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1594 MSVCRT_wchar_t **next_token )
1596 MSVCRT_wchar_t *ret;
1598 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1599 if (!MSVCRT_CHECK_PMT(next_token != NULL)) return NULL;
1600 if (!MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) return NULL;
1602 if (!str) str = *next_token;
1604 while (*str && strchrW( delim, *str )) str++;
1605 if (!*str) return NULL;
1606 ret = str++;
1607 while (*str && !strchrW( delim, *str )) str++;
1608 if (*str) *str++ = 0;
1609 *next_token = str;
1610 return ret;
1613 /*********************************************************************
1614 * wcstok (MSVCRT.@)
1616 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1618 return MSVCRT_wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1621 /*********************************************************************
1622 * _wctomb_s_l (MSVCRT.@)
1624 int CDECL MSVCRT__wctomb_s_l(int *len, char *mbchar, MSVCRT_size_t size,
1625 MSVCRT_wchar_t wch, MSVCRT__locale_t locale)
1627 MSVCRT_pthreadlocinfo locinfo;
1628 BOOL error;
1629 int mblen;
1631 if(!mbchar && size>0) {
1632 if(len)
1633 *len = 0;
1634 return 0;
1637 if(len)
1638 *len = -1;
1640 if(!MSVCRT_CHECK_PMT(size <= INT_MAX))
1641 return MSVCRT_EINVAL;
1643 if(!locale)
1644 locinfo = get_locinfo();
1645 else
1646 locinfo = locale->locinfo;
1648 if(!locinfo->lc_codepage) {
1649 if(wch > 0xff) {
1650 if(mbchar && size>0)
1651 memset(mbchar, 0, size);
1652 *MSVCRT__errno() = MSVCRT_EILSEQ;
1653 return MSVCRT_EILSEQ;
1656 if(!MSVCRT_CHECK_PMT_ERR(size >= 1, MSVCRT_ERANGE))
1657 return MSVCRT_ERANGE;
1659 *mbchar = wch;
1660 if(len)
1661 *len = 1;
1662 return 0;
1665 mblen = WideCharToMultiByte(locinfo->lc_codepage, 0, &wch, 1, mbchar, size, NULL, &error);
1666 if(!mblen || error) {
1667 if(!mblen && GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1668 if(mbchar && size>0)
1669 memset(mbchar, 0, size);
1671 MSVCRT_INVALID_PMT("insufficient buffer size", MSVCRT_ERANGE);
1672 return MSVCRT_ERANGE;
1675 *MSVCRT__errno() = MSVCRT_EILSEQ;
1676 return MSVCRT_EILSEQ;
1679 if(len)
1680 *len = mblen;
1681 return 0;
1684 /*********************************************************************
1685 * wctomb_s (MSVCRT.@)
1687 int CDECL MSVCRT_wctomb_s(int *len, char *mbchar, MSVCRT_size_t size, MSVCRT_wchar_t wch)
1689 return MSVCRT__wctomb_s_l(len, mbchar, size, wch, NULL);
1692 /*********************************************************************
1693 * _wctomb_l (MSVCRT.@)
1695 int CDECL MSVCRT__wctomb_l(char *dst, MSVCRT_wchar_t ch, MSVCRT__locale_t locale)
1697 int len;
1699 MSVCRT__wctomb_s_l(&len, dst, dst ? MSVCRT_MB_LEN_MAX : 0, ch, locale);
1700 return len;
1703 /*********************************************************************
1704 * wctomb (MSVCRT.@)
1706 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1708 return MSVCRT__wctomb_l(dst, ch, NULL);
1711 /*********************************************************************
1712 * wctob (MSVCRT.@)
1714 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1716 char out;
1717 BOOL error;
1718 UINT codepage = get_locinfo()->lc_codepage;
1720 if(!codepage) {
1721 if (wchar < 0xff)
1722 return (signed char)wchar;
1723 else
1724 return MSVCRT_EOF;
1725 } else if(WideCharToMultiByte( codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1726 return (INT)out;
1727 return MSVCRT_EOF;
1730 /*********************************************************************
1731 * wcrtomb (MSVCRT.@)
1733 MSVCRT_size_t CDECL MSVCRT_wcrtomb( char *dst, MSVCRT_wchar_t ch, MSVCRT_mbstate_t *s)
1735 if(s)
1736 *s = 0;
1737 return MSVCRT_wctomb(dst, ch);
1740 /*********************************************************************
1741 * iswalnum (MSVCRT.@)
1743 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1745 return isalnumW( wc );
1748 /*********************************************************************
1749 * iswalpha (MSVCRT.@)
1751 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1753 return isalphaW( wc );
1756 /*********************************************************************
1757 * iswalpha_l (MSVCRT.@)
1759 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1761 return isalphaW( wc );
1764 /*********************************************************************
1765 * iswcntrl (MSVCRT.@)
1767 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1769 return iscntrlW( wc );
1772 /*********************************************************************
1773 * iswdigit (MSVCRT.@)
1775 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1777 return isdigitW( wc );
1780 /*********************************************************************
1781 * _iswdigit_l (MSVCRT.@)
1783 INT CDECL MSVCRT__iswdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1785 return isdigitW( wc );
1788 /*********************************************************************
1789 * iswgraph (MSVCRT.@)
1791 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1793 return isgraphW( wc );
1796 /*********************************************************************
1797 * iswlower (MSVCRT.@)
1799 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1801 return islowerW( wc );
1804 /*********************************************************************
1805 * iswprint (MSVCRT.@)
1807 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1809 return isprintW( wc );
1812 /*********************************************************************
1813 * _iswpunct_l (MSVCRT.@)
1815 INT CDECL MSVCRT__iswpunct_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1817 return ispunctW( wc );
1820 /*********************************************************************
1821 * iswpunct (MSVCRT.@)
1823 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1825 return ispunctW( wc );
1828 /*********************************************************************
1829 * _iswspace_l (MSVCRT.@)
1831 INT CDECL MSVCRT__iswspace_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1833 return isspaceW( wc );
1836 /*********************************************************************
1837 * iswspace (MSVCRT.@)
1839 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1841 return isspaceW( wc );
1844 /*********************************************************************
1845 * iswupper (MSVCRT.@)
1847 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1849 return isupperW( wc );
1852 /*********************************************************************
1853 * iswxdigit (MSVCRT.@)
1855 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1857 return isxdigitW( wc );
1860 /*********************************************************************
1861 * _iswctype_l (MSVCRT.@)
1863 INT CDECL MSVCRT__iswctype_l( MSVCRT_wchar_t wc, MSVCRT_wctype_t type, MSVCRT__locale_t locale )
1865 return (get_char_typeW(wc) & 0xffff) & type;
1868 /*********************************************************************
1869 * _iswblank_l (MSVCRT.@)
1871 INT CDECL MSVCRT__iswblank_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1873 return wc == '\t' || MSVCRT__iswctype_l( wc, MSVCRT__BLANK, locale );
1876 /*********************************************************************
1877 * iswblank (MSVCRT.@)
1879 INT CDECL MSVCRT_iswblank( MSVCRT_wchar_t wc )
1881 return wc == '\t' || MSVCRT__iswctype_l( wc, MSVCRT__BLANK, NULL );
1884 /*********************************************************************
1885 * wcscpy_s (MSVCRT.@)
1887 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1889 MSVCRT_size_t size = 0;
1891 if(!MSVCRT_CHECK_PMT(wcDest)) return MSVCRT_EINVAL;
1892 if(!MSVCRT_CHECK_PMT(numElement)) return MSVCRT_EINVAL;
1894 wcDest[0] = 0;
1896 if(!MSVCRT_CHECK_PMT(wcSrc)) return MSVCRT_EINVAL;
1898 size = strlenW(wcSrc) + 1;
1900 if(!MSVCRT_CHECK_PMT_ERR(size <= numElement, MSVCRT_ERANGE))
1901 return MSVCRT_ERANGE;
1903 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1905 return 0;
1908 /******************************************************************
1909 * wcsncpy (MSVCRT.@)
1911 MSVCRT_wchar_t* __cdecl MSVCRT_wcsncpy( MSVCRT_wchar_t* s1,
1912 const MSVCRT_wchar_t *s2, MSVCRT_size_t n )
1914 MSVCRT_size_t i;
1916 for(i=0; i<n; i++)
1917 if(!(s1[i] = s2[i])) break;
1918 for(; i<n; i++)
1919 s1[i] = 0;
1920 return s1;
1923 /******************************************************************
1924 * wcsncpy_s (MSVCRT.@)
1926 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1927 MSVCRT_size_t count )
1929 WCHAR *p = wcDest;
1930 BOOL truncate = (count == MSVCRT__TRUNCATE);
1932 if(!wcDest && !numElement && !count)
1933 return 0;
1935 if (!wcDest || !numElement)
1936 return MSVCRT_EINVAL;
1938 if (!wcSrc)
1940 *wcDest = 0;
1941 return count ? MSVCRT_EINVAL : 0;
1944 while (numElement && count && *wcSrc)
1946 *p++ = *wcSrc++;
1947 numElement--;
1948 count--;
1950 if (!numElement && truncate)
1952 *(p-1) = 0;
1953 return MSVCRT_STRUNCATE;
1955 else if (!numElement)
1957 *wcDest = 0;
1958 return MSVCRT_ERANGE;
1961 *p = 0;
1962 return 0;
1965 /******************************************************************
1966 * wcscat_s (MSVCRT.@)
1969 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1971 MSVCRT_wchar_t* ptr = dst;
1973 if (!dst || elem == 0) return MSVCRT_EINVAL;
1974 if (!src)
1976 dst[0] = '\0';
1977 return MSVCRT_EINVAL;
1980 /* seek to end of dst string (or elem if no end of string is found */
1981 while (ptr < dst + elem && *ptr != '\0') ptr++;
1982 while (ptr < dst + elem)
1984 if ((*ptr++ = *src++) == '\0') return 0;
1986 /* not enough space */
1987 dst[0] = '\0';
1988 return MSVCRT_ERANGE;
1991 /*********************************************************************
1992 * wcsncat_s (MSVCRT.@)
1995 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1996 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1998 MSVCRT_size_t srclen;
1999 MSVCRT_wchar_t dststart;
2000 INT ret = 0;
2002 if (!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
2003 if (!MSVCRT_CHECK_PMT(elem > 0)) return MSVCRT_EINVAL;
2004 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return MSVCRT_EINVAL;
2006 if (count == 0)
2007 return 0;
2009 for (dststart = 0; dststart < elem; dststart++)
2011 if (dst[dststart] == '\0')
2012 break;
2014 if (dststart == elem)
2016 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", MSVCRT_EINVAL);
2017 return MSVCRT_EINVAL;
2020 if (count == MSVCRT__TRUNCATE)
2022 srclen = strlenW(src);
2023 if (srclen >= (elem - dststart))
2025 srclen = elem - dststart - 1;
2026 ret = MSVCRT_STRUNCATE;
2029 else
2030 srclen = min(strlenW(src), count);
2031 if (srclen < (elem - dststart))
2033 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
2034 dst[dststart+srclen] = '\0';
2035 return ret;
2037 MSVCRT_INVALID_PMT("dst[elem] is too small", MSVCRT_ERANGE);
2038 dst[0] = '\0';
2039 return MSVCRT_ERANGE;
2042 /*********************************************************************
2043 * wctoint (INTERNAL)
2045 static int wctoint(WCHAR c, int base)
2047 int v = -1;
2048 if ('0' <= c && c <= '9')
2049 v = c - '0';
2050 else if ('A' <= c && c <= 'Z')
2051 v = c - 'A' + 10;
2052 else if ('a' <= c && c <= 'z')
2053 v = c - 'a' + 10;
2054 else {
2055 /* NOTE: wine_fold_string(MAP_FOLDDIGITS) supports too many things. */
2056 /* Unicode points that contain digits 0-9; keep this sorted! */
2057 static const WCHAR zeros[] = {
2058 0x660, 0x6f0, 0x966, 0x9e6, 0xa66, 0xae6, 0xb66, 0xc66, 0xce6,
2059 0xd66, 0xe50, 0xed0, 0xf20, 0x1040, 0x17e0, 0x1810, 0xff10
2061 int i;
2062 for (i = 0; i < sizeof(zeros)/sizeof(zeros[0]) && c >= zeros[i]; ++i) {
2063 if (zeros[i] <= c && c <= zeros[i] + 9) {
2064 v = c - zeros[i];
2065 break;
2069 return v < base ? v : -1;
2072 /*********************************************************************
2073 * _wcstoi64_l (MSVCRT.@)
2075 * FIXME: locale parameter is ignored
2077 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
2078 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
2080 BOOL negative = FALSE, empty = TRUE;
2081 __int64 ret = 0;
2083 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
2085 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
2086 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
2087 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
2089 if(endptr)
2090 *endptr = (MSVCRT_wchar_t*)nptr;
2092 while(isspaceW(*nptr)) nptr++;
2094 if(*nptr == '-') {
2095 negative = TRUE;
2096 nptr++;
2097 } else if(*nptr == '+')
2098 nptr++;
2100 if((base==0 || base==16) && wctoint(*nptr, 1)==0 && tolowerW(*(nptr+1))=='x') {
2101 base = 16;
2102 nptr += 2;
2105 if(base == 0) {
2106 if(wctoint(*nptr, 1)==0)
2107 base = 8;
2108 else
2109 base = 10;
2112 while(*nptr) {
2113 int v = wctoint(*nptr, base);
2114 if(v<0)
2115 break;
2117 if(negative)
2118 v = -v;
2120 nptr++;
2121 empty = FALSE;
2123 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
2124 ret = MSVCRT_I64_MAX;
2125 *MSVCRT__errno() = MSVCRT_ERANGE;
2126 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
2127 ret = MSVCRT_I64_MIN;
2128 *MSVCRT__errno() = MSVCRT_ERANGE;
2129 } else
2130 ret = ret*base + v;
2133 if(endptr && !empty)
2134 *endptr = (MSVCRT_wchar_t*)nptr;
2136 return ret;
2139 /*********************************************************************
2140 * _wcstoi64 (MSVCRT.@)
2142 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
2143 MSVCRT_wchar_t **endptr, int base)
2145 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
2148 /*********************************************************************
2149 * _wcstol_l (MSVCRT.@)
2151 MSVCRT_long CDECL MSVCRT__wcstol_l(const MSVCRT_wchar_t *s,
2152 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2154 __int64 ret = MSVCRT__wcstoi64_l(s, end, base, locale);
2156 if(ret > MSVCRT_LONG_MAX) {
2157 ret = MSVCRT_LONG_MAX;
2158 *MSVCRT__errno() = MSVCRT_ERANGE;
2159 }else if(ret < MSVCRT_LONG_MIN) {
2160 ret = MSVCRT_LONG_MIN;
2161 *MSVCRT__errno() = MSVCRT_ERANGE;
2163 return ret;
2166 /*********************************************************************
2167 * _wtoi_l (MSVCRT.@)
2169 int __cdecl MSVCRT__wtoi_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2171 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2173 if(ret > INT_MAX) {
2174 ret = INT_MAX;
2175 *MSVCRT__errno() = MSVCRT_ERANGE;
2176 } else if(ret < INT_MIN) {
2177 ret = INT_MIN;
2178 *MSVCRT__errno() = MSVCRT_ERANGE;
2180 return ret;
2183 /*********************************************************************
2184 * _wtoi (MSVCRT.@)
2186 int __cdecl MSVCRT__wtoi(const MSVCRT_wchar_t *str)
2188 return MSVCRT__wtoi_l(str, NULL);
2191 /*********************************************************************
2192 * _wtol_l (MSVCRT.@)
2194 MSVCRT_long __cdecl MSVCRT__wtol_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2196 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2198 if(ret > MSVCRT_LONG_MAX) {
2199 ret = MSVCRT_LONG_MAX;
2200 *MSVCRT__errno() = MSVCRT_ERANGE;
2201 } else if(ret < MSVCRT_LONG_MIN) {
2202 ret = MSVCRT_LONG_MIN;
2203 *MSVCRT__errno() = MSVCRT_ERANGE;
2205 return ret;
2208 /*********************************************************************
2209 * _wtol (MSVCRT.@)
2211 MSVCRT_long __cdecl MSVCRT__wtol(const MSVCRT_wchar_t *str)
2213 return MSVCRT__wtol_l(str, NULL);
2216 /*********************************************************************
2217 * _wtoll_l (MSVCR120.@)
2219 MSVCRT_longlong __cdecl MSVCRT__wtoll_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2221 return MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2224 /*********************************************************************
2225 * _wtoll (MSVCR120.@)
2227 MSVCRT_longlong __cdecl MSVCRT__wtoll(const MSVCRT_wchar_t *str)
2229 return MSVCRT__wtoll_l(str, NULL);
2232 /*********************************************************************
2233 * _wcstoui64_l (MSVCRT.@)
2235 * FIXME: locale parameter is ignored
2237 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
2238 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
2240 BOOL negative = FALSE, empty = TRUE;
2241 unsigned __int64 ret = 0;
2243 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
2245 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
2246 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
2247 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
2249 if(endptr)
2250 *endptr = (MSVCRT_wchar_t*)nptr;
2252 while(isspaceW(*nptr)) nptr++;
2254 if(*nptr == '-') {
2255 negative = TRUE;
2256 nptr++;
2257 } else if(*nptr == '+')
2258 nptr++;
2260 if((base==0 || base==16) && wctoint(*nptr, 1)==0 && tolowerW(*(nptr+1))=='x') {
2261 base = 16;
2262 nptr += 2;
2265 if(base == 0) {
2266 if(wctoint(*nptr, 1)==0)
2267 base = 8;
2268 else
2269 base = 10;
2272 while(*nptr) {
2273 int v = wctoint(*nptr, base);
2274 if(v<0)
2275 break;
2277 nptr++;
2278 empty = FALSE;
2280 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
2281 ret = MSVCRT_UI64_MAX;
2282 *MSVCRT__errno() = MSVCRT_ERANGE;
2283 } else
2284 ret = ret*base + v;
2287 if(endptr && !empty)
2288 *endptr = (MSVCRT_wchar_t*)nptr;
2290 return negative ? -ret : ret;
2293 /*********************************************************************
2294 * _wcstoui64 (MSVCRT.@)
2296 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
2297 MSVCRT_wchar_t **endptr, int base)
2299 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
2302 /*********************************************************************
2303 * _wcstoul_l (MSVCRT.@)
2305 MSVCRT_ulong __cdecl MSVCRT__wcstoul_l(const MSVCRT_wchar_t *s,
2306 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2308 __int64 ret = MSVCRT__wcstoui64_l(s, end, base, locale);
2310 if(ret > MSVCRT_ULONG_MAX) {
2311 ret = MSVCRT_ULONG_MAX;
2312 *MSVCRT__errno() = MSVCRT_ERANGE;
2314 return ret;
2317 /*********************************************************************
2318 * wcstoul (MSVCRT.@)
2320 MSVCRT_ulong __cdecl MSVCRT_wcstoul(const MSVCRT_wchar_t *s, MSVCRT_wchar_t **end, int base)
2322 return MSVCRT__wcstoul_l(s, end, base, NULL);
2325 /******************************************************************
2326 * wcsnlen (MSVCRT.@)
2328 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
2330 MSVCRT_size_t i;
2332 for (i = 0; i < maxlen; i++)
2333 if (!s[i]) break;
2334 return i;
2337 /*********************************************************************
2338 * _towupper_l (MSVCRT.@)
2340 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2342 return toupperW(c);
2345 /*********************************************************************
2346 * towupper (MSVCRT.@)
2348 int CDECL MSVCRT_towupper(MSVCRT_wint_t c)
2350 return MSVCRT__towupper_l(c, NULL);
2353 /*********************************************************************
2354 * _towlower_l (MSVCRT.@)
2356 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2358 return tolowerW(c);
2361 /*********************************************************************
2362 * towlower (MSVCRT.@)
2364 int CDECL MSVCRT_towlower(MSVCRT_wint_t c)
2366 return MSVCRT__towlower_l(c, NULL);
2369 /*********************************************************************
2370 * wcschr (MSVCRT.@)
2372 MSVCRT_wchar_t* CDECL MSVCRT_wcschr(const MSVCRT_wchar_t *str, MSVCRT_wchar_t ch)
2374 return strchrW(str, ch);
2377 /***********************************************************************
2378 * wcslen (MSVCRT.@)
2380 int CDECL MSVCRT_wcslen(const MSVCRT_wchar_t *str)
2382 return strlenW(str);
2385 /*********************************************************************
2386 * wcsstr (MSVCRT.@)
2388 MSVCRT_wchar_t* CDECL MSVCRT_wcsstr(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *sub)
2390 return strstrW(str, sub);
2393 /*********************************************************************
2394 * _wtoi64_l (MSVCRT.@)
2396 __int64 CDECL MSVCRT__wtoi64_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2398 ULONGLONG RunningTotal = 0;
2399 BOOL bMinus = FALSE;
2401 while (isspaceW(*str)) {
2402 str++;
2403 } /* while */
2405 if (*str == '+') {
2406 str++;
2407 } else if (*str == '-') {
2408 bMinus = TRUE;
2409 str++;
2410 } /* if */
2412 while (*str >= '0' && *str <= '9') {
2413 RunningTotal = RunningTotal * 10 + *str - '0';
2414 str++;
2415 } /* while */
2417 return bMinus ? -RunningTotal : RunningTotal;
2420 /*********************************************************************
2421 * _wtoi64 (MSVCRT.@)
2423 __int64 CDECL MSVCRT__wtoi64(const MSVCRT_wchar_t *str)
2425 return MSVCRT__wtoi64_l(str, NULL);
2428 /*********************************************************************
2429 * wcsncmp (MSVCRT.@)
2431 int CDECL MSVCRT_wcsncmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, int n)
2433 return strncmpW(str1, str2, n);
2436 /*********************************************************************
2437 * _wcsxfrm_l (MSVCRT.@)
2439 MSVCRT_size_t CDECL MSVCRT__wcsxfrm_l(MSVCRT_wchar_t *dest, const MSVCRT_wchar_t *src,
2440 MSVCRT_size_t len, MSVCRT__locale_t locale)
2442 MSVCRT_pthreadlocinfo locinfo;
2443 int i, ret;
2445 if(!MSVCRT_CHECK_PMT(src)) return INT_MAX;
2446 if(!MSVCRT_CHECK_PMT(dest || !len)) return INT_MAX;
2448 if(len > INT_MAX) {
2449 FIXME("len > INT_MAX not supported\n");
2450 len = INT_MAX;
2453 if(!locale)
2454 locinfo = get_locinfo();
2455 else
2456 locinfo = locale->locinfo;
2458 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE]) {
2459 MSVCRT_wcsncpy(dest, src, len);
2460 return strlenW(src);
2463 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2464 LCMAP_SORTKEY, src, -1, NULL, 0);
2465 if(!ret) {
2466 if(len) dest[0] = 0;
2467 *MSVCRT__errno() = MSVCRT_EILSEQ;
2468 return INT_MAX;
2470 if(!len) return ret-1;
2472 if(ret > len) {
2473 dest[0] = 0;
2474 *MSVCRT__errno() = MSVCRT_ERANGE;
2475 return ret-1;
2478 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2479 LCMAP_SORTKEY, src, -1, dest, len) - 1;
2480 for(i=ret; i>=0; i--)
2481 dest[i] = ((unsigned char*)dest)[i];
2482 return ret;
2485 /*********************************************************************
2486 * wcsxfrm (MSVCRT.@)
2488 MSVCRT_size_t CDECL MSVCRT_wcsxfrm(MSVCRT_wchar_t *dest,
2489 const MSVCRT_wchar_t *src, MSVCRT_size_t len)
2491 return MSVCRT__wcsxfrm_l(dest, src, len, NULL);