2 * MSVCRT string functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define _ISOC99_SOURCE
26 #include "wine/port.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
39 /*********************************************************************
43 char* CDECL
MSVCRT__strdup(const char* str
)
47 char * ret
= MSVCRT_malloc(strlen(str
)+1);
48 if (ret
) strcpy( ret
, str
);
54 /*********************************************************************
55 * _strlwr_s_l (MSVCRT.@)
57 int CDECL
MSVCRT__strlwr_s_l(char *str
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
63 *MSVCRT__errno() = MSVCRT_EINVAL
;
76 *MSVCRT__errno() = MSVCRT_EINVAL
;
82 *str
= MSVCRT__tolower_l((unsigned char)*str
, locale
);
89 /*********************************************************************
90 * _strlwr_s (MSVCRT.@)
92 int CDECL
MSVCRT__strlwr_s(char *str
, MSVCRT_size_t len
)
94 return MSVCRT__strlwr_s_l(str
, len
, NULL
);
97 /*********************************************************************
98 * _strlwr_l (MSVCRT.@)
100 char* CDECL
_strlwr_l(char *str
, MSVCRT__locale_t locale
)
102 MSVCRT__strlwr_s_l(str
, -1, locale
);
106 /*********************************************************************
109 char* CDECL
MSVCRT__strlwr(char *str
)
111 MSVCRT__strlwr_s_l(str
, -1, NULL
);
115 /*********************************************************************
116 * _strupr_s_l (MSVCRT.@)
118 int CDECL
MSVCRT__strupr_s_l(char *str
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
124 *MSVCRT__errno() = MSVCRT_EINVAL
;
125 return MSVCRT_EINVAL
;
137 *MSVCRT__errno() = MSVCRT_EINVAL
;
138 return MSVCRT_EINVAL
;
143 *str
= MSVCRT__toupper_l((unsigned char)*str
, locale
);
150 /*********************************************************************
151 * _strupr_s (MSVCRT.@)
153 int CDECL
MSVCRT__strupr_s(char *str
, MSVCRT_size_t len
)
155 return MSVCRT__strupr_s_l(str
, len
, NULL
);
158 /*********************************************************************
159 * _strupr_l (MSVCRT.@)
161 char* CDECL
MSVCRT__strupr_l(char *str
, MSVCRT__locale_t locale
)
163 MSVCRT__strupr_s_l(str
, -1, locale
);
167 /*********************************************************************
170 char* CDECL
MSVCRT__strupr(char *str
)
172 MSVCRT__strupr_s_l(str
, -1, NULL
);
176 /*********************************************************************
177 * _strnset (MSVCRT.@)
179 char* CDECL
MSVCRT__strnset(char* str
, int value
, MSVCRT_size_t len
)
182 while (*str
&& len
--)
187 /*********************************************************************
190 char* CDECL
MSVCRT__strrev(char* str
)
196 for (p1
= str
, p2
= str
+ strlen(str
) - 1; p2
> p1
; ++p1
, --p2
)
206 /*********************************************************************
209 char* CDECL
_strset(char* str
, int value
)
218 /*********************************************************************
221 char * CDECL
MSVCRT_strtok( char *str
, const char *delim
)
223 thread_data_t
*data
= msvcrt_get_thread_data();
227 if (!(str
= data
->strtok_next
)) return NULL
;
229 while (*str
&& strchr( delim
, *str
)) str
++;
230 if (!*str
) return NULL
;
232 while (*str
&& !strchr( delim
, *str
)) str
++;
233 if (*str
) *str
++ = 0;
234 data
->strtok_next
= str
;
238 /*********************************************************************
239 * strtok_s (MSVCRT.@)
241 char * CDECL
MSVCRT_strtok_s(char *str
, const char *delim
, char **ctx
)
243 if (!MSVCRT_CHECK_PMT(delim
!= NULL
)) return NULL
;
244 if (!MSVCRT_CHECK_PMT(ctx
!= NULL
)) return NULL
;
245 if (!MSVCRT_CHECK_PMT(str
!= NULL
|| *ctx
!= NULL
)) return NULL
;
250 while(*str
&& strchr(delim
, *str
))
256 while(**ctx
&& !strchr(delim
, **ctx
))
264 /*********************************************************************
267 void CDECL
MSVCRT__swab(char* src
, char* dst
, int len
)
271 len
= (unsigned)len
>> 1;
283 static double strtod_helper(const char *str
, char **end
, MSVCRT__locale_t locale
, int *err
)
285 MSVCRT_pthreadlocinfo locinfo
;
286 unsigned __int64 d
=0, hlp
;
291 long double lret
=1, expcnt
= 10;
292 BOOL found_digit
= FALSE
, negexp
;
297 if(!MSVCRT_CHECK_PMT(str
!= NULL
)) return 0;
300 locinfo
= get_locinfo();
302 locinfo
= locale
->locinfo
;
304 /* FIXME: use *_l functions */
317 hlp
= d
*10+*(p
++)-'0';
318 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
) {
329 if(*p
== *locinfo
->lconv
->decimal_point
)
334 hlp
= d
*10+*(p
++)-'0';
335 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
)
350 if(*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D') {
362 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
368 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
369 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
372 if(*p
=='-' || *p
=='+')
378 fpcontrol
= _control87(0, 0);
379 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
380 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
389 expcnt
= expcnt
*expcnt
;
391 ret
= (long double)sign
* (negexp
? d
/lret
: d
*lret
);
393 _control87(fpcontrol
, 0xffffffff);
395 if((d
&& ret
==0.0) || isinf(ret
)) {
397 *err
= MSVCRT_ERANGE
;
399 *MSVCRT__errno() = MSVCRT_ERANGE
;
408 /*********************************************************************
409 * strtod_l (MSVCRT.@)
411 double CDECL
MSVCRT_strtod_l(const char *str
, char **end
, MSVCRT__locale_t locale
)
413 return strtod_helper(str
, end
, locale
, NULL
);
416 /*********************************************************************
419 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
421 return MSVCRT_strtod_l( str
, end
, NULL
);
424 /*********************************************************************
427 double CDECL
MSVCRT_atof( const char *str
)
429 return MSVCRT_strtod_l(str
, NULL
, NULL
);
432 /*********************************************************************
435 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
437 return MSVCRT_strtod_l(str
, NULL
, locale
);
440 /*********************************************************************
441 * _atoflt_l (MSVCRT.@)
443 int CDECL
MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT
*value
, char *str
, MSVCRT__locale_t locale
)
448 d
= strtod_helper(str
, NULL
, locale
, &err
);
451 return MSVCRT__OVERFLOW
;
452 if((d
!=0 || err
) && value
->f
>-MSVCRT_FLT_MIN
&& value
->f
<MSVCRT_FLT_MIN
)
453 return MSVCRT__UNDERFLOW
;
457 /*********************************************************************
458 * _atoflt (MSVCR100.@)
460 int CDECL
MSVCRT__atoflt(MSVCRT__CRT_FLOAT
*value
, char *str
)
462 return MSVCRT__atoflt_l(value
, str
, NULL
);
465 /*********************************************************************
466 * _atodbl_l (MSVCRT.@)
468 int CDECL
MSVCRT__atodbl_l(MSVCRT__CRT_DOUBLE
*value
, char *str
, MSVCRT__locale_t locale
)
472 value
->x
= strtod_helper(str
, NULL
, locale
, &err
);
474 return MSVCRT__OVERFLOW
;
475 if((value
->x
!=0 || err
) && value
->x
>-MSVCRT_DBL_MIN
&& value
->x
<MSVCRT_DBL_MIN
)
476 return MSVCRT__UNDERFLOW
;
480 /*********************************************************************
483 int CDECL
MSVCRT__atodbl(MSVCRT__CRT_DOUBLE
*value
, char *str
)
485 return MSVCRT__atodbl_l(value
, str
, NULL
);
488 /*********************************************************************
489 * _strcoll_l (MSVCRT.@)
491 int CDECL
MSVCRT_strcoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
493 MSVCRT_pthreadlocinfo locinfo
;
496 locinfo
= get_locinfo();
498 locinfo
= locale
->locinfo
;
500 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
501 return strcmp(str1
, str2
);
502 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, -1, str2
, -1)-CSTR_EQUAL
;
505 /*********************************************************************
508 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
510 return MSVCRT_strcoll_l(str1
, str2
, NULL
);
513 /*********************************************************************
514 * _stricoll_l (MSVCRT.@)
516 int CDECL
MSVCRT__stricoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
518 MSVCRT_pthreadlocinfo locinfo
;
521 locinfo
= get_locinfo();
523 locinfo
= locale
->locinfo
;
525 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
526 return strcasecmp(str1
, str2
);
527 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
528 str1
, -1, str2
, -1)-CSTR_EQUAL
;
531 /*********************************************************************
532 * _stricoll (MSVCRT.@)
534 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
536 return MSVCRT__stricoll_l(str1
, str2
, NULL
);
539 /*********************************************************************
540 * _strncoll_l (MSVCRT.@)
542 int CDECL
MSVCRT__strncoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
544 MSVCRT_pthreadlocinfo locinfo
;
547 locinfo
= get_locinfo();
549 locinfo
= locale
->locinfo
;
551 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
552 return strncmp(str1
, str2
, count
);
553 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, count
, str2
, count
)-CSTR_EQUAL
;
556 /*********************************************************************
557 * _strncoll (MSVCRT.@)
559 int CDECL
MSVCRT__strncoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
561 return MSVCRT__strncoll_l(str1
, str2
, count
, NULL
);
564 /*********************************************************************
565 * _strnicoll_l (MSVCRT.@)
567 int CDECL
MSVCRT__strnicoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
569 MSVCRT_pthreadlocinfo locinfo
;
572 locinfo
= get_locinfo();
574 locinfo
= locale
->locinfo
;
576 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
577 return strncasecmp(str1
, str2
, count
);
578 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
579 str1
, count
, str2
, count
)-CSTR_EQUAL
;
582 /*********************************************************************
583 * _strnicoll (MSVCRT.@)
585 int CDECL
MSVCRT__strnicoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
587 return MSVCRT__strnicoll_l(str1
, str2
, count
, NULL
);
590 /*********************************************************************
593 char* __cdecl
MSVCRT_strncpy(char *dst
, const char *src
, MSVCRT_size_t len
)
598 if((dst
[i
] = src
[i
]) == '\0') break;
600 while (i
< len
) dst
[i
++] = 0;
605 /*********************************************************************
606 * strcpy_s (MSVCRT.@)
608 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
611 if(!elem
) return MSVCRT_EINVAL
;
612 if(!dst
) return MSVCRT_EINVAL
;
616 return MSVCRT_EINVAL
;
619 for(i
= 0; i
< elem
; i
++)
621 if((dst
[i
] = src
[i
]) == '\0') return 0;
624 return MSVCRT_ERANGE
;
627 /*********************************************************************
628 * strcat_s (MSVCRT.@)
630 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
633 if(!dst
) return MSVCRT_EINVAL
;
634 if(elem
== 0) return MSVCRT_EINVAL
;
638 return MSVCRT_EINVAL
;
641 for(i
= 0; i
< elem
; i
++)
645 for(j
= 0; (j
+ i
) < elem
; j
++)
647 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
651 /* Set the first element to 0, not the first element after the skipped part */
653 return MSVCRT_ERANGE
;
656 /*********************************************************************
657 * strncat_s (MSVCRT.@)
659 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
663 if (!MSVCRT_CHECK_PMT(dst
!= 0)) return MSVCRT_EINVAL
;
664 if (!MSVCRT_CHECK_PMT(elem
!= 0)) return MSVCRT_EINVAL
;
665 if (!MSVCRT_CHECK_PMT(src
!= 0))
668 return MSVCRT_EINVAL
;
671 for(i
= 0; i
< elem
; i
++)
675 for(j
= 0; (j
+ i
) < elem
; j
++)
677 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
680 return MSVCRT_STRUNCATE
;
682 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
690 /* Set the first element to 0, not the first element after the skipped part */
692 return MSVCRT_ERANGE
;
695 /*********************************************************************
698 char* __cdecl
MSVCRT_strncat(char *dst
, const char *src
, MSVCRT_size_t len
)
700 return strncat(dst
, src
, len
);
703 /*********************************************************************
704 * _strxfrm_l (MSVCRT.@)
706 MSVCRT_size_t CDECL
MSVCRT__strxfrm_l( char *dest
, const char *src
,
707 MSVCRT_size_t len
, MSVCRT__locale_t locale
)
709 MSVCRT_pthreadlocinfo locinfo
;
712 if(!MSVCRT_CHECK_PMT(src
)) return INT_MAX
;
713 if(!MSVCRT_CHECK_PMT(dest
|| !len
)) return INT_MAX
;
716 FIXME("len > INT_MAX not supported\n");
721 locinfo
= get_locinfo();
723 locinfo
= locale
->locinfo
;
725 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
]) {
726 MSVCRT_strncpy(dest
, src
, len
);
730 ret
= LCMapStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
],
731 LCMAP_SORTKEY
, src
, -1, NULL
, 0);
734 *MSVCRT__errno() = MSVCRT_EILSEQ
;
737 if(!len
) return ret
-1;
741 *MSVCRT__errno() = MSVCRT_ERANGE
;
745 return LCMapStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
],
746 LCMAP_SORTKEY
, src
, -1, dest
, len
) - 1;
749 /*********************************************************************
752 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
754 return MSVCRT__strxfrm_l(dest
, src
, len
, NULL
);
757 /********************************************************************
758 * _atoldbl (MSVCRT.@)
760 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
762 /* FIXME needs error checking for huge/small values */
765 TRACE("str %s value %p\n",str
,value
);
767 memcpy(value
, &ld
, 10);
769 FIXME("stub, str %s value %p\n",str
,value
);
774 /********************************************************************
775 * __STRINGTOLD (MSVCRT.@)
777 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
781 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
783 memcpy(value
, &ld
, 10);
785 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
790 /*********************************************************************
793 MSVCRT_size_t __cdecl
MSVCRT_strlen(const char *str
)
798 /******************************************************************
801 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
805 for(i
=0; i
<maxlen
; i
++)
811 /*********************************************************************
812 * _strtoi64_l (MSVCRT.@)
814 * FIXME: locale parameter is ignored
816 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
818 BOOL negative
= FALSE
;
821 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
823 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
824 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
825 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
827 while(isspace(*nptr
)) nptr
++;
832 } else if(*nptr
== '+')
835 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
848 char cur
= tolower(*nptr
);
856 if(cur
<'a' || cur
>='a'+base
-10)
866 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
867 ret
= MSVCRT_I64_MAX
;
868 *MSVCRT__errno() = MSVCRT_ERANGE
;
869 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
870 ret
= MSVCRT_I64_MIN
;
871 *MSVCRT__errno() = MSVCRT_ERANGE
;
877 *endptr
= (char*)nptr
;
882 /*********************************************************************
883 * _strtoi64 (MSVCRT.@)
885 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
887 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
890 /*********************************************************************
893 int __cdecl
MSVCRT__atoi_l(const char *str
, MSVCRT__locale_t locale
)
895 __int64 ret
= MSVCRT_strtoi64_l(str
, NULL
, 10, locale
);
899 *MSVCRT__errno() = MSVCRT_ERANGE
;
900 } else if(ret
< INT_MIN
) {
902 *MSVCRT__errno() = MSVCRT_ERANGE
;
907 /*********************************************************************
911 int __cdecl
MSVCRT_atoi(const char *str
)
919 while(isspace(*str
)) str
++;
923 }else if(*str
== '-') {
928 while(*str
>='0' && *str
<='9') {
929 ret
= ret
*10+*str
-'0';
933 return minus
? -ret
: ret
;
936 int CDECL
MSVCRT_atoi(const char *str
)
938 return MSVCRT__atoi_l(str
, NULL
);
942 /******************************************************************
945 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
947 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, NULL
);
949 if(ret
> MSVCRT_LONG_MAX
) {
950 ret
= MSVCRT_LONG_MAX
;
951 *MSVCRT__errno() = MSVCRT_ERANGE
;
952 } else if(ret
< MSVCRT_LONG_MIN
) {
953 ret
= MSVCRT_LONG_MIN
;
954 *MSVCRT__errno() = MSVCRT_ERANGE
;
960 /******************************************************************
963 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
965 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, NULL
);
967 if(ret
> MSVCRT_ULONG_MAX
) {
968 ret
= MSVCRT_ULONG_MAX
;
969 *MSVCRT__errno() = MSVCRT_ERANGE
;
970 }else if(ret
< -(__int64
)MSVCRT_ULONG_MAX
) {
972 *MSVCRT__errno() = MSVCRT_ERANGE
;
978 /*********************************************************************
979 * _strtoui64_l (MSVCRT.@)
981 * FIXME: locale parameter is ignored
983 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
985 BOOL negative
= FALSE
;
986 unsigned __int64 ret
= 0;
988 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
990 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
991 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
992 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
994 while(isspace(*nptr
)) nptr
++;
999 } else if(*nptr
== '+')
1002 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
1015 char cur
= tolower(*nptr
);
1023 if(cur
<'a' || cur
>='a'+base
-10)
1030 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
1031 ret
= MSVCRT_UI64_MAX
;
1032 *MSVCRT__errno() = MSVCRT_ERANGE
;
1038 *endptr
= (char*)nptr
;
1040 return negative
? -ret
: ret
;
1043 /*********************************************************************
1044 * _strtoui64 (MSVCRT.@)
1046 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
1048 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
1051 static int ltoa_helper(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1056 char buffer
[33], *pos
;
1059 if (value
< 0 && radix
== 10)
1066 is_negative
= FALSE
;
1075 digit
= val
% radix
;
1079 *--pos
= '0' + digit
;
1081 *--pos
= 'a' + digit
- 10;
1088 len
= buffer
+ 33 - pos
;
1094 /* Copy the temporary buffer backwards up to the available number of
1095 * characters. Don't copy the negative sign if present. */
1103 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1107 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1108 return MSVCRT_ERANGE
;
1111 memcpy(str
, pos
, len
);
1115 /*********************************************************************
1116 * _ltoa_s (MSVCRT.@)
1118 int CDECL
MSVCRT__ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1120 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1121 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1122 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1125 return MSVCRT_EINVAL
;
1128 return ltoa_helper(value
, str
, size
, radix
);
1131 /*********************************************************************
1132 * _ltow_s (MSVCRT.@)
1134 int CDECL
MSVCRT__ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1139 MSVCRT_wchar_t buffer
[33], *pos
;
1142 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1143 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1144 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1147 return MSVCRT_EINVAL
;
1150 if (value
< 0 && radix
== 10)
1157 is_negative
= FALSE
;
1166 digit
= val
% radix
;
1170 *--pos
= '0' + digit
;
1172 *--pos
= 'a' + digit
- 10;
1179 len
= buffer
+ 33 - pos
;
1183 MSVCRT_wchar_t
*p
= str
;
1185 /* Copy the temporary buffer backwards up to the available number of
1186 * characters. Don't copy the negative sign if present. */
1194 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1198 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1199 return MSVCRT_ERANGE
;
1202 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1206 /*********************************************************************
1207 * _itoa_s (MSVCRT.@)
1209 int CDECL
MSVCRT__itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
1211 return MSVCRT__ltoa_s(value
, str
, size
, radix
);
1214 /*********************************************************************
1217 char* CDECL
MSVCRT__itoa(int value
, char *str
, int radix
)
1219 return ltoa_helper(value
, str
, MSVCRT_SIZE_MAX
, radix
) ? NULL
: str
;
1222 /*********************************************************************
1223 * _itow_s (MSVCRT.@)
1225 int CDECL
MSVCRT__itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1227 return MSVCRT__ltow_s(value
, str
, size
, radix
);
1230 /*********************************************************************
1231 * _ui64toa_s (MSVCRT.@)
1233 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
1234 MSVCRT_size_t size
, int radix
)
1236 char buffer
[65], *pos
;
1239 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1240 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1241 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1244 return MSVCRT_EINVAL
;
1251 digit
= value
%radix
;
1257 *--pos
= 'a'+digit
-10;
1260 if(buffer
-pos
+65 > size
) {
1261 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1262 return MSVCRT_EINVAL
;
1265 memcpy(str
, pos
, buffer
-pos
+65);
1269 /*********************************************************************
1270 * _ui64tow_s (MSVCRT.@)
1272 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1273 MSVCRT_size_t size
, int radix
)
1275 MSVCRT_wchar_t buffer
[65], *pos
;
1278 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1279 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1280 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1283 return MSVCRT_EINVAL
;
1290 digit
= value
% radix
;
1291 value
= value
/ radix
;
1293 *--pos
= '0' + digit
;
1295 *--pos
= 'a' + digit
- 10;
1296 } while (value
!= 0);
1298 if(buffer
-pos
+65 > size
) {
1299 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1300 return MSVCRT_EINVAL
;
1303 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(MSVCRT_wchar_t
));
1307 /*********************************************************************
1308 * _ultoa_s (MSVCRT.@)
1310 int CDECL
MSVCRT__ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1313 char buffer
[33], *pos
;
1316 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1321 *MSVCRT__errno() = MSVCRT_EINVAL
;
1322 return MSVCRT_EINVAL
;
1330 digit
= value
% radix
;
1334 *--pos
= '0' + digit
;
1336 *--pos
= 'a' + digit
- 10;
1340 len
= buffer
+ 33 - pos
;
1346 /* Copy the temporary buffer backwards up to the available number of
1349 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1353 *MSVCRT__errno() = MSVCRT_ERANGE
;
1354 return MSVCRT_ERANGE
;
1357 memcpy(str
, pos
, len
);
1361 /*********************************************************************
1362 * _ultow_s (MSVCRT.@)
1364 int CDECL
MSVCRT__ultow_s(MSVCRT_ulong value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1367 WCHAR buffer
[33], *pos
;
1370 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1375 *MSVCRT__errno() = MSVCRT_EINVAL
;
1376 return MSVCRT_EINVAL
;
1384 digit
= value
% radix
;
1388 *--pos
= '0' + digit
;
1390 *--pos
= 'a' + digit
- 10;
1394 len
= buffer
+ 33 - pos
;
1400 /* Copy the temporary buffer backwards up to the available number of
1403 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1407 *MSVCRT__errno() = MSVCRT_ERANGE
;
1408 return MSVCRT_ERANGE
;
1411 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1415 /*********************************************************************
1416 * _i64toa_s (MSVCRT.@)
1418 int CDECL
MSVCRT__i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1420 unsigned __int64 val
;
1423 char buffer
[65], *pos
;
1426 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1427 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1428 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1431 return MSVCRT_EINVAL
;
1434 if (value
< 0 && radix
== 10)
1441 is_negative
= FALSE
;
1450 digit
= val
% radix
;
1454 *--pos
= '0' + digit
;
1456 *--pos
= 'a' + digit
- 10;
1463 len
= buffer
+ 65 - pos
;
1469 /* Copy the temporary buffer backwards up to the available number of
1470 * characters. Don't copy the negative sign if present. */
1478 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1482 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1483 return MSVCRT_ERANGE
;
1486 memcpy(str
, pos
, len
);
1490 /*********************************************************************
1491 * _i64tow_s (MSVCRT.@)
1493 int CDECL
MSVCRT__i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1495 unsigned __int64 val
;
1498 MSVCRT_wchar_t buffer
[65], *pos
;
1501 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1502 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1503 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1506 return MSVCRT_EINVAL
;
1509 if (value
< 0 && radix
== 10)
1516 is_negative
= FALSE
;
1525 digit
= val
% radix
;
1529 *--pos
= '0' + digit
;
1531 *--pos
= 'a' + digit
- 10;
1538 len
= buffer
+ 65 - pos
;
1542 MSVCRT_wchar_t
*p
= str
;
1544 /* Copy the temporary buffer backwards up to the available number of
1545 * characters. Don't copy the negative sign if present. */
1553 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1557 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1558 return MSVCRT_ERANGE
;
1561 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1565 #define I10_OUTPUT_MAX_PREC 21
1566 /* Internal structure used by $I10_OUTPUT */
1567 struct _I10_OUTPUT_DATA
{
1571 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1574 /*********************************************************************
1575 * $I10_OUTPUT (MSVCRT.@)
1576 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1577 * prec - precision of part, we're interested in
1578 * flag - 0 for first prec digits, 1 for fractional part
1579 * data - data to be populated
1582 * 0 if given double is NaN or INF
1586 * Native sets last byte of data->str to '0' or '9', I don't know what
1587 * it means. Current implementation sets it always to '0'.
1589 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1591 static const char inf_str
[] = "1#INF";
1592 static const char nan_str
[] = "1#QNAN";
1594 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1595 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1596 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1600 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1603 memcpy(&ld
, &ld80
, 10);
1605 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1616 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1624 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1630 int exp
= 1+floor(log10(d
));
1638 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1639 prec
= I10_OUTPUT_MAX_PREC
-1;
1645 sprintf(format
, "%%.%dle", prec
);
1646 sprintf(buf
, format
, d
);
1649 data
->pos
= atoi(buf
+prec
+3);
1653 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1656 memcpy(data
->str
, buf
+1, data
->len
);
1657 data
->str
[data
->len
] = '\0';
1659 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1660 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1664 #undef I10_OUTPUT_MAX_PREC
1666 /*********************************************************************
1669 int __cdecl
MSVCRT_memcmp(const void *ptr1
, const void *ptr2
, MSVCRT_size_t n
)
1671 return memcmp(ptr1
, ptr2
, n
);
1674 /*********************************************************************
1677 void * __cdecl
MSVCRT_memcpy(void *dst
, const void *src
, MSVCRT_size_t n
)
1679 return memmove(dst
, src
, n
);
1682 /*********************************************************************
1683 * memmove (MSVCRT.@)
1685 void * __cdecl
MSVCRT_memmove(void *dst
, const void *src
, MSVCRT_size_t n
)
1687 return memmove(dst
, src
, n
);
1690 /*********************************************************************
1693 void* __cdecl
MSVCRT_memset(void *dst
, int c
, MSVCRT_size_t n
)
1695 return memset(dst
, c
, n
);
1698 /*********************************************************************
1701 char* __cdecl
MSVCRT_strchr(const char *str
, int c
)
1703 return strchr(str
, c
);
1706 /*********************************************************************
1707 * strrchr (MSVCRT.@)
1709 char* __cdecl
MSVCRT_strrchr(const char *str
, int c
)
1711 return strrchr(str
, c
);
1714 /*********************************************************************
1717 void* __cdecl
MSVCRT_memchr(const void *ptr
, int c
, MSVCRT_size_t n
)
1719 return memchr(ptr
, c
, n
);
1722 /*********************************************************************
1725 int __cdecl
MSVCRT_strcmp(const char *str1
, const char *str2
)
1727 return strcmp(str1
, str2
);
1730 /*********************************************************************
1731 * strncmp (MSVCRT.@)
1733 int __cdecl
MSVCRT_strncmp(const char *str1
, const char *str2
, MSVCRT_size_t len
)
1735 return strncmp(str1
, str2
, len
);
1738 /*********************************************************************
1739 * _strnicmp_l (MSVCRT.@)
1741 int __cdecl
MSVCRT__strnicmp_l(const char *s1
, const char *s2
,
1742 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1744 MSVCRT_pthreadlocinfo locinfo
;
1747 if(s1
==NULL
|| s2
==NULL
)
1748 return MSVCRT__NLSCMPERROR
;
1754 locinfo
= get_locinfo();
1756 locinfo
= locale
->locinfo
;
1758 if(!locinfo
->lc_handle
[MSVCRT_LC_CTYPE
])
1759 return strncasecmp(s1
, s2
, count
);
1762 c1
= MSVCRT__tolower_l(*s1
++, locale
);
1763 c2
= MSVCRT__tolower_l(*s2
++, locale
);
1764 }while(--count
&& c1
&& c1
==c2
);
1769 /*********************************************************************
1770 * _stricmp_l (MSVCRT.@)
1772 int __cdecl
MSVCRT__stricmp_l(const char *s1
, const char *s2
, MSVCRT__locale_t locale
)
1774 return MSVCRT__strnicmp_l(s1
, s2
, -1, locale
);
1777 /*********************************************************************
1778 * _strnicmp (MSVCRT.@)
1780 int __cdecl
MSVCRT__strnicmp(const char *s1
, const char *s2
, MSVCRT_size_t count
)
1782 return MSVCRT__strnicmp_l(s1
, s2
, count
, NULL
);
1785 /*********************************************************************
1786 * _stricmp (MSVCRT.@)
1788 int __cdecl
MSVCRT__stricmp(const char *s1
, const char *s2
)
1790 return MSVCRT__strnicmp_l(s1
, s2
, -1, NULL
);
1793 /*********************************************************************
1796 char* __cdecl
MSVCRT_strstr(const char *haystack
, const char *needle
)
1798 return strstr(haystack
, needle
);