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_s (MSVCRT.@)
179 int CDECL
MSVCRT__strnset_s(char *str
, MSVCRT_size_t size
, int c
, MSVCRT_size_t count
)
183 if(!str
&& !size
&& !count
) return 0;
184 if(!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
185 if(!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
187 for(i
=0; i
<size
-1 && i
<count
; i
++) {
188 if(!str
[i
]) return 0;
192 if(!str
[i
]) return 0;
195 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
196 *MSVCRT__errno() = MSVCRT_EINVAL
;
197 return MSVCRT_EINVAL
;
200 /*********************************************************************
201 * _strnset (MSVCRT.@)
203 char* CDECL
MSVCRT__strnset(char* str
, int value
, MSVCRT_size_t len
)
206 while (*str
&& len
--)
211 /*********************************************************************
214 char* CDECL
MSVCRT__strrev(char* str
)
220 for (p1
= str
, p2
= str
+ strlen(str
) - 1; p2
> p1
; ++p1
, --p2
)
230 /*********************************************************************
233 char* CDECL
_strset(char* str
, int value
)
242 /*********************************************************************
245 char * CDECL
MSVCRT_strtok( char *str
, const char *delim
)
247 thread_data_t
*data
= msvcrt_get_thread_data();
251 if (!(str
= data
->strtok_next
)) return NULL
;
253 while (*str
&& strchr( delim
, *str
)) str
++;
254 if (!*str
) return NULL
;
256 while (*str
&& !strchr( delim
, *str
)) str
++;
257 if (*str
) *str
++ = 0;
258 data
->strtok_next
= str
;
262 /*********************************************************************
263 * strtok_s (MSVCRT.@)
265 char * CDECL
MSVCRT_strtok_s(char *str
, const char *delim
, char **ctx
)
267 if (!MSVCRT_CHECK_PMT(delim
!= NULL
)) return NULL
;
268 if (!MSVCRT_CHECK_PMT(ctx
!= NULL
)) return NULL
;
269 if (!MSVCRT_CHECK_PMT(str
!= NULL
|| *ctx
!= NULL
)) return NULL
;
274 while(*str
&& strchr(delim
, *str
))
280 while(**ctx
&& !strchr(delim
, **ctx
))
288 /*********************************************************************
291 void CDECL
MSVCRT__swab(char* src
, char* dst
, int len
)
295 len
= (unsigned)len
>> 1;
307 static double strtod_helper(const char *str
, char **end
, MSVCRT__locale_t locale
, int *err
)
309 MSVCRT_pthreadlocinfo locinfo
;
310 unsigned __int64 d
=0, hlp
;
315 long double lret
=1, expcnt
= 10;
316 BOOL found_digit
= FALSE
, negexp
;
321 else if(!MSVCRT_CHECK_PMT(str
!= NULL
)) {
328 locinfo
= get_locinfo();
330 locinfo
= locale
->locinfo
;
332 /* FIXME: use *_l functions */
343 #if _MSVCR_VER >= 140
344 if(tolower(p
[0]) == 'i' && tolower(p
[1]) == 'n' && tolower(p
[2]) == 'f') {
346 *end
= (char*) &p
[3];
347 if(tolower(p
[3]) == 'i' && tolower(p
[4]) == 'n' && tolower(p
[5]) == 'i' &&
348 tolower(p
[6]) == 't' && tolower(p
[7]) == 'y' && end
)
349 *end
= (char*) &p
[8];
350 return sign
*INFINITY
;
352 if(tolower(p
[0]) == 'n' &&
353 tolower(p
[1]) == 'a' &&
354 tolower(p
[2]) == 'n') {
356 *end
= (char*) &p
[3];
360 if(p
[0] == '0' && tolower(p
[1]) == 'x') {
368 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F')))) {
374 else if (c
>= 'a' && c
<= 'f')
379 if(d
>MSVCRT_UI64_MAX
/base
|| hlp
<d
) {
386 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F')))) {
391 if(*p
== *locinfo
->lconv
->decimal_point
)
395 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F')))) {
401 else if (c
>= 'a' && c
<= 'f')
406 if(d
>MSVCRT_UI64_MAX
/base
|| hlp
<d
)
412 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F'))))
424 if((base
== 10 && (*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D')) ||
425 (base
== 16 && (*p
=='p' || *p
=='P'))) {
437 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
443 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
444 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
447 if(*p
=='-' || *p
=='+')
453 fpcontrol
= _control87(0, 0);
454 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
455 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
464 expcnt
= expcnt
*expcnt
;
466 ret
= (long double)sign
* (negexp
? d
/lret
: d
*lret
);
468 _control87(fpcontrol
, 0xffffffff);
470 if((d
&& ret
==0.0) || isinf(ret
)) {
472 *err
= MSVCRT_ERANGE
;
474 *MSVCRT__errno() = MSVCRT_ERANGE
;
483 /*********************************************************************
484 * strtod_l (MSVCRT.@)
486 double CDECL
MSVCRT_strtod_l(const char *str
, char **end
, MSVCRT__locale_t locale
)
488 return strtod_helper(str
, end
, locale
, NULL
);
491 /*********************************************************************
494 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
496 return MSVCRT_strtod_l( str
, end
, NULL
);
499 /*********************************************************************
502 double CDECL
MSVCRT_atof( const char *str
)
504 return MSVCRT_strtod_l(str
, NULL
, NULL
);
507 /*********************************************************************
510 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
512 return MSVCRT_strtod_l(str
, NULL
, locale
);
515 /*********************************************************************
516 * _atoflt_l (MSVCRT.@)
518 int CDECL
MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT
*value
, char *str
, MSVCRT__locale_t locale
)
523 d
= strtod_helper(str
, NULL
, locale
, &err
);
526 return MSVCRT__OVERFLOW
;
527 if((d
!=0 || err
) && value
->f
>-MSVCRT_FLT_MIN
&& value
->f
<MSVCRT_FLT_MIN
)
528 return MSVCRT__UNDERFLOW
;
532 /*********************************************************************
533 * _atoflt (MSVCR100.@)
535 int CDECL
MSVCRT__atoflt(MSVCRT__CRT_FLOAT
*value
, char *str
)
537 return MSVCRT__atoflt_l(value
, str
, NULL
);
540 /*********************************************************************
541 * _atodbl_l (MSVCRT.@)
543 int CDECL
MSVCRT__atodbl_l(MSVCRT__CRT_DOUBLE
*value
, char *str
, MSVCRT__locale_t locale
)
547 value
->x
= strtod_helper(str
, NULL
, locale
, &err
);
549 return MSVCRT__OVERFLOW
;
550 if((value
->x
!=0 || err
) && value
->x
>-MSVCRT_DBL_MIN
&& value
->x
<MSVCRT_DBL_MIN
)
551 return MSVCRT__UNDERFLOW
;
555 /*********************************************************************
558 int CDECL
MSVCRT__atodbl(MSVCRT__CRT_DOUBLE
*value
, char *str
)
560 return MSVCRT__atodbl_l(value
, str
, NULL
);
563 /*********************************************************************
564 * _strcoll_l (MSVCRT.@)
566 int CDECL
MSVCRT_strcoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
568 MSVCRT_pthreadlocinfo locinfo
;
571 locinfo
= get_locinfo();
573 locinfo
= locale
->locinfo
;
575 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
576 return strcmp(str1
, str2
);
577 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, -1, str2
, -1)-CSTR_EQUAL
;
580 /*********************************************************************
583 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
585 return MSVCRT_strcoll_l(str1
, str2
, NULL
);
588 /*********************************************************************
589 * _stricoll_l (MSVCRT.@)
591 int CDECL
MSVCRT__stricoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
593 MSVCRT_pthreadlocinfo locinfo
;
596 locinfo
= get_locinfo();
598 locinfo
= locale
->locinfo
;
600 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
601 return strcasecmp(str1
, str2
);
602 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
603 str1
, -1, str2
, -1)-CSTR_EQUAL
;
606 /*********************************************************************
607 * _stricoll (MSVCRT.@)
609 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
611 return MSVCRT__stricoll_l(str1
, str2
, NULL
);
614 /*********************************************************************
615 * _strncoll_l (MSVCRT.@)
617 int CDECL
MSVCRT__strncoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
619 MSVCRT_pthreadlocinfo locinfo
;
622 locinfo
= get_locinfo();
624 locinfo
= locale
->locinfo
;
626 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
627 return strncmp(str1
, str2
, count
);
628 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, count
, str2
, count
)-CSTR_EQUAL
;
631 /*********************************************************************
632 * _strncoll (MSVCRT.@)
634 int CDECL
MSVCRT__strncoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
636 return MSVCRT__strncoll_l(str1
, str2
, count
, NULL
);
639 /*********************************************************************
640 * _strnicoll_l (MSVCRT.@)
642 int CDECL
MSVCRT__strnicoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
644 MSVCRT_pthreadlocinfo locinfo
;
647 locinfo
= get_locinfo();
649 locinfo
= locale
->locinfo
;
651 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
652 return strncasecmp(str1
, str2
, count
);
653 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
654 str1
, count
, str2
, count
)-CSTR_EQUAL
;
657 /*********************************************************************
658 * _strnicoll (MSVCRT.@)
660 int CDECL
MSVCRT__strnicoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
662 return MSVCRT__strnicoll_l(str1
, str2
, count
, NULL
);
665 /*********************************************************************
668 char* __cdecl
MSVCRT_strncpy(char *dst
, const char *src
, MSVCRT_size_t len
)
673 if((dst
[i
] = src
[i
]) == '\0') break;
675 while (i
< len
) dst
[i
++] = 0;
680 /*********************************************************************
681 * strcpy_s (MSVCRT.@)
683 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
686 if(!elem
) return MSVCRT_EINVAL
;
687 if(!dst
) return MSVCRT_EINVAL
;
691 return MSVCRT_EINVAL
;
694 for(i
= 0; i
< elem
; i
++)
696 if((dst
[i
] = src
[i
]) == '\0') return 0;
699 return MSVCRT_ERANGE
;
702 /*********************************************************************
703 * strcat_s (MSVCRT.@)
705 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
708 if(!dst
) return MSVCRT_EINVAL
;
709 if(elem
== 0) return MSVCRT_EINVAL
;
713 return MSVCRT_EINVAL
;
716 for(i
= 0; i
< elem
; i
++)
720 for(j
= 0; (j
+ i
) < elem
; j
++)
722 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
726 /* Set the first element to 0, not the first element after the skipped part */
728 return MSVCRT_ERANGE
;
731 /*********************************************************************
732 * strncat_s (MSVCRT.@)
734 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
738 if (!MSVCRT_CHECK_PMT(dst
!= 0)) return MSVCRT_EINVAL
;
739 if (!MSVCRT_CHECK_PMT(elem
!= 0)) return MSVCRT_EINVAL
;
740 if (!MSVCRT_CHECK_PMT(src
!= 0))
743 return MSVCRT_EINVAL
;
746 for(i
= 0; i
< elem
; i
++)
750 for(j
= 0; (j
+ i
) < elem
; j
++)
752 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
755 return MSVCRT_STRUNCATE
;
757 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
765 /* Set the first element to 0, not the first element after the skipped part */
767 return MSVCRT_ERANGE
;
770 /*********************************************************************
773 char* __cdecl
MSVCRT_strncat(char *dst
, const char *src
, MSVCRT_size_t len
)
775 return strncat(dst
, src
, len
);
778 /*********************************************************************
779 * _strxfrm_l (MSVCRT.@)
781 MSVCRT_size_t CDECL
MSVCRT__strxfrm_l( char *dest
, const char *src
,
782 MSVCRT_size_t len
, MSVCRT__locale_t locale
)
784 MSVCRT_pthreadlocinfo locinfo
;
787 if(!MSVCRT_CHECK_PMT(src
)) return INT_MAX
;
788 if(!MSVCRT_CHECK_PMT(dest
|| !len
)) return INT_MAX
;
791 FIXME("len > INT_MAX not supported\n");
796 locinfo
= get_locinfo();
798 locinfo
= locale
->locinfo
;
800 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
]) {
801 MSVCRT_strncpy(dest
, src
, len
);
805 ret
= LCMapStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
],
806 LCMAP_SORTKEY
, src
, -1, NULL
, 0);
809 *MSVCRT__errno() = MSVCRT_EILSEQ
;
812 if(!len
) return ret
-1;
816 *MSVCRT__errno() = MSVCRT_ERANGE
;
820 return LCMapStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
],
821 LCMAP_SORTKEY
, src
, -1, dest
, len
) - 1;
824 /*********************************************************************
827 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
829 return MSVCRT__strxfrm_l(dest
, src
, len
, NULL
);
832 /********************************************************************
833 * _atoldbl (MSVCRT.@)
835 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
837 /* FIXME needs error checking for huge/small values */
840 TRACE("str %s value %p\n",str
,value
);
842 memcpy(value
, &ld
, 10);
844 FIXME("stub, str %s value %p\n",str
,value
);
849 /********************************************************************
850 * __STRINGTOLD (MSVCRT.@)
852 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
856 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
858 memcpy(value
, &ld
, 10);
860 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
865 /*********************************************************************
868 MSVCRT_size_t __cdecl
MSVCRT_strlen(const char *str
)
873 /******************************************************************
876 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
880 for(i
=0; i
<maxlen
; i
++)
886 /*********************************************************************
887 * _strtoi64_l (MSVCRT.@)
889 * FIXME: locale parameter is ignored
891 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
893 const char *p
= nptr
;
894 BOOL negative
= FALSE
;
895 BOOL got_digit
= FALSE
;
898 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
900 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
901 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
902 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
904 while(isspace(*nptr
)) nptr
++;
909 } else if(*nptr
== '+')
912 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
925 char cur
= tolower(*nptr
);
933 if(cur
<'a' || cur
>='a'+base
-10)
944 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
945 ret
= MSVCRT_I64_MAX
;
946 *MSVCRT__errno() = MSVCRT_ERANGE
;
947 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
948 ret
= MSVCRT_I64_MIN
;
949 *MSVCRT__errno() = MSVCRT_ERANGE
;
955 *endptr
= (char*)(got_digit
? nptr
: p
);
960 /*********************************************************************
961 * _strtoi64 (MSVCRT.@)
963 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
965 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
968 /*********************************************************************
971 int __cdecl
MSVCRT__atoi_l(const char *str
, MSVCRT__locale_t locale
)
973 __int64 ret
= MSVCRT_strtoi64_l(str
, NULL
, 10, locale
);
977 *MSVCRT__errno() = MSVCRT_ERANGE
;
978 } else if(ret
< INT_MIN
) {
980 *MSVCRT__errno() = MSVCRT_ERANGE
;
985 /*********************************************************************
989 int __cdecl
MSVCRT_atoi(const char *str
)
997 while(isspace(*str
)) str
++;
1001 }else if(*str
== '-') {
1006 while(*str
>='0' && *str
<='9') {
1007 ret
= ret
*10+*str
-'0';
1011 return minus
? -ret
: ret
;
1014 int CDECL
MSVCRT_atoi(const char *str
)
1016 return MSVCRT__atoi_l(str
, NULL
);
1020 /******************************************************************
1023 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
1025 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, NULL
);
1027 if(ret
> MSVCRT_LONG_MAX
) {
1028 ret
= MSVCRT_LONG_MAX
;
1029 *MSVCRT__errno() = MSVCRT_ERANGE
;
1030 } else if(ret
< MSVCRT_LONG_MIN
) {
1031 ret
= MSVCRT_LONG_MIN
;
1032 *MSVCRT__errno() = MSVCRT_ERANGE
;
1038 /******************************************************************
1039 * _strtoul_l (MSVCRT.@)
1041 MSVCRT_ulong CDECL
MSVCRT_strtoul_l(const char* nptr
, char** end
, int base
, MSVCRT__locale_t locale
)
1043 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, locale
);
1045 if(ret
> MSVCRT_ULONG_MAX
) {
1046 ret
= MSVCRT_ULONG_MAX
;
1047 *MSVCRT__errno() = MSVCRT_ERANGE
;
1048 }else if(ret
< -(__int64
)MSVCRT_ULONG_MAX
) {
1050 *MSVCRT__errno() = MSVCRT_ERANGE
;
1056 /******************************************************************
1057 * strtoul (MSVCRT.@)
1059 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
1061 return MSVCRT_strtoul_l(nptr
, end
, base
, NULL
);
1064 /*********************************************************************
1065 * _strtoui64_l (MSVCRT.@)
1067 * FIXME: locale parameter is ignored
1069 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
1071 const char *p
= nptr
;
1072 BOOL negative
= FALSE
;
1073 BOOL got_digit
= FALSE
;
1074 unsigned __int64 ret
= 0;
1076 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
1078 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
1079 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
1080 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
1082 while(isspace(*nptr
)) nptr
++;
1087 } else if(*nptr
== '+')
1090 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
1103 char cur
= tolower(*nptr
);
1111 if(cur
<'a' || cur
>='a'+base
-10)
1119 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
1120 ret
= MSVCRT_UI64_MAX
;
1121 *MSVCRT__errno() = MSVCRT_ERANGE
;
1127 *endptr
= (char*)(got_digit
? nptr
: p
);
1129 return negative
? -ret
: ret
;
1132 /*********************************************************************
1133 * _strtoui64 (MSVCRT.@)
1135 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
1137 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
1140 static int ltoa_helper(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1145 char buffer
[33], *pos
;
1148 if (value
< 0 && radix
== 10)
1155 is_negative
= FALSE
;
1164 digit
= val
% radix
;
1168 *--pos
= '0' + digit
;
1170 *--pos
= 'a' + digit
- 10;
1177 len
= buffer
+ 33 - pos
;
1183 /* Copy the temporary buffer backwards up to the available number of
1184 * characters. Don't copy the negative sign if present. */
1192 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1196 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1197 return MSVCRT_ERANGE
;
1200 memcpy(str
, pos
, len
);
1204 /*********************************************************************
1205 * _ltoa_s (MSVCRT.@)
1207 int CDECL
MSVCRT__ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1209 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1210 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1211 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1214 return MSVCRT_EINVAL
;
1217 return ltoa_helper(value
, str
, size
, radix
);
1220 /*********************************************************************
1221 * _ltow_s (MSVCRT.@)
1223 int CDECL
MSVCRT__ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1228 MSVCRT_wchar_t buffer
[33], *pos
;
1231 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1232 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1233 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1236 return MSVCRT_EINVAL
;
1239 if (value
< 0 && radix
== 10)
1246 is_negative
= FALSE
;
1255 digit
= val
% radix
;
1259 *--pos
= '0' + digit
;
1261 *--pos
= 'a' + digit
- 10;
1268 len
= buffer
+ 33 - pos
;
1272 MSVCRT_wchar_t
*p
= str
;
1274 /* Copy the temporary buffer backwards up to the available number of
1275 * characters. Don't copy the negative sign if present. */
1283 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1287 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1288 return MSVCRT_ERANGE
;
1291 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1295 /*********************************************************************
1296 * _itoa_s (MSVCRT.@)
1298 int CDECL
MSVCRT__itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
1300 return MSVCRT__ltoa_s(value
, str
, size
, radix
);
1303 /*********************************************************************
1306 char* CDECL
MSVCRT__itoa(int value
, char *str
, int radix
)
1308 return ltoa_helper(value
, str
, MSVCRT_SIZE_MAX
, radix
) ? NULL
: str
;
1311 /*********************************************************************
1312 * _itow_s (MSVCRT.@)
1314 int CDECL
MSVCRT__itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1316 return MSVCRT__ltow_s(value
, str
, size
, radix
);
1319 /*********************************************************************
1320 * _ui64toa_s (MSVCRT.@)
1322 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
1323 MSVCRT_size_t size
, int radix
)
1325 char buffer
[65], *pos
;
1328 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1329 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1330 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1333 return MSVCRT_EINVAL
;
1340 digit
= value
%radix
;
1346 *--pos
= 'a'+digit
-10;
1349 if(buffer
-pos
+65 > size
) {
1350 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1351 return MSVCRT_EINVAL
;
1354 memcpy(str
, pos
, buffer
-pos
+65);
1358 /*********************************************************************
1359 * _ui64tow_s (MSVCRT.@)
1361 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1362 MSVCRT_size_t size
, int radix
)
1364 MSVCRT_wchar_t buffer
[65], *pos
;
1367 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1368 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1369 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1372 return MSVCRT_EINVAL
;
1379 digit
= value
% radix
;
1380 value
= value
/ radix
;
1382 *--pos
= '0' + digit
;
1384 *--pos
= 'a' + digit
- 10;
1385 } while (value
!= 0);
1387 if(buffer
-pos
+65 > size
) {
1388 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1389 return MSVCRT_EINVAL
;
1392 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(MSVCRT_wchar_t
));
1396 /*********************************************************************
1397 * _ultoa_s (MSVCRT.@)
1399 int CDECL
MSVCRT__ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1402 char buffer
[33], *pos
;
1405 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1410 *MSVCRT__errno() = MSVCRT_EINVAL
;
1411 return MSVCRT_EINVAL
;
1419 digit
= value
% radix
;
1423 *--pos
= '0' + digit
;
1425 *--pos
= 'a' + digit
- 10;
1429 len
= buffer
+ 33 - pos
;
1435 /* Copy the temporary buffer backwards up to the available number of
1438 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1442 *MSVCRT__errno() = MSVCRT_ERANGE
;
1443 return MSVCRT_ERANGE
;
1446 memcpy(str
, pos
, len
);
1450 /*********************************************************************
1451 * _ultow_s (MSVCRT.@)
1453 int CDECL
MSVCRT__ultow_s(MSVCRT_ulong value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1456 WCHAR buffer
[33], *pos
;
1459 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1464 *MSVCRT__errno() = MSVCRT_EINVAL
;
1465 return MSVCRT_EINVAL
;
1473 digit
= value
% radix
;
1477 *--pos
= '0' + digit
;
1479 *--pos
= 'a' + digit
- 10;
1483 len
= buffer
+ 33 - pos
;
1489 /* Copy the temporary buffer backwards up to the available number of
1492 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1496 *MSVCRT__errno() = MSVCRT_ERANGE
;
1497 return MSVCRT_ERANGE
;
1500 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1504 /*********************************************************************
1505 * _i64toa_s (MSVCRT.@)
1507 int CDECL
MSVCRT__i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1509 unsigned __int64 val
;
1512 char buffer
[65], *pos
;
1515 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1516 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1517 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1520 return MSVCRT_EINVAL
;
1523 if (value
< 0 && radix
== 10)
1530 is_negative
= FALSE
;
1539 digit
= val
% radix
;
1543 *--pos
= '0' + digit
;
1545 *--pos
= 'a' + digit
- 10;
1552 len
= buffer
+ 65 - pos
;
1558 /* Copy the temporary buffer backwards up to the available number of
1559 * characters. Don't copy the negative sign if present. */
1567 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1571 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1572 return MSVCRT_ERANGE
;
1575 memcpy(str
, pos
, len
);
1579 /*********************************************************************
1580 * _i64tow_s (MSVCRT.@)
1582 int CDECL
MSVCRT__i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1584 unsigned __int64 val
;
1587 MSVCRT_wchar_t buffer
[65], *pos
;
1590 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1591 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1592 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1595 return MSVCRT_EINVAL
;
1598 if (value
< 0 && radix
== 10)
1605 is_negative
= FALSE
;
1614 digit
= val
% radix
;
1618 *--pos
= '0' + digit
;
1620 *--pos
= 'a' + digit
- 10;
1627 len
= buffer
+ 65 - pos
;
1631 MSVCRT_wchar_t
*p
= str
;
1633 /* Copy the temporary buffer backwards up to the available number of
1634 * characters. Don't copy the negative sign if present. */
1642 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1646 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1647 return MSVCRT_ERANGE
;
1650 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1654 #define I10_OUTPUT_MAX_PREC 21
1655 /* Internal structure used by $I10_OUTPUT */
1656 struct _I10_OUTPUT_DATA
{
1660 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1663 /*********************************************************************
1664 * $I10_OUTPUT (MSVCRT.@)
1665 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1666 * prec - precision of part, we're interested in
1667 * flag - 0 for first prec digits, 1 for fractional part
1668 * data - data to be populated
1671 * 0 if given double is NaN or INF
1675 * Native sets last byte of data->str to '0' or '9', I don't know what
1676 * it means. Current implementation sets it always to '0'.
1678 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1680 static const char inf_str
[] = "1#INF";
1681 static const char nan_str
[] = "1#QNAN";
1683 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1684 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1685 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1689 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1692 memcpy(&ld
, &ld80
, 10);
1694 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1705 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1713 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1719 int exp
= 1+floor(log10(d
));
1727 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1728 prec
= I10_OUTPUT_MAX_PREC
-1;
1734 sprintf(format
, "%%.%dle", prec
);
1735 sprintf(buf
, format
, d
);
1738 data
->pos
= atoi(buf
+prec
+3);
1742 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1745 memcpy(data
->str
, buf
+1, data
->len
);
1746 data
->str
[data
->len
] = '\0';
1748 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1749 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1753 #undef I10_OUTPUT_MAX_PREC
1755 /*********************************************************************
1758 int __cdecl
MSVCRT_memcmp(const void *ptr1
, const void *ptr2
, MSVCRT_size_t n
)
1760 return memcmp(ptr1
, ptr2
, n
);
1763 /*********************************************************************
1766 void * __cdecl
MSVCRT_memcpy(void *dst
, const void *src
, MSVCRT_size_t n
)
1768 return memmove(dst
, src
, n
);
1771 /*********************************************************************
1772 * memmove (MSVCRT.@)
1774 void * __cdecl
MSVCRT_memmove(void *dst
, const void *src
, MSVCRT_size_t n
)
1776 return memmove(dst
, src
, n
);
1779 /*********************************************************************
1782 void* __cdecl
MSVCRT_memset(void *dst
, int c
, MSVCRT_size_t n
)
1784 return memset(dst
, c
, n
);
1787 /*********************************************************************
1790 char* __cdecl
MSVCRT_strchr(const char *str
, int c
)
1792 return strchr(str
, c
);
1795 /*********************************************************************
1796 * strrchr (MSVCRT.@)
1798 char* __cdecl
MSVCRT_strrchr(const char *str
, int c
)
1800 return strrchr(str
, c
);
1803 /*********************************************************************
1806 void* __cdecl
MSVCRT_memchr(const void *ptr
, int c
, MSVCRT_size_t n
)
1808 return memchr(ptr
, c
, n
);
1811 /*********************************************************************
1814 int __cdecl
MSVCRT_strcmp(const char *str1
, const char *str2
)
1816 return strcmp(str1
, str2
);
1819 /*********************************************************************
1820 * strncmp (MSVCRT.@)
1822 int __cdecl
MSVCRT_strncmp(const char *str1
, const char *str2
, MSVCRT_size_t len
)
1824 return strncmp(str1
, str2
, len
);
1827 /*********************************************************************
1828 * _strnicmp_l (MSVCRT.@)
1830 int __cdecl
MSVCRT__strnicmp_l(const char *s1
, const char *s2
,
1831 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1833 MSVCRT_pthreadlocinfo locinfo
;
1836 if(s1
==NULL
|| s2
==NULL
)
1837 return MSVCRT__NLSCMPERROR
;
1843 locinfo
= get_locinfo();
1845 locinfo
= locale
->locinfo
;
1847 if(!locinfo
->lc_handle
[MSVCRT_LC_CTYPE
])
1848 return strncasecmp(s1
, s2
, count
);
1851 c1
= MSVCRT__tolower_l(*s1
++, locale
);
1852 c2
= MSVCRT__tolower_l(*s2
++, locale
);
1853 }while(--count
&& c1
&& c1
==c2
);
1858 /*********************************************************************
1859 * _stricmp_l (MSVCRT.@)
1861 int __cdecl
MSVCRT__stricmp_l(const char *s1
, const char *s2
, MSVCRT__locale_t locale
)
1863 return MSVCRT__strnicmp_l(s1
, s2
, -1, locale
);
1866 /*********************************************************************
1867 * _strnicmp (MSVCRT.@)
1869 int __cdecl
MSVCRT__strnicmp(const char *s1
, const char *s2
, MSVCRT_size_t count
)
1871 return MSVCRT__strnicmp_l(s1
, s2
, count
, NULL
);
1874 /*********************************************************************
1875 * _stricmp (MSVCRT.@)
1877 int __cdecl
MSVCRT__stricmp(const char *s1
, const char *s2
)
1879 return MSVCRT__strnicmp_l(s1
, s2
, -1, NULL
);
1882 /*********************************************************************
1885 char* __cdecl
MSVCRT_strstr(const char *haystack
, const char *needle
)
1887 return strstr(haystack
, needle
);