advapi32/tests: Changed an invalid comment.
[wine.git] / dlls / msvcrt / wcs.c
blobc2b8f587398a9bdff99a717a86bdd8d75286a629
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 (MSVCRT.@)
95 INT CDECL MSVCRT__wcsnicmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, INT n)
97 return strncmpiW(str1, str2, n);
100 /*********************************************************************
101 * _wcsicoll_l (MSVCRT.@)
103 int CDECL MSVCRT__wcsicoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
105 MSVCRT_pthreadlocinfo locinfo;
107 if(!locale)
108 locinfo = get_locinfo();
109 else
110 locinfo = locale->locinfo;
112 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
113 return strcmpiW(str1, str2);
114 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
115 str1, -1, str2, -1)-CSTR_EQUAL;
118 /*********************************************************************
119 * _wcsicoll (MSVCRT.@)
121 INT CDECL MSVCRT__wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
123 return MSVCRT__wcsicoll_l(str1, str2, NULL);
126 /*********************************************************************
127 * _wcsnicoll_l (MSVCRT.@)
129 int CDECL MSVCRT__wcsnicoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2,
130 MSVCRT_size_t count, MSVCRT__locale_t locale)
132 MSVCRT_pthreadlocinfo locinfo;
134 if(!locale)
135 locinfo = get_locinfo();
136 else
137 locinfo = locale->locinfo;
139 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
140 return strncmpiW(str1, str2, count);
141 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
142 str1, count, str2, count)-CSTR_EQUAL;
145 /*********************************************************************
146 * _wcsnicoll (MSVCRT.@)
148 INT CDECL MSVCRT__wcsnicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count )
150 return MSVCRT__wcsnicoll_l(str1, str2, count, NULL);
153 /*********************************************************************
154 * _wcsnset (MSVCRT.@)
156 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
158 MSVCRT_wchar_t* ret = str;
159 while ((n-- > 0) && *str) *str++ = c;
160 return ret;
163 /*********************************************************************
164 * _wcsrev (MSVCRT.@)
166 MSVCRT_wchar_t* CDECL MSVCRT__wcsrev( MSVCRT_wchar_t* str )
168 MSVCRT_wchar_t* ret = str;
169 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
170 while (end > str)
172 MSVCRT_wchar_t t = *end;
173 *end-- = *str;
174 *str++ = t;
176 return ret;
179 /*********************************************************************
180 * _wcsset (MSVCRT.@)
182 MSVCRT_wchar_t* CDECL MSVCRT__wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
184 MSVCRT_wchar_t* ret = str;
185 while (*str) *str++ = c;
186 return ret;
189 /******************************************************************
190 * _wcsupr_s_l (MSVCRT.@)
192 int CDECL MSVCRT__wcsupr_s_l( MSVCRT_wchar_t* str, MSVCRT_size_t n,
193 MSVCRT__locale_t locale )
195 MSVCRT_wchar_t* ptr = str;
197 if (!str || !n)
199 if (str) *str = '\0';
200 *MSVCRT__errno() = MSVCRT_EINVAL;
201 return MSVCRT_EINVAL;
204 while (n--)
206 if (!*ptr) return 0;
207 /* FIXME: add locale support */
208 *ptr = toupperW(*ptr);
209 ptr++;
212 /* MSDN claims that the function should return and set errno to
213 * ERANGE, which doesn't seem to be true based on the tests. */
214 *str = '\0';
215 *MSVCRT__errno() = MSVCRT_EINVAL;
216 return MSVCRT_EINVAL;
219 /******************************************************************
220 * _wcsupr_s (MSVCRT.@)
223 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
225 return MSVCRT__wcsupr_s_l( str, n, NULL );
228 /******************************************************************
229 * _wcsupr_l (MSVCRT.@)
231 MSVCRT_wchar_t* CDECL MSVCRT__wcsupr_l( MSVCRT_wchar_t *str, MSVCRT__locale_t locale )
233 MSVCRT__wcsupr_s_l( str, -1, locale);
234 return str;
237 /******************************************************************
238 * _wcslwr_s_l (MSVCRT.@)
240 int CDECL MSVCRT__wcslwr_s_l( MSVCRT_wchar_t* str, MSVCRT_size_t n, MSVCRT__locale_t locale )
242 MSVCRT_wchar_t* ptr = str;
244 if (!str || !n)
246 if (str) *str = '\0';
247 *MSVCRT__errno() = MSVCRT_EINVAL;
248 return MSVCRT_EINVAL;
251 while (n--)
253 if (!*ptr) return 0;
254 *ptr = tolowerW(*ptr);
255 ptr++;
258 /* MSDN claims that the function should return and set errno to
259 * ERANGE, which doesn't seem to be true based on the tests. */
260 *str = '\0';
261 *MSVCRT__errno() = MSVCRT_EINVAL;
262 return MSVCRT_EINVAL;
265 /******************************************************************
266 * _wcslwr_s (MSVCRT.@)
268 int CDECL MSVCRT__wcslwr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
270 return MSVCRT__wcslwr_s_l(str, n, NULL);
273 /******************************************************************
274 * _wcslwr_l (MSVCRT.@)
276 MSVCRT_wchar_t* CDECL MSVCRT__wcslwr_l( MSVCRT_wchar_t* str, MSVCRT__locale_t locale )
278 MSVCRT__wcslwr_s_l(str, -1, locale);
279 return str;
282 /******************************************************************
283 * _wcslwr (MSVCRT.@)
285 MSVCRT_wchar_t* CDECL MSVCRT__wcslwr( MSVCRT_wchar_t* str )
287 MSVCRT__wcslwr_s_l(str, -1, NULL);
288 return str;
291 /*********************************************************************
292 * _wcsncoll_l (MSVCRT.@)
294 int CDECL MSVCRT__wcsncoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2,
295 MSVCRT_size_t count, MSVCRT__locale_t locale)
297 MSVCRT_pthreadlocinfo locinfo;
299 if(!locale)
300 locinfo = get_locinfo();
301 else
302 locinfo = locale->locinfo;
304 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
305 return strncmpW(str1, str2, count);
306 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, count, str2, count)-CSTR_EQUAL;
309 /*********************************************************************
310 * _wcsncoll (MSVCRT.@)
312 int CDECL MSVCRT__wcsncoll(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count)
314 return MSVCRT__wcsncoll_l(str1, str2, count, NULL);
317 /*********************************************************************
318 * _wcstod_l (MSVCRT.@)
320 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
321 MSVCRT__locale_t locale)
323 MSVCRT_pthreadlocinfo locinfo;
324 unsigned __int64 d=0, hlp;
325 unsigned fpcontrol;
326 int exp=0, sign=1;
327 const MSVCRT_wchar_t *p;
328 double ret;
329 long double lret=1, expcnt = 10;
330 BOOL found_digit = FALSE, negexp;
332 if (!MSVCRT_CHECK_PMT(str != NULL)) return 0;
334 if(!locale)
335 locinfo = get_locinfo();
336 else
337 locinfo = locale->locinfo;
339 p = str;
340 while(isspaceW(*p))
341 p++;
343 if(*p == '-') {
344 sign = -1;
345 p++;
346 } else if(*p == '+')
347 p++;
349 while(isdigitW(*p)) {
350 found_digit = TRUE;
351 hlp = d*10+*(p++)-'0';
352 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
353 exp++;
354 break;
355 } else
356 d = hlp;
358 while(isdigitW(*p)) {
359 exp++;
360 p++;
362 if(*p == *locinfo->lconv->decimal_point)
363 p++;
365 while(isdigitW(*p)) {
366 found_digit = TRUE;
367 hlp = d*10+*(p++)-'0';
368 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
369 break;
371 d = hlp;
372 exp--;
374 while(isdigitW(*p))
375 p++;
377 if(!found_digit) {
378 if(end)
379 *end = (MSVCRT_wchar_t*)str;
380 return 0.0;
383 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
384 int e=0, s=1;
386 p++;
387 if(*p == '-') {
388 s = -1;
389 p++;
390 } else if(*p == '+')
391 p++;
393 if(isdigitW(*p)) {
394 while(isdigitW(*p)) {
395 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
396 e = INT_MAX;
397 p++;
399 e *= s;
401 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
402 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
403 else exp += e;
404 } else {
405 if(*p=='-' || *p=='+')
406 p--;
407 p--;
411 fpcontrol = _control87(0, 0);
412 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
413 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
415 negexp = (exp < 0);
416 if(negexp)
417 exp = -exp;
418 while(exp) {
419 if(exp & 1)
420 lret *= expcnt;
421 exp /= 2;
422 expcnt = expcnt*expcnt;
424 ret = (long double)sign * (negexp ? d/lret : d*lret);
426 _control87(fpcontrol, 0xffffffff);
428 if((d && ret==0.0) || isinf(ret))
429 *MSVCRT__errno() = MSVCRT_ERANGE;
431 if(end)
432 *end = (MSVCRT_wchar_t*)p;
434 return ret;
437 /*********************************************************************
438 * wcsrtombs_l (INTERNAL)
440 static MSVCRT_size_t MSVCRT_wcsrtombs_l(char *mbstr, const MSVCRT_wchar_t **wcstr,
441 MSVCRT_size_t count, MSVCRT__locale_t locale)
443 MSVCRT_pthreadlocinfo locinfo;
444 MSVCRT_size_t tmp = 0;
445 BOOL used_default;
447 if(!locale)
448 locinfo = get_locinfo();
449 else
450 locinfo = locale->locinfo;
452 if(!mbstr) {
453 tmp = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
454 *wcstr, -1, NULL, 0, NULL, &used_default)-1;
455 if(!tmp || used_default) {
456 *MSVCRT__errno() = MSVCRT_EILSEQ;
457 return -1;
459 return tmp;
462 while(**wcstr) {
463 char buf[3];
464 MSVCRT_size_t i, size;
466 size = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
467 *wcstr, 1, buf, 3, NULL, &used_default);
468 if(!size || used_default) {
469 *MSVCRT__errno() = MSVCRT_EILSEQ;
470 return -1;
472 if(tmp+size > count)
473 return tmp;
475 for(i=0; i<size; i++)
476 mbstr[tmp++] = buf[i];
477 (*wcstr)++;
480 if(tmp < count) {
481 mbstr[tmp] = '\0';
482 *wcstr = NULL;
484 return tmp;
487 /*********************************************************************
488 * _wcstombs_l (MSVCRT.@)
490 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
491 MSVCRT_size_t count, MSVCRT__locale_t locale)
493 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, locale);
496 /*********************************************************************
497 * wcstombs (MSVCRT.@)
499 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
500 MSVCRT_size_t count)
502 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, NULL);
505 /*********************************************************************
506 * wcsrtombs (MSVCRT.@)
508 MSVCRT_size_t CDECL MSVCRT_wcsrtombs(char *mbstr, const MSVCRT_wchar_t **wcstr,
509 MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
511 if(mbstate)
512 *mbstate = 0;
514 return MSVCRT_wcsrtombs_l(mbstr, wcstr, count, NULL);
517 /*********************************************************************
518 * MSVCRT_wcsrtombs_s_l (INTERNAL)
520 static int MSVCRT_wcsrtombs_s_l(MSVCRT_size_t *ret, char *mbstr,
521 MSVCRT_size_t size, const MSVCRT_wchar_t **wcstr,
522 MSVCRT_size_t count, MSVCRT__locale_t locale)
524 MSVCRT_size_t conv;
526 if(!mbstr && !size && wcstr) {
527 conv = MSVCRT_wcsrtombs_l(NULL, wcstr, 0, locale);
528 if(conv == -1)
529 return *MSVCRT__errno();
530 if(ret)
531 *ret = conv+1;
532 return 0;
535 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) return MSVCRT_EINVAL;
536 if (size) mbstr[0] = '\0';
537 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
538 if (!MSVCRT_CHECK_PMT(*wcstr != NULL)) return MSVCRT_EINVAL;
540 if(count==MSVCRT__TRUNCATE || size<count)
541 conv = size;
542 else
543 conv = count;
545 conv = MSVCRT_wcsrtombs_l(mbstr, wcstr, conv, locale);
546 if(conv == -1) {
547 if(size)
548 mbstr[0] = '\0';
549 return *MSVCRT__errno();
550 }else if(conv < size)
551 mbstr[conv++] = '\0';
552 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
553 mbstr[conv-1] = '\0';
554 else {
555 MSVCRT_INVALID_PMT("mbstr[size] is too small", MSVCRT_ERANGE);
556 if(size)
557 mbstr[0] = '\0';
558 return MSVCRT_ERANGE;
561 if(ret)
562 *ret = conv;
563 return 0;
566 /*********************************************************************
567 * _wcstombs_s_l (MSVCRT.@)
569 int CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
570 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
571 MSVCRT_size_t count, MSVCRT__locale_t locale)
573 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr,count, locale);
576 /*********************************************************************
577 * wcstombs_s (MSVCRT.@)
579 int CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
580 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
582 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr, count, NULL);
585 /*********************************************************************
586 * wcsrtombs_s (MSVCRT.@)
588 int CDECL MSVCRT_wcsrtombs_s(MSVCRT_size_t *ret, char *mbstr, MSVCRT_size_t size,
589 const MSVCRT_wchar_t **wcstr, MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
591 if(mbstate)
592 *mbstate = 0;
594 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, wcstr, count, NULL);
597 /*********************************************************************
598 * wcstod (MSVCRT.@)
600 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
602 return MSVCRT__wcstod_l(lpszStr, end, NULL);
605 /*********************************************************************
606 * _wtof (MSVCRT.@)
608 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
610 return MSVCRT__wcstod_l(str, NULL, NULL);
613 /*********************************************************************
614 * _wtof_l (MSVCRT.@)
616 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
618 return MSVCRT__wcstod_l(str, NULL, locale);
621 /*********************************************************************
622 * arg_clbk_valist (INTERNAL)
624 printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valist)
626 printf_arg ret;
628 if(type == VT_I8)
629 ret.get_longlong = va_arg(*valist, LONGLONG);
630 else if(type == VT_INT)
631 ret.get_int = va_arg(*valist, int);
632 else if(type == VT_R8)
633 ret.get_double = va_arg(*valist, double);
634 else if(type == VT_PTR)
635 ret.get_ptr = va_arg(*valist, void*);
636 else {
637 ERR("Incorrect type\n");
638 ret.get_int = 0;
641 return ret;
644 /*********************************************************************
645 * arg_clbk_positional (INTERNAL)
647 static printf_arg arg_clbk_positional(void *ctx, int pos, int type, __ms_va_list *valist)
649 printf_arg *args = ctx;
650 return args[pos];
653 /*********************************************************************
654 * _vsnprintf (MSVCRT.@)
656 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
657 const char *format, __ms_va_list valist )
659 static const char nullbyte = '\0';
660 struct _str_ctx_a ctx = {len, str};
661 int ret;
663 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, FALSE, FALSE,
664 arg_clbk_valist, NULL, &valist);
665 puts_clbk_str_a(&ctx, 1, &nullbyte);
666 return ret;
669 /*********************************************************************
670 * _vsnprintf_l (MSVCRT.@)
672 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
673 MSVCRT__locale_t locale, __ms_va_list valist )
675 static const char nullbyte = '\0';
676 struct _str_ctx_a ctx = {len, str};
677 int ret;
679 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, FALSE,
680 arg_clbk_valist, NULL, &valist);
681 puts_clbk_str_a(&ctx, 1, &nullbyte);
682 return ret;
685 /*********************************************************************
686 * _vsprintf_l (MSVCRT.@)
688 int CDECL MSVCRT_vsprintf_l( char *str, const char *format,
689 MSVCRT__locale_t locale, __ms_va_list valist )
691 return MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
694 /*********************************************************************
695 * _sprintf_l (MSVCRT.@)
697 int CDECL MSVCRT_sprintf_l(char *str, const char *format,
698 MSVCRT__locale_t locale, ...)
700 int retval;
701 __ms_va_list valist;
702 __ms_va_start(valist, locale);
703 retval = MSVCRT_vsnprintf_l(str, INT_MAX, format, locale, valist);
704 __ms_va_end(valist);
705 return retval;
708 /*********************************************************************
709 * _vsnprintf_s_l (MSVCRT.@)
711 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
712 MSVCRT_size_t count, const char *format,
713 MSVCRT__locale_t locale, __ms_va_list valist )
715 static const char nullbyte = '\0';
716 struct _str_ctx_a ctx;
717 int len, ret;
719 if(sizeOfBuffer<count+1 || count==-1)
720 len = sizeOfBuffer;
721 else
722 len = count+1;
724 ctx.len = len;
725 ctx.buf = str;
726 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, TRUE,
727 arg_clbk_valist, NULL, &valist);
728 puts_clbk_str_a(&ctx, 1, &nullbyte);
730 if(ret<0 || ret==len) {
731 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
732 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
733 memset(str, 0, sizeOfBuffer);
734 } else
735 str[len-1] = '\0';
737 return -1;
740 return ret;
743 /*********************************************************************
744 * _vsprintf_s_l (MSVCRT.@)
746 int CDECL MSVCRT_vsprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
747 MSVCRT__locale_t locale, __ms_va_list valist )
749 return MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
752 /*********************************************************************
753 * _sprintf_s_l (MSVCRT.@)
755 int CDECL MSVCRT_sprintf_s_l( char *str, MSVCRT_size_t count, const char *format,
756 MSVCRT__locale_t locale, ...)
758 int retval;
759 __ms_va_list valist;
760 __ms_va_start(valist, locale);
761 retval = MSVCRT_vsnprintf_s_l(str, INT_MAX, count, format, locale, valist);
762 __ms_va_end(valist);
763 return retval;
766 /*********************************************************************
767 * _vsnprintf_s (MSVCRT.@)
769 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
770 MSVCRT_size_t count, const char *format, __ms_va_list valist )
772 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
775 /*********************************************************************
776 * vsprintf (MSVCRT.@)
778 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
780 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
783 /*********************************************************************
784 * vsprintf_s (MSVCRT.@)
786 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
788 return MSVCRT_vsnprintf(str, num, format, valist);
791 /*********************************************************************
792 * _vscprintf (MSVCRT.@)
794 int CDECL MSVCRT__vscprintf( const char *format, __ms_va_list valist )
796 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
799 /*********************************************************************
800 * _snprintf (MSVCRT.@)
802 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
804 int retval;
805 __ms_va_list valist;
806 __ms_va_start(valist, format);
807 retval = MSVCRT_vsnprintf(str, len, format, valist);
808 __ms_va_end(valist);
809 return retval;
812 /*********************************************************************
813 * _snprintf_s (MSVCRT.@)
815 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
816 const char *format, ...)
818 int retval;
819 __ms_va_list valist;
820 __ms_va_start(valist, format);
821 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
822 __ms_va_end(valist);
823 return retval;
826 /*********************************************************************
827 * _scprintf (MSVCRT.@)
829 int CDECL MSVCRT__scprintf(const char *format, ...)
831 int retval;
832 __ms_va_list valist;
833 __ms_va_start(valist, format);
834 retval = MSVCRT__vscprintf(format, valist);
835 __ms_va_end(valist);
836 return retval;
839 /*********************************************************************
840 * _vsnwprintf (MSVCRT.@)
842 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
843 const MSVCRT_wchar_t *format, __ms_va_list valist)
845 static const MSVCRT_wchar_t nullbyte = '\0';
846 struct _str_ctx_w ctx = {len, str};
847 int ret;
849 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, FALSE, FALSE,
850 arg_clbk_valist, NULL, &valist);
851 puts_clbk_str_w(&ctx, 1, &nullbyte);
852 return ret;
855 /*********************************************************************
856 * _vsnwprintf_l (MSVCRT.@)
858 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
859 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
860 __ms_va_list valist)
862 static const MSVCRT_wchar_t nullbyte = '\0';
863 struct _str_ctx_w ctx = {len, str};
864 int ret;
866 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, FALSE,
867 arg_clbk_valist, NULL, &valist);
868 puts_clbk_str_w(&ctx, 1, &nullbyte);
869 return ret;
872 /*********************************************************************
873 * _vswprintf_p_l (MSVCRT.@)
875 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
876 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
878 static const MSVCRT_wchar_t nullbyte = '\0';
879 printf_arg args_ctx[MSVCRT__ARGMAX+1];
880 struct _str_ctx_w puts_ctx = {length, buffer};
881 int ret;
883 memset(args_ctx, 0, sizeof(args_ctx));
885 ret = create_positional_ctx_w(args_ctx, format, args);
886 if(ret < 0) {
887 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
888 *MSVCRT__errno() = MSVCRT_EINVAL;
889 return ret;
890 } else if(ret == 0)
891 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, FALSE, TRUE,
892 arg_clbk_valist, NULL, &args);
893 else
894 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, TRUE, TRUE,
895 arg_clbk_positional, args_ctx, NULL);
897 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
898 return ret;
901 /*********************************************************************
902 * _vswprintf_p (MSVCR100.@)
904 int CDECL MSVCRT__vswprintf_p(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
905 const MSVCRT_wchar_t *format, __ms_va_list args)
907 return MSVCRT_vswprintf_p_l(buffer, length, format, NULL, args);
910 /*********************************************************************
911 * _vsnwprintf_s_l (MSVCRT.@)
913 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
914 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
915 MSVCRT__locale_t locale, __ms_va_list valist)
917 static const MSVCRT_wchar_t nullbyte = '\0';
918 struct _str_ctx_w ctx;
919 int len, ret;
921 len = sizeOfBuffer;
922 if(count!=-1 && len>count+1)
923 len = count+1;
925 ctx.len = len;
926 ctx.buf = str;
927 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, TRUE,
928 arg_clbk_valist, NULL, &valist);
929 puts_clbk_str_w(&ctx, 1, &nullbyte);
931 if(ret<0 || ret==len) {
932 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
933 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
934 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
935 } else
936 str[len-1] = '\0';
938 return -1;
941 return ret;
944 /*********************************************************************
945 * _vsnwprintf_s (MSVCRT.@)
947 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
948 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
950 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
951 format, NULL, valist);
954 /*********************************************************************
955 * _snwprintf (MSVCRT.@)
957 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
959 int retval;
960 __ms_va_list valist;
961 __ms_va_start(valist, format);
962 retval = MSVCRT_vsnwprintf(str, len, format, valist);
963 __ms_va_end(valist);
964 return retval;
967 /*********************************************************************
968 * _snwprintf_l (MSVCRT.@)
970 int CDECL MSVCRT__snwprintf_l( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format,
971 MSVCRT__locale_t locale, ...)
973 int retval;
974 __ms_va_list valist;
975 __ms_va_start(valist, locale);
976 retval = MSVCRT_vsnwprintf_l(str, len, format, locale, valist);
977 __ms_va_end(valist);
978 return retval;
981 /*********************************************************************
982 * _snwprintf_s (MSVCRT.@)
984 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
985 const MSVCRT_wchar_t *format, ...)
987 int retval;
988 __ms_va_list valist;
989 __ms_va_start(valist, format);
990 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
991 __ms_va_end(valist);
992 return retval;
995 /*********************************************************************
996 * _snwprintf_s_l (MSVCRT.@)
998 int CDECL MSVCRT__snwprintf_s_l( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
999 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1001 int retval;
1002 __ms_va_list valist;
1003 __ms_va_start(valist, locale);
1004 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, locale, valist);
1005 __ms_va_end(valist);
1006 return retval;
1009 /*********************************************************************
1010 * sprintf (MSVCRT.@)
1012 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
1014 __ms_va_list ap;
1015 int r;
1017 __ms_va_start( ap, format );
1018 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
1019 __ms_va_end( ap );
1020 return r;
1023 /*********************************************************************
1024 * sprintf_s (MSVCRT.@)
1026 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
1028 __ms_va_list ap;
1029 int r;
1031 __ms_va_start( ap, format );
1032 r = MSVCRT_vsnprintf( str, num, format, ap );
1033 __ms_va_end( ap );
1034 return r;
1037 /*********************************************************************
1038 * _scwprintf (MSVCRT.@)
1040 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
1042 __ms_va_list ap;
1043 int r;
1045 __ms_va_start( ap, format );
1046 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
1047 __ms_va_end( ap );
1048 return r;
1051 /*********************************************************************
1052 * swprintf (MSVCRT.@)
1054 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
1056 __ms_va_list ap;
1057 int r;
1059 __ms_va_start( ap, format );
1060 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
1061 __ms_va_end( ap );
1062 return r;
1065 /*********************************************************************
1066 * swprintf_s (MSVCRT.@)
1068 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1069 const MSVCRT_wchar_t *format, ... )
1071 __ms_va_list ap;
1072 int r;
1074 __ms_va_start(ap, format);
1075 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
1076 __ms_va_end(ap);
1078 return r;
1081 /*********************************************************************
1082 * _swprintf_s_l (MSVCRT.@)
1084 int CDECL MSVCRT__swprintf_s_l(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
1085 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
1087 __ms_va_list ap;
1088 int r;
1090 __ms_va_start(ap, locale);
1091 r = MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX, format, locale, ap);
1092 __ms_va_end(ap);
1094 return r;
1097 /*********************************************************************
1098 * _vswprintf (MSVCRT.@)
1100 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1102 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1105 /*********************************************************************
1106 * _vswprintf (MSVCRT.@)
1108 int CDECL MSVCRT_vswprintf_l( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format,
1109 MSVCRT__locale_t locale, __ms_va_list args )
1111 return MSVCRT_vsnwprintf_l( str, INT_MAX, format, locale, args );
1114 /*********************************************************************
1115 * _vscwprintf (MSVCRT.@)
1117 int CDECL MSVCRT__vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1119 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1122 /*********************************************************************
1123 * _vscwprintf_l (MSVCRT.@)
1125 int CDECL MSVCRT__vscwprintf_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1127 return MSVCRT_vsnwprintf_l( NULL, INT_MAX, format, locale, args );
1130 /*********************************************************************
1131 * _vscwprintf_p_l (MSVCRT.@)
1133 int CDECL MSVCRT__vscwprintf_p_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1135 return MSVCRT_vswprintf_p_l( NULL, INT_MAX, format, locale, args );
1138 /*********************************************************************
1139 * _vscwprintf_p (MSVCR100.@)
1141 int CDECL MSVCRT__vscwprintf_p(const MSVCRT_wchar_t *format, __ms_va_list args)
1143 return MSVCRT_vswprintf_p_l(NULL, INT_MAX, format, NULL, args);
1146 /*********************************************************************
1147 * vswprintf_s (MSVCRT.@)
1149 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1150 const MSVCRT_wchar_t* format, __ms_va_list args)
1152 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1155 /*********************************************************************
1156 * _vswprintf_s_l (MSVCRT.@)
1158 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1159 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1161 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1162 format, locale, args );
1165 /*********************************************************************
1166 * _vsprintf_p_l (MSVCRT.@)
1168 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
1169 MSVCRT__locale_t locale, __ms_va_list args)
1171 static const char nullbyte = '\0';
1172 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1173 struct _str_ctx_a puts_ctx = {length, buffer};
1174 int ret;
1176 memset(args_ctx, 0, sizeof(args_ctx));
1178 ret = create_positional_ctx_a(args_ctx, format, args);
1179 if(ret < 0) {
1180 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1181 *MSVCRT__errno() = MSVCRT_EINVAL;
1182 return ret;
1183 } else if(ret == 0)
1184 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, FALSE, TRUE,
1185 arg_clbk_valist, NULL, &args);
1186 else
1187 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, TRUE, TRUE,
1188 arg_clbk_positional, args_ctx, NULL);
1190 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
1191 return ret;
1194 /*********************************************************************
1195 * _vsprintf_p (MSVCRT.@)
1197 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
1198 const char *format, __ms_va_list args)
1200 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
1203 /*********************************************************************
1204 * _sprintf_p_l (MSVCRT.@)
1206 int CDECL MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
1207 const char *format, MSVCRT__locale_t locale, ...)
1209 __ms_va_list valist;
1210 int r;
1212 __ms_va_start(valist, locale);
1213 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
1214 __ms_va_end(valist);
1216 return r;
1219 /*********************************************************************
1220 * _sprintf_p (MSVCR100.@)
1222 int CDECL MSVCRT__sprintf_p(char *buffer, MSVCRT_size_t length, const char *format, ...)
1224 __ms_va_list valist;
1225 int r;
1227 __ms_va_start(valist, format);
1228 r = MSVCRT_vsprintf_p_l(buffer, length, format, NULL, valist);
1229 __ms_va_end(valist);
1231 return r;
1234 /*********************************************************************
1235 * _swprintf_p_l (MSVCRT.@)
1237 int CDECL MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1238 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
1240 __ms_va_list valist;
1241 int r;
1243 __ms_va_start(valist, locale);
1244 r = MSVCRT_vswprintf_p_l(buffer, length, format, locale, valist);
1245 __ms_va_end(valist);
1247 return r;
1250 /*********************************************************************
1251 * _wcscoll_l (MSVCRT.@)
1253 int CDECL MSVCRT__wcscoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
1255 MSVCRT_pthreadlocinfo locinfo;
1257 if(!locale)
1258 locinfo = get_locinfo();
1259 else
1260 locinfo = locale->locinfo;
1262 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE])
1263 return strcmpW(str1, str2);
1264 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
1267 /*********************************************************************
1268 * wcscoll (MSVCRT.@)
1270 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1272 return MSVCRT__wcscoll_l(str1, str2, NULL);
1275 /*********************************************************************
1276 * wcspbrk (MSVCRT.@)
1278 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1280 const MSVCRT_wchar_t* p;
1282 while (*str)
1284 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1285 str++;
1287 return NULL;
1290 /*********************************************************************
1291 * wcstok_s (MSVCRT.@)
1293 MSVCRT_wchar_t * CDECL MSVCRT_wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1294 MSVCRT_wchar_t **next_token )
1296 MSVCRT_wchar_t *ret;
1298 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1299 if (!MSVCRT_CHECK_PMT(next_token != NULL)) return NULL;
1300 if (!MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) return NULL;
1302 if (!str) str = *next_token;
1304 while (*str && strchrW( delim, *str )) str++;
1305 if (!*str) return NULL;
1306 ret = str++;
1307 while (*str && !strchrW( delim, *str )) str++;
1308 if (*str) *str++ = 0;
1309 *next_token = str;
1310 return ret;
1313 /*********************************************************************
1314 * wcstok (MSVCRT.@)
1316 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1318 return MSVCRT_wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1321 /*********************************************************************
1322 * _wctomb_s_l (MSVCRT.@)
1324 int CDECL MSVCRT__wctomb_s_l(int *len, char *mbchar, MSVCRT_size_t size,
1325 MSVCRT_wchar_t wch, MSVCRT__locale_t locale)
1327 MSVCRT_pthreadlocinfo locinfo;
1328 BOOL error;
1329 int mblen;
1331 if(!mbchar && size>0) {
1332 if(len)
1333 *len = 0;
1334 return 0;
1337 if(len)
1338 *len = -1;
1340 if(!MSVCRT_CHECK_PMT(size <= INT_MAX))
1341 return MSVCRT_EINVAL;
1343 if(!locale)
1344 locinfo = get_locinfo();
1345 else
1346 locinfo = locale->locinfo;
1348 if(!locinfo->lc_codepage) {
1349 if(wch > 0xff) {
1350 if(mbchar && size>0)
1351 memset(mbchar, 0, size);
1352 *MSVCRT__errno() = MSVCRT_EILSEQ;
1353 return MSVCRT_EILSEQ;
1356 if(!MSVCRT_CHECK_PMT_ERR(size >= 1, MSVCRT_ERANGE))
1357 return MSVCRT_ERANGE;
1359 *mbchar = wch;
1360 if(len)
1361 *len = 1;
1362 return 0;
1365 mblen = WideCharToMultiByte(locinfo->lc_codepage, 0, &wch, 1, mbchar, size, NULL, &error);
1366 if(!mblen || error) {
1367 if(!mblen && GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1368 if(mbchar && size>0)
1369 memset(mbchar, 0, size);
1371 MSVCRT_INVALID_PMT("insufficient buffer size", MSVCRT_ERANGE);
1372 return MSVCRT_ERANGE;
1375 *MSVCRT__errno() = MSVCRT_EILSEQ;
1376 return MSVCRT_EILSEQ;
1379 if(len)
1380 *len = mblen;
1381 return 0;
1384 /*********************************************************************
1385 * wctomb_s (MSVCRT.@)
1387 int CDECL MSVCRT_wctomb_s(int *len, char *mbchar, MSVCRT_size_t size, MSVCRT_wchar_t wch)
1389 return MSVCRT__wctomb_s_l(len, mbchar, size, wch, NULL);
1392 /*********************************************************************
1393 * _wctomb_l (MSVCRT.@)
1395 int CDECL MSVCRT__wctomb_l(char *dst, MSVCRT_wchar_t ch, MSVCRT__locale_t locale)
1397 int len;
1399 MSVCRT__wctomb_s_l(&len, dst, dst ? 6 : 0, ch, locale);
1400 return len;
1403 /*********************************************************************
1404 * wctomb (MSVCRT.@)
1406 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1408 return MSVCRT__wctomb_l(dst, ch, NULL);
1411 /*********************************************************************
1412 * wctob (MSVCRT.@)
1414 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1416 char out;
1417 BOOL error;
1418 UINT codepage = get_locinfo()->lc_codepage;
1420 if(!codepage) {
1421 if (wchar < 0xff)
1422 return (signed char)wchar;
1423 else
1424 return MSVCRT_EOF;
1425 } else if(WideCharToMultiByte( codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1426 return (INT)out;
1427 return MSVCRT_EOF;
1430 /*********************************************************************
1431 * wcrtomb (MSVCRT.@)
1433 MSVCRT_size_t CDECL MSVCRT_wcrtomb( char *dst, MSVCRT_wchar_t ch, MSVCRT_mbstate_t *s)
1435 if(s)
1436 *s = 0;
1437 return MSVCRT_wctomb(dst, ch);
1440 /*********************************************************************
1441 * iswalnum (MSVCRT.@)
1443 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1445 return isalnumW( wc );
1448 /*********************************************************************
1449 * iswalpha (MSVCRT.@)
1451 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1453 return isalphaW( wc );
1456 /*********************************************************************
1457 * iswalpha_l (MSVCRT.@)
1459 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1461 return isalphaW( wc );
1464 /*********************************************************************
1465 * iswcntrl (MSVCRT.@)
1467 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1469 return iscntrlW( wc );
1472 /*********************************************************************
1473 * iswdigit (MSVCRT.@)
1475 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1477 return isdigitW( wc );
1480 /*********************************************************************
1481 * _iswdigit_l (MSVCRT.@)
1483 INT CDECL MSVCRT__iswdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1485 return isdigitW( wc );
1488 /*********************************************************************
1489 * iswgraph (MSVCRT.@)
1491 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1493 return isgraphW( wc );
1496 /*********************************************************************
1497 * iswlower (MSVCRT.@)
1499 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1501 return islowerW( wc );
1504 /*********************************************************************
1505 * iswprint (MSVCRT.@)
1507 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1509 return isprintW( wc );
1512 /*********************************************************************
1513 * iswpunct (MSVCRT.@)
1515 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1517 return ispunctW( wc );
1520 /*********************************************************************
1521 * iswspace (MSVCRT.@)
1523 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1525 return isspaceW( wc );
1528 /*********************************************************************
1529 * iswupper (MSVCRT.@)
1531 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1533 return isupperW( wc );
1536 /*********************************************************************
1537 * iswxdigit (MSVCRT.@)
1539 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1541 return isxdigitW( wc );
1544 /*********************************************************************
1545 * wcscpy_s (MSVCRT.@)
1547 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1549 MSVCRT_size_t size = 0;
1551 if(!MSVCRT_CHECK_PMT(wcDest)) return MSVCRT_EINVAL;
1552 if(!MSVCRT_CHECK_PMT(numElement)) return MSVCRT_EINVAL;
1554 wcDest[0] = 0;
1556 if(!MSVCRT_CHECK_PMT(wcSrc)) return MSVCRT_EINVAL;
1558 size = strlenW(wcSrc) + 1;
1560 if(!MSVCRT_CHECK_PMT_ERR(size <= numElement, MSVCRT_ERANGE))
1561 return MSVCRT_ERANGE;
1563 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1565 return 0;
1568 /******************************************************************
1569 * wcsncpy (MSVCRT.@)
1571 MSVCRT_wchar_t* __cdecl MSVCRT_wcsncpy( MSVCRT_wchar_t* s1,
1572 const MSVCRT_wchar_t *s2, MSVCRT_size_t n )
1574 MSVCRT_size_t i;
1576 for(i=0; i<n; i++)
1577 if(!(s1[i] = s2[i])) break;
1578 for(; i<n; i++)
1579 s1[i] = 0;
1580 return s1;
1583 /******************************************************************
1584 * wcsncpy_s (MSVCRT.@)
1586 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1587 MSVCRT_size_t count )
1589 WCHAR *p = wcDest;
1590 BOOL truncate = (count == MSVCRT__TRUNCATE);
1592 if(!wcDest && !numElement && !count)
1593 return 0;
1595 if (!wcDest || !numElement)
1596 return MSVCRT_EINVAL;
1598 if (!wcSrc)
1600 *wcDest = 0;
1601 return count ? MSVCRT_EINVAL : 0;
1604 while (numElement && count && *wcSrc)
1606 *p++ = *wcSrc++;
1607 numElement--;
1608 count--;
1610 if (!numElement && truncate)
1612 *(p-1) = 0;
1613 return MSVCRT_STRUNCATE;
1615 else if (!numElement)
1617 *wcDest = 0;
1618 return MSVCRT_ERANGE;
1621 *p = 0;
1622 return 0;
1625 /******************************************************************
1626 * wcscat_s (MSVCRT.@)
1629 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1631 MSVCRT_wchar_t* ptr = dst;
1633 if (!dst || elem == 0) return MSVCRT_EINVAL;
1634 if (!src)
1636 dst[0] = '\0';
1637 return MSVCRT_EINVAL;
1640 /* seek to end of dst string (or elem if no end of string is found */
1641 while (ptr < dst + elem && *ptr != '\0') ptr++;
1642 while (ptr < dst + elem)
1644 if ((*ptr++ = *src++) == '\0') return 0;
1646 /* not enough space */
1647 dst[0] = '\0';
1648 return MSVCRT_ERANGE;
1651 /*********************************************************************
1652 * wcsncat_s (MSVCRT.@)
1655 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1656 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1658 MSVCRT_size_t srclen;
1659 MSVCRT_wchar_t dststart;
1660 INT ret = 0;
1662 if (!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
1663 if (!MSVCRT_CHECK_PMT(elem > 0)) return MSVCRT_EINVAL;
1664 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return MSVCRT_EINVAL;
1666 if (count == 0)
1667 return 0;
1669 for (dststart = 0; dststart < elem; dststart++)
1671 if (dst[dststart] == '\0')
1672 break;
1674 if (dststart == elem)
1676 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", MSVCRT_EINVAL);
1677 return MSVCRT_EINVAL;
1680 if (count == MSVCRT__TRUNCATE)
1682 srclen = strlenW(src);
1683 if (srclen >= (elem - dststart))
1685 srclen = elem - dststart - 1;
1686 ret = MSVCRT_STRUNCATE;
1689 else
1690 srclen = min(strlenW(src), count);
1691 if (srclen < (elem - dststart))
1693 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1694 dst[dststart+srclen] = '\0';
1695 return ret;
1697 MSVCRT_INVALID_PMT("dst[elem] is too small", MSVCRT_ERANGE);
1698 dst[0] = '\0';
1699 return MSVCRT_ERANGE;
1702 /*********************************************************************
1703 * _wcstoi64_l (MSVCRT.@)
1705 * FIXME: locale parameter is ignored
1707 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1708 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1710 BOOL negative = FALSE;
1711 __int64 ret = 0;
1713 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1715 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1716 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1717 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1719 while(isspaceW(*nptr)) nptr++;
1721 if(*nptr == '-') {
1722 negative = TRUE;
1723 nptr++;
1724 } else if(*nptr == '+')
1725 nptr++;
1727 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1728 base = 16;
1729 nptr += 2;
1732 if(base == 0) {
1733 if(*nptr=='0')
1734 base = 8;
1735 else
1736 base = 10;
1739 while(*nptr) {
1740 MSVCRT_wchar_t cur = tolowerW(*nptr);
1741 int v;
1743 if(isdigitW(cur)) {
1744 if(cur >= '0'+base)
1745 break;
1746 v = cur-'0';
1747 } else {
1748 if(cur<'a' || cur>='a'+base-10)
1749 break;
1750 v = cur-'a'+10;
1753 if(negative)
1754 v = -v;
1756 nptr++;
1758 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1759 ret = MSVCRT_I64_MAX;
1760 *MSVCRT__errno() = MSVCRT_ERANGE;
1761 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1762 ret = MSVCRT_I64_MIN;
1763 *MSVCRT__errno() = MSVCRT_ERANGE;
1764 } else
1765 ret = ret*base + v;
1768 if(endptr)
1769 *endptr = (MSVCRT_wchar_t*)nptr;
1771 return ret;
1774 /*********************************************************************
1775 * _wcstoi64 (MSVCRT.@)
1777 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1778 MSVCRT_wchar_t **endptr, int base)
1780 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1783 /*********************************************************************
1784 * _wcstol_l (MSVCRT.@)
1786 MSVCRT_long CDECL MSVCRT__wcstol_l(const MSVCRT_wchar_t *s,
1787 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
1789 __int64 ret = MSVCRT__wcstoi64_l(s, end, base, locale);
1791 if(ret > MSVCRT_LONG_MAX) {
1792 ret = MSVCRT_LONG_MAX;
1793 *MSVCRT__errno() = MSVCRT_ERANGE;
1794 }else if(ret < MSVCRT_LONG_MIN) {
1795 ret = MSVCRT_LONG_MIN;
1796 *MSVCRT__errno() = MSVCRT_ERANGE;
1798 return ret;
1801 /*********************************************************************
1802 * _wtoi_l (MSVCRT.@)
1804 int __cdecl MSVCRT__wtoi_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
1806 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
1808 if(ret > INT_MAX) {
1809 ret = INT_MAX;
1810 *MSVCRT__errno() = MSVCRT_ERANGE;
1811 } else if(ret < INT_MIN) {
1812 ret = INT_MIN;
1813 *MSVCRT__errno() = MSVCRT_ERANGE;
1815 return ret;
1818 /*********************************************************************
1819 * _wtoi (MSVCRT.@)
1821 int __cdecl MSVCRT__wtoi(const MSVCRT_wchar_t *str)
1823 return MSVCRT__wtoi_l(str, NULL);
1826 /*********************************************************************
1827 * _wtol_l (MSVCRT.@)
1829 MSVCRT_long __cdecl MSVCRT__wtol_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
1831 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
1833 if(ret > MSVCRT_LONG_MAX) {
1834 ret = MSVCRT_LONG_MAX;
1835 *MSVCRT__errno() = MSVCRT_ERANGE;
1836 } else if(ret < MSVCRT_LONG_MIN) {
1837 ret = MSVCRT_LONG_MIN;
1838 *MSVCRT__errno() = MSVCRT_ERANGE;
1840 return ret;
1843 /*********************************************************************
1844 * _wtol (MSVCRT.@)
1846 MSVCRT_long __cdecl MSVCRT__wtol(const MSVCRT_wchar_t *str)
1848 return MSVCRT__wtol_l(str, NULL);
1851 /*********************************************************************
1852 * _wcstoui64_l (MSVCRT.@)
1854 * FIXME: locale parameter is ignored
1856 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1857 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1859 BOOL negative = FALSE;
1860 unsigned __int64 ret = 0;
1862 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1864 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1865 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1866 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1868 while(isspaceW(*nptr)) nptr++;
1870 if(*nptr == '-') {
1871 negative = TRUE;
1872 nptr++;
1873 } else if(*nptr == '+')
1874 nptr++;
1876 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1877 base = 16;
1878 nptr += 2;
1881 if(base == 0) {
1882 if(*nptr=='0')
1883 base = 8;
1884 else
1885 base = 10;
1888 while(*nptr) {
1889 MSVCRT_wchar_t cur = tolowerW(*nptr);
1890 int v;
1892 if(isdigitW(cur)) {
1893 if(cur >= '0'+base)
1894 break;
1895 v = *nptr-'0';
1896 } else {
1897 if(cur<'a' || cur>='a'+base-10)
1898 break;
1899 v = cur-'a'+10;
1902 nptr++;
1904 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1905 ret = MSVCRT_UI64_MAX;
1906 *MSVCRT__errno() = MSVCRT_ERANGE;
1907 } else
1908 ret = ret*base + v;
1911 if(endptr)
1912 *endptr = (MSVCRT_wchar_t*)nptr;
1914 return negative ? -ret : ret;
1917 /*********************************************************************
1918 * _wcstoui64 (MSVCRT.@)
1920 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1921 MSVCRT_wchar_t **endptr, int base)
1923 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
1926 /*********************************************************************
1927 * _wcstoul_l (MSVCRT.@)
1929 MSVCRT_ulong __cdecl MSVCRT__wcstoul_l(const MSVCRT_wchar_t *s,
1930 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
1932 __int64 ret = MSVCRT__wcstoui64_l(s, end, base, locale);
1934 if(ret > MSVCRT_ULONG_MAX) {
1935 ret = MSVCRT_ULONG_MAX;
1936 *MSVCRT__errno() = MSVCRT_ERANGE;
1938 return ret;
1941 /*********************************************************************
1942 * wcstoul (MSVCRT.@)
1944 MSVCRT_ulong __cdecl MSVCRT_wcstoul(const MSVCRT_wchar_t *s, MSVCRT_wchar_t **end, int base)
1946 return MSVCRT__wcstoul_l(s, end, base, NULL);
1949 /******************************************************************
1950 * wcsnlen (MSVCRT.@)
1952 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
1954 MSVCRT_size_t i;
1956 for (i = 0; i < maxlen; i++)
1957 if (!s[i]) break;
1958 return i;
1961 /*********************************************************************
1962 * _towupper_l (MSVCRT.@)
1964 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
1966 return toupperW(c);
1969 /*********************************************************************
1970 * towupper (MSVCRT.@)
1972 int CDECL MSVCRT_towupper(MSVCRT_wint_t c)
1974 return MSVCRT__towupper_l(c, NULL);
1977 /*********************************************************************
1978 * _towlower_l (MSVCRT.@)
1980 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
1982 return tolowerW(c);
1985 /*********************************************************************
1986 * towlower (MSVCRT.@)
1988 int CDECL MSVCRT_towlower(MSVCRT_wint_t c)
1990 return MSVCRT__towlower_l(c, NULL);
1993 /*********************************************************************
1994 * wcschr (MSVCRT.@)
1996 MSVCRT_wchar_t* CDECL MSVCRT_wcschr(const MSVCRT_wchar_t *str, MSVCRT_wchar_t ch)
1998 return strchrW(str, ch);
2001 /***********************************************************************
2002 * wcslen (MSVCRT.@)
2004 int CDECL MSVCRT_wcslen(const MSVCRT_wchar_t *str)
2006 return strlenW(str);
2009 /*********************************************************************
2010 * wcsstr (MSVCRT.@)
2012 MSVCRT_wchar_t* CDECL MSVCRT_wcsstr(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *sub)
2014 return strstrW(str, sub);
2017 /*********************************************************************
2018 * _wtoi64_l (MSVCRT.@)
2020 __int64 CDECL MSVCRT__wtoi64_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
2022 ULONGLONG RunningTotal = 0;
2023 BOOL bMinus = FALSE;
2025 while (isspaceW(*str)) {
2026 str++;
2027 } /* while */
2029 if (*str == '+') {
2030 str++;
2031 } else if (*str == '-') {
2032 bMinus = TRUE;
2033 str++;
2034 } /* if */
2036 while (*str >= '0' && *str <= '9') {
2037 RunningTotal = RunningTotal * 10 + *str - '0';
2038 str++;
2039 } /* while */
2041 return bMinus ? -RunningTotal : RunningTotal;
2044 /*********************************************************************
2045 * _wtoi64 (MSVCRT.@)
2047 __int64 CDECL MSVCRT__wtoi64(const MSVCRT_wchar_t *str)
2049 return MSVCRT__wtoi64_l(str, NULL);
2052 /*********************************************************************
2053 * wcsncmp (MSVCRT.@)
2055 int CDECL MSVCRT_wcsncmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, int n)
2057 return strncmpW(str1, str2, n);
2060 /*********************************************************************
2061 * _wcsxfrm_l (MSVCRT.@)
2063 MSVCRT_size_t CDECL MSVCRT__wcsxfrm_l(MSVCRT_wchar_t *dest, const MSVCRT_wchar_t *src,
2064 MSVCRT_size_t len, MSVCRT__locale_t locale)
2066 MSVCRT_pthreadlocinfo locinfo;
2067 int i, ret;
2069 if(!MSVCRT_CHECK_PMT(src)) return INT_MAX;
2070 if(!MSVCRT_CHECK_PMT(dest || !len)) return INT_MAX;
2072 if(len > INT_MAX) {
2073 FIXME("len > INT_MAX not supported\n");
2074 len = INT_MAX;
2077 if(!locale)
2078 locinfo = get_locinfo();
2079 else
2080 locinfo = locale->locinfo;
2082 if(!locinfo->lc_handle[MSVCRT_LC_COLLATE]) {
2083 MSVCRT_wcsncpy(dest, src, len);
2084 return strlenW(src);
2087 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2088 LCMAP_SORTKEY, src, -1, NULL, 0);
2089 if(!ret) {
2090 if(len) dest[0] = 0;
2091 *MSVCRT__errno() = MSVCRT_EILSEQ;
2092 return INT_MAX;
2094 if(!len) return ret-1;
2096 if(ret > len) {
2097 dest[0] = 0;
2098 *MSVCRT__errno() = MSVCRT_ERANGE;
2099 return ret-1;
2102 ret = LCMapStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE],
2103 LCMAP_SORTKEY, src, -1, dest, len) - 1;
2104 for(i=ret; i>=0; i--)
2105 dest[i] = ((unsigned char*)dest)[i];
2106 return ret;
2109 /*********************************************************************
2110 * wcsxfrm (MSVCRT.@)
2112 MSVCRT_size_t CDECL MSVCRT_wcsxfrm(MSVCRT_wchar_t *dest,
2113 const MSVCRT_wchar_t *src, MSVCRT_size_t len)
2115 return MSVCRT__wcsxfrm_l(dest, src, len, NULL);