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 if(!MSVCRT_CHECK_PMT(str
!= NULL
)) return 0;
324 locinfo
= get_locinfo();
326 locinfo
= locale
->locinfo
;
328 /* FIXME: use *_l functions */
341 hlp
= d
*10+*(p
++)-'0';
342 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
) {
353 if(*p
== *locinfo
->lconv
->decimal_point
)
358 hlp
= d
*10+*(p
++)-'0';
359 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
)
374 if(*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D') {
386 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
392 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
393 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
396 if(*p
=='-' || *p
=='+')
402 fpcontrol
= _control87(0, 0);
403 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
404 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
413 expcnt
= expcnt
*expcnt
;
415 ret
= (long double)sign
* (negexp
? d
/lret
: d
*lret
);
417 _control87(fpcontrol
, 0xffffffff);
419 if((d
&& ret
==0.0) || isinf(ret
)) {
421 *err
= MSVCRT_ERANGE
;
423 *MSVCRT__errno() = MSVCRT_ERANGE
;
432 /*********************************************************************
433 * strtod_l (MSVCRT.@)
435 double CDECL
MSVCRT_strtod_l(const char *str
, char **end
, MSVCRT__locale_t locale
)
437 return strtod_helper(str
, end
, locale
, NULL
);
440 /*********************************************************************
443 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
445 return MSVCRT_strtod_l( str
, end
, NULL
);
448 /*********************************************************************
451 double CDECL
MSVCRT_atof( const char *str
)
453 return MSVCRT_strtod_l(str
, NULL
, NULL
);
456 /*********************************************************************
459 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
461 return MSVCRT_strtod_l(str
, NULL
, locale
);
464 /*********************************************************************
465 * _atoflt_l (MSVCRT.@)
467 int CDECL
MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT
*value
, char *str
, MSVCRT__locale_t locale
)
472 d
= strtod_helper(str
, NULL
, locale
, &err
);
475 return MSVCRT__OVERFLOW
;
476 if((d
!=0 || err
) && value
->f
>-MSVCRT_FLT_MIN
&& value
->f
<MSVCRT_FLT_MIN
)
477 return MSVCRT__UNDERFLOW
;
481 /*********************************************************************
482 * _atoflt (MSVCR100.@)
484 int CDECL
MSVCRT__atoflt(MSVCRT__CRT_FLOAT
*value
, char *str
)
486 return MSVCRT__atoflt_l(value
, str
, NULL
);
489 /*********************************************************************
490 * _atodbl_l (MSVCRT.@)
492 int CDECL
MSVCRT__atodbl_l(MSVCRT__CRT_DOUBLE
*value
, char *str
, MSVCRT__locale_t locale
)
496 value
->x
= strtod_helper(str
, NULL
, locale
, &err
);
498 return MSVCRT__OVERFLOW
;
499 if((value
->x
!=0 || err
) && value
->x
>-MSVCRT_DBL_MIN
&& value
->x
<MSVCRT_DBL_MIN
)
500 return MSVCRT__UNDERFLOW
;
504 /*********************************************************************
507 int CDECL
MSVCRT__atodbl(MSVCRT__CRT_DOUBLE
*value
, char *str
)
509 return MSVCRT__atodbl_l(value
, str
, NULL
);
512 /*********************************************************************
513 * _strcoll_l (MSVCRT.@)
515 int CDECL
MSVCRT_strcoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
517 MSVCRT_pthreadlocinfo locinfo
;
520 locinfo
= get_locinfo();
522 locinfo
= locale
->locinfo
;
524 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
525 return strcmp(str1
, str2
);
526 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, -1, str2
, -1)-CSTR_EQUAL
;
529 /*********************************************************************
532 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
534 return MSVCRT_strcoll_l(str1
, str2
, NULL
);
537 /*********************************************************************
538 * _stricoll_l (MSVCRT.@)
540 int CDECL
MSVCRT__stricoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
542 MSVCRT_pthreadlocinfo locinfo
;
545 locinfo
= get_locinfo();
547 locinfo
= locale
->locinfo
;
549 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
550 return strcasecmp(str1
, str2
);
551 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
552 str1
, -1, str2
, -1)-CSTR_EQUAL
;
555 /*********************************************************************
556 * _stricoll (MSVCRT.@)
558 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
560 return MSVCRT__stricoll_l(str1
, str2
, NULL
);
563 /*********************************************************************
564 * _strncoll_l (MSVCRT.@)
566 int CDECL
MSVCRT__strncoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, 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 strncmp(str1
, str2
, count
);
577 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, count
, str2
, count
)-CSTR_EQUAL
;
580 /*********************************************************************
581 * _strncoll (MSVCRT.@)
583 int CDECL
MSVCRT__strncoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
585 return MSVCRT__strncoll_l(str1
, str2
, count
, NULL
);
588 /*********************************************************************
589 * _strnicoll_l (MSVCRT.@)
591 int CDECL
MSVCRT__strnicoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, 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 strncasecmp(str1
, str2
, count
);
602 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
603 str1
, count
, str2
, count
)-CSTR_EQUAL
;
606 /*********************************************************************
607 * _strnicoll (MSVCRT.@)
609 int CDECL
MSVCRT__strnicoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
611 return MSVCRT__strnicoll_l(str1
, str2
, count
, NULL
);
614 /*********************************************************************
617 char* __cdecl
MSVCRT_strncpy(char *dst
, const char *src
, MSVCRT_size_t len
)
622 if((dst
[i
] = src
[i
]) == '\0') break;
624 while (i
< len
) dst
[i
++] = 0;
629 /*********************************************************************
630 * strcpy_s (MSVCRT.@)
632 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
635 if(!elem
) return MSVCRT_EINVAL
;
636 if(!dst
) return MSVCRT_EINVAL
;
640 return MSVCRT_EINVAL
;
643 for(i
= 0; i
< elem
; i
++)
645 if((dst
[i
] = src
[i
]) == '\0') return 0;
648 return MSVCRT_ERANGE
;
651 /*********************************************************************
652 * strcat_s (MSVCRT.@)
654 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
657 if(!dst
) return MSVCRT_EINVAL
;
658 if(elem
== 0) return MSVCRT_EINVAL
;
662 return MSVCRT_EINVAL
;
665 for(i
= 0; i
< elem
; i
++)
669 for(j
= 0; (j
+ i
) < elem
; j
++)
671 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
675 /* Set the first element to 0, not the first element after the skipped part */
677 return MSVCRT_ERANGE
;
680 /*********************************************************************
681 * strncat_s (MSVCRT.@)
683 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
687 if (!MSVCRT_CHECK_PMT(dst
!= 0)) return MSVCRT_EINVAL
;
688 if (!MSVCRT_CHECK_PMT(elem
!= 0)) return MSVCRT_EINVAL
;
689 if (!MSVCRT_CHECK_PMT(src
!= 0))
692 return MSVCRT_EINVAL
;
695 for(i
= 0; i
< elem
; i
++)
699 for(j
= 0; (j
+ i
) < elem
; j
++)
701 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
704 return MSVCRT_STRUNCATE
;
706 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
714 /* Set the first element to 0, not the first element after the skipped part */
716 return MSVCRT_ERANGE
;
719 /*********************************************************************
722 char* __cdecl
MSVCRT_strncat(char *dst
, const char *src
, MSVCRT_size_t len
)
724 return strncat(dst
, src
, len
);
727 /*********************************************************************
728 * _strxfrm_l (MSVCRT.@)
730 MSVCRT_size_t CDECL
MSVCRT__strxfrm_l( char *dest
, const char *src
,
731 MSVCRT_size_t len
, MSVCRT__locale_t locale
)
733 MSVCRT_pthreadlocinfo locinfo
;
736 if(!MSVCRT_CHECK_PMT(src
)) return INT_MAX
;
737 if(!MSVCRT_CHECK_PMT(dest
|| !len
)) return INT_MAX
;
740 FIXME("len > INT_MAX not supported\n");
745 locinfo
= get_locinfo();
747 locinfo
= locale
->locinfo
;
749 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
]) {
750 MSVCRT_strncpy(dest
, src
, len
);
754 ret
= LCMapStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
],
755 LCMAP_SORTKEY
, src
, -1, NULL
, 0);
758 *MSVCRT__errno() = MSVCRT_EILSEQ
;
761 if(!len
) return ret
-1;
765 *MSVCRT__errno() = MSVCRT_ERANGE
;
769 return LCMapStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
],
770 LCMAP_SORTKEY
, src
, -1, dest
, len
) - 1;
773 /*********************************************************************
776 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
778 return MSVCRT__strxfrm_l(dest
, src
, len
, NULL
);
781 /********************************************************************
782 * _atoldbl (MSVCRT.@)
784 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
786 /* FIXME needs error checking for huge/small values */
789 TRACE("str %s value %p\n",str
,value
);
791 memcpy(value
, &ld
, 10);
793 FIXME("stub, str %s value %p\n",str
,value
);
798 /********************************************************************
799 * __STRINGTOLD (MSVCRT.@)
801 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
805 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
807 memcpy(value
, &ld
, 10);
809 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
814 /*********************************************************************
817 MSVCRT_size_t __cdecl
MSVCRT_strlen(const char *str
)
822 /******************************************************************
825 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
829 for(i
=0; i
<maxlen
; i
++)
835 /*********************************************************************
836 * _strtoi64_l (MSVCRT.@)
838 * FIXME: locale parameter is ignored
840 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
842 BOOL negative
= FALSE
;
845 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
847 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
848 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
849 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
851 while(isspace(*nptr
)) nptr
++;
856 } else if(*nptr
== '+')
859 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
872 char cur
= tolower(*nptr
);
880 if(cur
<'a' || cur
>='a'+base
-10)
890 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
891 ret
= MSVCRT_I64_MAX
;
892 *MSVCRT__errno() = MSVCRT_ERANGE
;
893 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
894 ret
= MSVCRT_I64_MIN
;
895 *MSVCRT__errno() = MSVCRT_ERANGE
;
901 *endptr
= (char*)nptr
;
906 /*********************************************************************
907 * _strtoi64 (MSVCRT.@)
909 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
911 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
914 /*********************************************************************
917 int __cdecl
MSVCRT__atoi_l(const char *str
, MSVCRT__locale_t locale
)
919 __int64 ret
= MSVCRT_strtoi64_l(str
, NULL
, 10, locale
);
923 *MSVCRT__errno() = MSVCRT_ERANGE
;
924 } else if(ret
< INT_MIN
) {
926 *MSVCRT__errno() = MSVCRT_ERANGE
;
931 /*********************************************************************
935 int __cdecl
MSVCRT_atoi(const char *str
)
943 while(isspace(*str
)) str
++;
947 }else if(*str
== '-') {
952 while(*str
>='0' && *str
<='9') {
953 ret
= ret
*10+*str
-'0';
957 return minus
? -ret
: ret
;
960 int CDECL
MSVCRT_atoi(const char *str
)
962 return MSVCRT__atoi_l(str
, NULL
);
966 /******************************************************************
969 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
971 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, NULL
);
973 if(ret
> MSVCRT_LONG_MAX
) {
974 ret
= MSVCRT_LONG_MAX
;
975 *MSVCRT__errno() = MSVCRT_ERANGE
;
976 } else if(ret
< MSVCRT_LONG_MIN
) {
977 ret
= MSVCRT_LONG_MIN
;
978 *MSVCRT__errno() = MSVCRT_ERANGE
;
984 /******************************************************************
987 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
989 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, NULL
);
991 if(ret
> MSVCRT_ULONG_MAX
) {
992 ret
= MSVCRT_ULONG_MAX
;
993 *MSVCRT__errno() = MSVCRT_ERANGE
;
994 }else if(ret
< -(__int64
)MSVCRT_ULONG_MAX
) {
996 *MSVCRT__errno() = MSVCRT_ERANGE
;
1002 /*********************************************************************
1003 * _strtoui64_l (MSVCRT.@)
1005 * FIXME: locale parameter is ignored
1007 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
1009 BOOL negative
= FALSE
;
1010 unsigned __int64 ret
= 0;
1012 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
1014 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
1015 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
1016 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
1018 while(isspace(*nptr
)) nptr
++;
1023 } else if(*nptr
== '+')
1026 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
1039 char cur
= tolower(*nptr
);
1047 if(cur
<'a' || cur
>='a'+base
-10)
1054 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
1055 ret
= MSVCRT_UI64_MAX
;
1056 *MSVCRT__errno() = MSVCRT_ERANGE
;
1062 *endptr
= (char*)nptr
;
1064 return negative
? -ret
: ret
;
1067 /*********************************************************************
1068 * _strtoui64 (MSVCRT.@)
1070 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
1072 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
1075 static int ltoa_helper(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1080 char buffer
[33], *pos
;
1083 if (value
< 0 && radix
== 10)
1090 is_negative
= FALSE
;
1099 digit
= val
% radix
;
1103 *--pos
= '0' + digit
;
1105 *--pos
= 'a' + digit
- 10;
1112 len
= buffer
+ 33 - pos
;
1118 /* Copy the temporary buffer backwards up to the available number of
1119 * characters. Don't copy the negative sign if present. */
1127 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1131 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1132 return MSVCRT_ERANGE
;
1135 memcpy(str
, pos
, len
);
1139 /*********************************************************************
1140 * _ltoa_s (MSVCRT.@)
1142 int CDECL
MSVCRT__ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1144 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1145 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1146 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1149 return MSVCRT_EINVAL
;
1152 return ltoa_helper(value
, str
, size
, radix
);
1155 /*********************************************************************
1156 * _ltow_s (MSVCRT.@)
1158 int CDECL
MSVCRT__ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1163 MSVCRT_wchar_t buffer
[33], *pos
;
1166 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1167 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1168 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1171 return MSVCRT_EINVAL
;
1174 if (value
< 0 && radix
== 10)
1181 is_negative
= FALSE
;
1190 digit
= val
% radix
;
1194 *--pos
= '0' + digit
;
1196 *--pos
= 'a' + digit
- 10;
1203 len
= buffer
+ 33 - pos
;
1207 MSVCRT_wchar_t
*p
= str
;
1209 /* Copy the temporary buffer backwards up to the available number of
1210 * characters. Don't copy the negative sign if present. */
1218 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1222 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1223 return MSVCRT_ERANGE
;
1226 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1230 /*********************************************************************
1231 * _itoa_s (MSVCRT.@)
1233 int CDECL
MSVCRT__itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
1235 return MSVCRT__ltoa_s(value
, str
, size
, radix
);
1238 /*********************************************************************
1241 char* CDECL
MSVCRT__itoa(int value
, char *str
, int radix
)
1243 return ltoa_helper(value
, str
, MSVCRT_SIZE_MAX
, radix
) ? NULL
: str
;
1246 /*********************************************************************
1247 * _itow_s (MSVCRT.@)
1249 int CDECL
MSVCRT__itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1251 return MSVCRT__ltow_s(value
, str
, size
, radix
);
1254 /*********************************************************************
1255 * _ui64toa_s (MSVCRT.@)
1257 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
1258 MSVCRT_size_t size
, int radix
)
1260 char buffer
[65], *pos
;
1263 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1264 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1265 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1268 return MSVCRT_EINVAL
;
1275 digit
= value
%radix
;
1281 *--pos
= 'a'+digit
-10;
1284 if(buffer
-pos
+65 > size
) {
1285 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1286 return MSVCRT_EINVAL
;
1289 memcpy(str
, pos
, buffer
-pos
+65);
1293 /*********************************************************************
1294 * _ui64tow_s (MSVCRT.@)
1296 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1297 MSVCRT_size_t size
, int radix
)
1299 MSVCRT_wchar_t buffer
[65], *pos
;
1302 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1303 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1304 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1307 return MSVCRT_EINVAL
;
1314 digit
= value
% radix
;
1315 value
= value
/ radix
;
1317 *--pos
= '0' + digit
;
1319 *--pos
= 'a' + digit
- 10;
1320 } while (value
!= 0);
1322 if(buffer
-pos
+65 > size
) {
1323 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1324 return MSVCRT_EINVAL
;
1327 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(MSVCRT_wchar_t
));
1331 /*********************************************************************
1332 * _ultoa_s (MSVCRT.@)
1334 int CDECL
MSVCRT__ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1337 char buffer
[33], *pos
;
1340 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1345 *MSVCRT__errno() = MSVCRT_EINVAL
;
1346 return MSVCRT_EINVAL
;
1354 digit
= value
% radix
;
1358 *--pos
= '0' + digit
;
1360 *--pos
= 'a' + digit
- 10;
1364 len
= buffer
+ 33 - pos
;
1370 /* Copy the temporary buffer backwards up to the available number of
1373 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1377 *MSVCRT__errno() = MSVCRT_ERANGE
;
1378 return MSVCRT_ERANGE
;
1381 memcpy(str
, pos
, len
);
1385 /*********************************************************************
1386 * _ultow_s (MSVCRT.@)
1388 int CDECL
MSVCRT__ultow_s(MSVCRT_ulong value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1391 WCHAR buffer
[33], *pos
;
1394 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1399 *MSVCRT__errno() = MSVCRT_EINVAL
;
1400 return MSVCRT_EINVAL
;
1408 digit
= value
% radix
;
1412 *--pos
= '0' + digit
;
1414 *--pos
= 'a' + digit
- 10;
1418 len
= buffer
+ 33 - pos
;
1424 /* Copy the temporary buffer backwards up to the available number of
1427 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1431 *MSVCRT__errno() = MSVCRT_ERANGE
;
1432 return MSVCRT_ERANGE
;
1435 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1439 /*********************************************************************
1440 * _i64toa_s (MSVCRT.@)
1442 int CDECL
MSVCRT__i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1444 unsigned __int64 val
;
1447 char buffer
[65], *pos
;
1450 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1451 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1452 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1455 return MSVCRT_EINVAL
;
1458 if (value
< 0 && radix
== 10)
1465 is_negative
= FALSE
;
1474 digit
= val
% radix
;
1478 *--pos
= '0' + digit
;
1480 *--pos
= 'a' + digit
- 10;
1487 len
= buffer
+ 65 - pos
;
1493 /* Copy the temporary buffer backwards up to the available number of
1494 * characters. Don't copy the negative sign if present. */
1502 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1506 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1507 return MSVCRT_ERANGE
;
1510 memcpy(str
, pos
, len
);
1514 /*********************************************************************
1515 * _i64tow_s (MSVCRT.@)
1517 int CDECL
MSVCRT__i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1519 unsigned __int64 val
;
1522 MSVCRT_wchar_t buffer
[65], *pos
;
1525 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1526 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1527 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1530 return MSVCRT_EINVAL
;
1533 if (value
< 0 && radix
== 10)
1540 is_negative
= FALSE
;
1549 digit
= val
% radix
;
1553 *--pos
= '0' + digit
;
1555 *--pos
= 'a' + digit
- 10;
1562 len
= buffer
+ 65 - pos
;
1566 MSVCRT_wchar_t
*p
= str
;
1568 /* Copy the temporary buffer backwards up to the available number of
1569 * characters. Don't copy the negative sign if present. */
1577 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1581 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1582 return MSVCRT_ERANGE
;
1585 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1589 #define I10_OUTPUT_MAX_PREC 21
1590 /* Internal structure used by $I10_OUTPUT */
1591 struct _I10_OUTPUT_DATA
{
1595 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1598 /*********************************************************************
1599 * $I10_OUTPUT (MSVCRT.@)
1600 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1601 * prec - precision of part, we're interested in
1602 * flag - 0 for first prec digits, 1 for fractional part
1603 * data - data to be populated
1606 * 0 if given double is NaN or INF
1610 * Native sets last byte of data->str to '0' or '9', I don't know what
1611 * it means. Current implementation sets it always to '0'.
1613 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1615 static const char inf_str
[] = "1#INF";
1616 static const char nan_str
[] = "1#QNAN";
1618 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1619 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1620 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1624 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1627 memcpy(&ld
, &ld80
, 10);
1629 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1640 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1648 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1654 int exp
= 1+floor(log10(d
));
1662 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1663 prec
= I10_OUTPUT_MAX_PREC
-1;
1669 sprintf(format
, "%%.%dle", prec
);
1670 sprintf(buf
, format
, d
);
1673 data
->pos
= atoi(buf
+prec
+3);
1677 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1680 memcpy(data
->str
, buf
+1, data
->len
);
1681 data
->str
[data
->len
] = '\0';
1683 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1684 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1688 #undef I10_OUTPUT_MAX_PREC
1690 /*********************************************************************
1693 int __cdecl
MSVCRT_memcmp(const void *ptr1
, const void *ptr2
, MSVCRT_size_t n
)
1695 return memcmp(ptr1
, ptr2
, n
);
1698 /*********************************************************************
1701 void * __cdecl
MSVCRT_memcpy(void *dst
, const void *src
, MSVCRT_size_t n
)
1703 return memmove(dst
, src
, n
);
1706 /*********************************************************************
1707 * memmove (MSVCRT.@)
1709 void * __cdecl
MSVCRT_memmove(void *dst
, const void *src
, MSVCRT_size_t n
)
1711 return memmove(dst
, src
, n
);
1714 /*********************************************************************
1717 void* __cdecl
MSVCRT_memset(void *dst
, int c
, MSVCRT_size_t n
)
1719 return memset(dst
, c
, n
);
1722 /*********************************************************************
1725 char* __cdecl
MSVCRT_strchr(const char *str
, int c
)
1727 return strchr(str
, c
);
1730 /*********************************************************************
1731 * strrchr (MSVCRT.@)
1733 char* __cdecl
MSVCRT_strrchr(const char *str
, int c
)
1735 return strrchr(str
, c
);
1738 /*********************************************************************
1741 void* __cdecl
MSVCRT_memchr(const void *ptr
, int c
, MSVCRT_size_t n
)
1743 return memchr(ptr
, c
, n
);
1746 /*********************************************************************
1749 int __cdecl
MSVCRT_strcmp(const char *str1
, const char *str2
)
1751 return strcmp(str1
, str2
);
1754 /*********************************************************************
1755 * strncmp (MSVCRT.@)
1757 int __cdecl
MSVCRT_strncmp(const char *str1
, const char *str2
, MSVCRT_size_t len
)
1759 return strncmp(str1
, str2
, len
);
1762 /*********************************************************************
1763 * _strnicmp_l (MSVCRT.@)
1765 int __cdecl
MSVCRT__strnicmp_l(const char *s1
, const char *s2
,
1766 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1768 MSVCRT_pthreadlocinfo locinfo
;
1771 if(s1
==NULL
|| s2
==NULL
)
1772 return MSVCRT__NLSCMPERROR
;
1778 locinfo
= get_locinfo();
1780 locinfo
= locale
->locinfo
;
1782 if(!locinfo
->lc_handle
[MSVCRT_LC_CTYPE
])
1783 return strncasecmp(s1
, s2
, count
);
1786 c1
= MSVCRT__tolower_l(*s1
++, locale
);
1787 c2
= MSVCRT__tolower_l(*s2
++, locale
);
1788 }while(--count
&& c1
&& c1
==c2
);
1793 /*********************************************************************
1794 * _stricmp_l (MSVCRT.@)
1796 int __cdecl
MSVCRT__stricmp_l(const char *s1
, const char *s2
, MSVCRT__locale_t locale
)
1798 return MSVCRT__strnicmp_l(s1
, s2
, -1, locale
);
1801 /*********************************************************************
1802 * _strnicmp (MSVCRT.@)
1804 int __cdecl
MSVCRT__strnicmp(const char *s1
, const char *s2
, MSVCRT_size_t count
)
1806 return MSVCRT__strnicmp_l(s1
, s2
, count
, NULL
);
1809 /*********************************************************************
1810 * _stricmp (MSVCRT.@)
1812 int __cdecl
MSVCRT__stricmp(const char *s1
, const char *s2
)
1814 return MSVCRT__strnicmp_l(s1
, s2
, -1, NULL
);
1817 /*********************************************************************
1820 char* __cdecl
MSVCRT_strstr(const char *haystack
, const char *needle
)
1822 return strstr(haystack
, needle
);