windowscodecs/tests: Use BOOL type where appropriate.
[wine.git] / dlls / msvcrt / wcs.c
blob97d916f30db7a15a833803dd3d7fd4316d13f571
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 /* _get_printf_count_output - not exported in native msvcrt */
44 int CDECL MSVCRT__get_printf_count_output( void )
46 return n_format_enabled ? 1 : 0;
49 /* _set_printf_count_output - not exported in native msvcrt */
50 int CDECL MSVCRT__set_printf_count_output( int enable )
52 BOOL old = n_format_enabled;
53 n_format_enabled = enable != 0;
54 return old ? 1 : 0;
57 /*********************************************************************
58 * _wcsdup (MSVCRT.@)
60 MSVCRT_wchar_t* CDECL MSVCRT__wcsdup( const MSVCRT_wchar_t* str )
62 MSVCRT_wchar_t* ret = NULL;
63 if (str)
65 int size = (strlenW(str) + 1) * sizeof(MSVCRT_wchar_t);
66 ret = MSVCRT_malloc( size );
67 if (ret) memcpy( ret, str, size );
69 return ret;
72 INT CDECL MSVCRT__wcsicmp_l(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, MSVCRT__locale_t locale)
74 if(!MSVCRT_CHECK_PMT(str1 != NULL) || !MSVCRT_CHECK_PMT(str2 != NULL))
75 return MSVCRT__NLSCMPERROR;
77 return strcmpiW(str1, str2);
80 /*********************************************************************
81 * _wcsicmp (MSVCRT.@)
83 INT CDECL MSVCRT__wcsicmp( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
85 return strcmpiW( str1, str2 );
88 /*********************************************************************
89 * _wcsnicmp (MSVCRT.@)
91 INT CDECL MSVCRT__wcsnicmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, INT n)
93 return strncmpiW(str1, str2, n);
96 /*********************************************************************
97 * _wcsicoll_l (MSVCRT.@)
99 int CDECL MSVCRT__wcsicoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
101 MSVCRT_pthreadlocinfo locinfo;
103 if(!locale)
104 locinfo = get_locinfo();
105 else
106 locinfo = locale->locinfo;
108 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
109 str1, -1, str2, -1)-CSTR_EQUAL;
112 /*********************************************************************
113 * _wcsicoll (MSVCRT.@)
115 INT CDECL MSVCRT__wcsicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
117 return MSVCRT__wcsicoll_l(str1, str2, NULL);
120 /*********************************************************************
121 * _wcsnicoll_l (MSVCRT.@)
123 int CDECL MSVCRT__wcsnicoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2,
124 MSVCRT_size_t count, MSVCRT__locale_t locale)
126 MSVCRT_pthreadlocinfo locinfo;
128 if(!locale)
129 locinfo = get_locinfo();
130 else
131 locinfo = locale->locinfo;
133 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
134 str1, count, str2, count)-CSTR_EQUAL;
137 /*********************************************************************
138 * _wcsnicoll (MSVCRT.@)
140 INT CDECL MSVCRT__wcsnicoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count )
142 return MSVCRT__wcsnicoll_l(str1, str2, count, NULL);
145 /*********************************************************************
146 * _wcsnset (MSVCRT.@)
148 MSVCRT_wchar_t* CDECL MSVCRT__wcsnset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c, MSVCRT_size_t n )
150 MSVCRT_wchar_t* ret = str;
151 while ((n-- > 0) && *str) *str++ = c;
152 return ret;
155 /*********************************************************************
156 * _wcsrev (MSVCRT.@)
158 MSVCRT_wchar_t* CDECL MSVCRT__wcsrev( MSVCRT_wchar_t* str )
160 MSVCRT_wchar_t* ret = str;
161 MSVCRT_wchar_t* end = str + strlenW(str) - 1;
162 while (end > str)
164 MSVCRT_wchar_t t = *end;
165 *end-- = *str;
166 *str++ = t;
168 return ret;
171 /*********************************************************************
172 * _wcsset (MSVCRT.@)
174 MSVCRT_wchar_t* CDECL MSVCRT__wcsset( MSVCRT_wchar_t* str, MSVCRT_wchar_t c )
176 MSVCRT_wchar_t* ret = str;
177 while (*str) *str++ = c;
178 return ret;
181 /******************************************************************
182 * _wcsupr_s_l (MSVCRT.@)
184 int CDECL MSVCRT__wcsupr_s_l( MSVCRT_wchar_t* str, MSVCRT_size_t n,
185 MSVCRT__locale_t locale )
187 MSVCRT_wchar_t* ptr = str;
189 if (!str || !n)
191 if (str) *str = '\0';
192 *MSVCRT__errno() = MSVCRT_EINVAL;
193 return MSVCRT_EINVAL;
196 while (n--)
198 if (!*ptr) return 0;
199 /* FIXME: add locale support */
200 *ptr = toupperW(*ptr);
201 ptr++;
204 /* MSDN claims that the function should return and set errno to
205 * ERANGE, which doesn't seem to be true based on the tests. */
206 *str = '\0';
207 *MSVCRT__errno() = MSVCRT_EINVAL;
208 return MSVCRT_EINVAL;
211 /******************************************************************
212 * _wcsupr_s (MSVCRT.@)
215 INT CDECL MSVCRT__wcsupr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
217 return MSVCRT__wcsupr_s_l( str, n, NULL );
220 /******************************************************************
221 * _wcslwr_s (MSVCRT.@)
223 int CDECL MSVCRT__wcslwr_s( MSVCRT_wchar_t* str, MSVCRT_size_t n )
225 MSVCRT_wchar_t* ptr = str;
227 if (!str || !n)
229 if (str) *str = '\0';
230 *MSVCRT__errno() = MSVCRT_EINVAL;
231 return MSVCRT_EINVAL;
234 while (n--)
236 if (!*ptr) return 0;
237 *ptr = tolowerW(*ptr);
238 ptr++;
241 /* MSDN claims that the function should return and set errno to
242 * ERANGE, which doesn't seem to be true based on the tests. */
243 *str = '\0';
244 *MSVCRT__errno() = MSVCRT_EINVAL;
245 return MSVCRT_EINVAL;
248 /*********************************************************************
249 * _wcsncoll_l (MSVCRT.@)
251 int CDECL MSVCRT__wcsncoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2,
252 MSVCRT_size_t count, MSVCRT__locale_t locale)
254 MSVCRT_pthreadlocinfo locinfo;
256 if(!locale)
257 locinfo = get_locinfo();
258 else
259 locinfo = locale->locinfo;
261 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, count, str2, count)-CSTR_EQUAL;
264 /*********************************************************************
265 * _wcsncoll (MSVCRT.@)
267 int CDECL MSVCRT__wcsncoll(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT_size_t count)
269 return MSVCRT__wcsncoll_l(str1, str2, count, NULL);
272 /*********************************************************************
273 * _wcstod_l (MSVCRT.@)
275 double CDECL MSVCRT__wcstod_l(const MSVCRT_wchar_t* str, MSVCRT_wchar_t** end,
276 MSVCRT__locale_t locale)
278 MSVCRT_pthreadlocinfo locinfo;
279 unsigned __int64 d=0, hlp;
280 unsigned fpcontrol;
281 int exp=0, sign=1;
282 const MSVCRT_wchar_t *p;
283 double ret;
284 long double lret=1, expcnt = 10;
285 BOOL found_digit = FALSE, negexp;
287 if (!MSVCRT_CHECK_PMT(str != NULL)) return 0;
289 if(!locale)
290 locinfo = get_locinfo();
291 else
292 locinfo = locale->locinfo;
294 p = str;
295 while(isspaceW(*p))
296 p++;
298 if(*p == '-') {
299 sign = -1;
300 p++;
301 } else if(*p == '+')
302 p++;
304 while(isdigitW(*p)) {
305 found_digit = TRUE;
306 hlp = d*10+*(p++)-'0';
307 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
308 exp++;
309 break;
310 } else
311 d = hlp;
313 while(isdigitW(*p)) {
314 exp++;
315 p++;
317 if(*p == *locinfo->lconv->decimal_point)
318 p++;
320 while(isdigitW(*p)) {
321 found_digit = TRUE;
322 hlp = d*10+*(p++)-'0';
323 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
324 break;
326 d = hlp;
327 exp--;
329 while(isdigitW(*p))
330 p++;
332 if(!found_digit) {
333 if(end)
334 *end = (MSVCRT_wchar_t*)str;
335 return 0.0;
338 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
339 int e=0, s=1;
341 p++;
342 if(*p == '-') {
343 s = -1;
344 p++;
345 } else if(*p == '+')
346 p++;
348 if(isdigitW(*p)) {
349 while(isdigitW(*p)) {
350 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
351 e = INT_MAX;
352 p++;
354 e *= s;
356 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
357 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
358 else exp += e;
359 } else {
360 if(*p=='-' || *p=='+')
361 p--;
362 p--;
366 fpcontrol = _control87(0, 0);
367 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
368 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
370 negexp = (exp < 0);
371 if(negexp)
372 exp = -exp;
373 while(exp) {
374 if(exp & 1)
375 lret *= expcnt;
376 exp /= 2;
377 expcnt = expcnt*expcnt;
379 ret = (long double)sign * (negexp ? d/lret : d*lret);
381 _control87(fpcontrol, 0xffffffff);
383 if((d && ret==0.0) || isinf(ret))
384 *MSVCRT__errno() = MSVCRT_ERANGE;
386 if(end)
387 *end = (MSVCRT_wchar_t*)p;
389 return ret;
392 /*********************************************************************
393 * wcsrtombs_l (INTERNAL)
395 static MSVCRT_size_t MSVCRT_wcsrtombs_l(char *mbstr, const MSVCRT_wchar_t **wcstr,
396 MSVCRT_size_t count, MSVCRT__locale_t locale)
398 MSVCRT_pthreadlocinfo locinfo;
399 MSVCRT_size_t tmp = 0;
400 BOOL used_default;
402 if(!locale)
403 locinfo = get_locinfo();
404 else
405 locinfo = locale->locinfo;
407 if(!mbstr) {
408 tmp = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
409 *wcstr, -1, NULL, 0, NULL, &used_default)-1;
410 if(!tmp || used_default) {
411 *MSVCRT__errno() = MSVCRT_EILSEQ;
412 return -1;
414 return tmp;
417 while(**wcstr) {
418 char buf[3];
419 MSVCRT_size_t i, size;
421 size = WideCharToMultiByte(locinfo->lc_codepage, WC_NO_BEST_FIT_CHARS,
422 *wcstr, 1, buf, 3, NULL, &used_default);
423 if(!size || used_default) {
424 *MSVCRT__errno() = MSVCRT_EILSEQ;
425 return -1;
427 if(tmp+size > count)
428 return tmp;
430 for(i=0; i<size; i++)
431 mbstr[tmp++] = buf[i];
432 (*wcstr)++;
435 if(tmp < count) {
436 mbstr[tmp] = '\0';
437 *wcstr = NULL;
439 return tmp;
442 /*********************************************************************
443 * _wcstombs_l (MSVCRT.@)
445 MSVCRT_size_t CDECL MSVCRT__wcstombs_l(char *mbstr, const MSVCRT_wchar_t *wcstr,
446 MSVCRT_size_t count, MSVCRT__locale_t locale)
448 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, locale);
451 /*********************************************************************
452 * wcstombs (MSVCRT.@)
454 MSVCRT_size_t CDECL MSVCRT_wcstombs(char *mbstr, const MSVCRT_wchar_t *wcstr,
455 MSVCRT_size_t count)
457 return MSVCRT_wcsrtombs_l(mbstr, &wcstr, count, NULL);
460 /*********************************************************************
461 * wcsrtombs (MSVCRT.@)
463 MSVCRT_size_t CDECL MSVCRT_wcsrtombs(char *mbstr, const MSVCRT_wchar_t **wcstr,
464 MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
466 if(mbstate)
467 *mbstate = 0;
469 return MSVCRT_wcsrtombs_l(mbstr, wcstr, count, NULL);
472 /*********************************************************************
473 * MSVCRT_wcsrtombs_s_l (INTERNAL)
475 static int MSVCRT_wcsrtombs_s_l(MSVCRT_size_t *ret, char *mbstr,
476 MSVCRT_size_t size, const MSVCRT_wchar_t **wcstr,
477 MSVCRT_size_t count, MSVCRT__locale_t locale)
479 MSVCRT_size_t conv;
481 if(!mbstr && !size && wcstr) {
482 conv = MSVCRT_wcsrtombs_l(NULL, wcstr, 0, locale);
483 if(conv == -1)
484 return *MSVCRT__errno();
485 if(ret)
486 *ret = conv+1;
487 return 0;
490 if (!MSVCRT_CHECK_PMT(mbstr != NULL)) return MSVCRT_EINVAL;
491 if (size) mbstr[0] = '\0';
492 if (!MSVCRT_CHECK_PMT(wcstr != NULL)) return MSVCRT_EINVAL;
493 if (!MSVCRT_CHECK_PMT(*wcstr != NULL)) return MSVCRT_EINVAL;
495 if(count==MSVCRT__TRUNCATE || size<count)
496 conv = size;
497 else
498 conv = count;
500 conv = MSVCRT_wcsrtombs_l(mbstr, wcstr, conv, locale);
501 if(conv == -1) {
502 if(size)
503 mbstr[0] = '\0';
504 return *MSVCRT__errno();
505 }else if(conv < size)
506 mbstr[conv++] = '\0';
507 else if(conv==size && (count==MSVCRT__TRUNCATE || mbstr[conv-1]=='\0'))
508 mbstr[conv-1] = '\0';
509 else {
510 MSVCRT_INVALID_PMT("mbstr[size] is too small", MSVCRT_ERANGE);
511 if(size)
512 mbstr[0] = '\0';
513 return MSVCRT_ERANGE;
516 if(ret)
517 *ret = conv;
518 return 0;
521 /*********************************************************************
522 * _wcstombs_s_l (MSVCRT.@)
524 int CDECL MSVCRT__wcstombs_s_l(MSVCRT_size_t *ret, char *mbstr,
525 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr,
526 MSVCRT_size_t count, MSVCRT__locale_t locale)
528 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr,count, locale);
531 /*********************************************************************
532 * wcstombs_s (MSVCRT.@)
534 int CDECL MSVCRT_wcstombs_s(MSVCRT_size_t *ret, char *mbstr,
535 MSVCRT_size_t size, const MSVCRT_wchar_t *wcstr, MSVCRT_size_t count)
537 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, &wcstr, count, NULL);
540 /*********************************************************************
541 * wcsrtombs_s (MSVCRT.@)
543 int CDECL MSVCRT_wcsrtombs_s(MSVCRT_size_t *ret, char *mbstr, MSVCRT_size_t size,
544 const MSVCRT_wchar_t **wcstr, MSVCRT_size_t count, MSVCRT_mbstate_t *mbstate)
546 if(mbstate)
547 *mbstate = 0;
549 return MSVCRT_wcsrtombs_s_l(ret, mbstr, size, wcstr, count, NULL);
552 /*********************************************************************
553 * wcstod (MSVCRT.@)
555 double CDECL MSVCRT_wcstod(const MSVCRT_wchar_t* lpszStr, MSVCRT_wchar_t** end)
557 return MSVCRT__wcstod_l(lpszStr, end, NULL);
560 /*********************************************************************
561 * _wtof (MSVCRT.@)
563 double CDECL MSVCRT__wtof(const MSVCRT_wchar_t *str)
565 return MSVCRT__wcstod_l(str, NULL, NULL);
568 /*********************************************************************
569 * _wtof_l (MSVCRT.@)
571 double CDECL MSVCRT__wtof_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
573 return MSVCRT__wcstod_l(str, NULL, locale);
576 /*********************************************************************
577 * arg_clbk_valist (INTERNAL)
579 printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valist)
581 printf_arg ret;
583 if(type == VT_I8)
584 ret.get_longlong = va_arg(*valist, LONGLONG);
585 else if(type == VT_INT)
586 ret.get_int = va_arg(*valist, int);
587 else if(type == VT_R8)
588 ret.get_double = va_arg(*valist, double);
589 else if(type == VT_PTR)
590 ret.get_ptr = va_arg(*valist, void*);
591 else {
592 ERR("Incorrect type\n");
593 ret.get_int = 0;
596 return ret;
599 /*********************************************************************
600 * arg_clbk_positional (INTERNAL)
602 static printf_arg arg_clbk_positional(void *ctx, int pos, int type, __ms_va_list *valist)
604 printf_arg *args = ctx;
605 return args[pos];
608 /*********************************************************************
609 * _vsnprintf (MSVCRT.@)
611 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
612 const char *format, __ms_va_list valist )
614 static const char nullbyte = '\0';
615 struct _str_ctx_a ctx = {len, str};
616 int ret;
618 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, FALSE, FALSE,
619 arg_clbk_valist, NULL, &valist);
620 puts_clbk_str_a(&ctx, 1, &nullbyte);
621 return ret;
624 /*********************************************************************
625 * _vsnprintf_l (MSVCRT.@)
627 int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
628 MSVCRT__locale_t locale, __ms_va_list valist )
630 static const char nullbyte = '\0';
631 struct _str_ctx_a ctx = {len, str};
632 int ret;
634 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, FALSE,
635 arg_clbk_valist, NULL, &valist);
636 puts_clbk_str_a(&ctx, 1, &nullbyte);
637 return ret;
640 /*********************************************************************
641 * _vsnprintf_s_l (MSVCRT.@)
643 int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
644 MSVCRT_size_t count, const char *format,
645 MSVCRT__locale_t locale, __ms_va_list valist )
647 static const char nullbyte = '\0';
648 struct _str_ctx_a ctx;
649 int len, ret;
651 if(sizeOfBuffer<count+1 || count==-1)
652 len = sizeOfBuffer;
653 else
654 len = count+1;
656 ctx.len = len;
657 ctx.buf = str;
658 ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, TRUE,
659 arg_clbk_valist, NULL, &valist);
660 puts_clbk_str_a(&ctx, 1, &nullbyte);
662 if(ret<0 || ret==len) {
663 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
664 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
665 memset(str, 0, sizeOfBuffer);
666 } else
667 str[len-1] = '\0';
669 return -1;
672 return ret;
675 /*********************************************************************
676 * _vsnprintf_s (MSVCRT.@)
678 int CDECL MSVCRT_vsnprintf_s( char *str, MSVCRT_size_t sizeOfBuffer,
679 MSVCRT_size_t count, const char *format, __ms_va_list valist )
681 return MSVCRT_vsnprintf_s_l(str,sizeOfBuffer, count, format, NULL, valist);
684 /*********************************************************************
685 * vsprintf (MSVCRT.@)
687 int CDECL MSVCRT_vsprintf( char *str, const char *format, __ms_va_list valist)
689 return MSVCRT_vsnprintf(str, INT_MAX, format, valist);
692 /*********************************************************************
693 * vsprintf_s (MSVCRT.@)
695 int CDECL MSVCRT_vsprintf_s( char *str, MSVCRT_size_t num, const char *format, __ms_va_list valist)
697 return MSVCRT_vsnprintf(str, num, format, valist);
700 /*********************************************************************
701 * _vscprintf (MSVCRT.@)
703 int CDECL MSVCRT__vscprintf( const char *format, __ms_va_list valist )
705 return MSVCRT_vsnprintf( NULL, INT_MAX, format, valist );
708 /*********************************************************************
709 * _snprintf (MSVCRT.@)
711 int CDECL MSVCRT__snprintf(char *str, unsigned int len, const char *format, ...)
713 int retval;
714 __ms_va_list valist;
715 __ms_va_start(valist, format);
716 retval = MSVCRT_vsnprintf(str, len, format, valist);
717 __ms_va_end(valist);
718 return retval;
721 /*********************************************************************
722 * _snprintf_s (MSVCRT.@)
724 int CDECL MSVCRT__snprintf_s(char *str, unsigned int len, unsigned int count,
725 const char *format, ...)
727 int retval;
728 __ms_va_list valist;
729 __ms_va_start(valist, format);
730 retval = MSVCRT_vsnprintf_s_l(str, len, count, format, NULL, valist);
731 __ms_va_end(valist);
732 return retval;
735 /*********************************************************************
736 * _scprintf (MSVCRT.@)
738 int CDECL MSVCRT__scprintf(const char *format, ...)
740 int retval;
741 __ms_va_list valist;
742 __ms_va_start(valist, format);
743 retval = MSVCRT__vscprintf(format, valist);
744 __ms_va_end(valist);
745 return retval;
748 /*********************************************************************
749 * _vsnwprintf (MSVCRT.@)
751 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
752 const MSVCRT_wchar_t *format, __ms_va_list valist)
754 static const MSVCRT_wchar_t nullbyte = '\0';
755 struct _str_ctx_w ctx = {len, str};
756 int ret;
758 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, FALSE, FALSE,
759 arg_clbk_valist, NULL, &valist);
760 puts_clbk_str_w(&ctx, 1, &nullbyte);
761 return ret;
764 /*********************************************************************
765 * _vsnwprintf_l (MSVCRT.@)
767 int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
768 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
769 __ms_va_list valist)
771 static const MSVCRT_wchar_t nullbyte = '\0';
772 struct _str_ctx_w ctx = {len, str};
773 int ret;
775 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, FALSE,
776 arg_clbk_valist, NULL, &valist);
777 puts_clbk_str_w(&ctx, 1, &nullbyte);
778 return ret;
781 /*********************************************************************
782 * _vswprintf_p_l (MSVCRT.@)
784 int CDECL MSVCRT_vswprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
785 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args)
787 static const MSVCRT_wchar_t nullbyte = '\0';
788 printf_arg args_ctx[MSVCRT__ARGMAX+1];
789 struct _str_ctx_w puts_ctx = {length, buffer};
790 int ret;
792 memset(args_ctx, 0, sizeof(args_ctx));
794 ret = create_positional_ctx_w(args_ctx, format, args);
795 if(ret < 0) {
796 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
797 *MSVCRT__errno() = MSVCRT_EINVAL;
798 return ret;
799 } else if(ret == 0)
800 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, FALSE, TRUE,
801 arg_clbk_valist, NULL, &args);
802 else
803 ret = pf_printf_w(puts_clbk_str_w, &puts_ctx, format, locale, TRUE, TRUE,
804 arg_clbk_positional, args_ctx, NULL);
806 puts_clbk_str_w(&puts_ctx, 1, &nullbyte);
807 return ret;
810 /*********************************************************************
811 * _vsnwprintf_s_l (MSVCRT.@)
813 int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
814 MSVCRT_size_t count, const MSVCRT_wchar_t *format,
815 MSVCRT__locale_t locale, __ms_va_list valist)
817 static const MSVCRT_wchar_t nullbyte = '\0';
818 struct _str_ctx_w ctx;
819 int len, ret;
821 len = sizeOfBuffer;
822 if(count!=-1 && len>count+1)
823 len = count+1;
825 ctx.len = len;
826 ctx.buf = str;
827 ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, TRUE,
828 arg_clbk_valist, NULL, &valist);
829 puts_clbk_str_w(&ctx, 1, &nullbyte);
831 if(ret<0 || ret==len) {
832 if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
833 MSVCRT_INVALID_PMT("str[sizeOfBuffer] is too small", MSVCRT_ERANGE);
834 memset(str, 0, sizeOfBuffer*sizeof(MSVCRT_wchar_t));
835 } else
836 str[len-1] = '\0';
838 return -1;
841 return ret;
844 /*********************************************************************
845 * _vsnwprintf_s (MSVCRT.@)
847 int CDECL MSVCRT_vsnwprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer,
848 MSVCRT_size_t count, const MSVCRT_wchar_t *format, __ms_va_list valist)
850 return MSVCRT_vsnwprintf_s_l(str, sizeOfBuffer, count,
851 format, NULL, valist);
854 /*********************************************************************
855 * _snwprintf (MSVCRT.@)
857 int CDECL MSVCRT__snwprintf( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format, ...)
859 int retval;
860 __ms_va_list valist;
861 __ms_va_start(valist, format);
862 retval = MSVCRT_vsnwprintf(str, len, format, valist);
863 __ms_va_end(valist);
864 return retval;
867 /*********************************************************************
868 * _snwprintf_l (MSVCRT.@)
870 int CDECL MSVCRT__snwprintf_l( MSVCRT_wchar_t *str, unsigned int len, const MSVCRT_wchar_t *format,
871 MSVCRT__locale_t locale, ...)
873 int retval;
874 __ms_va_list valist;
875 __ms_va_start(valist, locale);
876 retval = MSVCRT_vsnwprintf_l(str, len, format, locale, valist);
877 __ms_va_end(valist);
878 return retval;
881 /*********************************************************************
882 * _snwprintf_s (MSVCRT.@)
884 int CDECL MSVCRT__snwprintf_s( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
885 const MSVCRT_wchar_t *format, ...)
887 int retval;
888 __ms_va_list valist;
889 __ms_va_start(valist, format);
890 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, NULL, valist);
891 __ms_va_end(valist);
892 return retval;
895 /*********************************************************************
896 * _snwprintf_s_l (MSVCRT.@)
898 int CDECL MSVCRT__snwprintf_s_l( MSVCRT_wchar_t *str, unsigned int len, unsigned int count,
899 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
901 int retval;
902 __ms_va_list valist;
903 __ms_va_start(valist, locale);
904 retval = MSVCRT_vsnwprintf_s_l(str, len, count, format, locale, valist);
905 __ms_va_end(valist);
906 return retval;
909 /*********************************************************************
910 * sprintf (MSVCRT.@)
912 int CDECL MSVCRT_sprintf( char *str, const char *format, ... )
914 __ms_va_list ap;
915 int r;
917 __ms_va_start( ap, format );
918 r = MSVCRT_vsnprintf( str, INT_MAX, format, ap );
919 __ms_va_end( ap );
920 return r;
923 /*********************************************************************
924 * sprintf_s (MSVCRT.@)
926 int CDECL MSVCRT_sprintf_s( char *str, MSVCRT_size_t num, const char *format, ... )
928 __ms_va_list ap;
929 int r;
931 __ms_va_start( ap, format );
932 r = MSVCRT_vsnprintf( str, num, format, ap );
933 __ms_va_end( ap );
934 return r;
937 /*********************************************************************
938 * _scwprintf (MSVCRT.@)
940 int CDECL MSVCRT__scwprintf( const MSVCRT_wchar_t *format, ... )
942 __ms_va_list ap;
943 int r;
945 __ms_va_start( ap, format );
946 r = MSVCRT_vsnwprintf( NULL, INT_MAX, format, ap );
947 __ms_va_end( ap );
948 return r;
951 /*********************************************************************
952 * swprintf (MSVCRT.@)
954 int CDECL MSVCRT_swprintf( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ... )
956 __ms_va_list ap;
957 int r;
959 __ms_va_start( ap, format );
960 r = MSVCRT_vsnwprintf( str, INT_MAX, format, ap );
961 __ms_va_end( ap );
962 return r;
965 /*********************************************************************
966 * swprintf_s (MSVCRT.@)
968 int CDECL MSVCRT_swprintf_s(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
969 const MSVCRT_wchar_t *format, ... )
971 __ms_va_list ap;
972 int r;
974 __ms_va_start(ap, format);
975 r = MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, ap);
976 __ms_va_end(ap);
978 return r;
981 /*********************************************************************
982 * _swprintf_s_l (MSVCRT.@)
984 int CDECL MSVCRT__swprintf_s_l(MSVCRT_wchar_t *str, MSVCRT_size_t numberOfElements,
985 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ... )
987 __ms_va_list ap;
988 int r;
990 __ms_va_start(ap, locale);
991 r = MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX, format, locale, ap);
992 __ms_va_end(ap);
994 return r;
997 /*********************************************************************
998 * _vswprintf (MSVCRT.@)
1000 int CDECL MSVCRT_vswprintf( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format, __ms_va_list args )
1002 return MSVCRT_vsnwprintf( str, INT_MAX, format, args );
1005 /*********************************************************************
1006 * _vswprintf (MSVCRT.@)
1008 int CDECL MSVCRT_vswprintf_l( MSVCRT_wchar_t* str, const MSVCRT_wchar_t* format,
1009 MSVCRT__locale_t locale, __ms_va_list args )
1011 return MSVCRT_vsnwprintf_l( str, INT_MAX, format, locale, args );
1014 /*********************************************************************
1015 * _vscwprintf (MSVCRT.@)
1017 int CDECL MSVCRT__vscwprintf( const MSVCRT_wchar_t *format, __ms_va_list args )
1019 return MSVCRT_vsnwprintf( NULL, INT_MAX, format, args );
1022 /*********************************************************************
1023 * _vscwprintf_l (MSVCRT.@)
1025 int CDECL MSVCRT__vscwprintf_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1027 return MSVCRT_vsnwprintf_l( NULL, INT_MAX, format, locale, args );
1030 /*********************************************************************
1031 * _vscwprintf_p_l (MSVCRT.@)
1033 int CDECL MSVCRT__vscwprintf_p_l( const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, __ms_va_list args )
1035 return MSVCRT_vswprintf_p_l( NULL, INT_MAX, format, locale, args );
1038 /*********************************************************************
1039 * vswprintf_s (MSVCRT.@)
1041 int CDECL MSVCRT_vswprintf_s(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1042 const MSVCRT_wchar_t* format, __ms_va_list args)
1044 return MSVCRT_vsnwprintf_s(str, numberOfElements, INT_MAX, format, args );
1047 /*********************************************************************
1048 * _vswprintf_s_l (MSVCRT.@)
1050 int CDECL MSVCRT_vswprintf_s_l(MSVCRT_wchar_t* str, MSVCRT_size_t numberOfElements,
1051 const MSVCRT_wchar_t* format, MSVCRT__locale_t locale, __ms_va_list args)
1053 return MSVCRT_vsnwprintf_s_l(str, numberOfElements, INT_MAX,
1054 format, locale, args );
1057 /*********************************************************************
1058 * _vsprintf_p_l (MSVCRT.@)
1060 int CDECL MSVCRT_vsprintf_p_l(char *buffer, MSVCRT_size_t length, const char *format,
1061 MSVCRT__locale_t locale, __ms_va_list args)
1063 static const char nullbyte = '\0';
1064 printf_arg args_ctx[MSVCRT__ARGMAX+1];
1065 struct _str_ctx_a puts_ctx = {length, buffer};
1066 int ret;
1068 memset(args_ctx, 0, sizeof(args_ctx));
1070 ret = create_positional_ctx_a(args_ctx, format, args);
1071 if(ret < 0) {
1072 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
1073 *MSVCRT__errno() = MSVCRT_EINVAL;
1074 return ret;
1075 } else if(ret == 0)
1076 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, FALSE, TRUE,
1077 arg_clbk_valist, NULL, &args);
1078 else
1079 ret = pf_printf_a(puts_clbk_str_a, &puts_ctx, format, locale, TRUE, TRUE,
1080 arg_clbk_positional, args_ctx, NULL);
1082 puts_clbk_str_a(&puts_ctx, 1, &nullbyte);
1083 return ret;
1086 /*********************************************************************
1087 * _vsprintf_p (MSVCRT.@)
1089 int CDECL MSVCRT_vsprintf_p(char *buffer, MSVCRT_size_t length,
1090 const char *format, __ms_va_list args)
1092 return MSVCRT_vsprintf_p_l(buffer, length, format, NULL, args);
1095 /*********************************************************************
1096 * _sprintf_p_l (MSVCRT.@)
1098 int CDECL MSVCRT_sprintf_p_l(char *buffer, MSVCRT_size_t length,
1099 const char *format, MSVCRT__locale_t locale, ...)
1101 __ms_va_list valist;
1102 int r;
1104 __ms_va_start(valist, locale);
1105 r = MSVCRT_vsprintf_p_l(buffer, length, format, locale, valist);
1106 __ms_va_end(valist);
1108 return r;
1111 /*********************************************************************
1112 * _swprintf_p_l (MSVCRT.@)
1114 int CDECL MSVCRT_swprintf_p_l(MSVCRT_wchar_t *buffer, MSVCRT_size_t length,
1115 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
1117 __ms_va_list valist;
1118 int r;
1120 __ms_va_start(valist, locale);
1121 r = MSVCRT_vswprintf_p_l(buffer, length, format, locale, valist);
1122 __ms_va_end(valist);
1124 return r;
1127 /*********************************************************************
1128 * _wcscoll_l (MSVCRT.@)
1130 int CDECL MSVCRT__wcscoll_l(const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2, MSVCRT__locale_t locale)
1132 MSVCRT_pthreadlocinfo locinfo;
1134 if(!locale)
1135 locinfo = get_locinfo();
1136 else
1137 locinfo = locale->locinfo;
1139 return CompareStringW(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
1142 /*********************************************************************
1143 * wcscoll (MSVCRT.@)
1145 int CDECL MSVCRT_wcscoll( const MSVCRT_wchar_t* str1, const MSVCRT_wchar_t* str2 )
1147 return MSVCRT__wcscoll_l(str1, str2, NULL);
1150 /*********************************************************************
1151 * wcspbrk (MSVCRT.@)
1153 MSVCRT_wchar_t* CDECL MSVCRT_wcspbrk( const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* accept )
1155 const MSVCRT_wchar_t* p;
1157 while (*str)
1159 for (p = accept; *p; p++) if (*p == *str) return (MSVCRT_wchar_t*)str;
1160 str++;
1162 return NULL;
1165 /*********************************************************************
1166 * wcstok_s (MSVCRT.@)
1168 MSVCRT_wchar_t * CDECL wcstok_s( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim,
1169 MSVCRT_wchar_t **next_token )
1171 MSVCRT_wchar_t *ret;
1173 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
1174 if (!MSVCRT_CHECK_PMT(next_token != NULL)) return NULL;
1175 if (!MSVCRT_CHECK_PMT(str != NULL || *next_token != NULL)) return NULL;
1177 if (!str) str = *next_token;
1179 while (*str && strchrW( delim, *str )) str++;
1180 if (!*str) return NULL;
1181 ret = str++;
1182 while (*str && !strchrW( delim, *str )) str++;
1183 if (*str) *str++ = 0;
1184 *next_token = str;
1185 return ret;
1188 /*********************************************************************
1189 * wcstok (MSVCRT.@)
1191 MSVCRT_wchar_t * CDECL MSVCRT_wcstok( MSVCRT_wchar_t *str, const MSVCRT_wchar_t *delim )
1193 return wcstok_s(str, delim, &msvcrt_get_thread_data()->wcstok_next);
1196 /*********************************************************************
1197 * _wctomb_s_l (MSVCRT.@)
1199 int CDECL MSVCRT__wctomb_s_l(int *len, char *mbchar, MSVCRT_size_t size,
1200 MSVCRT_wchar_t wch, MSVCRT__locale_t locale)
1202 MSVCRT_pthreadlocinfo locinfo;
1203 BOOL error;
1204 int mblen;
1206 if(!mbchar && size>0) {
1207 if(len)
1208 *len = 0;
1209 return 0;
1212 if(len)
1213 *len = -1;
1215 if(!MSVCRT_CHECK_PMT(size <= INT_MAX))
1216 return MSVCRT_EINVAL;
1218 if(!locale)
1219 locinfo = get_locinfo();
1220 else
1221 locinfo = locale->locinfo;
1223 if(!locinfo->lc_codepage) {
1224 if(wch > 0xff) {
1225 if(mbchar && size>0)
1226 memset(mbchar, 0, size);
1227 *MSVCRT__errno() = MSVCRT_EILSEQ;
1228 return MSVCRT_EILSEQ;
1231 if(!MSVCRT_CHECK_PMT_ERR(size >= 1, MSVCRT_ERANGE))
1232 return MSVCRT_ERANGE;
1234 *mbchar = wch;
1235 if(len)
1236 *len = 1;
1237 return 0;
1240 mblen = WideCharToMultiByte(locinfo->lc_codepage, 0, &wch, 1, mbchar, size, NULL, &error);
1241 if(!mblen || error) {
1242 if(!mblen && GetLastError()==ERROR_INSUFFICIENT_BUFFER) {
1243 if(mbchar && size>0)
1244 memset(mbchar, 0, size);
1246 MSVCRT_INVALID_PMT("insufficient buffer size", MSVCRT_ERANGE);
1247 return MSVCRT_ERANGE;
1250 *MSVCRT__errno() = MSVCRT_EILSEQ;
1251 return MSVCRT_EILSEQ;
1254 if(len)
1255 *len = mblen;
1256 return 0;
1259 /*********************************************************************
1260 * wctomb_s (MSVCRT.@)
1262 int CDECL MSVCRT_wctomb_s(int *len, char *mbchar, MSVCRT_size_t size, MSVCRT_wchar_t wch)
1264 return MSVCRT__wctomb_s_l(len, mbchar, size, wch, NULL);
1267 /*********************************************************************
1268 * _wctomb_l (MSVCRT.@)
1270 int CDECL MSVCRT__wctomb_l(char *dst, MSVCRT_wchar_t ch, MSVCRT__locale_t locale)
1272 int len;
1274 MSVCRT__wctomb_s_l(&len, dst, dst ? 6 : 0, ch, locale);
1275 return len;
1278 /*********************************************************************
1279 * wctomb (MSVCRT.@)
1281 INT CDECL MSVCRT_wctomb( char *dst, MSVCRT_wchar_t ch )
1283 return MSVCRT__wctomb_l(dst, ch, NULL);
1286 /*********************************************************************
1287 * wctob (MSVCRT.@)
1289 INT CDECL MSVCRT_wctob( MSVCRT_wint_t wchar )
1291 char out;
1292 BOOL error;
1293 UINT codepage = get_locinfo()->lc_codepage;
1295 if(!codepage) {
1296 if (wchar < 0xff)
1297 return (signed char)wchar;
1298 else
1299 return MSVCRT_EOF;
1300 } else if(WideCharToMultiByte( codepage, 0, &wchar, 1, &out, 1, NULL, &error ) && !error)
1301 return (INT)out;
1302 return MSVCRT_EOF;
1305 /*********************************************************************
1306 * wcrtomb (MSVCRT.@)
1308 MSVCRT_size_t CDECL MSVCRT_wcrtomb( char *dst, MSVCRT_wchar_t ch, MSVCRT_mbstate_t *s)
1310 if(s)
1311 *s = 0;
1312 return MSVCRT_wctomb(dst, ch);
1315 /*********************************************************************
1316 * iswalnum (MSVCRT.@)
1318 INT CDECL MSVCRT_iswalnum( MSVCRT_wchar_t wc )
1320 return isalnumW( wc );
1323 /*********************************************************************
1324 * iswalpha (MSVCRT.@)
1326 INT CDECL MSVCRT_iswalpha( MSVCRT_wchar_t wc )
1328 return isalphaW( wc );
1331 /*********************************************************************
1332 * iswalpha_l (MSVCRT.@)
1334 INT CDECL MSVCRT__iswalpha_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1336 return isalphaW( wc );
1339 /*********************************************************************
1340 * iswcntrl (MSVCRT.@)
1342 INT CDECL MSVCRT_iswcntrl( MSVCRT_wchar_t wc )
1344 return iscntrlW( wc );
1347 /*********************************************************************
1348 * iswdigit (MSVCRT.@)
1350 INT CDECL MSVCRT_iswdigit( MSVCRT_wchar_t wc )
1352 return isdigitW( wc );
1355 /*********************************************************************
1356 * _iswdigit_l (MSVCRT.@)
1358 INT CDECL MSVCRT__iswdigit_l( MSVCRT_wchar_t wc, MSVCRT__locale_t locale )
1360 return isdigitW( wc );
1363 /*********************************************************************
1364 * iswgraph (MSVCRT.@)
1366 INT CDECL MSVCRT_iswgraph( MSVCRT_wchar_t wc )
1368 return isgraphW( wc );
1371 /*********************************************************************
1372 * iswlower (MSVCRT.@)
1374 INT CDECL MSVCRT_iswlower( MSVCRT_wchar_t wc )
1376 return islowerW( wc );
1379 /*********************************************************************
1380 * iswprint (MSVCRT.@)
1382 INT CDECL MSVCRT_iswprint( MSVCRT_wchar_t wc )
1384 return isprintW( wc );
1387 /*********************************************************************
1388 * iswpunct (MSVCRT.@)
1390 INT CDECL MSVCRT_iswpunct( MSVCRT_wchar_t wc )
1392 return ispunctW( wc );
1395 /*********************************************************************
1396 * iswspace (MSVCRT.@)
1398 INT CDECL MSVCRT_iswspace( MSVCRT_wchar_t wc )
1400 return isspaceW( wc );
1403 /*********************************************************************
1404 * iswupper (MSVCRT.@)
1406 INT CDECL MSVCRT_iswupper( MSVCRT_wchar_t wc )
1408 return isupperW( wc );
1411 /*********************************************************************
1412 * iswxdigit (MSVCRT.@)
1414 INT CDECL MSVCRT_iswxdigit( MSVCRT_wchar_t wc )
1416 return isxdigitW( wc );
1419 /*********************************************************************
1420 * wcscpy_s (MSVCRT.@)
1422 INT CDECL MSVCRT_wcscpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc)
1424 MSVCRT_size_t size = 0;
1426 if(!MSVCRT_CHECK_PMT(wcDest)) return MSVCRT_EINVAL;
1427 if(!MSVCRT_CHECK_PMT(numElement)) return MSVCRT_EINVAL;
1429 wcDest[0] = 0;
1431 if(!MSVCRT_CHECK_PMT(wcSrc)) return MSVCRT_EINVAL;
1433 size = strlenW(wcSrc) + 1;
1435 if(!MSVCRT_CHECK_PMT_ERR(size <= numElement, MSVCRT_ERANGE))
1436 return MSVCRT_ERANGE;
1438 memcpy( wcDest, wcSrc, size*sizeof(WCHAR) );
1440 return 0;
1443 /******************************************************************
1444 * wcsncpy (MSVCRT.@)
1446 MSVCRT_wchar_t* __cdecl MSVCRT_wcsncpy( MSVCRT_wchar_t* s1,
1447 const MSVCRT_wchar_t *s2, MSVCRT_size_t n )
1449 MSVCRT_size_t i;
1451 for(i=0; i<n; i++)
1452 if(!(s1[i] = s2[i])) break;
1453 for(; i<n; i++)
1454 s1[i] = 0;
1455 return s1;
1458 /******************************************************************
1459 * wcsncpy_s (MSVCRT.@)
1461 INT CDECL MSVCRT_wcsncpy_s( MSVCRT_wchar_t* wcDest, MSVCRT_size_t numElement, const MSVCRT_wchar_t *wcSrc,
1462 MSVCRT_size_t count )
1464 WCHAR *p = wcDest;
1465 BOOL truncate = (count == MSVCRT__TRUNCATE);
1467 if(!wcDest && !numElement && !count)
1468 return 0;
1470 if (!wcDest || !numElement)
1471 return MSVCRT_EINVAL;
1473 if (!wcSrc)
1475 *wcDest = 0;
1476 return count ? MSVCRT_EINVAL : 0;
1479 while (numElement && count && *wcSrc)
1481 *p++ = *wcSrc++;
1482 numElement--;
1483 count--;
1485 if (!numElement && truncate)
1487 *(p-1) = 0;
1488 return MSVCRT_STRUNCATE;
1490 else if (!numElement)
1492 *wcDest = 0;
1493 return MSVCRT_ERANGE;
1496 *p = 0;
1497 return 0;
1500 /******************************************************************
1501 * wcscat_s (MSVCRT.@)
1504 INT CDECL MSVCRT_wcscat_s(MSVCRT_wchar_t* dst, MSVCRT_size_t elem, const MSVCRT_wchar_t* src)
1506 MSVCRT_wchar_t* ptr = dst;
1508 if (!dst || elem == 0) return MSVCRT_EINVAL;
1509 if (!src)
1511 dst[0] = '\0';
1512 return MSVCRT_EINVAL;
1515 /* seek to end of dst string (or elem if no end of string is found */
1516 while (ptr < dst + elem && *ptr != '\0') ptr++;
1517 while (ptr < dst + elem)
1519 if ((*ptr++ = *src++) == '\0') return 0;
1521 /* not enough space */
1522 dst[0] = '\0';
1523 return MSVCRT_ERANGE;
1526 /*********************************************************************
1527 * wcsncat_s (MSVCRT.@)
1530 INT CDECL MSVCRT_wcsncat_s(MSVCRT_wchar_t *dst, MSVCRT_size_t elem,
1531 const MSVCRT_wchar_t *src, MSVCRT_size_t count)
1533 MSVCRT_size_t srclen;
1534 MSVCRT_wchar_t dststart;
1535 INT ret = 0;
1537 if (!MSVCRT_CHECK_PMT(dst != NULL)) return MSVCRT_EINVAL;
1538 if (!MSVCRT_CHECK_PMT(elem > 0)) return MSVCRT_EINVAL;
1539 if (!MSVCRT_CHECK_PMT(src != NULL || count == 0)) return MSVCRT_EINVAL;
1541 if (count == 0)
1542 return 0;
1544 for (dststart = 0; dststart < elem; dststart++)
1546 if (dst[dststart] == '\0')
1547 break;
1549 if (dststart == elem)
1551 MSVCRT_INVALID_PMT("dst[elem] is not NULL terminated\n", MSVCRT_EINVAL);
1552 return MSVCRT_EINVAL;
1555 if (count == MSVCRT__TRUNCATE)
1557 srclen = strlenW(src);
1558 if (srclen >= (elem - dststart))
1560 srclen = elem - dststart - 1;
1561 ret = MSVCRT_STRUNCATE;
1564 else
1565 srclen = min(strlenW(src), count);
1566 if (srclen < (elem - dststart))
1568 memcpy(&dst[dststart], src, srclen*sizeof(MSVCRT_wchar_t));
1569 dst[dststart+srclen] = '\0';
1570 return ret;
1572 MSVCRT_INVALID_PMT("dst[elem] is too small", MSVCRT_ERANGE);
1573 dst[0] = '\0';
1574 return MSVCRT_ERANGE;
1577 /*********************************************************************
1578 * _wcstoi64_l (MSVCRT.@)
1580 * FIXME: locale parameter is ignored
1582 __int64 CDECL MSVCRT__wcstoi64_l(const MSVCRT_wchar_t *nptr,
1583 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1585 BOOL negative = FALSE;
1586 __int64 ret = 0;
1588 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1590 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1591 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1592 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1594 while(isspaceW(*nptr)) nptr++;
1596 if(*nptr == '-') {
1597 negative = TRUE;
1598 nptr++;
1599 } else if(*nptr == '+')
1600 nptr++;
1602 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1603 base = 16;
1604 nptr += 2;
1607 if(base == 0) {
1608 if(*nptr=='0')
1609 base = 8;
1610 else
1611 base = 10;
1614 while(*nptr) {
1615 MSVCRT_wchar_t cur = tolowerW(*nptr);
1616 int v;
1618 if(isdigitW(cur)) {
1619 if(cur >= '0'+base)
1620 break;
1621 v = cur-'0';
1622 } else {
1623 if(cur<'a' || cur>='a'+base-10)
1624 break;
1625 v = cur-'a'+10;
1628 if(negative)
1629 v = -v;
1631 nptr++;
1633 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
1634 ret = MSVCRT_I64_MAX;
1635 *MSVCRT__errno() = MSVCRT_ERANGE;
1636 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
1637 ret = MSVCRT_I64_MIN;
1638 *MSVCRT__errno() = MSVCRT_ERANGE;
1639 } else
1640 ret = ret*base + v;
1643 if(endptr)
1644 *endptr = (MSVCRT_wchar_t*)nptr;
1646 return ret;
1649 /*********************************************************************
1650 * _wcstoi64 (MSVCRT.@)
1652 __int64 CDECL MSVCRT__wcstoi64(const MSVCRT_wchar_t *nptr,
1653 MSVCRT_wchar_t **endptr, int base)
1655 return MSVCRT__wcstoi64_l(nptr, endptr, base, NULL);
1658 /*********************************************************************
1659 * _wcstol_l (MSVCRT.@)
1661 MSVCRT_long CDECL MSVCRT__wcstol_l(const MSVCRT_wchar_t *s,
1662 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
1664 __int64 ret = MSVCRT__wcstoi64_l(s, end, base, locale);
1666 if(ret > MSVCRT_LONG_MAX) {
1667 ret = MSVCRT_LONG_MAX;
1668 *MSVCRT__errno() = MSVCRT_ERANGE;
1669 }else if(ret < MSVCRT_LONG_MIN) {
1670 ret = MSVCRT_LONG_MIN;
1671 *MSVCRT__errno() = MSVCRT_ERANGE;
1673 return ret;
1676 /*********************************************************************
1677 * _wtoi_l (MSVCRT.@)
1679 int __cdecl MSVCRT__wtoi_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
1681 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
1683 if(ret > INT_MAX) {
1684 ret = INT_MAX;
1685 *MSVCRT__errno() = MSVCRT_ERANGE;
1686 } else if(ret < INT_MIN) {
1687 ret = INT_MIN;
1688 *MSVCRT__errno() = MSVCRT_ERANGE;
1690 return ret;
1693 /*********************************************************************
1694 * _wtoi (MSVCRT.@)
1696 int __cdecl MSVCRT__wtoi(const MSVCRT_wchar_t *str)
1698 return MSVCRT__wtoi_l(str, NULL);
1701 /*********************************************************************
1702 * _wtol_l (MSVCRT.@)
1704 MSVCRT_long __cdecl MSVCRT__wtol_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
1706 __int64 ret = MSVCRT__wcstoi64_l(str, NULL, 10, locale);
1708 if(ret > MSVCRT_LONG_MAX) {
1709 ret = MSVCRT_LONG_MAX;
1710 *MSVCRT__errno() = MSVCRT_ERANGE;
1711 } else if(ret < MSVCRT_LONG_MIN) {
1712 ret = MSVCRT_LONG_MIN;
1713 *MSVCRT__errno() = MSVCRT_ERANGE;
1715 return ret;
1718 /*********************************************************************
1719 * _wtol (MSVCRT.@)
1721 MSVCRT_long __cdecl MSVCRT__wtol(const MSVCRT_wchar_t *str)
1723 return MSVCRT__wtol_l(str, NULL);
1726 /*********************************************************************
1727 * _wcstoui64_l (MSVCRT.@)
1729 * FIXME: locale parameter is ignored
1731 unsigned __int64 CDECL MSVCRT__wcstoui64_l(const MSVCRT_wchar_t *nptr,
1732 MSVCRT_wchar_t **endptr, int base, MSVCRT__locale_t locale)
1734 BOOL negative = FALSE;
1735 unsigned __int64 ret = 0;
1737 TRACE("(%s %p %d %p)\n", debugstr_w(nptr), endptr, base, locale);
1739 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
1740 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
1741 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
1743 while(isspaceW(*nptr)) nptr++;
1745 if(*nptr == '-') {
1746 negative = TRUE;
1747 nptr++;
1748 } else if(*nptr == '+')
1749 nptr++;
1751 if((base==0 || base==16) && *nptr=='0' && tolowerW(*(nptr+1))=='x') {
1752 base = 16;
1753 nptr += 2;
1756 if(base == 0) {
1757 if(*nptr=='0')
1758 base = 8;
1759 else
1760 base = 10;
1763 while(*nptr) {
1764 MSVCRT_wchar_t cur = tolowerW(*nptr);
1765 int v;
1767 if(isdigitW(cur)) {
1768 if(cur >= '0'+base)
1769 break;
1770 v = *nptr-'0';
1771 } else {
1772 if(cur<'a' || cur>='a'+base-10)
1773 break;
1774 v = cur-'a'+10;
1777 nptr++;
1779 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
1780 ret = MSVCRT_UI64_MAX;
1781 *MSVCRT__errno() = MSVCRT_ERANGE;
1782 } else
1783 ret = ret*base + v;
1786 if(endptr)
1787 *endptr = (MSVCRT_wchar_t*)nptr;
1789 return negative ? -ret : ret;
1792 /*********************************************************************
1793 * _wcstoui64 (MSVCRT.@)
1795 unsigned __int64 CDECL MSVCRT__wcstoui64(const MSVCRT_wchar_t *nptr,
1796 MSVCRT_wchar_t **endptr, int base)
1798 return MSVCRT__wcstoui64_l(nptr, endptr, base, NULL);
1801 /*********************************************************************
1802 * _wcstoul_l (MSVCRT.@)
1804 MSVCRT_ulong __cdecl MSVCRT__wcstoul_l(const MSVCRT_wchar_t *s,
1805 MSVCRT_wchar_t **end, int base, MSVCRT__locale_t locale)
1807 __int64 ret = MSVCRT__wcstoui64_l(s, end, base, locale);
1809 if(ret > MSVCRT_ULONG_MAX) {
1810 ret = MSVCRT_ULONG_MAX;
1811 *MSVCRT__errno() = MSVCRT_ERANGE;
1813 return ret;
1816 /*********************************************************************
1817 * wcstoul (MSVCRT.@)
1819 MSVCRT_ulong __cdecl MSVCRT_wcstoul(const MSVCRT_wchar_t *s, MSVCRT_wchar_t **end, int base)
1821 return MSVCRT__wcstoul_l(s, end, base, NULL);
1824 /******************************************************************
1825 * wcsnlen (MSVCRT.@)
1827 MSVCRT_size_t CDECL MSVCRT_wcsnlen(const MSVCRT_wchar_t *s, MSVCRT_size_t maxlen)
1829 MSVCRT_size_t i;
1831 for (i = 0; i < maxlen; i++)
1832 if (!s[i]) break;
1833 return i;
1836 /*********************************************************************
1837 * _towupper_l (MSVCRT.@)
1839 int CDECL MSVCRT__towupper_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
1841 return toupperW(c);
1844 /*********************************************************************
1845 * towupper (MSVCRT.@)
1847 int CDECL MSVCRT_towupper(MSVCRT_wint_t c)
1849 return MSVCRT__towupper_l(c, NULL);
1852 /*********************************************************************
1853 * _towlower_l (MSVCRT.@)
1855 int CDECL MSVCRT__towlower_l(MSVCRT_wint_t c, MSVCRT__locale_t locale)
1857 return tolowerW(c);
1860 /*********************************************************************
1861 * towlower (MSVCRT.@)
1863 int CDECL MSVCRT_towlower(MSVCRT_wint_t c)
1865 return MSVCRT__towlower_l(c, NULL);
1868 /*********************************************************************
1869 * wcschr (MSVCRT.@)
1871 MSVCRT_wchar_t* CDECL MSVCRT_wcschr(const MSVCRT_wchar_t *str, MSVCRT_wchar_t ch)
1873 return strchrW(str, ch);
1876 /***********************************************************************
1877 * wcslen (MSVCRT.@)
1879 int CDECL MSVCRT_wcslen(const MSVCRT_wchar_t *str)
1881 return strlenW(str);
1884 /*********************************************************************
1885 * wcsstr (MSVCRT.@)
1887 MSVCRT_wchar_t* CDECL MSVCRT_wcsstr(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *sub)
1889 return strstrW(str, sub);
1892 /*********************************************************************
1893 * _wtoi64_l (MSVCRT.@)
1895 __int64 CDECL _wtoi64_l(const MSVCRT_wchar_t *str, MSVCRT__locale_t locale)
1897 ULONGLONG RunningTotal = 0;
1898 char bMinus = 0;
1900 while (isspaceW(*str)) {
1901 str++;
1902 } /* while */
1904 if (*str == '+') {
1905 str++;
1906 } else if (*str == '-') {
1907 bMinus = 1;
1908 str++;
1909 } /* if */
1911 while (*str >= '0' && *str <= '9') {
1912 RunningTotal = RunningTotal * 10 + *str - '0';
1913 str++;
1914 } /* while */
1916 return bMinus ? -RunningTotal : RunningTotal;
1919 /*********************************************************************
1920 * _wtoi64 (MSVCRT.@)
1922 __int64 CDECL _wtoi64(const MSVCRT_wchar_t *str)
1924 return _wtoi64_l(str, NULL);
1927 /*********************************************************************
1928 * wcsncmp (MSVCRT.@)
1930 int CDECL MSVCRT_wcsncmp(const MSVCRT_wchar_t *str1, const MSVCRT_wchar_t *str2, int n)
1932 return strncmpW(str1, str2, n);