ucrtbase: Implement the new printf corner case behaviour.
[wine.git] / dlls / msvcrt / wcs.c
blob56625e94b82a6cc11458e2545e089848f60a4722
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(isdigitW(*p)) {
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(isdigitW(*p)) {
387 exp++;
388 p++;
390 if(*p == *locinfo->lconv->decimal_point)
391 p++;
393 while(isdigitW(*p)) {
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(isdigitW(*p))
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(isdigitW(*p)) {
422 while(isdigitW(*p)) {
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 * arg_clbk_valist (INTERNAL)
652 printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valist)
654 printf_arg ret;
656 if(type == VT_I8)
657 ret.get_longlong = va_arg(*valist, LONGLONG);
658 else if(type == VT_INT)
659 ret.get_int = va_arg(*valist, int);
660 else if(type == VT_R8)
661 ret.get_double = va_arg(*valist, double);
662 else if(type == VT_PTR)
663 ret.get_ptr = va_arg(*valist, void*);
664 else {
665 ERR("Incorrect type\n");
666 ret.get_int = 0;
669 return ret;
672 /*********************************************************************
673 * arg_clbk_positional (INTERNAL)
675 static printf_arg arg_clbk_positional(void *ctx, int pos, int type, __ms_va_list *valist)
677 printf_arg *args = ctx;
678 return args[pos];
681 /*********************************************************************
682 * _vsnprintf (MSVCRT.@)
684 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
685 const char *format, __ms_va_list valist )
687 static const char nullbyte = '\0';
688 struct _str_ctx_a ctx = {len, str};
689 int ret;
691 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, 0,
692 arg_clbk_valist, NULL, &valist);
693 puts_clbk_str_a(&ctx, 1, &nullbyte);
694 return ret;
697 static int puts_clbk_str_c99_a(void *ctx, int len, const char *str)
699 struct _str_ctx_a *out = ctx;
701 if(!out->buf)
702 return len;
704 if(out->len < len) {
705 memcpy(out->buf, str, out->len);
706 out->buf += out->len;
707 out->len = 0;
708 return len;
711 memcpy(out->buf, str, len);
712 out->buf += len;
713 out->len -= len;
714 return len;
717 /*********************************************************************
718 * __stdio_common_vsprintf (MSVCRT.@)
720 int CDECL MSVCRT__stdio_common_vsprintf( unsigned __int64 options, char *str, MSVCRT_size_t len, const char *format,
721 MSVCRT__locale_t locale, __ms_va_list valist )
723 static const char nullbyte = '\0';
724 struct _str_ctx_a ctx = {len, str};
725 int ret;
727 if (options & ~UCRTBASE_PRINTF_MASK)
728 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
729 ret = pf_printf_a(puts_clbk_str_c99_a,
730 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
731 puts_clbk_str_a(&ctx, 1, &nullbyte);
733 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
734 return ret>len ? -1 : ret;
735 if(ret>=len) {
736 if(len) str[len-1] = 0;
737 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
739 return ret;
742 /*********************************************************************
743 * _vsnprintf_l (MSVCRT.@)
745 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
746 MSVCRT__locale_t locale, __ms_va_list valist )
748 static const char nullbyte = '\0';
749 struct _str_ctx_a ctx = {len, str};
750 int ret;
752 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, 0,
753 arg_clbk_valist, NULL, &valist);
754 puts_clbk_str_a(&ctx, 1, &nullbyte);
755 return ret;
758 /*********************************************************************
759 * _vsprintf_l (MSVCRT.@)
761 int CDECL MSVCRT_vsprintf_l( char *str, const char *format,
762 MSVCRT__locale_t locale, __ms_va_list valist )
764 return MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
767 /*********************************************************************
768 * _sprintf_l (MSVCRT.@)
770 int CDECL MSVCRT_sprintf_l(char *str, const char *format,
771 MSVCRT__locale_t locale, ...)
773 int retval;
774 __ms_va_list valist;
775 __ms_va_start(valist, locale);
776 retval = MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
777 __ms_va_end(valist);
778 return retval;
781 static int CDECL MSVCRT_vsnprintf_s_l_opt( char *str, MSVCRT_size_t sizeOfBuffer,
782 MSVCRT_size_t count, const char *format, DWORD options,
783 MSVCRT__locale_t locale, __ms_va_list valist )
785 static const char nullbyte = '\0';
786 struct _str_ctx_a ctx;
787 int len, ret;
789 if(sizeOfBuffer<count+1 || count==-1)
790 len = sizeOfBuffer;
791 else
792 len = count+1;
794 ctx.len = len;
795 ctx.buf = str;
796 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
797 arg_clbk_valist, NULL, &valist);
798 puts_clbk_str_a(&ctx, 1, &nullbyte);
800 if(ret<0 || ret==len) {
801 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
802 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
803 memset(str, 0, sizeOfBuffer);
804 } else
805 str[len-1] = '\0';
807 return -1;
810 return ret;
813 /*********************************************************************
814 * _vsnprintf_s_l (MSVCRT.@)
816 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
817 MSVCRT_size_t count, const char *format,
818 MSVCRT__locale_t locale, __ms_va_list valist )
820 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, 0, locale, valist);
823 /*********************************************************************
824 * _vsprintf_s_l (MSVCRT.@)
826 int CDECL MSVCRT_vsprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
827 MSVCRT__locale_t locale, __ms_va_list valist )
829 return MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
832 /*********************************************************************
833 * _sprintf_s_l (MSVCRT.@)
835 int CDECL MSVCRT_sprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
836 MSVCRT__locale_t locale, ...)
838 int retval;
839 __ms_va_list valist;
840 __ms_va_start(valist, locale);
841 retval = MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
842 __ms_va_end(valist);
843 return retval;
846 /*********************************************************************
847 * _vsnprintf_s (MSVCRT.@)
849 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
850 MSVCRT_size_t count, const char *format, __ms_va_list valist )
852 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
855 /*********************************************************************
856 * __stdio_common_vsnprintf_s (MSVCRT.@)
858 int CDECL MSVCRT__stdio_common_vsnprintf_s( unsigned __int64 options,
859 char *str, MSVCRT_size_t sizeOfBuffer, MSVCRT_size_t count,
860 const char *format, MSVCRT__locale_t locale, __ms_va_list valist )
862 if (options & ~UCRTBASE_PRINTF_MASK)
863 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
864 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
867 /*********************************************************************
868 * __stdio_common_vsprintf_s (MSVCRT.@)
870 int CDECL MSVCRT__stdio_common_vsprintf_s( unsigned __int64 options,
871 char *str, MSVCRT_size_t count, const char *format,
872 MSVCRT__locale_t locale, __ms_va_list valist )
874 if (options & ~UCRTBASE_PRINTF_MASK)
875 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
876 return MSVCRT_vsnprintf_s_l_opt(str, INT_MAX, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
879 /*********************************************************************
880 * vsprintf (MSVCRT.@)
882 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
884 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
887 /*********************************************************************
888 * vsprintf_s (MSVCRT.@)
890 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
892 return MSVCRT_vsnprintf(str, num, format, valist);
895 /*********************************************************************
896 * _vscprintf (MSVCRT.@)
898 int CDECL MSVCRT__vscprintf( const char *format, __ms_va_list valist )
900 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
903 /*********************************************************************
904 * _vscprintf_p_l (MSVCRT.@)
906 int CDECL MSVCRT__vscprintf_p_l(const char *format,
907 MSVCRT__locale_t locale, __ms_va_list args)
909 printf_arg args_ctx[MSVCRT__ARGMAX+1];
910 struct _str_ctx_a puts_ctx = {INT_MAX, NULL};
911 int ret;
913 memset(args_ctx, 0, sizeof(args_ctx));
915 ret = create_positional_ctx_a(args_ctx, format, args);
916 if(ret < 0) {
917 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
918 *MSVCRT__errno() = MSVCRT_EINVAL;
919 return ret;
920 } else if(ret == 0) {
921 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
922 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
923 arg_clbk_valist, NULL, &args);
924 } else {
925 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
926 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
927 arg_clbk_positional, args_ctx, NULL);
930 return ret;
933 /*********************************************************************
934 * _vscprintf_p (MSVCR80.@)
936 int CDECL MSVCRT__vscprintf_p(const char *format, __ms_va_list argptr)
938 return MSVCRT__vscprintf_p_l(format, NULL, argptr);
941 /*********************************************************************
942 * _snprintf (MSVCRT.@)
944 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
946 int retval;
947 __ms_va_list valist;
948 __ms_va_start(valist, format);
949 retval = MSVCRT_vsnprintf(str, len, format, valist);
950 __ms_va_end(valist);
951 return retval;
954 /*********************************************************************
955 * _snprintf_l (MSVCRT.@)
957 int CDECL MSVCRT__snprintf_l(char *str, unsigned int count, MSVCRT__locale_t locale,
958 const char *format, ...)
960 int retval;
961 __ms_va_list valist;
962 __ms_va_start(valist, format);
963 retval = MSVCRT_vsnprintf_l(str, count, format, locale, valist);
964 __ms_va_end(valist);
965 return retval;
968 /*********************************************************************
969 * _snprintf_s (MSVCRT.@)
971 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
972 const char *format, ...)
974 int retval;
975 __ms_va_list valist;
976 __ms_va_start(valist, format);
977 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
978 __ms_va_end(valist);
979 return retval;
982 /*********************************************************************
983 * _scprintf (MSVCRT.@)
985 int CDECL MSVCRT__scprintf(const char *format, ...)
987 int retval;
988 __ms_va_list valist;
989 __ms_va_start(valist, format);
990 retval = MSVCRT__vscprintf(format, valist);
991 __ms_va_end(valist);
992 return retval;
995 /*********************************************************************
996 * _vsnwprintf (MSVCRT.@)
998 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
999 const MSVCRT_wchar_t *format, __ms_va_list valist)
1001 static const MSVCRT_wchar_t nullbyte = '\0';
1002 struct _str_ctx_w ctx = {len, str};
1003 int ret;
1005 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, 0,
1006 arg_clbk_valist, NULL, &valist);
1007 puts_clbk_str_w(&ctx, 1, &nullbyte);
1008 return ret;
1011 /*********************************************************************
1012 * _vsnwprintf_l (MSVCRT.@)
1014 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1015 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1016 __ms_va_list valist)
1018 static const MSVCRT_wchar_t nullbyte = '\0';
1019 struct _str_ctx_w ctx = {len, str};
1020 int ret;
1022 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, 0,
1023 arg_clbk_valist, NULL, &valist);
1024 puts_clbk_str_w(&ctx, 1, &nullbyte);
1025 return ret;
1028 /*********************************************************************
1029 * _vswprintf_p_l (MSVCRT.@)
1031 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1032 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
1034 static const MSVCRT_wchar_t nullbyte = '\0';
1035 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1036 struct _str_ctx_w puts_ctx = {length, buffer};
1037 int ret;
1039 memset(args_ctx, 0, sizeof(args_ctx));
1041 ret = create_positional_ctx_w(args_ctx, format, args);
1042 if(ret < 0) {
1043 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1044 *MSVCRT__errno() = MSVCRT_EINVAL;
1045 return ret;
1046 } else if(ret == 0)
1047 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1048 arg_clbk_valist, NULL, &args);
1049 else
1050 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale,
1051 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1052 arg_clbk_positional, args_ctx, NULL);
1054 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
1055 return ret;
1058 /*********************************************************************
1059 * _vswprintf_p (MSVCR100.@)
1061 int CDECL MSVCRT__vswprintf_p(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1062 const MSVCRT_wchar_t *format, __ms_va_list args)
1064 return MSVCRT_vswprintf_p_l(buffer, length, format, NULL, args);
1067 /*********************************************************************
1068 * _vsnwprintf_s_l (MSVCRT.@)
1070 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1071 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1072 MSVCRT__locale_t locale, __ms_va_list valist)
1074 static const MSVCRT_wchar_t nullbyte = '\0';
1075 struct _str_ctx_w ctx;
1076 int len, ret;
1078 len = sizeOfBuffer;
1079 if(count!=-1 && len>count+1)
1080 len = count+1;
1082 ctx.len = len;
1083 ctx.buf = str;
1084 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1085 arg_clbk_valist, NULL, &valist);
1086 puts_clbk_str_w(&ctx, 1, &nullbyte);
1088 if(ret<0 || ret==len) {
1089 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1090 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
1091 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
1092 } else
1093 str[len-1] = '\0';
1095 return -1;
1098 return ret;
1101 /*********************************************************************
1102 * _vsnwprintf_s (MSVCRT.@)
1104 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1105 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1107 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1108 format, NULL, valist);
1111 /*********************************************************************
1112 * _snwprintf (MSVCRT.@)
1114 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1116 int retval;
1117 __ms_va_list valist;
1118 __ms_va_start(valist, format);
1119 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1120 __ms_va_end(valist);
1121 return retval;
1124 /*********************************************************************
1125 * _snwprintf_l (MSVCRT.@)
1127 int CDECL MSVCRT__snwprintf_l( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format,
1128 MSVCRT__locale_t locale, ...)
1130 int retval;
1131 __ms_va_list valist;
1132 __ms_va_start(valist, locale);
1133 retval = MSVCRT_vsnwprintf_l(str, len, format, locale, valist);
1134 __ms_va_end(valist);
1135 return retval;
1138 /*********************************************************************
1139 * _snwprintf_s (MSVCRT.@)
1141 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1142 const MSVCRT_wchar_t *format, ...)
1144 int retval;
1145 __ms_va_list valist;
1146 __ms_va_start(valist, format);
1147 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1148 __ms_va_end(valist);
1149 return retval;
1152 /*********************************************************************
1153 * _snwprintf_s_l (MSVCRT.@)
1155 int CDECL MSVCRT__snwprintf_s_l( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1156 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1158 int retval;
1159 __ms_va_list valist;
1160 __ms_va_start(valist, locale);
1161 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, locale, valist);
1162 __ms_va_end(valist);
1163 return retval;
1166 static int puts_clbk_str_c99_w(void *ctx, int len, const MSVCRT_wchar_t *str)
1168 struct _str_ctx_w *out = ctx;
1170 if(!out->buf)
1171 return len;
1173 if(out->len < len) {
1174 memcpy(out->buf, str, out->len*sizeof(MSVCRT_wchar_t));
1175 out->buf += out->len;
1176 out->len = 0;
1177 return len;
1180 memcpy(out->buf, str, len*sizeof(MSVCRT_wchar_t));
1181 out->buf += len;
1182 out->len -= len;
1183 return len;
1186 /*********************************************************************
1187 * __stdio_common_vswprintf (MSVCRT.@)
1189 int CDECL MSVCRT__stdio_common_vswprintf( unsigned __int64 options,
1190 MSVCRT_wchar_t *str, MSVCRT_size_t len, const MSVCRT_wchar_t *format,
1191 MSVCRT__locale_t locale, __ms_va_list valist )
1193 static const MSVCRT_wchar_t nullbyte = '\0';
1194 struct _str_ctx_w ctx = {len, str};
1195 int ret;
1197 if (options & ~UCRTBASE_PRINTF_MASK)
1198 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1199 ret = pf_printf_w(puts_clbk_str_c99_w,
1200 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
1201 puts_clbk_str_w(&ctx, 1, &nullbyte);
1203 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
1204 return ret>len ? -1 : ret;
1205 if(ret>=len) {
1206 if(len) str[len-1] = 0;
1207 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
1209 return ret;
1212 /*********************************************************************
1213 * sprintf (MSVCRT.@)
1215 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1217 __ms_va_list ap;
1218 int r;
1220 __ms_va_start( ap, format );
1221 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1222 __ms_va_end( ap );
1223 return r;
1226 /*********************************************************************
1227 * sprintf_s (MSVCRT.@)
1229 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1231 __ms_va_list ap;
1232 int r;
1234 __ms_va_start( ap, format );
1235 r = MSVCRT_vsnprintf( str, num, format, ap );
1236 __ms_va_end( ap );
1237 return r;
1240 /*********************************************************************
1241 * _scwprintf (MSVCRT.@)
1243 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1245 __ms_va_list ap;
1246 int r;
1248 __ms_va_start( ap, format );
1249 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1250 __ms_va_end( ap );
1251 return r;
1254 /*********************************************************************
1255 * swprintf (MSVCRT.@)
1257 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1259 __ms_va_list ap;
1260 int r;
1262 __ms_va_start( ap, format );
1263 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1264 __ms_va_end( ap );
1265 return r;
1268 /*********************************************************************
1269 * swprintf_s (MSVCRT.@)
1271 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1272 const MSVCRT_wchar_t *format, ... )
1274 __ms_va_list ap;
1275 int r;
1277 __ms_va_start(ap, format);
1278 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1279 __ms_va_end(ap);
1281 return r;
1284 /*********************************************************************
1285 * _swprintf_s_l (MSVCRT.@)
1287 int CDECL MSVCRT__swprintf_s_l(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1288 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1290 __ms_va_list ap;
1291 int r;
1293 __ms_va_start(ap, locale);
1294 r = MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX, format, locale, ap);
1295 __ms_va_end(ap);
1297 return r;
1300 /*********************************************************************
1301 * _vswprintf (MSVCRT.@)
1303 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1305 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1308 /*********************************************************************
1309 * _vswprintf (MSVCRT.@)
1311 int CDECL MSVCRT_vswprintf_l( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format,
1312 MSVCRT__locale_t locale, __ms_va_list args )
1314 return MSVCRT_vsnwprintf_l( str, INT_MAX, format, locale, args );
1317 /*********************************************************************
1318 * _vscwprintf (MSVCRT.@)
1320 int CDECL MSVCRT__vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1322 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1325 /*********************************************************************
1326 * _vscwprintf_l (MSVCRT.@)
1328 int CDECL MSVCRT__vscwprintf_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1330 return MSVCRT_vsnwprintf_l( NULL, INT_MAX, format, locale, args );
1333 /*********************************************************************
1334 * _vscwprintf_p_l (MSVCRT.@)
1336 int CDECL MSVCRT__vscwprintf_p_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1338 return MSVCRT_vswprintf_p_l( NULL, INT_MAX, format, locale, args );
1341 /*********************************************************************
1342 * _vscwprintf_p (MSVCR100.@)
1344 int CDECL MSVCRT__vscwprintf_p(const MSVCRT_wchar_t *format, __ms_va_list args)
1346 return MSVCRT_vswprintf_p_l(NULL, INT_MAX, format, NULL, args);
1349 /*********************************************************************
1350 * vswprintf_s (MSVCRT.@)
1352 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1353 const MSVCRT_wchar_t* format, __ms_va_list args)
1355 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1358 /*********************************************************************
1359 * _vswprintf_s_l (MSVCRT.@)
1361 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1362 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1364 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1365 format, locale, args );
1368 /*********************************************************************
1369 * _vsprintf_p_l (MSVCRT.@)
1371 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
1372 MSVCRT__locale_t locale, __ms_va_list args)
1374 static const char nullbyte = '\0';
1375 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1376 struct _str_ctx_a puts_ctx = {length, buffer};
1377 int ret;
1379 memset(args_ctx, 0, sizeof(args_ctx));
1381 ret = create_positional_ctx_a(args_ctx, format, args);
1382 if(ret < 0) {
1383 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1384 *MSVCRT__errno() = MSVCRT_EINVAL;
1385 return ret;
1386 } else if(ret == 0)
1387 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1388 arg_clbk_valist, NULL, &args);
1389 else
1390 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1391 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1392 arg_clbk_positional, args_ctx, NULL);
1394 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
1395 return ret;
1398 /*********************************************************************
1399 * _vsprintf_p (MSVCRT.@)
1401 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
1402 const char *format, __ms_va_list args)
1404 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
1407 /*********************************************************************
1408 * _sprintf_p_l (MSVCRT.@)
1410 int CDECL MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
1411 const char *format, MSVCRT__locale_t locale, ...)
1413 __ms_va_list valist;
1414 int r;
1416 __ms_va_start(valist, locale);
1417 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
1418 __ms_va_end(valist);
1420 return r;
1423 /*********************************************************************
1424 * __swprintf_l (MSVCRT.@)
1426 int CDECL MSVCRT___swprintf_l( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
1427 MSVCRT__locale_t locale, ...)
1429 int retval;
1430 __ms_va_list valist;
1431 __ms_va_start(valist, locale);
1432 retval = MSVCRT_vswprintf_l(str, format, locale, valist);
1433 __ms_va_end(valist);
1434 return retval;
1437 /*********************************************************************
1438 * _sprintf_p (MSVCR100.@)
1440 int CDECL MSVCRT__sprintf_p(char *buffer, MSVCRT_size_t length, const char *format, ...)
1442 __ms_va_list valist;
1443 int r;
1445 __ms_va_start(valist, format);
1446 r = MSVCRT_vsprintf_p_l(buffer, length, format, NULL, valist);
1447 __ms_va_end(valist);
1449 return r;
1452 /*********************************************************************
1453 * _swprintf_p_l (MSVCRT.@)
1455 int CDECL MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1456 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
1458 __ms_va_list valist;
1459 int r;
1461 __ms_va_start(valist, locale);
1462 r = MSVCRT_vswprintf_p_l(buffer, length, format, locale, valist);
1463 __ms_va_end(valist);
1465 return r;
1468 /*********************************************************************
1469 * _wcscoll_l (MSVCRT.@)
1471 int CDECL MSVCRT__wcscoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
1473 MSVCRT_pthreadlocinfo locinfo;
1475 if(!locale)
1476 locinfo = get_locinfo();
1477 else
1478 locinfo = locale->locinfo;
1480 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
1481 return strcmpW(str1, str2);
1482 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
1485 /*********************************************************************
1486 * wcscoll (MSVCRT.@)
1488 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1490 return MSVCRT__wcscoll_l(str1, str2, NULL);
1493 /*********************************************************************
1494 * wcspbrk (MSVCRT.@)
1496 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1498 const MSVCRT_wchar_t* p;
1500 while (*str)
1502 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1503 str++;
1505 return NULL;
1508 /*********************************************************************
1509 * wcstok_s (MSVCRT.@)
1511 MSVCRT_wchar_t * CDECL MSVCRT_wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1512 MSVCRT_wchar_t **next_token )
1514 MSVCRT_wchar_t *ret;
1516 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1517 if (!MSVCRT_CHECK_PMT(next_token != NULL)) return NULL;
1518 if (!MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) return NULL;
1520 if (!str) str = *next_token;
1522 while (*str && strchrW( delim, *str )) str++;
1523 if (!*str) return NULL;
1524 ret = str++;
1525 while (*str && !strchrW( delim, *str )) str++;
1526 if (*str) *str++ = 0;
1527 *next_token = str;
1528 return ret;
1531 /*********************************************************************
1532 * wcstok (MSVCRT.@)
1534 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1536 return MSVCRT_wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1539 /*********************************************************************
1540 * _wctomb_s_l (MSVCRT.@)
1542 int CDECL MSVCRT__wctomb_s_l(int *len, char *mbchar, MSVCRT_size_t size,
1543 MSVCRT_wchar_t wch, MSVCRT__locale_t locale)
1545 MSVCRT_pthreadlocinfo locinfo;
1546 BOOL error;
1547 int mblen;
1549 if(!mbchar && size>0) {
1550 if(len)
1551 *len = 0;
1552 return 0;
1555 if(len)
1556 *len = -1;
1558 if(!MSVCRT_CHECK_PMT(size <= INT_MAX))
1559 return MSVCRT_EINVAL;
1561 if(!locale)
1562 locinfo = get_locinfo();
1563 else
1564 locinfo = locale->locinfo;
1566 if(!locinfo->lc_codepage) {
1567 if(wch > 0xff) {
1568 if(mbchar && size>0)
1569 memset(mbchar, 0, size);
1570 *MSVCRT__errno() = MSVCRT_EILSEQ;
1571 return MSVCRT_EILSEQ;
1574 if(!MSVCRT_CHECK_PMT_ERR(size >= 1, MSVCRT_ERANGE))
1575 return MSVCRT_ERANGE;
1577 *mbchar = wch;
1578 if(len)
1579 *len = 1;
1580 return 0;
1583 mblen = WideCharToMultiByte(locinfo->lc_codepage, 0, &wch, 1, mbchar, size, NULL, &error);
1584 if(!mblen || error) {
1585 if(!mblen && GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1586 if(mbchar && size>0)
1587 memset(mbchar, 0, size);
1589 MSVCRT_INVALID_PMT("insufficient buffer size", MSVCRT_ERANGE);
1590 return MSVCRT_ERANGE;
1593 *MSVCRT__errno() = MSVCRT_EILSEQ;
1594 return MSVCRT_EILSEQ;
1597 if(len)
1598 *len = mblen;
1599 return 0;
1602 /*********************************************************************
1603 * wctomb_s (MSVCRT.@)
1605 int CDECL MSVCRT_wctomb_s(int *len, char *mbchar, MSVCRT_size_t size, MSVCRT_wchar_t wch)
1607 return MSVCRT__wctomb_s_l(len, mbchar, size, wch, NULL);
1610 /*********************************************************************
1611 * _wctomb_l (MSVCRT.@)
1613 int CDECL MSVCRT__wctomb_l(char *dst, MSVCRT_wchar_t ch, MSVCRT__locale_t locale)
1615 int len;
1617 MSVCRT__wctomb_s_l(&len, dst, dst ? MSVCRT_MB_LEN_MAX : 0, ch, locale);
1618 return len;
1621 /*********************************************************************
1622 * wctomb (MSVCRT.@)
1624 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1626 return MSVCRT__wctomb_l(dst, ch, NULL);
1629 /*********************************************************************
1630 * wctob (MSVCRT.@)
1632 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1634 char out;
1635 BOOL error;
1636 UINT codepage = get_locinfo()->lc_codepage;
1638 if(!codepage) {
1639 if (wchar < 0xff)
1640 return (signed char)wchar;
1641 else
1642 return MSVCRT_EOF;
1643 } else if(WideCharToMultiByte( codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1644 return (INT)out;
1645 return MSVCRT_EOF;
1648 /*********************************************************************
1649 * wcrtomb (MSVCRT.@)
1651 MSVCRT_size_t CDECL MSVCRT_wcrtomb( char *dst, MSVCRT_wchar_t ch, MSVCRT_mbstate_t *s)
1653 if(s)
1654 *s = 0;
1655 return MSVCRT_wctomb(dst, ch);
1658 /*********************************************************************
1659 * iswalnum (MSVCRT.@)
1661 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1663 return isalnumW( wc );
1666 /*********************************************************************
1667 * iswalpha (MSVCRT.@)
1669 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1671 return isalphaW( wc );
1674 /*********************************************************************
1675 * iswalpha_l (MSVCRT.@)
1677 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1679 return isalphaW( wc );
1682 /*********************************************************************
1683 * iswcntrl (MSVCRT.@)
1685 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1687 return iscntrlW( wc );
1690 /*********************************************************************
1691 * iswdigit (MSVCRT.@)
1693 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1695 return isdigitW( wc );
1698 /*********************************************************************
1699 * _iswdigit_l (MSVCRT.@)
1701 INT CDECL MSVCRT__iswdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1703 return isdigitW( wc );
1706 /*********************************************************************
1707 * iswgraph (MSVCRT.@)
1709 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1711 return isgraphW( wc );
1714 /*********************************************************************
1715 * iswlower (MSVCRT.@)
1717 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1719 return islowerW( wc );
1722 /*********************************************************************
1723 * iswprint (MSVCRT.@)
1725 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1727 return isprintW( wc );
1730 /*********************************************************************
1731 * _iswpunct_l (MSVCRT.@)
1733 INT CDECL MSVCRT__iswpunct_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1735 return ispunctW( wc );
1738 /*********************************************************************
1739 * iswpunct (MSVCRT.@)
1741 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1743 return ispunctW( wc );
1746 /*********************************************************************
1747 * _iswspace_l (MSVCRT.@)
1749 INT CDECL MSVCRT__iswspace_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1751 return isspaceW( wc );
1754 /*********************************************************************
1755 * iswspace (MSVCRT.@)
1757 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1759 return isspaceW( wc );
1762 /*********************************************************************
1763 * iswupper (MSVCRT.@)
1765 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1767 return isupperW( wc );
1770 /*********************************************************************
1771 * iswxdigit (MSVCRT.@)
1773 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1775 return isxdigitW( wc );
1778 /*********************************************************************
1779 * wcscpy_s (MSVCRT.@)
1781 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1783 MSVCRT_size_t size = 0;
1785 if(!MSVCRT_CHECK_PMT(wcDest)) return MSVCRT_EINVAL;
1786 if(!MSVCRT_CHECK_PMT(numElement)) return MSVCRT_EINVAL;
1788 wcDest[0] = 0;
1790 if(!MSVCRT_CHECK_PMT(wcSrc)) return MSVCRT_EINVAL;
1792 size = strlenW(wcSrc) + 1;
1794 if(!MSVCRT_CHECK_PMT_ERR(size <= numElement, MSVCRT_ERANGE))
1795 return MSVCRT_ERANGE;
1797 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1799 return 0;
1802 /******************************************************************
1803 * wcsncpy (MSVCRT.@)
1805 MSVCRT_wchar_t* __cdecl MSVCRT_wcsncpy( MSVCRT_wchar_t* s1,
1806 const MSVCRT_wchar_t *s2, MSVCRT_size_t n )
1808 MSVCRT_size_t i;
1810 for(i=0; i<n; i++)
1811 if(!(s1[i] = s2[i])) break;
1812 for(; i<n; i++)
1813 s1[i] = 0;
1814 return s1;
1817 /******************************************************************
1818 * wcsncpy_s (MSVCRT.@)
1820 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1821 MSVCRT_size_t count )
1823 WCHAR *p = wcDest;
1824 BOOL truncate = (count == MSVCRT__TRUNCATE);
1826 if(!wcDest && !numElement && !count)
1827 return 0;
1829 if (!wcDest || !numElement)
1830 return MSVCRT_EINVAL;
1832 if (!wcSrc)
1834 *wcDest = 0;
1835 return count ? MSVCRT_EINVAL : 0;
1838 while (numElement && count && *wcSrc)
1840 *p++ = *wcSrc++;
1841 numElement--;
1842 count--;
1844 if (!numElement && truncate)
1846 *(p-1) = 0;
1847 return MSVCRT_STRUNCATE;
1849 else if (!numElement)
1851 *wcDest = 0;
1852 return MSVCRT_ERANGE;
1855 *p = 0;
1856 return 0;
1859 /******************************************************************
1860 * wcscat_s (MSVCRT.@)
1863 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1865 MSVCRT_wchar_t* ptr = dst;
1867 if (!dst || elem == 0) return MSVCRT_EINVAL;
1868 if (!src)
1870 dst[0] = '\0';
1871 return MSVCRT_EINVAL;
1874 /* seek to end of dst string (or elem if no end of string is found */
1875 while (ptr < dst + elem && *ptr != '\0') ptr++;
1876 while (ptr < dst + elem)
1878 if ((*ptr++ = *src++) == '\0') return 0;
1880 /* not enough space */
1881 dst[0] = '\0';
1882 return MSVCRT_ERANGE;
1885 /*********************************************************************
1886 * wcsncat_s (MSVCRT.@)
1889 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1890 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1892 MSVCRT_size_t srclen;
1893 MSVCRT_wchar_t dststart;
1894 INT ret = 0;
1896 if (!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
1897 if (!MSVCRT_CHECK_PMT(elem > 0)) return MSVCRT_EINVAL;
1898 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return MSVCRT_EINVAL;
1900 if (count == 0)
1901 return 0;
1903 for (dststart = 0; dststart < elem; dststart++)
1905 if (dst[dststart] == '\0')
1906 break;
1908 if (dststart == elem)
1910 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", MSVCRT_EINVAL);
1911 return MSVCRT_EINVAL;
1914 if (count == MSVCRT__TRUNCATE)
1916 srclen = strlenW(src);
1917 if (srclen >= (elem - dststart))
1919 srclen = elem - dststart - 1;
1920 ret = MSVCRT_STRUNCATE;
1923 else
1924 srclen = min(strlenW(src), count);
1925 if (srclen < (elem - dststart))
1927 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1928 dst[dststart+srclen] = '\0';
1929 return ret;
1931 MSVCRT_INVALID_PMT("dst[elem] is too small", MSVCRT_ERANGE);
1932 dst[0] = '\0';
1933 return MSVCRT_ERANGE;
1936 /*********************************************************************
1937 * _wcstoi64_l (MSVCRT.@)
1939 * FIXME: locale parameter is ignored
1941 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1942 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1944 BOOL negative = FALSE;
1945 __int64 ret = 0;
1947 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1949 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1950 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1951 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1953 while(isspaceW(*nptr)) nptr++;
1955 if(*nptr == '-') {
1956 negative = TRUE;
1957 nptr++;
1958 } else if(*nptr == '+')
1959 nptr++;
1961 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1962 base = 16;
1963 nptr += 2;
1966 if(base == 0) {
1967 if(*nptr=='0')
1968 base = 8;
1969 else
1970 base = 10;
1973 while(*nptr) {
1974 MSVCRT_wchar_t cur = tolowerW(*nptr);
1975 int v;
1977 if(isdigitW(cur)) {
1978 if(cur >= '0'+base)
1979 break;
1980 v = cur-'0';
1981 } else {
1982 if(cur<'a' || cur>='a'+base-10)
1983 break;
1984 v = cur-'a'+10;
1987 if(negative)
1988 v = -v;
1990 nptr++;
1992 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1993 ret = MSVCRT_I64_MAX;
1994 *MSVCRT__errno() = MSVCRT_ERANGE;
1995 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1996 ret = MSVCRT_I64_MIN;
1997 *MSVCRT__errno() = MSVCRT_ERANGE;
1998 } else
1999 ret = ret*base + v;
2002 if(endptr)
2003 *endptr = (MSVCRT_wchar_t*)nptr;
2005 return ret;
2008 /*********************************************************************
2009 * _wcstoi64 (MSVCRT.@)
2011 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
2012 MSVCRT_wchar_t **endptr, int base)
2014 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
2017 /*********************************************************************
2018 * _wcstol_l (MSVCRT.@)
2020 MSVCRT_long CDECL MSVCRT__wcstol_l(const MSVCRT_wchar_t *s,
2021 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2023 __int64 ret = MSVCRT__wcstoi64_l(s, end, base, locale);
2025 if(ret > MSVCRT_LONG_MAX) {
2026 ret = MSVCRT_LONG_MAX;
2027 *MSVCRT__errno() = MSVCRT_ERANGE;
2028 }else if(ret < MSVCRT_LONG_MIN) {
2029 ret = MSVCRT_LONG_MIN;
2030 *MSVCRT__errno() = MSVCRT_ERANGE;
2032 return ret;
2035 /*********************************************************************
2036 * _wtoi_l (MSVCRT.@)
2038 int __cdecl MSVCRT__wtoi_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2040 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2042 if(ret > INT_MAX) {
2043 ret = INT_MAX;
2044 *MSVCRT__errno() = MSVCRT_ERANGE;
2045 } else if(ret < INT_MIN) {
2046 ret = INT_MIN;
2047 *MSVCRT__errno() = MSVCRT_ERANGE;
2049 return ret;
2052 /*********************************************************************
2053 * _wtoi (MSVCRT.@)
2055 int __cdecl MSVCRT__wtoi(const MSVCRT_wchar_t *str)
2057 return MSVCRT__wtoi_l(str, NULL);
2060 /*********************************************************************
2061 * _wtol_l (MSVCRT.@)
2063 MSVCRT_long __cdecl MSVCRT__wtol_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2065 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2067 if(ret > MSVCRT_LONG_MAX) {
2068 ret = MSVCRT_LONG_MAX;
2069 *MSVCRT__errno() = MSVCRT_ERANGE;
2070 } else if(ret < MSVCRT_LONG_MIN) {
2071 ret = MSVCRT_LONG_MIN;
2072 *MSVCRT__errno() = MSVCRT_ERANGE;
2074 return ret;
2077 /*********************************************************************
2078 * _wtol (MSVCRT.@)
2080 MSVCRT_long __cdecl MSVCRT__wtol(const MSVCRT_wchar_t *str)
2082 return MSVCRT__wtol_l(str, NULL);
2085 /*********************************************************************
2086 * _wcstoui64_l (MSVCRT.@)
2088 * FIXME: locale parameter is ignored
2090 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
2091 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
2093 BOOL negative = FALSE;
2094 unsigned __int64 ret = 0;
2096 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
2098 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
2099 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
2100 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
2102 while(isspaceW(*nptr)) nptr++;
2104 if(*nptr == '-') {
2105 negative = TRUE;
2106 nptr++;
2107 } else if(*nptr == '+')
2108 nptr++;
2110 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
2111 base = 16;
2112 nptr += 2;
2115 if(base == 0) {
2116 if(*nptr=='0')
2117 base = 8;
2118 else
2119 base = 10;
2122 while(*nptr) {
2123 MSVCRT_wchar_t cur = tolowerW(*nptr);
2124 int v;
2126 if(isdigitW(cur)) {
2127 if(cur >= '0'+base)
2128 break;
2129 v = *nptr-'0';
2130 } else {
2131 if(cur<'a' || cur>='a'+base-10)
2132 break;
2133 v = cur-'a'+10;
2136 nptr++;
2138 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
2139 ret = MSVCRT_UI64_MAX;
2140 *MSVCRT__errno() = MSVCRT_ERANGE;
2141 } else
2142 ret = ret*base + v;
2145 if(endptr)
2146 *endptr = (MSVCRT_wchar_t*)nptr;
2148 return negative ? -ret : ret;
2151 /*********************************************************************
2152 * _wcstoui64 (MSVCRT.@)
2154 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
2155 MSVCRT_wchar_t **endptr, int base)
2157 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
2160 /*********************************************************************
2161 * _wcstoul_l (MSVCRT.@)
2163 MSVCRT_ulong __cdecl MSVCRT__wcstoul_l(const MSVCRT_wchar_t *s,
2164 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2166 __int64 ret = MSVCRT__wcstoui64_l(s, end, base, locale);
2168 if(ret > MSVCRT_ULONG_MAX) {
2169 ret = MSVCRT_ULONG_MAX;
2170 *MSVCRT__errno() = MSVCRT_ERANGE;
2172 return ret;
2175 /*********************************************************************
2176 * wcstoul (MSVCRT.@)
2178 MSVCRT_ulong __cdecl MSVCRT_wcstoul(const MSVCRT_wchar_t *s, MSVCRT_wchar_t **end, int base)
2180 return MSVCRT__wcstoul_l(s, end, base, NULL);
2183 /******************************************************************
2184 * wcsnlen (MSVCRT.@)
2186 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
2188 MSVCRT_size_t i;
2190 for (i = 0; i < maxlen; i++)
2191 if (!s[i]) break;
2192 return i;
2195 /*********************************************************************
2196 * _towupper_l (MSVCRT.@)
2198 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2200 return toupperW(c);
2203 /*********************************************************************
2204 * towupper (MSVCRT.@)
2206 int CDECL MSVCRT_towupper(MSVCRT_wint_t c)
2208 return MSVCRT__towupper_l(c, NULL);
2211 /*********************************************************************
2212 * _towlower_l (MSVCRT.@)
2214 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2216 return tolowerW(c);
2219 /*********************************************************************
2220 * towlower (MSVCRT.@)
2222 int CDECL MSVCRT_towlower(MSVCRT_wint_t c)
2224 return MSVCRT__towlower_l(c, NULL);
2227 /*********************************************************************
2228 * wcschr (MSVCRT.@)
2230 MSVCRT_wchar_t* CDECL MSVCRT_wcschr(const MSVCRT_wchar_t *str, MSVCRT_wchar_t ch)
2232 return strchrW(str, ch);
2235 /***********************************************************************
2236 * wcslen (MSVCRT.@)
2238 int CDECL MSVCRT_wcslen(const MSVCRT_wchar_t *str)
2240 return strlenW(str);
2243 /*********************************************************************
2244 * wcsstr (MSVCRT.@)
2246 MSVCRT_wchar_t* CDECL MSVCRT_wcsstr(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *sub)
2248 return strstrW(str, sub);
2251 /*********************************************************************
2252 * _wtoi64_l (MSVCRT.@)
2254 __int64 CDECL MSVCRT__wtoi64_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2256 ULONGLONG RunningTotal = 0;
2257 BOOL bMinus = FALSE;
2259 while (isspaceW(*str)) {
2260 str++;
2261 } /* while */
2263 if (*str == '+') {
2264 str++;
2265 } else if (*str == '-') {
2266 bMinus = TRUE;
2267 str++;
2268 } /* if */
2270 while (*str >= '0' && *str <= '9') {
2271 RunningTotal = RunningTotal * 10 + *str - '0';
2272 str++;
2273 } /* while */
2275 return bMinus ? -RunningTotal : RunningTotal;
2278 /*********************************************************************
2279 * _wtoi64 (MSVCRT.@)
2281 __int64 CDECL MSVCRT__wtoi64(const MSVCRT_wchar_t *str)
2283 return MSVCRT__wtoi64_l(str, NULL);
2286 /*********************************************************************
2287 * wcsncmp (MSVCRT.@)
2289 int CDECL MSVCRT_wcsncmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, int n)
2291 return strncmpW(str1, str2, n);
2294 /*********************************************************************
2295 * _wcsxfrm_l (MSVCRT.@)
2297 MSVCRT_size_t CDECL MSVCRT__wcsxfrm_l(MSVCRT_wchar_t *dest, const MSVCRT_wchar_t *src,
2298 MSVCRT_size_t len, MSVCRT__locale_t locale)
2300 MSVCRT_pthreadlocinfo locinfo;
2301 int i, ret;
2303 if(!MSVCRT_CHECK_PMT(src)) return INT_MAX;
2304 if(!MSVCRT_CHECK_PMT(dest || !len)) return INT_MAX;
2306 if(len > INT_MAX) {
2307 FIXME("len > INT_MAX not supported\n");
2308 len = INT_MAX;
2311 if(!locale)
2312 locinfo = get_locinfo();
2313 else
2314 locinfo = locale->locinfo;
2316 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE]) {
2317 MSVCRT_wcsncpy(dest, src, len);
2318 return strlenW(src);
2321 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2322 LCMAP_SORTKEY, src, -1, NULL, 0);
2323 if(!ret) {
2324 if(len) dest[0] = 0;
2325 *MSVCRT__errno() = MSVCRT_EILSEQ;
2326 return INT_MAX;
2328 if(!len) return ret-1;
2330 if(ret > len) {
2331 dest[0] = 0;
2332 *MSVCRT__errno() = MSVCRT_ERANGE;
2333 return ret-1;
2336 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2337 LCMAP_SORTKEY, src, -1, dest, len) - 1;
2338 for(i=ret; i>=0; i--)
2339 dest[i] = ((unsigned char*)dest)[i];
2340 return ret;
2343 /*********************************************************************
2344 * wcsxfrm (MSVCRT.@)
2346 MSVCRT_size_t CDECL MSVCRT_wcsxfrm(MSVCRT_wchar_t *dest,
2347 const MSVCRT_wchar_t *src, MSVCRT_size_t len)
2349 return MSVCRT__wcsxfrm_l(dest, src, len, NULL);