dwrite: Ignore fonts with dot-prefixed names.
[wine.git] / dlls / msvcrt / wcs.c
blob4935e547baae9c3749c37b2c1f5b868c484cc4c1
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 /*********************************************************************
848 * _vsnprintf_s_l (MSVCRT.@)
850 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
851 MSVCRT_size_t count, const char *format,
852 MSVCRT__locale_t locale, __ms_va_list valist )
854 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, 0, locale, valist);
857 /*********************************************************************
858 * _vsprintf_s_l (MSVCRT.@)
860 int CDECL MSVCRT_vsprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
861 MSVCRT__locale_t locale, __ms_va_list valist )
863 return MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
866 /*********************************************************************
867 * _sprintf_s_l (MSVCRT.@)
869 int CDECL MSVCRT_sprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
870 MSVCRT__locale_t locale, ...)
872 int retval;
873 __ms_va_list valist;
874 __ms_va_start(valist, locale);
875 retval = MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
876 __ms_va_end(valist);
877 return retval;
880 /*********************************************************************
881 * _vsnprintf_s (MSVCRT.@)
883 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
884 MSVCRT_size_t count, const char *format, __ms_va_list valist )
886 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
889 /*********************************************************************
890 * __stdio_common_vsnprintf_s (MSVCRT.@)
892 int CDECL MSVCRT__stdio_common_vsnprintf_s( unsigned __int64 options,
893 char *str, MSVCRT_size_t sizeOfBuffer, MSVCRT_size_t count,
894 const char *format, MSVCRT__locale_t locale, __ms_va_list valist )
896 if (options & ~UCRTBASE_PRINTF_MASK)
897 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
898 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
901 /*********************************************************************
902 * __stdio_common_vsprintf_s (MSVCRT.@)
904 int CDECL MSVCRT__stdio_common_vsprintf_s( unsigned __int64 options,
905 char *str, MSVCRT_size_t count, const char *format,
906 MSVCRT__locale_t locale, __ms_va_list valist )
908 if (options & ~UCRTBASE_PRINTF_MASK)
909 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
910 return MSVCRT_vsnprintf_s_l_opt(str, INT_MAX, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
913 /*********************************************************************
914 * vsprintf (MSVCRT.@)
916 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
918 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
921 /*********************************************************************
922 * vsprintf_s (MSVCRT.@)
924 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
926 return MSVCRT_vsnprintf(str, num, format, valist);
929 /*********************************************************************
930 * _vscprintf (MSVCRT.@)
932 int CDECL MSVCRT__vscprintf( const char *format, __ms_va_list valist )
934 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
937 /*********************************************************************
938 * _vscprintf_p_l (MSVCRT.@)
940 int CDECL MSVCRT__vscprintf_p_l(const char *format,
941 MSVCRT__locale_t locale, __ms_va_list args)
943 printf_arg args_ctx[MSVCRT__ARGMAX+1];
944 struct _str_ctx_a puts_ctx = {INT_MAX, NULL};
945 int ret;
947 memset(args_ctx, 0, sizeof(args_ctx));
949 ret = create_positional_ctx_a(args_ctx, format, args);
950 if(ret < 0) {
951 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
952 *MSVCRT__errno() = MSVCRT_EINVAL;
953 return ret;
954 } else if(ret == 0) {
955 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
956 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
957 arg_clbk_valist, NULL, &args);
958 } else {
959 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
960 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
961 arg_clbk_positional, args_ctx, NULL);
964 return ret;
967 /*********************************************************************
968 * _vscprintf_p (MSVCR80.@)
970 int CDECL MSVCRT__vscprintf_p(const char *format, __ms_va_list argptr)
972 return MSVCRT__vscprintf_p_l(format, NULL, argptr);
975 /*********************************************************************
976 * _snprintf (MSVCRT.@)
978 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
980 int retval;
981 __ms_va_list valist;
982 __ms_va_start(valist, format);
983 retval = MSVCRT_vsnprintf(str, len, format, valist);
984 __ms_va_end(valist);
985 return retval;
988 /*********************************************************************
989 * _snprintf_l (MSVCRT.@)
991 int CDECL MSVCRT__snprintf_l(char *str, unsigned int count, MSVCRT__locale_t locale,
992 const char *format, ...)
994 int retval;
995 __ms_va_list valist;
996 __ms_va_start(valist, format);
997 retval = MSVCRT_vsnprintf_l(str, count, format, locale, valist);
998 __ms_va_end(valist);
999 return retval;
1002 /*********************************************************************
1003 * _snprintf_s (MSVCRT.@)
1005 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
1006 const char *format, ...)
1008 int retval;
1009 __ms_va_list valist;
1010 __ms_va_start(valist, format);
1011 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
1012 __ms_va_end(valist);
1013 return retval;
1016 /*********************************************************************
1017 * _scprintf (MSVCRT.@)
1019 int CDECL MSVCRT__scprintf(const char *format, ...)
1021 int retval;
1022 __ms_va_list valist;
1023 __ms_va_start(valist, format);
1024 retval = MSVCRT__vscprintf(format, valist);
1025 __ms_va_end(valist);
1026 return retval;
1029 /*********************************************************************
1030 * _vsnwprintf (MSVCRT.@)
1032 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1033 const MSVCRT_wchar_t *format, __ms_va_list valist)
1035 static const MSVCRT_wchar_t nullbyte = '\0';
1036 struct _str_ctx_w ctx = {len, str};
1037 int ret;
1039 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, 0,
1040 arg_clbk_valist, NULL, &valist);
1041 puts_clbk_str_w(&ctx, 1, &nullbyte);
1042 return ret;
1045 /*********************************************************************
1046 * _vsnwprintf_l (MSVCRT.@)
1048 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1049 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1050 __ms_va_list valist)
1052 static const MSVCRT_wchar_t nullbyte = '\0';
1053 struct _str_ctx_w ctx = {len, str};
1054 int ret;
1056 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, 0,
1057 arg_clbk_valist, NULL, &valist);
1058 puts_clbk_str_w(&ctx, 1, &nullbyte);
1059 return ret;
1062 /*********************************************************************
1063 * _vswprintf_p_l (MSVCRT.@)
1065 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1066 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
1068 static const MSVCRT_wchar_t nullbyte = '\0';
1069 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1070 struct _str_ctx_w puts_ctx = {length, buffer};
1071 int ret;
1073 memset(args_ctx, 0, sizeof(args_ctx));
1075 ret = create_positional_ctx_w(args_ctx, format, args);
1076 if(ret < 0) {
1077 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1078 *MSVCRT__errno() = MSVCRT_EINVAL;
1079 return ret;
1080 } else if(ret == 0)
1081 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1082 arg_clbk_valist, NULL, &args);
1083 else
1084 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale,
1085 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1086 arg_clbk_positional, args_ctx, NULL);
1088 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
1089 return ret;
1092 /*********************************************************************
1093 * _vswprintf_p (MSVCR100.@)
1095 int CDECL MSVCRT__vswprintf_p(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1096 const MSVCRT_wchar_t *format, __ms_va_list args)
1098 return MSVCRT_vswprintf_p_l(buffer, length, format, NULL, args);
1101 /*********************************************************************
1102 * _vsnwprintf_s_l (MSVCRT.@)
1104 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1105 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1106 MSVCRT__locale_t locale, __ms_va_list valist)
1108 static const MSVCRT_wchar_t nullbyte = '\0';
1109 struct _str_ctx_w ctx;
1110 int len, ret;
1112 len = sizeOfBuffer;
1113 if(count!=-1 && len>count+1)
1114 len = count+1;
1116 ctx.len = len;
1117 ctx.buf = str;
1118 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1119 arg_clbk_valist, NULL, &valist);
1120 puts_clbk_str_w(&ctx, 1, &nullbyte);
1122 if(ret<0 || ret==len) {
1123 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
1124 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
1125 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
1126 } else
1127 str[len-1] = '\0';
1129 return -1;
1132 return ret;
1135 /*********************************************************************
1136 * _vsnwprintf_s (MSVCRT.@)
1138 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1139 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1141 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1142 format, NULL, valist);
1145 /*********************************************************************
1146 * _snwprintf (MSVCRT.@)
1148 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1150 int retval;
1151 __ms_va_list valist;
1152 __ms_va_start(valist, format);
1153 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1154 __ms_va_end(valist);
1155 return retval;
1158 /*********************************************************************
1159 * _snwprintf_l (MSVCRT.@)
1161 int CDECL MSVCRT__snwprintf_l( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format,
1162 MSVCRT__locale_t locale, ...)
1164 int retval;
1165 __ms_va_list valist;
1166 __ms_va_start(valist, locale);
1167 retval = MSVCRT_vsnwprintf_l(str, len, format, locale, valist);
1168 __ms_va_end(valist);
1169 return retval;
1172 /*********************************************************************
1173 * _snwprintf_s (MSVCRT.@)
1175 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1176 const MSVCRT_wchar_t *format, ...)
1178 int retval;
1179 __ms_va_list valist;
1180 __ms_va_start(valist, format);
1181 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1182 __ms_va_end(valist);
1183 return retval;
1186 /*********************************************************************
1187 * _snwprintf_s_l (MSVCRT.@)
1189 int CDECL MSVCRT__snwprintf_s_l( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1190 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1192 int retval;
1193 __ms_va_list valist;
1194 __ms_va_start(valist, locale);
1195 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, locale, valist);
1196 __ms_va_end(valist);
1197 return retval;
1200 static int puts_clbk_str_c99_w(void *ctx, int len, const MSVCRT_wchar_t *str)
1202 struct _str_ctx_w *out = ctx;
1204 if(!out->buf)
1205 return len;
1207 if(out->len < len) {
1208 memcpy(out->buf, str, out->len*sizeof(MSVCRT_wchar_t));
1209 out->buf += out->len;
1210 out->len = 0;
1211 return len;
1214 memcpy(out->buf, str, len*sizeof(MSVCRT_wchar_t));
1215 out->buf += len;
1216 out->len -= len;
1217 return len;
1220 /*********************************************************************
1221 * __stdio_common_vswprintf (MSVCRT.@)
1223 int CDECL MSVCRT__stdio_common_vswprintf( unsigned __int64 options,
1224 MSVCRT_wchar_t *str, MSVCRT_size_t len, const MSVCRT_wchar_t *format,
1225 MSVCRT__locale_t locale, __ms_va_list valist )
1227 static const MSVCRT_wchar_t nullbyte = '\0';
1228 struct _str_ctx_w ctx = {len, str};
1229 int ret;
1231 if (options & ~UCRTBASE_PRINTF_MASK)
1232 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1233 ret = pf_printf_w(puts_clbk_str_c99_w,
1234 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
1235 puts_clbk_str_w(&ctx, 1, &nullbyte);
1237 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
1238 return ret>len ? -1 : ret;
1239 if(ret>=len) {
1240 if(len) str[len-1] = 0;
1241 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
1243 return ret;
1246 /*********************************************************************
1247 * sprintf (MSVCRT.@)
1249 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1251 __ms_va_list ap;
1252 int r;
1254 __ms_va_start( ap, format );
1255 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1256 __ms_va_end( ap );
1257 return r;
1260 /*********************************************************************
1261 * sprintf_s (MSVCRT.@)
1263 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1265 __ms_va_list ap;
1266 int r;
1268 __ms_va_start( ap, format );
1269 r = MSVCRT_vsnprintf( str, num, format, ap );
1270 __ms_va_end( ap );
1271 return r;
1274 /*********************************************************************
1275 * _scwprintf (MSVCRT.@)
1277 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1279 __ms_va_list ap;
1280 int r;
1282 __ms_va_start( ap, format );
1283 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1284 __ms_va_end( ap );
1285 return r;
1288 /*********************************************************************
1289 * swprintf (MSVCRT.@)
1291 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1293 __ms_va_list ap;
1294 int r;
1296 __ms_va_start( ap, format );
1297 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1298 __ms_va_end( ap );
1299 return r;
1302 /*********************************************************************
1303 * swprintf_s (MSVCRT.@)
1305 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1306 const MSVCRT_wchar_t *format, ... )
1308 __ms_va_list ap;
1309 int r;
1311 __ms_va_start(ap, format);
1312 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1313 __ms_va_end(ap);
1315 return r;
1318 /*********************************************************************
1319 * _swprintf_s_l (MSVCRT.@)
1321 int CDECL MSVCRT__swprintf_s_l(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1322 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1324 __ms_va_list ap;
1325 int r;
1327 __ms_va_start(ap, locale);
1328 r = MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX, format, locale, ap);
1329 __ms_va_end(ap);
1331 return r;
1334 /*********************************************************************
1335 * _vswprintf (MSVCRT.@)
1337 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1339 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1342 /*********************************************************************
1343 * _vswprintf (MSVCRT.@)
1345 int CDECL MSVCRT_vswprintf_l( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format,
1346 MSVCRT__locale_t locale, __ms_va_list args )
1348 return MSVCRT_vsnwprintf_l( str, INT_MAX, format, locale, args );
1351 /*********************************************************************
1352 * _vscwprintf (MSVCRT.@)
1354 int CDECL MSVCRT__vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1356 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1359 /*********************************************************************
1360 * _vscwprintf_l (MSVCRT.@)
1362 int CDECL MSVCRT__vscwprintf_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1364 return MSVCRT_vsnwprintf_l( NULL, INT_MAX, format, locale, args );
1367 /*********************************************************************
1368 * _vscwprintf_p_l (MSVCRT.@)
1370 int CDECL MSVCRT__vscwprintf_p_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1372 return MSVCRT_vswprintf_p_l( NULL, INT_MAX, format, locale, args );
1375 /*********************************************************************
1376 * _vscwprintf_p (MSVCR100.@)
1378 int CDECL MSVCRT__vscwprintf_p(const MSVCRT_wchar_t *format, __ms_va_list args)
1380 return MSVCRT_vswprintf_p_l(NULL, INT_MAX, format, NULL, args);
1383 /*********************************************************************
1384 * vswprintf_s (MSVCRT.@)
1386 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1387 const MSVCRT_wchar_t* format, __ms_va_list args)
1389 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1392 /*********************************************************************
1393 * _vswprintf_s_l (MSVCRT.@)
1395 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1396 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1398 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1399 format, locale, args );
1402 /*********************************************************************
1403 * _vsprintf_p_l (MSVCRT.@)
1405 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
1406 MSVCRT__locale_t locale, __ms_va_list args)
1408 static const char nullbyte = '\0';
1409 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1410 struct _str_ctx_a puts_ctx = {length, buffer};
1411 int ret;
1413 memset(args_ctx, 0, sizeof(args_ctx));
1415 ret = create_positional_ctx_a(args_ctx, format, args);
1416 if(ret < 0) {
1417 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1418 *MSVCRT__errno() = MSVCRT_EINVAL;
1419 return ret;
1420 } else if(ret == 0)
1421 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1422 arg_clbk_valist, NULL, &args);
1423 else
1424 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1425 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1426 arg_clbk_positional, args_ctx, NULL);
1428 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
1429 return ret;
1432 /*********************************************************************
1433 * _vsprintf_p (MSVCRT.@)
1435 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
1436 const char *format, __ms_va_list args)
1438 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
1441 /*********************************************************************
1442 * _sprintf_p_l (MSVCRT.@)
1444 int CDECL MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
1445 const char *format, MSVCRT__locale_t locale, ...)
1447 __ms_va_list valist;
1448 int r;
1450 __ms_va_start(valist, locale);
1451 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
1452 __ms_va_end(valist);
1454 return r;
1457 /*********************************************************************
1458 * __swprintf_l (MSVCRT.@)
1460 int CDECL MSVCRT___swprintf_l( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
1461 MSVCRT__locale_t locale, ...)
1463 int retval;
1464 __ms_va_list valist;
1465 __ms_va_start(valist, locale);
1466 retval = MSVCRT_vswprintf_l(str, format, locale, valist);
1467 __ms_va_end(valist);
1468 return retval;
1471 /*********************************************************************
1472 * _sprintf_p (MSVCR100.@)
1474 int CDECL MSVCRT__sprintf_p(char *buffer, MSVCRT_size_t length, const char *format, ...)
1476 __ms_va_list valist;
1477 int r;
1479 __ms_va_start(valist, format);
1480 r = MSVCRT_vsprintf_p_l(buffer, length, format, NULL, valist);
1481 __ms_va_end(valist);
1483 return r;
1486 /*********************************************************************
1487 * _swprintf_p_l (MSVCRT.@)
1489 int CDECL MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1490 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
1492 __ms_va_list valist;
1493 int r;
1495 __ms_va_start(valist, locale);
1496 r = MSVCRT_vswprintf_p_l(buffer, length, format, locale, valist);
1497 __ms_va_end(valist);
1499 return r;
1502 /*********************************************************************
1503 * _wcscoll_l (MSVCRT.@)
1505 int CDECL MSVCRT__wcscoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
1507 MSVCRT_pthreadlocinfo locinfo;
1509 if(!locale)
1510 locinfo = get_locinfo();
1511 else
1512 locinfo = locale->locinfo;
1514 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
1515 return strcmpW(str1, str2);
1516 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
1519 /*********************************************************************
1520 * wcscoll (MSVCRT.@)
1522 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1524 return MSVCRT__wcscoll_l(str1, str2, NULL);
1527 /*********************************************************************
1528 * wcspbrk (MSVCRT.@)
1530 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1532 const MSVCRT_wchar_t* p;
1534 while (*str)
1536 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1537 str++;
1539 return NULL;
1542 /*********************************************************************
1543 * wcstok_s (MSVCRT.@)
1545 MSVCRT_wchar_t * CDECL MSVCRT_wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1546 MSVCRT_wchar_t **next_token )
1548 MSVCRT_wchar_t *ret;
1550 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1551 if (!MSVCRT_CHECK_PMT(next_token != NULL)) return NULL;
1552 if (!MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) return NULL;
1554 if (!str) str = *next_token;
1556 while (*str && strchrW( delim, *str )) str++;
1557 if (!*str) return NULL;
1558 ret = str++;
1559 while (*str && !strchrW( delim, *str )) str++;
1560 if (*str) *str++ = 0;
1561 *next_token = str;
1562 return ret;
1565 /*********************************************************************
1566 * wcstok (MSVCRT.@)
1568 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1570 return MSVCRT_wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1573 /*********************************************************************
1574 * _wctomb_s_l (MSVCRT.@)
1576 int CDECL MSVCRT__wctomb_s_l(int *len, char *mbchar, MSVCRT_size_t size,
1577 MSVCRT_wchar_t wch, MSVCRT__locale_t locale)
1579 MSVCRT_pthreadlocinfo locinfo;
1580 BOOL error;
1581 int mblen;
1583 if(!mbchar && size>0) {
1584 if(len)
1585 *len = 0;
1586 return 0;
1589 if(len)
1590 *len = -1;
1592 if(!MSVCRT_CHECK_PMT(size <= INT_MAX))
1593 return MSVCRT_EINVAL;
1595 if(!locale)
1596 locinfo = get_locinfo();
1597 else
1598 locinfo = locale->locinfo;
1600 if(!locinfo->lc_codepage) {
1601 if(wch > 0xff) {
1602 if(mbchar && size>0)
1603 memset(mbchar, 0, size);
1604 *MSVCRT__errno() = MSVCRT_EILSEQ;
1605 return MSVCRT_EILSEQ;
1608 if(!MSVCRT_CHECK_PMT_ERR(size >= 1, MSVCRT_ERANGE))
1609 return MSVCRT_ERANGE;
1611 *mbchar = wch;
1612 if(len)
1613 *len = 1;
1614 return 0;
1617 mblen = WideCharToMultiByte(locinfo->lc_codepage, 0, &wch, 1, mbchar, size, NULL, &error);
1618 if(!mblen || error) {
1619 if(!mblen && GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1620 if(mbchar && size>0)
1621 memset(mbchar, 0, size);
1623 MSVCRT_INVALID_PMT("insufficient buffer size", MSVCRT_ERANGE);
1624 return MSVCRT_ERANGE;
1627 *MSVCRT__errno() = MSVCRT_EILSEQ;
1628 return MSVCRT_EILSEQ;
1631 if(len)
1632 *len = mblen;
1633 return 0;
1636 /*********************************************************************
1637 * wctomb_s (MSVCRT.@)
1639 int CDECL MSVCRT_wctomb_s(int *len, char *mbchar, MSVCRT_size_t size, MSVCRT_wchar_t wch)
1641 return MSVCRT__wctomb_s_l(len, mbchar, size, wch, NULL);
1644 /*********************************************************************
1645 * _wctomb_l (MSVCRT.@)
1647 int CDECL MSVCRT__wctomb_l(char *dst, MSVCRT_wchar_t ch, MSVCRT__locale_t locale)
1649 int len;
1651 MSVCRT__wctomb_s_l(&len, dst, dst ? MSVCRT_MB_LEN_MAX : 0, ch, locale);
1652 return len;
1655 /*********************************************************************
1656 * wctomb (MSVCRT.@)
1658 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1660 return MSVCRT__wctomb_l(dst, ch, NULL);
1663 /*********************************************************************
1664 * wctob (MSVCRT.@)
1666 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1668 char out;
1669 BOOL error;
1670 UINT codepage = get_locinfo()->lc_codepage;
1672 if(!codepage) {
1673 if (wchar < 0xff)
1674 return (signed char)wchar;
1675 else
1676 return MSVCRT_EOF;
1677 } else if(WideCharToMultiByte( codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1678 return (INT)out;
1679 return MSVCRT_EOF;
1682 /*********************************************************************
1683 * wcrtomb (MSVCRT.@)
1685 MSVCRT_size_t CDECL MSVCRT_wcrtomb( char *dst, MSVCRT_wchar_t ch, MSVCRT_mbstate_t *s)
1687 if(s)
1688 *s = 0;
1689 return MSVCRT_wctomb(dst, ch);
1692 /*********************************************************************
1693 * iswalnum (MSVCRT.@)
1695 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1697 return isalnumW( wc );
1700 /*********************************************************************
1701 * iswalpha (MSVCRT.@)
1703 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1705 return isalphaW( wc );
1708 /*********************************************************************
1709 * iswalpha_l (MSVCRT.@)
1711 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1713 return isalphaW( wc );
1716 /*********************************************************************
1717 * iswcntrl (MSVCRT.@)
1719 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1721 return iscntrlW( wc );
1724 /*********************************************************************
1725 * iswdigit (MSVCRT.@)
1727 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1729 return isdigitW( wc );
1732 /*********************************************************************
1733 * _iswdigit_l (MSVCRT.@)
1735 INT CDECL MSVCRT__iswdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1737 return isdigitW( wc );
1740 /*********************************************************************
1741 * iswgraph (MSVCRT.@)
1743 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1745 return isgraphW( wc );
1748 /*********************************************************************
1749 * iswlower (MSVCRT.@)
1751 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1753 return islowerW( wc );
1756 /*********************************************************************
1757 * iswprint (MSVCRT.@)
1759 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1761 return isprintW( wc );
1764 /*********************************************************************
1765 * _iswpunct_l (MSVCRT.@)
1767 INT CDECL MSVCRT__iswpunct_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1769 return ispunctW( wc );
1772 /*********************************************************************
1773 * iswpunct (MSVCRT.@)
1775 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1777 return ispunctW( wc );
1780 /*********************************************************************
1781 * _iswspace_l (MSVCRT.@)
1783 INT CDECL MSVCRT__iswspace_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1785 return isspaceW( wc );
1788 /*********************************************************************
1789 * iswspace (MSVCRT.@)
1791 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1793 return isspaceW( wc );
1796 /*********************************************************************
1797 * iswupper (MSVCRT.@)
1799 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1801 return isupperW( wc );
1804 /*********************************************************************
1805 * iswxdigit (MSVCRT.@)
1807 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1809 return isxdigitW( wc );
1812 /*********************************************************************
1813 * wcscpy_s (MSVCRT.@)
1815 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1817 MSVCRT_size_t size = 0;
1819 if(!MSVCRT_CHECK_PMT(wcDest)) return MSVCRT_EINVAL;
1820 if(!MSVCRT_CHECK_PMT(numElement)) return MSVCRT_EINVAL;
1822 wcDest[0] = 0;
1824 if(!MSVCRT_CHECK_PMT(wcSrc)) return MSVCRT_EINVAL;
1826 size = strlenW(wcSrc) + 1;
1828 if(!MSVCRT_CHECK_PMT_ERR(size <= numElement, MSVCRT_ERANGE))
1829 return MSVCRT_ERANGE;
1831 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1833 return 0;
1836 /******************************************************************
1837 * wcsncpy (MSVCRT.@)
1839 MSVCRT_wchar_t* __cdecl MSVCRT_wcsncpy( MSVCRT_wchar_t* s1,
1840 const MSVCRT_wchar_t *s2, MSVCRT_size_t n )
1842 MSVCRT_size_t i;
1844 for(i=0; i<n; i++)
1845 if(!(s1[i] = s2[i])) break;
1846 for(; i<n; i++)
1847 s1[i] = 0;
1848 return s1;
1851 /******************************************************************
1852 * wcsncpy_s (MSVCRT.@)
1854 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1855 MSVCRT_size_t count )
1857 WCHAR *p = wcDest;
1858 BOOL truncate = (count == MSVCRT__TRUNCATE);
1860 if(!wcDest && !numElement && !count)
1861 return 0;
1863 if (!wcDest || !numElement)
1864 return MSVCRT_EINVAL;
1866 if (!wcSrc)
1868 *wcDest = 0;
1869 return count ? MSVCRT_EINVAL : 0;
1872 while (numElement && count && *wcSrc)
1874 *p++ = *wcSrc++;
1875 numElement--;
1876 count--;
1878 if (!numElement && truncate)
1880 *(p-1) = 0;
1881 return MSVCRT_STRUNCATE;
1883 else if (!numElement)
1885 *wcDest = 0;
1886 return MSVCRT_ERANGE;
1889 *p = 0;
1890 return 0;
1893 /******************************************************************
1894 * wcscat_s (MSVCRT.@)
1897 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1899 MSVCRT_wchar_t* ptr = dst;
1901 if (!dst || elem == 0) return MSVCRT_EINVAL;
1902 if (!src)
1904 dst[0] = '\0';
1905 return MSVCRT_EINVAL;
1908 /* seek to end of dst string (or elem if no end of string is found */
1909 while (ptr < dst + elem && *ptr != '\0') ptr++;
1910 while (ptr < dst + elem)
1912 if ((*ptr++ = *src++) == '\0') return 0;
1914 /* not enough space */
1915 dst[0] = '\0';
1916 return MSVCRT_ERANGE;
1919 /*********************************************************************
1920 * wcsncat_s (MSVCRT.@)
1923 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1924 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1926 MSVCRT_size_t srclen;
1927 MSVCRT_wchar_t dststart;
1928 INT ret = 0;
1930 if (!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
1931 if (!MSVCRT_CHECK_PMT(elem > 0)) return MSVCRT_EINVAL;
1932 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return MSVCRT_EINVAL;
1934 if (count == 0)
1935 return 0;
1937 for (dststart = 0; dststart < elem; dststart++)
1939 if (dst[dststart] == '\0')
1940 break;
1942 if (dststart == elem)
1944 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", MSVCRT_EINVAL);
1945 return MSVCRT_EINVAL;
1948 if (count == MSVCRT__TRUNCATE)
1950 srclen = strlenW(src);
1951 if (srclen >= (elem - dststart))
1953 srclen = elem - dststart - 1;
1954 ret = MSVCRT_STRUNCATE;
1957 else
1958 srclen = min(strlenW(src), count);
1959 if (srclen < (elem - dststart))
1961 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1962 dst[dststart+srclen] = '\0';
1963 return ret;
1965 MSVCRT_INVALID_PMT("dst[elem] is too small", MSVCRT_ERANGE);
1966 dst[0] = '\0';
1967 return MSVCRT_ERANGE;
1970 /*********************************************************************
1971 * _wcstoi64_l (MSVCRT.@)
1973 * FIXME: locale parameter is ignored
1975 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1976 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1978 BOOL negative = FALSE;
1979 __int64 ret = 0;
1981 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1983 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1984 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1985 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1987 while(isspaceW(*nptr)) nptr++;
1989 if(*nptr == '-') {
1990 negative = TRUE;
1991 nptr++;
1992 } else if(*nptr == '+')
1993 nptr++;
1995 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1996 base = 16;
1997 nptr += 2;
2000 if(base == 0) {
2001 if(*nptr=='0')
2002 base = 8;
2003 else
2004 base = 10;
2007 while(*nptr) {
2008 MSVCRT_wchar_t cur = tolowerW(*nptr);
2009 int v;
2011 if(cur>='0' && cur<='9') {
2012 if(cur >= '0'+base)
2013 break;
2014 v = cur-'0';
2015 } else {
2016 if(cur<'a' || cur>='a'+base-10)
2017 break;
2018 v = cur-'a'+10;
2021 if(negative)
2022 v = -v;
2024 nptr++;
2026 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
2027 ret = MSVCRT_I64_MAX;
2028 *MSVCRT__errno() = MSVCRT_ERANGE;
2029 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
2030 ret = MSVCRT_I64_MIN;
2031 *MSVCRT__errno() = MSVCRT_ERANGE;
2032 } else
2033 ret = ret*base + v;
2036 if(endptr)
2037 *endptr = (MSVCRT_wchar_t*)nptr;
2039 return ret;
2042 /*********************************************************************
2043 * _wcstoi64 (MSVCRT.@)
2045 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
2046 MSVCRT_wchar_t **endptr, int base)
2048 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
2051 /*********************************************************************
2052 * _wcstol_l (MSVCRT.@)
2054 MSVCRT_long CDECL MSVCRT__wcstol_l(const MSVCRT_wchar_t *s,
2055 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2057 __int64 ret = MSVCRT__wcstoi64_l(s, end, base, locale);
2059 if(ret > MSVCRT_LONG_MAX) {
2060 ret = MSVCRT_LONG_MAX;
2061 *MSVCRT__errno() = MSVCRT_ERANGE;
2062 }else if(ret < MSVCRT_LONG_MIN) {
2063 ret = MSVCRT_LONG_MIN;
2064 *MSVCRT__errno() = MSVCRT_ERANGE;
2066 return ret;
2069 /*********************************************************************
2070 * _wtoi_l (MSVCRT.@)
2072 int __cdecl MSVCRT__wtoi_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2074 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2076 if(ret > INT_MAX) {
2077 ret = INT_MAX;
2078 *MSVCRT__errno() = MSVCRT_ERANGE;
2079 } else if(ret < INT_MIN) {
2080 ret = INT_MIN;
2081 *MSVCRT__errno() = MSVCRT_ERANGE;
2083 return ret;
2086 /*********************************************************************
2087 * _wtoi (MSVCRT.@)
2089 int __cdecl MSVCRT__wtoi(const MSVCRT_wchar_t *str)
2091 return MSVCRT__wtoi_l(str, NULL);
2094 /*********************************************************************
2095 * _wtol_l (MSVCRT.@)
2097 MSVCRT_long __cdecl MSVCRT__wtol_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2099 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2101 if(ret > MSVCRT_LONG_MAX) {
2102 ret = MSVCRT_LONG_MAX;
2103 *MSVCRT__errno() = MSVCRT_ERANGE;
2104 } else if(ret < MSVCRT_LONG_MIN) {
2105 ret = MSVCRT_LONG_MIN;
2106 *MSVCRT__errno() = MSVCRT_ERANGE;
2108 return ret;
2111 /*********************************************************************
2112 * _wtol (MSVCRT.@)
2114 MSVCRT_long __cdecl MSVCRT__wtol(const MSVCRT_wchar_t *str)
2116 return MSVCRT__wtol_l(str, NULL);
2119 /*********************************************************************
2120 * _wtoll_l (MSVCR120.@)
2122 MSVCRT_longlong __cdecl MSVCRT__wtoll_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2124 return MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2127 /*********************************************************************
2128 * _wtoll (MSVCR120.@)
2130 MSVCRT_longlong __cdecl MSVCRT__wtoll(const MSVCRT_wchar_t *str)
2132 return MSVCRT__wtoll_l(str, NULL);
2135 /*********************************************************************
2136 * _wcstoui64_l (MSVCRT.@)
2138 * FIXME: locale parameter is ignored
2140 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
2141 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
2143 BOOL negative = FALSE;
2144 unsigned __int64 ret = 0;
2146 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
2148 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
2149 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
2150 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
2152 while(isspaceW(*nptr)) nptr++;
2154 if(*nptr == '-') {
2155 negative = TRUE;
2156 nptr++;
2157 } else if(*nptr == '+')
2158 nptr++;
2160 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
2161 base = 16;
2162 nptr += 2;
2165 if(base == 0) {
2166 if(*nptr=='0')
2167 base = 8;
2168 else
2169 base = 10;
2172 while(*nptr) {
2173 MSVCRT_wchar_t cur = tolowerW(*nptr);
2174 int v;
2176 if(cur>='0' && cur<='9') {
2177 if(cur >= '0'+base)
2178 break;
2179 v = *nptr-'0';
2180 } else {
2181 if(cur<'a' || cur>='a'+base-10)
2182 break;
2183 v = cur-'a'+10;
2186 nptr++;
2188 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
2189 ret = MSVCRT_UI64_MAX;
2190 *MSVCRT__errno() = MSVCRT_ERANGE;
2191 } else
2192 ret = ret*base + v;
2195 if(endptr)
2196 *endptr = (MSVCRT_wchar_t*)nptr;
2198 return negative ? -ret : ret;
2201 /*********************************************************************
2202 * _wcstoui64 (MSVCRT.@)
2204 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
2205 MSVCRT_wchar_t **endptr, int base)
2207 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
2210 /*********************************************************************
2211 * _wcstoul_l (MSVCRT.@)
2213 MSVCRT_ulong __cdecl MSVCRT__wcstoul_l(const MSVCRT_wchar_t *s,
2214 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2216 __int64 ret = MSVCRT__wcstoui64_l(s, end, base, locale);
2218 if(ret > MSVCRT_ULONG_MAX) {
2219 ret = MSVCRT_ULONG_MAX;
2220 *MSVCRT__errno() = MSVCRT_ERANGE;
2222 return ret;
2225 /*********************************************************************
2226 * wcstoul (MSVCRT.@)
2228 MSVCRT_ulong __cdecl MSVCRT_wcstoul(const MSVCRT_wchar_t *s, MSVCRT_wchar_t **end, int base)
2230 return MSVCRT__wcstoul_l(s, end, base, NULL);
2233 /******************************************************************
2234 * wcsnlen (MSVCRT.@)
2236 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
2238 MSVCRT_size_t i;
2240 for (i = 0; i < maxlen; i++)
2241 if (!s[i]) break;
2242 return i;
2245 /*********************************************************************
2246 * _towupper_l (MSVCRT.@)
2248 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2250 return toupperW(c);
2253 /*********************************************************************
2254 * towupper (MSVCRT.@)
2256 int CDECL MSVCRT_towupper(MSVCRT_wint_t c)
2258 return MSVCRT__towupper_l(c, NULL);
2261 /*********************************************************************
2262 * _towlower_l (MSVCRT.@)
2264 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2266 return tolowerW(c);
2269 /*********************************************************************
2270 * towlower (MSVCRT.@)
2272 int CDECL MSVCRT_towlower(MSVCRT_wint_t c)
2274 return MSVCRT__towlower_l(c, NULL);
2277 /*********************************************************************
2278 * wcschr (MSVCRT.@)
2280 MSVCRT_wchar_t* CDECL MSVCRT_wcschr(const MSVCRT_wchar_t *str, MSVCRT_wchar_t ch)
2282 return strchrW(str, ch);
2285 /***********************************************************************
2286 * wcslen (MSVCRT.@)
2288 int CDECL MSVCRT_wcslen(const MSVCRT_wchar_t *str)
2290 return strlenW(str);
2293 /*********************************************************************
2294 * wcsstr (MSVCRT.@)
2296 MSVCRT_wchar_t* CDECL MSVCRT_wcsstr(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *sub)
2298 return strstrW(str, sub);
2301 /*********************************************************************
2302 * _wtoi64_l (MSVCRT.@)
2304 __int64 CDECL MSVCRT__wtoi64_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2306 ULONGLONG RunningTotal = 0;
2307 BOOL bMinus = FALSE;
2309 while (isspaceW(*str)) {
2310 str++;
2311 } /* while */
2313 if (*str == '+') {
2314 str++;
2315 } else if (*str == '-') {
2316 bMinus = TRUE;
2317 str++;
2318 } /* if */
2320 while (*str >= '0' && *str <= '9') {
2321 RunningTotal = RunningTotal * 10 + *str - '0';
2322 str++;
2323 } /* while */
2325 return bMinus ? -RunningTotal : RunningTotal;
2328 /*********************************************************************
2329 * _wtoi64 (MSVCRT.@)
2331 __int64 CDECL MSVCRT__wtoi64(const MSVCRT_wchar_t *str)
2333 return MSVCRT__wtoi64_l(str, NULL);
2336 /*********************************************************************
2337 * wcsncmp (MSVCRT.@)
2339 int CDECL MSVCRT_wcsncmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, int n)
2341 return strncmpW(str1, str2, n);
2344 /*********************************************************************
2345 * _wcsxfrm_l (MSVCRT.@)
2347 MSVCRT_size_t CDECL MSVCRT__wcsxfrm_l(MSVCRT_wchar_t *dest, const MSVCRT_wchar_t *src,
2348 MSVCRT_size_t len, MSVCRT__locale_t locale)
2350 MSVCRT_pthreadlocinfo locinfo;
2351 int i, ret;
2353 if(!MSVCRT_CHECK_PMT(src)) return INT_MAX;
2354 if(!MSVCRT_CHECK_PMT(dest || !len)) return INT_MAX;
2356 if(len > INT_MAX) {
2357 FIXME("len > INT_MAX not supported\n");
2358 len = INT_MAX;
2361 if(!locale)
2362 locinfo = get_locinfo();
2363 else
2364 locinfo = locale->locinfo;
2366 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE]) {
2367 MSVCRT_wcsncpy(dest, src, len);
2368 return strlenW(src);
2371 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2372 LCMAP_SORTKEY, src, -1, NULL, 0);
2373 if(!ret) {
2374 if(len) dest[0] = 0;
2375 *MSVCRT__errno() = MSVCRT_EILSEQ;
2376 return INT_MAX;
2378 if(!len) return ret-1;
2380 if(ret > len) {
2381 dest[0] = 0;
2382 *MSVCRT__errno() = MSVCRT_ERANGE;
2383 return ret-1;
2386 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2387 LCMAP_SORTKEY, src, -1, dest, len) - 1;
2388 for(i=ret; i>=0; i--)
2389 dest[i] = ((unsigned char*)dest)[i];
2390 return ret;
2393 /*********************************************************************
2394 * wcsxfrm (MSVCRT.@)
2396 MSVCRT_size_t CDECL MSVCRT_wcsxfrm(MSVCRT_wchar_t *dest,
2397 const MSVCRT_wchar_t *src, MSVCRT_size_t len)
2399 return MSVCRT__wcsxfrm_l(dest, src, len, NULL);