ddraw/tests: Mark GetDC() failures on VMware as broken.
[wine.git] / dlls / msvcrt / wcs.c
blob1aa8d98b125131bad84fd18092af9219b93b1e63
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 #if _MSVCR_VER>=120
669 /*********************************************************************
670 * _wcstof_l (MSVCR120.@)
672 float CDECL MSVCRT__wcstof_l( const MSVCRT_wchar_t *str, MSVCRT_wchar_t **end, MSVCRT__locale_t locale )
674 return MSVCRT__wcstod_l(str, end, locale);
677 /*********************************************************************
678 * wcstof (MSVCR120.@)
680 float CDECL MSVCRT_wcstof( const MSVCRT_wchar_t *str, MSVCRT_wchar_t **end )
682 return MSVCRT__wcstof_l(str, end, NULL);
685 #endif /* _MSVCR_VER>=120 */
687 /*********************************************************************
688 * arg_clbk_valist (INTERNAL)
690 printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valist)
692 printf_arg ret;
694 if(type == VT_I8)
695 ret.get_longlong = va_arg(*valist, LONGLONG);
696 else if(type == VT_INT)
697 ret.get_int = va_arg(*valist, int);
698 else if(type == VT_R8)
699 ret.get_double = va_arg(*valist, double);
700 else if(type == VT_PTR)
701 ret.get_ptr = va_arg(*valist, void*);
702 else {
703 ERR("Incorrect type\n");
704 ret.get_int = 0;
707 return ret;
710 /*********************************************************************
711 * arg_clbk_positional (INTERNAL)
713 printf_arg arg_clbk_positional(void *ctx, int pos, int type, __ms_va_list *valist)
715 printf_arg *args = ctx;
716 return args[pos];
719 /*********************************************************************
720 * _vsnprintf (MSVCRT.@)
722 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
723 const char *format, __ms_va_list valist )
725 static const char nullbyte = '\0';
726 struct _str_ctx_a ctx = {len, str};
727 int ret;
729 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, 0,
730 arg_clbk_valist, NULL, &valist);
731 puts_clbk_str_a(&ctx, 1, &nullbyte);
732 return ret;
735 #if _MSVCR_VER>=140
737 static int puts_clbk_str_c99_a(void *ctx, int len, const char *str)
739 struct _str_ctx_a *out = ctx;
741 if(!out->buf)
742 return len;
744 if(out->len < len) {
745 memcpy(out->buf, str, out->len);
746 out->buf += out->len;
747 out->len = 0;
748 return len;
751 memcpy(out->buf, str, len);
752 out->buf += len;
753 out->len -= len;
754 return len;
757 /*********************************************************************
758 * __stdio_common_vsprintf (UCRTBASE.@)
760 int CDECL MSVCRT__stdio_common_vsprintf( unsigned __int64 options, char *str, MSVCRT_size_t len, const char *format,
761 MSVCRT__locale_t locale, __ms_va_list valist )
763 static const char nullbyte = '\0';
764 struct _str_ctx_a ctx = {len, str};
765 int ret;
767 if (options & ~UCRTBASE_PRINTF_MASK)
768 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
769 ret = pf_printf_a(puts_clbk_str_c99_a,
770 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
771 puts_clbk_str_a(&ctx, 1, &nullbyte);
773 if(!str)
774 return ret;
775 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
776 return ret>len ? -1 : ret;
777 if(ret>=len) {
778 if(len) str[len-1] = 0;
779 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
781 return ret;
784 #endif /* _MSVCR_VER>=140 */
786 /*********************************************************************
787 * _vsnprintf_l (MSVCRT.@)
789 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
790 MSVCRT__locale_t locale, __ms_va_list valist )
792 static const char nullbyte = '\0';
793 struct _str_ctx_a ctx = {len, str};
794 int ret;
796 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, 0,
797 arg_clbk_valist, NULL, &valist);
798 puts_clbk_str_a(&ctx, 1, &nullbyte);
799 return ret;
802 /*********************************************************************
803 * _vsprintf_l (MSVCRT.@)
805 int CDECL MSVCRT_vsprintf_l( char *str, const char *format,
806 MSVCRT__locale_t locale, __ms_va_list valist )
808 return MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
811 /*********************************************************************
812 * _sprintf_l (MSVCRT.@)
814 int WINAPIV MSVCRT_sprintf_l(char *str, const char *format,
815 MSVCRT__locale_t locale, ...)
817 int retval;
818 __ms_va_list valist;
819 __ms_va_start(valist, locale);
820 retval = MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
821 __ms_va_end(valist);
822 return retval;
825 static int CDECL MSVCRT_vsnprintf_s_l_opt( char *str, MSVCRT_size_t sizeOfBuffer,
826 MSVCRT_size_t count, const char *format, DWORD options,
827 MSVCRT__locale_t locale, __ms_va_list valist )
829 static const char nullbyte = '\0';
830 struct _str_ctx_a ctx;
831 int len, ret;
833 if(sizeOfBuffer<count+1 || count==-1)
834 len = sizeOfBuffer;
835 else
836 len = count+1;
838 ctx.len = len;
839 ctx.buf = str;
840 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
841 arg_clbk_valist, NULL, &valist);
842 puts_clbk_str_a(&ctx, 1, &nullbyte);
844 if(ret<0 || ret==len) {
845 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
846 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
847 memset(str, 0, sizeOfBuffer);
848 } else
849 str[len-1] = '\0';
851 return -1;
854 return ret;
857 static int MSVCRT_vsnwprintf_s_l_opt( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
858 MSVCRT_size_t count, const MSVCRT_wchar_t *format, DWORD options,
859 MSVCRT__locale_t locale, __ms_va_list valist)
861 static const MSVCRT_wchar_t nullbyte = '\0';
862 struct _str_ctx_w ctx;
863 int len, ret;
865 len = sizeOfBuffer;
866 if(count!=-1 && len>count+1)
867 len = count+1;
869 ctx.len = len;
870 ctx.buf = str;
871 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
872 arg_clbk_valist, NULL, &valist);
873 puts_clbk_str_w(&ctx, 1, &nullbyte);
875 if(ret<0 || ret==len) {
876 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
877 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
878 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
879 } else
880 str[len-1] = '\0';
882 return -1;
885 return ret;
888 /*********************************************************************
889 * _vsnprintf_s_l (MSVCRT.@)
891 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
892 MSVCRT_size_t count, const char *format,
893 MSVCRT__locale_t locale, __ms_va_list valist )
895 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, 0, locale, valist);
898 /*********************************************************************
899 * _vsprintf_s_l (MSVCRT.@)
901 int CDECL MSVCRT_vsprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
902 MSVCRT__locale_t locale, __ms_va_list valist )
904 return MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
907 /*********************************************************************
908 * _sprintf_s_l (MSVCRT.@)
910 int WINAPIV MSVCRT_sprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
911 MSVCRT__locale_t locale, ...)
913 int retval;
914 __ms_va_list valist;
915 __ms_va_start(valist, locale);
916 retval = MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
917 __ms_va_end(valist);
918 return retval;
921 /*********************************************************************
922 * _vsnprintf_s (MSVCRT.@)
924 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
925 MSVCRT_size_t count, const char *format, __ms_va_list valist )
927 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
930 #if _MSVCR_VER>=140
932 /*********************************************************************
933 * __stdio_common_vsnprintf_s (UCRTBASE.@)
935 int CDECL MSVCRT__stdio_common_vsnprintf_s( unsigned __int64 options,
936 char *str, MSVCRT_size_t sizeOfBuffer, MSVCRT_size_t count,
937 const char *format, MSVCRT__locale_t locale, __ms_va_list valist )
939 if (options & ~UCRTBASE_PRINTF_MASK)
940 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
941 return MSVCRT_vsnprintf_s_l_opt(str, sizeOfBuffer, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
944 /*********************************************************************
945 * __stdio_common_vsnwprintf_s (UCRTBASE.@)
947 int CDECL MSVCRT__stdio_common_vsnwprintf_s( unsigned __int64 options,
948 MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer, MSVCRT_size_t count,
949 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list valist )
951 if (options & ~UCRTBASE_PRINTF_MASK)
952 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
953 return MSVCRT_vsnwprintf_s_l_opt(str, sizeOfBuffer, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
956 /*********************************************************************
957 * __stdio_common_vswprintf_s (UCRTBASE.@)
959 int CDECL MSVCRT__stdio_common_vswprintf_s( unsigned __int64 options,
960 MSVCRT_wchar_t *str, MSVCRT_size_t count, const MSVCRT_wchar_t *format,
961 MSVCRT__locale_t locale, __ms_va_list valist )
963 return MSVCRT__stdio_common_vsnwprintf_s(options, str, INT_MAX, count, format, locale, valist);
966 /*********************************************************************
967 * __stdio_common_vsprintf_s (UCRTBASE.@)
969 int CDECL MSVCRT__stdio_common_vsprintf_s( unsigned __int64 options,
970 char *str, MSVCRT_size_t count, const char *format,
971 MSVCRT__locale_t locale, __ms_va_list valist )
973 if (options & ~UCRTBASE_PRINTF_MASK)
974 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
975 return MSVCRT_vsnprintf_s_l_opt(str, INT_MAX, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
978 #endif /* _MSVCR_VER>=140 */
980 /*********************************************************************
981 * vsprintf (MSVCRT.@)
983 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
985 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
988 /*********************************************************************
989 * vsprintf_s (MSVCRT.@)
991 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
993 return MSVCRT_vsnprintf(str, num, format, valist);
996 /*********************************************************************
997 * _vscprintf (MSVCRT.@)
999 int CDECL MSVCRT__vscprintf( const char *format, __ms_va_list valist )
1001 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
1004 /*********************************************************************
1005 * _vscprintf_p_l (MSVCRT.@)
1007 int CDECL MSVCRT__vscprintf_p_l(const char *format,
1008 MSVCRT__locale_t locale, __ms_va_list args)
1010 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1011 struct _str_ctx_a puts_ctx = {INT_MAX, NULL};
1012 int ret;
1014 memset(args_ctx, 0, sizeof(args_ctx));
1016 ret = create_positional_ctx_a(args_ctx, format, args);
1017 if(ret < 0) {
1018 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1019 *MSVCRT__errno() = MSVCRT_EINVAL;
1020 return ret;
1021 } else if(ret == 0) {
1022 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1023 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1024 arg_clbk_valist, NULL, &args);
1025 } else {
1026 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1027 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER,
1028 arg_clbk_positional, args_ctx, NULL);
1031 return ret;
1034 /*********************************************************************
1035 * _vscprintf_p (MSVCR80.@)
1037 int CDECL MSVCRT__vscprintf_p(const char *format, __ms_va_list argptr)
1039 return MSVCRT__vscprintf_p_l(format, NULL, argptr);
1042 /*********************************************************************
1043 * _snprintf (MSVCRT.@)
1045 int WINAPIV MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
1047 int retval;
1048 __ms_va_list valist;
1049 __ms_va_start(valist, format);
1050 retval = MSVCRT_vsnprintf(str, len, format, valist);
1051 __ms_va_end(valist);
1052 return retval;
1055 /*********************************************************************
1056 * _snprintf_l (MSVCRT.@)
1058 int WINAPIV MSVCRT__snprintf_l(char *str, unsigned int count, MSVCRT__locale_t locale,
1059 const char *format, ...)
1061 int retval;
1062 __ms_va_list valist;
1063 __ms_va_start(valist, format);
1064 retval = MSVCRT_vsnprintf_l(str, count, format, locale, valist);
1065 __ms_va_end(valist);
1066 return retval;
1069 /*********************************************************************
1070 * _snprintf_s (MSVCRT.@)
1072 int WINAPIV MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
1073 const char *format, ...)
1075 int retval;
1076 __ms_va_list valist;
1077 __ms_va_start(valist, format);
1078 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
1079 __ms_va_end(valist);
1080 return retval;
1083 /*********************************************************************
1084 * _scprintf (MSVCRT.@)
1086 int WINAPIV MSVCRT__scprintf(const char *format, ...)
1088 int retval;
1089 __ms_va_list valist;
1090 __ms_va_start(valist, format);
1091 retval = MSVCRT__vscprintf(format, valist);
1092 __ms_va_end(valist);
1093 return retval;
1096 /*********************************************************************
1097 * _vsnwprintf (MSVCRT.@)
1099 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1100 const MSVCRT_wchar_t *format, __ms_va_list valist)
1102 static const MSVCRT_wchar_t nullbyte = '\0';
1103 struct _str_ctx_w ctx = {len, str};
1104 int ret;
1106 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, 0,
1107 arg_clbk_valist, NULL, &valist);
1108 puts_clbk_str_w(&ctx, 1, &nullbyte);
1109 return ret;
1112 /*********************************************************************
1113 * _vsnwprintf_l (MSVCRT.@)
1115 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
1116 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
1117 __ms_va_list valist)
1119 static const MSVCRT_wchar_t nullbyte = '\0';
1120 struct _str_ctx_w ctx = {len, str};
1121 int ret;
1123 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, 0,
1124 arg_clbk_valist, NULL, &valist);
1125 puts_clbk_str_w(&ctx, 1, &nullbyte);
1126 return ret;
1129 static int MSVCRT_vswprintf_p_l_opt(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1130 const MSVCRT_wchar_t *format, DWORD options, MSVCRT__locale_t locale, __ms_va_list args)
1132 static const MSVCRT_wchar_t nullbyte = '\0';
1133 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1134 struct _str_ctx_w puts_ctx = {length, buffer};
1135 int ret;
1137 memset(args_ctx, 0, sizeof(args_ctx));
1139 ret = create_positional_ctx_w(args_ctx, format, args);
1140 if(ret < 0) {
1141 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1142 *MSVCRT__errno() = MSVCRT_EINVAL;
1143 return ret;
1144 } else if(ret == 0)
1145 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
1146 arg_clbk_valist, NULL, &args);
1147 else
1148 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale,
1149 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
1150 arg_clbk_positional, args_ctx, NULL);
1152 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
1153 return ret;
1156 /*********************************************************************
1157 * _vswprintf_p_l (MSVCRT.@)
1159 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1160 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
1162 return MSVCRT_vswprintf_p_l_opt(buffer, length, format, 0, locale, args);
1165 #if _MSVCR_VER>=80
1166 /*********************************************************************
1167 * _vswprintf_p (MSVCR80.@)
1169 int CDECL MSVCRT__vswprintf_p(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1170 const MSVCRT_wchar_t *format, __ms_va_list args)
1172 return MSVCRT_vswprintf_p_l_opt(buffer, length, format, 0, NULL, args);
1174 #endif
1176 #if _MSVCR_VER>=140
1177 /*********************************************************************
1178 * __stdio_common_vswprintf_p (UCRTBASE.@)
1180 int CDECL MSVCRT__stdio_common_vswprintf_p( unsigned __int64 options,
1181 MSVCRT_wchar_t *str, MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1182 MSVCRT__locale_t locale, __ms_va_list valist )
1184 if (options & ~UCRTBASE_PRINTF_MASK)
1185 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1186 return MSVCRT_vswprintf_p_l_opt(str, count, format, options & UCRTBASE_PRINTF_MASK, locale, valist);
1188 #endif
1190 /*********************************************************************
1191 * _vsnwprintf_s_l (MSVCRT.@)
1193 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1194 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
1195 MSVCRT__locale_t locale, __ms_va_list valist)
1197 return MSVCRT_vsnwprintf_s_l_opt(str, sizeOfBuffer, count, format, 0, locale, valist);
1200 /*********************************************************************
1201 * _vsnwprintf_s (MSVCRT.@)
1203 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
1204 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
1206 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
1207 format, NULL, valist);
1210 /*********************************************************************
1211 * _snwprintf (MSVCRT.@)
1213 int WINAPIV MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
1215 int retval;
1216 __ms_va_list valist;
1217 __ms_va_start(valist, format);
1218 retval = MSVCRT_vsnwprintf(str, len, format, valist);
1219 __ms_va_end(valist);
1220 return retval;
1223 /*********************************************************************
1224 * _snwprintf_l (MSVCRT.@)
1226 int WINAPIV MSVCRT__snwprintf_l( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format,
1227 MSVCRT__locale_t locale, ...)
1229 int retval;
1230 __ms_va_list valist;
1231 __ms_va_start(valist, locale);
1232 retval = MSVCRT_vsnwprintf_l(str, len, format, locale, valist);
1233 __ms_va_end(valist);
1234 return retval;
1237 /*********************************************************************
1238 * _snwprintf_s (MSVCRT.@)
1240 int WINAPIV MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1241 const MSVCRT_wchar_t *format, ...)
1243 int retval;
1244 __ms_va_list valist;
1245 __ms_va_start(valist, format);
1246 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
1247 __ms_va_end(valist);
1248 return retval;
1251 /*********************************************************************
1252 * _snwprintf_s_l (MSVCRT.@)
1254 int WINAPIV MSVCRT__snwprintf_s_l( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
1255 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1257 int retval;
1258 __ms_va_list valist;
1259 __ms_va_start(valist, locale);
1260 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, locale, valist);
1261 __ms_va_end(valist);
1262 return retval;
1265 #if _MSVCR_VER>=140
1267 static int puts_clbk_str_c99_w(void *ctx, int len, const MSVCRT_wchar_t *str)
1269 struct _str_ctx_w *out = ctx;
1271 if(!out->buf)
1272 return len;
1274 if(out->len < len) {
1275 memcpy(out->buf, str, out->len*sizeof(MSVCRT_wchar_t));
1276 out->buf += out->len;
1277 out->len = 0;
1278 return len;
1281 memcpy(out->buf, str, len*sizeof(MSVCRT_wchar_t));
1282 out->buf += len;
1283 out->len -= len;
1284 return len;
1287 /*********************************************************************
1288 * __stdio_common_vswprintf (UCRTBASE.@)
1290 int CDECL MSVCRT__stdio_common_vswprintf( unsigned __int64 options,
1291 MSVCRT_wchar_t *str, MSVCRT_size_t len, const MSVCRT_wchar_t *format,
1292 MSVCRT__locale_t locale, __ms_va_list valist )
1294 static const MSVCRT_wchar_t nullbyte = '\0';
1295 struct _str_ctx_w ctx = {len, str};
1296 int ret;
1298 if (options & ~UCRTBASE_PRINTF_MASK)
1299 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1300 ret = pf_printf_w(puts_clbk_str_c99_w,
1301 &ctx, format, locale, options & UCRTBASE_PRINTF_MASK, arg_clbk_valist, NULL, &valist);
1302 puts_clbk_str_w(&ctx, 1, &nullbyte);
1304 if(!str)
1305 return ret;
1306 if(options & UCRTBASE_PRINTF_LEGACY_VSPRINTF_NULL_TERMINATION)
1307 return ret>len ? -1 : ret;
1308 if(ret>=len) {
1309 if(len) str[len-1] = 0;
1310 return (options & UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR) ? ret : -2;
1312 return ret;
1315 #endif /* _MSVCR_VER>=140 */
1317 /*********************************************************************
1318 * sprintf (MSVCRT.@)
1320 int WINAPIV MSVCRT_sprintf( char *str, const char *format, ... )
1322 __ms_va_list ap;
1323 int r;
1325 __ms_va_start( ap, format );
1326 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1327 __ms_va_end( ap );
1328 return r;
1331 /*********************************************************************
1332 * sprintf_s (MSVCRT.@)
1334 int WINAPIV MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1336 __ms_va_list ap;
1337 int r;
1339 __ms_va_start( ap, format );
1340 r = MSVCRT_vsnprintf( str, num, format, ap );
1341 __ms_va_end( ap );
1342 return r;
1345 /*********************************************************************
1346 * _scwprintf (MSVCRT.@)
1348 int WINAPIV MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1350 __ms_va_list ap;
1351 int r;
1353 __ms_va_start( ap, format );
1354 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1355 __ms_va_end( ap );
1356 return r;
1359 /*********************************************************************
1360 * swprintf (MSVCRT.@)
1362 int WINAPIV MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1364 __ms_va_list ap;
1365 int r;
1367 __ms_va_start( ap, format );
1368 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1369 __ms_va_end( ap );
1370 return r;
1373 /*********************************************************************
1374 * swprintf_s (MSVCRT.@)
1376 int WINAPIV MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1377 const MSVCRT_wchar_t *format, ... )
1379 __ms_va_list ap;
1380 int r;
1382 __ms_va_start(ap, format);
1383 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1384 __ms_va_end(ap);
1386 return r;
1389 /*********************************************************************
1390 * _swprintf_s_l (MSVCRT.@)
1392 int WINAPIV MSVCRT__swprintf_s_l(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1393 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1395 __ms_va_list ap;
1396 int r;
1398 __ms_va_start(ap, locale);
1399 r = MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX, format, locale, ap);
1400 __ms_va_end(ap);
1402 return r;
1405 /*********************************************************************
1406 * _vswprintf (MSVCRT.@)
1408 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1410 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1413 /*********************************************************************
1414 * _vswprintf (MSVCRT.@)
1416 int CDECL MSVCRT_vswprintf_l( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format,
1417 MSVCRT__locale_t locale, __ms_va_list args )
1419 return MSVCRT_vsnwprintf_l( str, INT_MAX, format, locale, args );
1422 /*********************************************************************
1423 * _vscwprintf (MSVCRT.@)
1425 int CDECL MSVCRT__vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1427 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1430 /*********************************************************************
1431 * _vscwprintf_l (MSVCRT.@)
1433 int CDECL MSVCRT__vscwprintf_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1435 return MSVCRT_vsnwprintf_l( NULL, INT_MAX, format, locale, args );
1438 /*********************************************************************
1439 * _vscwprintf_p_l (MSVCRT.@)
1441 int CDECL MSVCRT__vscwprintf_p_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1443 return MSVCRT_vswprintf_p_l_opt( NULL, INT_MAX, format, 0, locale, args );
1446 #if _MSVCR_VER>=80
1447 /*********************************************************************
1448 * _vscwprintf_p (MSVCR80.@)
1450 int CDECL MSVCRT__vscwprintf_p(const MSVCRT_wchar_t *format, __ms_va_list args)
1452 return MSVCRT_vswprintf_p_l_opt(NULL, INT_MAX, format, 0, NULL, args);
1454 #endif
1456 /*********************************************************************
1457 * vswprintf_s (MSVCRT.@)
1459 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1460 const MSVCRT_wchar_t* format, __ms_va_list args)
1462 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1465 /*********************************************************************
1466 * _vswprintf_s_l (MSVCRT.@)
1468 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1469 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1471 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1472 format, locale, args );
1475 static int MSVCRT_vsprintf_p_l_opt(char *buffer, MSVCRT_size_t length, const char *format,
1476 DWORD options, MSVCRT__locale_t locale, __ms_va_list args)
1478 static const char nullbyte = '\0';
1479 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1480 struct _str_ctx_a puts_ctx = {length, buffer};
1481 int ret;
1483 memset(args_ctx, 0, sizeof(args_ctx));
1485 ret = create_positional_ctx_a(args_ctx, format, args);
1486 if(ret < 0) {
1487 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1488 *MSVCRT__errno() = MSVCRT_EINVAL;
1489 return ret;
1490 } else if(ret == 0)
1491 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1492 MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options, arg_clbk_valist, NULL, &args);
1493 else
1494 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale,
1495 MSVCRT_PRINTF_POSITIONAL_PARAMS | MSVCRT_PRINTF_INVOKE_INVALID_PARAM_HANDLER | options,
1496 arg_clbk_positional, args_ctx, NULL);
1498 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
1499 return ret;
1502 /*********************************************************************
1503 * _vsprintf_p_l (MSVCRT.@)
1505 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
1506 MSVCRT__locale_t locale, __ms_va_list args)
1508 return MSVCRT_vsprintf_p_l_opt(buffer, length, format, 0, locale, args);
1511 /*********************************************************************
1512 * _vsprintf_p (MSVCRT.@)
1514 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
1515 const char *format, __ms_va_list args)
1517 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
1520 #if _MSVCR_VER>=140
1521 /*********************************************************************
1522 * __stdio_common_vsprintf_p (UCRTBASE.@)
1524 int CDECL MSVCRT__stdio_common_vsprintf_p(unsigned __int64 options, char *buffer, MSVCRT_size_t length,
1525 const char *format, MSVCRT__locale_t locale, __ms_va_list args)
1527 if (options & ~UCRTBASE_PRINTF_MASK)
1528 FIXME("options %s not handled\n", wine_dbgstr_longlong(options));
1529 return MSVCRT_vsprintf_p_l_opt(buffer, length, format, options & UCRTBASE_PRINTF_MASK, locale, args);
1531 #endif
1533 /*********************************************************************
1534 * _sprintf_p_l (MSVCRT.@)
1536 int WINAPIV MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
1537 const char *format, MSVCRT__locale_t locale, ...)
1539 __ms_va_list valist;
1540 int r;
1542 __ms_va_start(valist, locale);
1543 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
1544 __ms_va_end(valist);
1546 return r;
1549 /*********************************************************************
1550 * __swprintf_l (MSVCRT.@)
1552 int WINAPIV MSVCRT___swprintf_l( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
1553 MSVCRT__locale_t locale, ...)
1555 int retval;
1556 __ms_va_list valist;
1557 __ms_va_start(valist, locale);
1558 retval = MSVCRT_vswprintf_l(str, format, locale, valist);
1559 __ms_va_end(valist);
1560 return retval;
1563 #if _MSVCR_VER>=80
1564 /*********************************************************************
1565 * _sprintf_p (MSVCR80.@)
1567 int WINAPIV MSVCRT__sprintf_p(char *buffer, MSVCRT_size_t length, const char *format, ...)
1569 __ms_va_list valist;
1570 int r;
1572 __ms_va_start(valist, format);
1573 r = MSVCRT_vsprintf_p_l(buffer, length, format, NULL, valist);
1574 __ms_va_end(valist);
1576 return r;
1578 #endif
1580 /*********************************************************************
1581 * _swprintf_p_l (MSVCRT.@)
1583 int WINAPIV MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1584 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
1586 __ms_va_list valist;
1587 int r;
1589 __ms_va_start(valist, locale);
1590 r = MSVCRT_vswprintf_p_l_opt(buffer, length, format, 0, locale, valist);
1591 __ms_va_end(valist);
1593 return r;
1596 /*********************************************************************
1597 * _wcscoll_l (MSVCRT.@)
1599 int CDECL MSVCRT__wcscoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
1601 MSVCRT_pthreadlocinfo locinfo;
1603 if(!locale)
1604 locinfo = get_locinfo();
1605 else
1606 locinfo = locale->locinfo;
1608 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
1609 return strcmpW(str1, str2);
1610 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
1613 /*********************************************************************
1614 * wcscoll (MSVCRT.@)
1616 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1618 return MSVCRT__wcscoll_l(str1, str2, NULL);
1621 /*********************************************************************
1622 * wcspbrk (MSVCRT.@)
1624 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1626 const MSVCRT_wchar_t* p;
1628 while (*str)
1630 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1631 str++;
1633 return NULL;
1636 /*********************************************************************
1637 * wcstok_s (MSVCRT.@)
1639 MSVCRT_wchar_t * CDECL MSVCRT_wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1640 MSVCRT_wchar_t **next_token )
1642 MSVCRT_wchar_t *ret;
1644 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1645 if (!MSVCRT_CHECK_PMT(next_token != NULL)) return NULL;
1646 if (!MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) return NULL;
1648 if (!str) str = *next_token;
1650 while (*str && strchrW( delim, *str )) str++;
1651 if (!*str) return NULL;
1652 ret = str++;
1653 while (*str && !strchrW( delim, *str )) str++;
1654 if (*str) *str++ = 0;
1655 *next_token = str;
1656 return ret;
1659 /*********************************************************************
1660 * wcstok (MSVCRT.@)
1662 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1664 return MSVCRT_wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1667 /*********************************************************************
1668 * _wctomb_s_l (MSVCRT.@)
1670 int CDECL MSVCRT__wctomb_s_l(int *len, char *mbchar, MSVCRT_size_t size,
1671 MSVCRT_wchar_t wch, MSVCRT__locale_t locale)
1673 MSVCRT_pthreadlocinfo locinfo;
1674 BOOL error;
1675 int mblen;
1677 if(!mbchar && size>0) {
1678 if(len)
1679 *len = 0;
1680 return 0;
1683 if(len)
1684 *len = -1;
1686 if(!MSVCRT_CHECK_PMT(size <= INT_MAX))
1687 return MSVCRT_EINVAL;
1689 if(!locale)
1690 locinfo = get_locinfo();
1691 else
1692 locinfo = locale->locinfo;
1694 if(!locinfo->lc_codepage) {
1695 if(wch > 0xff) {
1696 if(mbchar && size>0)
1697 memset(mbchar, 0, size);
1698 *MSVCRT__errno() = MSVCRT_EILSEQ;
1699 return MSVCRT_EILSEQ;
1702 if(!MSVCRT_CHECK_PMT_ERR(size >= 1, MSVCRT_ERANGE))
1703 return MSVCRT_ERANGE;
1705 *mbchar = wch;
1706 if(len)
1707 *len = 1;
1708 return 0;
1711 mblen = WideCharToMultiByte(locinfo->lc_codepage, 0, &wch, 1, mbchar, size, NULL, &error);
1712 if(!mblen || error) {
1713 if(!mblen && GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1714 if(mbchar && size>0)
1715 memset(mbchar, 0, size);
1717 MSVCRT_INVALID_PMT("insufficient buffer size", MSVCRT_ERANGE);
1718 return MSVCRT_ERANGE;
1721 *MSVCRT__errno() = MSVCRT_EILSEQ;
1722 return MSVCRT_EILSEQ;
1725 if(len)
1726 *len = mblen;
1727 return 0;
1730 /*********************************************************************
1731 * wctomb_s (MSVCRT.@)
1733 int CDECL MSVCRT_wctomb_s(int *len, char *mbchar, MSVCRT_size_t size, MSVCRT_wchar_t wch)
1735 return MSVCRT__wctomb_s_l(len, mbchar, size, wch, NULL);
1738 /*********************************************************************
1739 * _wctomb_l (MSVCRT.@)
1741 int CDECL MSVCRT__wctomb_l(char *dst, MSVCRT_wchar_t ch, MSVCRT__locale_t locale)
1743 int len;
1745 MSVCRT__wctomb_s_l(&len, dst, dst ? MSVCRT_MB_LEN_MAX : 0, ch, locale);
1746 return len;
1749 /*********************************************************************
1750 * wctomb (MSVCRT.@)
1752 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1754 return MSVCRT__wctomb_l(dst, ch, NULL);
1757 /*********************************************************************
1758 * wctob (MSVCRT.@)
1760 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1762 char out;
1763 BOOL error;
1764 UINT codepage = get_locinfo()->lc_codepage;
1766 if(!codepage) {
1767 if (wchar < 0xff)
1768 return (signed char)wchar;
1769 else
1770 return MSVCRT_EOF;
1771 } else if(WideCharToMultiByte( codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1772 return (INT)out;
1773 return MSVCRT_EOF;
1776 /*********************************************************************
1777 * wcrtomb (MSVCRT.@)
1779 MSVCRT_size_t CDECL MSVCRT_wcrtomb( char *dst, MSVCRT_wchar_t ch, MSVCRT_mbstate_t *s)
1781 if(s)
1782 *s = 0;
1783 return MSVCRT_wctomb(dst, ch);
1786 /*********************************************************************
1787 * iswalnum (MSVCRT.@)
1789 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1791 return isalnumW( wc );
1794 /*********************************************************************
1795 * iswalpha (MSVCRT.@)
1797 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1799 return isalphaW( wc );
1802 /*********************************************************************
1803 * iswalpha_l (MSVCRT.@)
1805 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1807 return isalphaW( wc );
1810 /*********************************************************************
1811 * iswcntrl (MSVCRT.@)
1813 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1815 return iscntrlW( wc );
1818 /*********************************************************************
1819 * iswdigit (MSVCRT.@)
1821 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1823 return isdigitW( wc );
1826 /*********************************************************************
1827 * _iswdigit_l (MSVCRT.@)
1829 INT CDECL MSVCRT__iswdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1831 return isdigitW( wc );
1834 /*********************************************************************
1835 * iswgraph (MSVCRT.@)
1837 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1839 return isgraphW( wc );
1842 /*********************************************************************
1843 * iswlower (MSVCRT.@)
1845 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1847 return islowerW( wc );
1850 /*********************************************************************
1851 * iswprint (MSVCRT.@)
1853 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1855 return isprintW( wc );
1858 /*********************************************************************
1859 * _iswpunct_l (MSVCRT.@)
1861 INT CDECL MSVCRT__iswpunct_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1863 return ispunctW( wc );
1866 /*********************************************************************
1867 * iswpunct (MSVCRT.@)
1869 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1871 return ispunctW( wc );
1874 /*********************************************************************
1875 * _iswspace_l (MSVCRT.@)
1877 INT CDECL MSVCRT__iswspace_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1879 return isspaceW( wc );
1882 /*********************************************************************
1883 * iswspace (MSVCRT.@)
1885 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1887 return isspaceW( wc );
1890 /*********************************************************************
1891 * iswupper (MSVCRT.@)
1893 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1895 return isupperW( wc );
1898 /*********************************************************************
1899 * iswxdigit (MSVCRT.@)
1901 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1903 return isxdigitW( wc );
1906 /*********************************************************************
1907 * _iswctype_l (MSVCRT.@)
1909 INT CDECL MSVCRT__iswctype_l( MSVCRT_wchar_t wc, MSVCRT_wctype_t type, MSVCRT__locale_t locale )
1911 return (get_char_typeW(wc) & 0xffff) & type;
1914 /*********************************************************************
1915 * _iswblank_l (MSVCRT.@)
1917 INT CDECL MSVCRT__iswblank_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1919 return wc == '\t' || MSVCRT__iswctype_l( wc, MSVCRT__BLANK, locale );
1922 /*********************************************************************
1923 * iswblank (MSVCRT.@)
1925 INT CDECL MSVCRT_iswblank( MSVCRT_wchar_t wc )
1927 return wc == '\t' || MSVCRT__iswctype_l( wc, MSVCRT__BLANK, NULL );
1930 /*********************************************************************
1931 * wcscpy_s (MSVCRT.@)
1933 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1935 MSVCRT_size_t size = 0;
1937 if(!MSVCRT_CHECK_PMT(wcDest)) return MSVCRT_EINVAL;
1938 if(!MSVCRT_CHECK_PMT(numElement)) return MSVCRT_EINVAL;
1940 wcDest[0] = 0;
1942 if(!MSVCRT_CHECK_PMT(wcSrc)) return MSVCRT_EINVAL;
1944 size = strlenW(wcSrc) + 1;
1946 if(!MSVCRT_CHECK_PMT_ERR(size <= numElement, MSVCRT_ERANGE))
1947 return MSVCRT_ERANGE;
1949 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1951 return 0;
1954 /******************************************************************
1955 * wcsncpy (MSVCRT.@)
1957 MSVCRT_wchar_t* __cdecl MSVCRT_wcsncpy( MSVCRT_wchar_t* s1,
1958 const MSVCRT_wchar_t *s2, MSVCRT_size_t n )
1960 MSVCRT_size_t i;
1962 for(i=0; i<n; i++)
1963 if(!(s1[i] = s2[i])) break;
1964 for(; i<n; i++)
1965 s1[i] = 0;
1966 return s1;
1969 /******************************************************************
1970 * wcsncpy_s (MSVCRT.@)
1972 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1973 MSVCRT_size_t count )
1975 WCHAR *p = wcDest;
1976 BOOL truncate = (count == MSVCRT__TRUNCATE);
1978 if(!wcDest && !numElement && !count)
1979 return 0;
1981 if (!wcDest || !numElement)
1982 return MSVCRT_EINVAL;
1984 if (!wcSrc)
1986 *wcDest = 0;
1987 return count ? MSVCRT_EINVAL : 0;
1990 while (numElement && count && *wcSrc)
1992 *p++ = *wcSrc++;
1993 numElement--;
1994 count--;
1996 if (!numElement && truncate)
1998 *(p-1) = 0;
1999 return MSVCRT_STRUNCATE;
2001 else if (!numElement)
2003 *wcDest = 0;
2004 return MSVCRT_ERANGE;
2007 *p = 0;
2008 return 0;
2011 /******************************************************************
2012 * wcscat_s (MSVCRT.@)
2015 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
2017 MSVCRT_wchar_t* ptr = dst;
2019 if (!dst || elem == 0) return MSVCRT_EINVAL;
2020 if (!src)
2022 dst[0] = '\0';
2023 return MSVCRT_EINVAL;
2026 /* seek to end of dst string (or elem if no end of string is found */
2027 while (ptr < dst + elem && *ptr != '\0') ptr++;
2028 while (ptr < dst + elem)
2030 if ((*ptr++ = *src++) == '\0') return 0;
2032 /* not enough space */
2033 dst[0] = '\0';
2034 return MSVCRT_ERANGE;
2037 /*********************************************************************
2038 * wcsncat_s (MSVCRT.@)
2041 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
2042 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
2044 MSVCRT_size_t srclen;
2045 MSVCRT_wchar_t dststart;
2046 INT ret = 0;
2048 if (!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
2049 if (!MSVCRT_CHECK_PMT(elem > 0)) return MSVCRT_EINVAL;
2050 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return MSVCRT_EINVAL;
2052 if (count == 0)
2053 return 0;
2055 for (dststart = 0; dststart < elem; dststart++)
2057 if (dst[dststart] == '\0')
2058 break;
2060 if (dststart == elem)
2062 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", MSVCRT_EINVAL);
2063 return MSVCRT_EINVAL;
2066 if (count == MSVCRT__TRUNCATE)
2068 srclen = strlenW(src);
2069 if (srclen >= (elem - dststart))
2071 srclen = elem - dststart - 1;
2072 ret = MSVCRT_STRUNCATE;
2075 else
2076 srclen = min(strlenW(src), count);
2077 if (srclen < (elem - dststart))
2079 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
2080 dst[dststart+srclen] = '\0';
2081 return ret;
2083 MSVCRT_INVALID_PMT("dst[elem] is too small", MSVCRT_ERANGE);
2084 dst[0] = '\0';
2085 return MSVCRT_ERANGE;
2088 /*********************************************************************
2089 * wctoint (INTERNAL)
2091 static int wctoint(WCHAR c, int base)
2093 int v = -1;
2094 if ('0' <= c && c <= '9')
2095 v = c - '0';
2096 else if ('A' <= c && c <= 'Z')
2097 v = c - 'A' + 10;
2098 else if ('a' <= c && c <= 'z')
2099 v = c - 'a' + 10;
2100 else {
2101 /* NOTE: wine_fold_string(MAP_FOLDDIGITS) supports too many things. */
2102 /* Unicode points that contain digits 0-9; keep this sorted! */
2103 static const WCHAR zeros[] = {
2104 0x660, 0x6f0, 0x966, 0x9e6, 0xa66, 0xae6, 0xb66, 0xc66, 0xce6,
2105 0xd66, 0xe50, 0xed0, 0xf20, 0x1040, 0x17e0, 0x1810, 0xff10
2107 int i;
2108 for (i = 0; i < sizeof(zeros)/sizeof(zeros[0]) && c >= zeros[i]; ++i) {
2109 if (zeros[i] <= c && c <= zeros[i] + 9) {
2110 v = c - zeros[i];
2111 break;
2115 return v < base ? v : -1;
2118 /*********************************************************************
2119 * _wcstoi64_l (MSVCRT.@)
2121 * FIXME: locale parameter is ignored
2123 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
2124 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
2126 BOOL negative = FALSE, empty = TRUE;
2127 __int64 ret = 0;
2129 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
2131 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
2132 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
2133 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
2135 if(endptr)
2136 *endptr = (MSVCRT_wchar_t*)nptr;
2138 while(isspaceW(*nptr)) nptr++;
2140 if(*nptr == '-') {
2141 negative = TRUE;
2142 nptr++;
2143 } else if(*nptr == '+')
2144 nptr++;
2146 if((base==0 || base==16) && wctoint(*nptr, 1)==0 && tolowerW(*(nptr+1))=='x') {
2147 base = 16;
2148 nptr += 2;
2151 if(base == 0) {
2152 if(wctoint(*nptr, 1)==0)
2153 base = 8;
2154 else
2155 base = 10;
2158 while(*nptr) {
2159 int v = wctoint(*nptr, base);
2160 if(v<0)
2161 break;
2163 if(negative)
2164 v = -v;
2166 nptr++;
2167 empty = FALSE;
2169 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
2170 ret = MSVCRT_I64_MAX;
2171 *MSVCRT__errno() = MSVCRT_ERANGE;
2172 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
2173 ret = MSVCRT_I64_MIN;
2174 *MSVCRT__errno() = MSVCRT_ERANGE;
2175 } else
2176 ret = ret*base + v;
2179 if(endptr && !empty)
2180 *endptr = (MSVCRT_wchar_t*)nptr;
2182 return ret;
2185 /*********************************************************************
2186 * _wcstoi64 (MSVCRT.@)
2188 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
2189 MSVCRT_wchar_t **endptr, int base)
2191 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
2194 /*********************************************************************
2195 * _wcstol_l (MSVCRT.@)
2197 MSVCRT_long CDECL MSVCRT__wcstol_l(const MSVCRT_wchar_t *s,
2198 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2200 __int64 ret = MSVCRT__wcstoi64_l(s, end, base, locale);
2202 if(ret > MSVCRT_LONG_MAX) {
2203 ret = MSVCRT_LONG_MAX;
2204 *MSVCRT__errno() = MSVCRT_ERANGE;
2205 }else if(ret < MSVCRT_LONG_MIN) {
2206 ret = MSVCRT_LONG_MIN;
2207 *MSVCRT__errno() = MSVCRT_ERANGE;
2209 return ret;
2212 /*********************************************************************
2213 * _wtoi_l (MSVCRT.@)
2215 int __cdecl MSVCRT__wtoi_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2217 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2219 if(ret > INT_MAX) {
2220 ret = INT_MAX;
2221 *MSVCRT__errno() = MSVCRT_ERANGE;
2222 } else if(ret < INT_MIN) {
2223 ret = INT_MIN;
2224 *MSVCRT__errno() = MSVCRT_ERANGE;
2226 return ret;
2229 /*********************************************************************
2230 * _wtoi (MSVCRT.@)
2232 int __cdecl MSVCRT__wtoi(const MSVCRT_wchar_t *str)
2234 return MSVCRT__wtoi_l(str, NULL);
2237 /*********************************************************************
2238 * _wtol_l (MSVCRT.@)
2240 MSVCRT_long __cdecl MSVCRT__wtol_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2242 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2244 if(ret > MSVCRT_LONG_MAX) {
2245 ret = MSVCRT_LONG_MAX;
2246 *MSVCRT__errno() = MSVCRT_ERANGE;
2247 } else if(ret < MSVCRT_LONG_MIN) {
2248 ret = MSVCRT_LONG_MIN;
2249 *MSVCRT__errno() = MSVCRT_ERANGE;
2251 return ret;
2254 /*********************************************************************
2255 * _wtol (MSVCRT.@)
2257 MSVCRT_long __cdecl MSVCRT__wtol(const MSVCRT_wchar_t *str)
2259 return MSVCRT__wtol_l(str, NULL);
2262 #if _MSVCR_VER>=120
2264 /*********************************************************************
2265 * _wtoll_l (MSVCR120.@)
2267 MSVCRT_longlong __cdecl MSVCRT__wtoll_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2269 return MSVCRT__wcstoi64_l(str, NULL, 10, locale);
2272 /*********************************************************************
2273 * _wtoll (MSVCR120.@)
2275 MSVCRT_longlong __cdecl MSVCRT__wtoll(const MSVCRT_wchar_t *str)
2277 return MSVCRT__wtoll_l(str, NULL);
2280 #endif /* _MSVCR_VER>=120 */
2282 /*********************************************************************
2283 * _wcstoui64_l (MSVCRT.@)
2285 * FIXME: locale parameter is ignored
2287 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
2288 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
2290 BOOL negative = FALSE, empty = TRUE;
2291 unsigned __int64 ret = 0;
2293 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
2295 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
2296 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
2297 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
2299 if(endptr)
2300 *endptr = (MSVCRT_wchar_t*)nptr;
2302 while(isspaceW(*nptr)) nptr++;
2304 if(*nptr == '-') {
2305 negative = TRUE;
2306 nptr++;
2307 } else if(*nptr == '+')
2308 nptr++;
2310 if((base==0 || base==16) && wctoint(*nptr, 1)==0 && tolowerW(*(nptr+1))=='x') {
2311 base = 16;
2312 nptr += 2;
2315 if(base == 0) {
2316 if(wctoint(*nptr, 1)==0)
2317 base = 8;
2318 else
2319 base = 10;
2322 while(*nptr) {
2323 int v = wctoint(*nptr, base);
2324 if(v<0)
2325 break;
2327 nptr++;
2328 empty = FALSE;
2330 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
2331 ret = MSVCRT_UI64_MAX;
2332 *MSVCRT__errno() = MSVCRT_ERANGE;
2333 } else
2334 ret = ret*base + v;
2337 if(endptr && !empty)
2338 *endptr = (MSVCRT_wchar_t*)nptr;
2340 return negative ? -ret : ret;
2343 /*********************************************************************
2344 * _wcstoui64 (MSVCRT.@)
2346 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
2347 MSVCRT_wchar_t **endptr, int base)
2349 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
2352 /*********************************************************************
2353 * _wcstoul_l (MSVCRT.@)
2355 MSVCRT_ulong __cdecl MSVCRT__wcstoul_l(const MSVCRT_wchar_t *s,
2356 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
2358 __int64 ret = MSVCRT__wcstoui64_l(s, end, base, locale);
2360 if(ret > MSVCRT_ULONG_MAX) {
2361 ret = MSVCRT_ULONG_MAX;
2362 *MSVCRT__errno() = MSVCRT_ERANGE;
2364 return ret;
2367 /*********************************************************************
2368 * wcstoul (MSVCRT.@)
2370 MSVCRT_ulong __cdecl MSVCRT_wcstoul(const MSVCRT_wchar_t *s, MSVCRT_wchar_t **end, int base)
2372 return MSVCRT__wcstoul_l(s, end, base, NULL);
2375 /******************************************************************
2376 * wcsnlen (MSVCRT.@)
2378 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
2380 MSVCRT_size_t i;
2382 for (i = 0; i < maxlen; i++)
2383 if (!s[i]) break;
2384 return i;
2387 /*********************************************************************
2388 * _towupper_l (MSVCRT.@)
2390 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2392 return toupperW(c);
2395 /*********************************************************************
2396 * towupper (MSVCRT.@)
2398 int CDECL MSVCRT_towupper(MSVCRT_wint_t c)
2400 return MSVCRT__towupper_l(c, NULL);
2403 /*********************************************************************
2404 * _towlower_l (MSVCRT.@)
2406 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
2408 return tolowerW(c);
2411 /*********************************************************************
2412 * towlower (MSVCRT.@)
2414 int CDECL MSVCRT_towlower(MSVCRT_wint_t c)
2416 return MSVCRT__towlower_l(c, NULL);
2419 /*********************************************************************
2420 * wcschr (MSVCRT.@)
2422 MSVCRT_wchar_t* CDECL MSVCRT_wcschr(const MSVCRT_wchar_t *str, MSVCRT_wchar_t ch)
2424 return strchrW(str, ch);
2427 /***********************************************************************
2428 * wcslen (MSVCRT.@)
2430 int CDECL MSVCRT_wcslen(const MSVCRT_wchar_t *str)
2432 return strlenW(str);
2435 /*********************************************************************
2436 * wcsstr (MSVCRT.@)
2438 MSVCRT_wchar_t* CDECL MSVCRT_wcsstr(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *sub)
2440 return strstrW(str, sub);
2443 /*********************************************************************
2444 * _wtoi64_l (MSVCRT.@)
2446 __int64 CDECL MSVCRT__wtoi64_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2448 ULONGLONG RunningTotal = 0;
2449 BOOL bMinus = FALSE;
2451 while (isspaceW(*str)) {
2452 str++;
2453 } /* while */
2455 if (*str == '+') {
2456 str++;
2457 } else if (*str == '-') {
2458 bMinus = TRUE;
2459 str++;
2460 } /* if */
2462 while (*str >= '0' && *str <= '9') {
2463 RunningTotal = RunningTotal * 10 + *str - '0';
2464 str++;
2465 } /* while */
2467 return bMinus ? -RunningTotal : RunningTotal;
2470 /*********************************************************************
2471 * _wtoi64 (MSVCRT.@)
2473 __int64 CDECL MSVCRT__wtoi64(const MSVCRT_wchar_t *str)
2475 return MSVCRT__wtoi64_l(str, NULL);
2478 /*********************************************************************
2479 * wcsncmp (MSVCRT.@)
2481 int CDECL MSVCRT_wcsncmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, int n)
2483 return strncmpW(str1, str2, n);
2486 /*********************************************************************
2487 * _wcsxfrm_l (MSVCRT.@)
2489 MSVCRT_size_t CDECL MSVCRT__wcsxfrm_l(MSVCRT_wchar_t *dest, const MSVCRT_wchar_t *src,
2490 MSVCRT_size_t len, MSVCRT__locale_t locale)
2492 MSVCRT_pthreadlocinfo locinfo;
2493 int i, ret;
2495 if(!MSVCRT_CHECK_PMT(src)) return INT_MAX;
2496 if(!MSVCRT_CHECK_PMT(dest || !len)) return INT_MAX;
2498 if(len > INT_MAX) {
2499 FIXME("len > INT_MAX not supported\n");
2500 len = INT_MAX;
2503 if(!locale)
2504 locinfo = get_locinfo();
2505 else
2506 locinfo = locale->locinfo;
2508 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE]) {
2509 MSVCRT_wcsncpy(dest, src, len);
2510 return strlenW(src);
2513 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2514 LCMAP_SORTKEY, src, -1, NULL, 0);
2515 if(!ret) {
2516 if(len) dest[0] = 0;
2517 *MSVCRT__errno() = MSVCRT_EILSEQ;
2518 return INT_MAX;
2520 if(!len) return ret-1;
2522 if(ret > len) {
2523 dest[0] = 0;
2524 *MSVCRT__errno() = MSVCRT_ERANGE;
2525 return ret-1;
2528 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2529 LCMAP_SORTKEY, src, -1, dest, len) - 1;
2530 for(i=ret; i>=0; i--)
2531 dest[i] = ((unsigned char*)dest)[i];
2532 return ret;
2535 /*********************************************************************
2536 * wcsxfrm (MSVCRT.@)
2538 MSVCRT_size_t CDECL MSVCRT_wcsxfrm(MSVCRT_wchar_t *dest,
2539 const MSVCRT_wchar_t *src, MSVCRT_size_t len)
2541 return MSVCRT__wcsxfrm_l(dest, src, len, NULL);