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
_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
_strlwr_s(char *str
, MSVCRT_size_t len
)
94 return _strlwr_s_l(str
, len
, NULL
);
97 /*********************************************************************
98 * _strlwr_l (MSVCRT.@)
100 char* CDECL
_strlwr_l(char *str
, MSVCRT__locale_t locale
)
102 _strlwr_s_l(str
, -1, locale
);
106 /*********************************************************************
109 char* CDECL
MSVCRT__strlwr(char *str
)
111 _strlwr_s_l(str
, -1, NULL
);
115 /*********************************************************************
116 * _strupr_s_l (MSVCRT.@)
118 int CDECL
_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
_strupr_s(char *str
, MSVCRT_size_t len
)
155 return _strupr_s_l(str
, len
, NULL
);
158 /*********************************************************************
159 * _strupr_l (MSVCRT.@)
161 char* CDECL
MSVCRT__strupr_l(char *str
, MSVCRT__locale_t locale
)
163 _strupr_s_l(str
, -1, locale
);
167 /*********************************************************************
170 char* CDECL
MSVCRT__strupr(char *str
)
172 _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 * _atodbl_l (MSVCRT.@)
460 int CDECL
MSVCRT__atodbl_l(MSVCRT__CRT_DOUBLE
*value
, char *str
, MSVCRT__locale_t locale
)
464 value
->x
= strtod_helper(str
, NULL
, locale
, &err
);
466 return MSVCRT__OVERFLOW
;
467 if((value
->x
!=0 || err
) && value
->x
>-MSVCRT_DBL_MIN
&& value
->x
<MSVCRT_DBL_MIN
)
468 return MSVCRT__UNDERFLOW
;
472 /*********************************************************************
475 int CDECL
MSVCRT__atodbl(MSVCRT__CRT_DOUBLE
*value
, char *str
)
477 return MSVCRT__atodbl_l(value
, str
, NULL
);
480 /*********************************************************************
481 * _strcoll_l (MSVCRT.@)
483 int CDECL
MSVCRT_strcoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
485 MSVCRT_pthreadlocinfo locinfo
;
488 locinfo
= get_locinfo();
490 locinfo
= locale
->locinfo
;
492 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, -1, str2
, -1)-CSTR_EQUAL
;
495 /*********************************************************************
498 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
500 return MSVCRT_strcoll_l(str1
, str2
, NULL
);
503 /*********************************************************************
504 * _stricoll_l (MSVCRT.@)
506 int CDECL
MSVCRT__stricoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
508 MSVCRT_pthreadlocinfo locinfo
;
511 locinfo
= get_locinfo();
513 locinfo
= locale
->locinfo
;
515 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
516 str1
, -1, str2
, -1)-CSTR_EQUAL
;
519 /*********************************************************************
520 * _stricoll (MSVCRT.@)
522 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
524 return MSVCRT__stricoll_l(str1
, str2
, NULL
);
527 /*********************************************************************
528 * _strncoll_l (MSVCRT.@)
530 int CDECL
MSVCRT__strncoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
532 MSVCRT_pthreadlocinfo locinfo
;
535 locinfo
= get_locinfo();
537 locinfo
= locale
->locinfo
;
539 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, count
, str2
, count
)-CSTR_EQUAL
;
542 /*********************************************************************
543 * _strncoll (MSVCRT.@)
545 int CDECL
MSVCRT__strncoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
547 return MSVCRT__strncoll_l(str1
, str2
, count
, NULL
);
550 /*********************************************************************
551 * _strnicoll_l (MSVCRT.@)
553 int CDECL
MSVCRT__strnicoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
555 MSVCRT_pthreadlocinfo locinfo
;
558 locinfo
= get_locinfo();
560 locinfo
= locale
->locinfo
;
562 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
563 str1
, count
, str2
, count
)-CSTR_EQUAL
;
566 /*********************************************************************
567 * _strnicoll (MSVCRT.@)
569 int CDECL
MSVCRT__strnicoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
571 return MSVCRT__strnicoll_l(str1
, str2
, count
, NULL
);
574 /*********************************************************************
577 char* __cdecl
MSVCRT_strncpy(char *dst
, const char *src
, MSVCRT_size_t len
)
582 if((dst
[i
] = src
[i
]) == '\0') break;
584 while (i
< len
) dst
[i
++] = 0;
589 /*********************************************************************
590 * strcpy_s (MSVCRT.@)
592 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
595 if(!elem
) return MSVCRT_EINVAL
;
596 if(!dst
) return MSVCRT_EINVAL
;
600 return MSVCRT_EINVAL
;
603 for(i
= 0; i
< elem
; i
++)
605 if((dst
[i
] = src
[i
]) == '\0') return 0;
608 return MSVCRT_ERANGE
;
611 /*********************************************************************
612 * strcat_s (MSVCRT.@)
614 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
617 if(!dst
) return MSVCRT_EINVAL
;
618 if(elem
== 0) return MSVCRT_EINVAL
;
622 return MSVCRT_EINVAL
;
625 for(i
= 0; i
< elem
; i
++)
629 for(j
= 0; (j
+ i
) < elem
; j
++)
631 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
635 /* Set the first element to 0, not the first element after the skipped part */
637 return MSVCRT_ERANGE
;
640 /*********************************************************************
641 * strncat_s (MSVCRT.@)
643 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
647 if (!MSVCRT_CHECK_PMT(dst
!= 0)) return MSVCRT_EINVAL
;
648 if (!MSVCRT_CHECK_PMT(elem
!= 0)) return MSVCRT_EINVAL
;
649 if (!MSVCRT_CHECK_PMT(src
!= 0))
652 return MSVCRT_EINVAL
;
655 for(i
= 0; i
< elem
; i
++)
659 for(j
= 0; (j
+ i
) < elem
; j
++)
661 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
664 return MSVCRT_STRUNCATE
;
666 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
674 /* Set the first element to 0, not the first element after the skipped part */
676 return MSVCRT_ERANGE
;
679 /*********************************************************************
682 char* __cdecl
MSVCRT_strncat(char *dst
, const char *src
, MSVCRT_size_t len
)
684 return strncat(dst
, src
, len
);
687 /*********************************************************************
690 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
692 /* FIXME: handle Windows locale */
693 return strxfrm( dest
, src
, len
);
696 /********************************************************************
697 * _atoldbl (MSVCRT.@)
699 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
701 /* FIXME needs error checking for huge/small values */
704 TRACE("str %s value %p\n",str
,value
);
706 memcpy(value
, &ld
, 10);
708 FIXME("stub, str %s value %p\n",str
,value
);
713 /********************************************************************
714 * __STRINGTOLD (MSVCRT.@)
716 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
720 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
722 memcpy(value
, &ld
, 10);
724 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
729 /*********************************************************************
732 MSVCRT_size_t __cdecl
MSVCRT_strlen(const char *str
)
737 /******************************************************************
740 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
744 for(i
=0; i
<maxlen
; i
++)
750 /*********************************************************************
751 * _strtoi64_l (MSVCRT.@)
753 * FIXME: locale parameter is ignored
755 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
757 BOOL negative
= FALSE
;
760 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
762 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
763 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
764 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
766 while(isspace(*nptr
)) nptr
++;
771 } else if(*nptr
== '+')
774 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
787 char cur
= tolower(*nptr
);
795 if(cur
<'a' || cur
>='a'+base
-10)
805 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
806 ret
= MSVCRT_I64_MAX
;
807 *MSVCRT__errno() = MSVCRT_ERANGE
;
808 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
809 ret
= MSVCRT_I64_MIN
;
810 *MSVCRT__errno() = MSVCRT_ERANGE
;
816 *endptr
= (char*)nptr
;
821 /*********************************************************************
822 * _strtoi64 (MSVCRT.@)
824 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
826 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
829 /*********************************************************************
832 int __cdecl
MSVCRT__atoi_l(const char *str
, MSVCRT__locale_t locale
)
834 __int64 ret
= MSVCRT_strtoi64_l(str
, NULL
, 10, locale
);
838 *MSVCRT__errno() = MSVCRT_ERANGE
;
839 } else if(ret
< INT_MIN
) {
841 *MSVCRT__errno() = MSVCRT_ERANGE
;
846 /*********************************************************************
849 int __cdecl
MSVCRT_atoi(const char *str
)
857 while(isspace(*str
)) str
++;
861 }else if(*str
== '-') {
866 while(*str
>='0' && *str
<='9') {
867 ret
= ret
*10+*str
-'0';
871 return minus
? -ret
: ret
;
874 /******************************************************************
877 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
879 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, NULL
);
881 if(ret
> MSVCRT_LONG_MAX
) {
882 ret
= MSVCRT_LONG_MAX
;
883 *MSVCRT__errno() = MSVCRT_ERANGE
;
884 } else if(ret
< MSVCRT_LONG_MIN
) {
885 ret
= MSVCRT_LONG_MIN
;
886 *MSVCRT__errno() = MSVCRT_ERANGE
;
892 /******************************************************************
895 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
897 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, NULL
);
899 if(ret
> MSVCRT_ULONG_MAX
) {
900 ret
= MSVCRT_ULONG_MAX
;
901 *MSVCRT__errno() = MSVCRT_ERANGE
;
902 }else if(ret
< -(__int64
)MSVCRT_ULONG_MAX
) {
904 *MSVCRT__errno() = MSVCRT_ERANGE
;
910 /*********************************************************************
911 * _strtoui64_l (MSVCRT.@)
913 * FIXME: locale parameter is ignored
915 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
917 BOOL negative
= FALSE
;
918 unsigned __int64 ret
= 0;
920 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
922 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
923 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
924 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
926 while(isspace(*nptr
)) nptr
++;
931 } else if(*nptr
== '+')
934 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
947 char cur
= tolower(*nptr
);
955 if(cur
<'a' || cur
>='a'+base
-10)
962 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
963 ret
= MSVCRT_UI64_MAX
;
964 *MSVCRT__errno() = MSVCRT_ERANGE
;
970 *endptr
= (char*)nptr
;
972 return negative
? -ret
: ret
;
975 /*********************************************************************
976 * _strtoui64 (MSVCRT.@)
978 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
980 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
983 static int ltoa_helper(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
988 char buffer
[33], *pos
;
991 if (value
< 0 && radix
== 10)
1007 digit
= val
% radix
;
1011 *--pos
= '0' + digit
;
1013 *--pos
= 'a' + digit
- 10;
1020 len
= buffer
+ 33 - pos
;
1026 /* Copy the temporary buffer backwards up to the available number of
1027 * characters. Don't copy the negative sign if present. */
1035 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1039 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1040 return MSVCRT_ERANGE
;
1043 memcpy(str
, pos
, len
);
1047 /*********************************************************************
1048 * _ltoa_s (MSVCRT.@)
1050 int CDECL
_ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1052 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1053 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1054 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1057 return MSVCRT_EINVAL
;
1060 return ltoa_helper(value
, str
, size
, radix
);
1063 /*********************************************************************
1064 * _ltow_s (MSVCRT.@)
1066 int CDECL
_ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1071 MSVCRT_wchar_t buffer
[33], *pos
;
1074 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1075 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1076 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1079 return MSVCRT_EINVAL
;
1082 if (value
< 0 && radix
== 10)
1089 is_negative
= FALSE
;
1098 digit
= val
% radix
;
1102 *--pos
= '0' + digit
;
1104 *--pos
= 'a' + digit
- 10;
1111 len
= buffer
+ 33 - pos
;
1115 MSVCRT_wchar_t
*p
= str
;
1117 /* Copy the temporary buffer backwards up to the available number of
1118 * characters. Don't copy the negative sign if present. */
1126 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1130 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1131 return MSVCRT_ERANGE
;
1134 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1138 /*********************************************************************
1139 * _itoa_s (MSVCRT.@)
1141 int CDECL
_itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
1143 return _ltoa_s(value
, str
, size
, radix
);
1146 /*********************************************************************
1149 char* CDECL
_itoa(int value
, char *str
, int radix
)
1151 return ltoa_helper(value
, str
, MSVCRT_SIZE_MAX
, radix
) ? NULL
: str
;
1154 /*********************************************************************
1155 * _itow_s (MSVCRT.@)
1157 int CDECL
_itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1159 return _ltow_s(value
, str
, size
, radix
);
1162 /*********************************************************************
1163 * _ui64toa_s (MSVCRT.@)
1165 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
1166 MSVCRT_size_t size
, int radix
)
1168 char buffer
[65], *pos
;
1171 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1172 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1173 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1176 return MSVCRT_EINVAL
;
1183 digit
= value
%radix
;
1189 *--pos
= 'a'+digit
-10;
1192 if(buffer
-pos
+65 > size
) {
1193 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1194 return MSVCRT_EINVAL
;
1197 memcpy(str
, pos
, buffer
-pos
+65);
1201 /*********************************************************************
1202 * _ui64tow_s (MSVCRT.@)
1204 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1205 MSVCRT_size_t size
, int radix
)
1207 MSVCRT_wchar_t buffer
[65], *pos
;
1210 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1211 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1212 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1215 return MSVCRT_EINVAL
;
1222 digit
= value
% radix
;
1223 value
= value
/ radix
;
1225 *--pos
= '0' + digit
;
1227 *--pos
= 'a' + digit
- 10;
1228 } while (value
!= 0);
1230 if(buffer
-pos
+65 > size
) {
1231 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1232 return MSVCRT_EINVAL
;
1235 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(MSVCRT_wchar_t
));
1239 /*********************************************************************
1240 * _ultoa_s (MSVCRT.@)
1242 int CDECL
_ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1245 char buffer
[33], *pos
;
1248 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1253 *MSVCRT__errno() = MSVCRT_EINVAL
;
1254 return MSVCRT_EINVAL
;
1262 digit
= value
% radix
;
1266 *--pos
= '0' + digit
;
1268 *--pos
= 'a' + digit
- 10;
1272 len
= buffer
+ 33 - pos
;
1278 /* Copy the temporary buffer backwards up to the available number of
1281 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1285 *MSVCRT__errno() = MSVCRT_ERANGE
;
1286 return MSVCRT_ERANGE
;
1289 memcpy(str
, pos
, len
);
1293 /*********************************************************************
1294 * _ultow_s (MSVCRT.@)
1296 int CDECL
_ultow_s(MSVCRT_ulong value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1299 WCHAR buffer
[33], *pos
;
1302 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1307 *MSVCRT__errno() = MSVCRT_EINVAL
;
1308 return MSVCRT_EINVAL
;
1316 digit
= value
% radix
;
1320 *--pos
= '0' + digit
;
1322 *--pos
= 'a' + digit
- 10;
1326 len
= buffer
+ 33 - pos
;
1332 /* Copy the temporary buffer backwards up to the available number of
1335 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1339 *MSVCRT__errno() = MSVCRT_ERANGE
;
1340 return MSVCRT_ERANGE
;
1343 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1347 /*********************************************************************
1348 * _i64toa_s (MSVCRT.@)
1350 int CDECL
_i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1352 unsigned __int64 val
;
1355 char buffer
[65], *pos
;
1358 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1359 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1360 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1363 return MSVCRT_EINVAL
;
1366 if (value
< 0 && radix
== 10)
1373 is_negative
= FALSE
;
1382 digit
= val
% radix
;
1386 *--pos
= '0' + digit
;
1388 *--pos
= 'a' + digit
- 10;
1395 len
= buffer
+ 65 - pos
;
1401 /* Copy the temporary buffer backwards up to the available number of
1402 * characters. Don't copy the negative sign if present. */
1410 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1414 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1415 return MSVCRT_ERANGE
;
1418 memcpy(str
, pos
, len
);
1422 /*********************************************************************
1423 * _i64tow_s (MSVCRT.@)
1425 int CDECL
_i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1427 unsigned __int64 val
;
1430 MSVCRT_wchar_t buffer
[65], *pos
;
1433 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1434 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1435 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1438 return MSVCRT_EINVAL
;
1441 if (value
< 0 && radix
== 10)
1448 is_negative
= FALSE
;
1457 digit
= val
% radix
;
1461 *--pos
= '0' + digit
;
1463 *--pos
= 'a' + digit
- 10;
1470 len
= buffer
+ 65 - pos
;
1474 MSVCRT_wchar_t
*p
= str
;
1476 /* Copy the temporary buffer backwards up to the available number of
1477 * characters. Don't copy the negative sign if present. */
1485 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1489 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1490 return MSVCRT_ERANGE
;
1493 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1497 #define I10_OUTPUT_MAX_PREC 21
1498 /* Internal structure used by $I10_OUTPUT */
1499 struct _I10_OUTPUT_DATA
{
1503 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1506 /*********************************************************************
1507 * $I10_OUTPUT (MSVCRT.@)
1508 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1509 * prec - precision of part, we're interested in
1510 * flag - 0 for first prec digits, 1 for fractional part
1511 * data - data to be populated
1514 * 0 if given double is NaN or INF
1518 * Native sets last byte of data->str to '0' or '9', I don't know what
1519 * it means. Current implementation sets it always to '0'.
1521 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1523 static const char inf_str
[] = "1#INF";
1524 static const char nan_str
[] = "1#QNAN";
1526 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1527 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1528 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1532 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1535 memcpy(&ld
, &ld80
, 10);
1537 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1548 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1556 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1562 int exp
= 1+floor(log10(d
));
1570 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1571 prec
= I10_OUTPUT_MAX_PREC
-1;
1577 sprintf(format
, "%%.%dle", prec
);
1578 sprintf(buf
, format
, d
);
1581 data
->pos
= atoi(buf
+prec
+3);
1585 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1588 memcpy(data
->str
, buf
+1, data
->len
);
1589 data
->str
[data
->len
] = '\0';
1591 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1592 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1596 #undef I10_OUTPUT_MAX_PREC
1598 /*********************************************************************
1601 int __cdecl
MSVCRT_memcmp(const void *ptr1
, const void *ptr2
, MSVCRT_size_t n
)
1603 return memcmp(ptr1
, ptr2
, n
);
1606 /*********************************************************************
1609 void * __cdecl
MSVCRT_memcpy(void *dst
, const void *src
, MSVCRT_size_t n
)
1611 return memmove(dst
, src
, n
);
1614 /*********************************************************************
1615 * memmove (MSVCRT.@)
1617 void * __cdecl
MSVCRT_memmove(void *dst
, const void *src
, MSVCRT_size_t n
)
1619 return memmove(dst
, src
, n
);
1622 /*********************************************************************
1625 void* __cdecl
MSVCRT_memset(void *dst
, int c
, MSVCRT_size_t n
)
1627 return memset(dst
, c
, n
);
1630 /*********************************************************************
1633 char* __cdecl
MSVCRT_strchr(const char *str
, int c
)
1635 return strchr(str
, c
);
1638 /*********************************************************************
1639 * strrchr (MSVCRT.@)
1641 char* __cdecl
MSVCRT_strrchr(const char *str
, int c
)
1643 return strrchr(str
, c
);
1646 /*********************************************************************
1649 void* __cdecl
MSVCRT_memchr(const void *ptr
, int c
, MSVCRT_size_t n
)
1651 return memchr(ptr
, c
, n
);
1654 /*********************************************************************
1657 int __cdecl
MSVCRT_strcmp(const char *str1
, const char *str2
)
1659 return strcmp(str1
, str2
);
1662 /*********************************************************************
1663 * strncmp (MSVCRT.@)
1665 int __cdecl
MSVCRT_strncmp(const char *str1
, const char *str2
, MSVCRT_size_t len
)
1667 return strncmp(str1
, str2
, len
);
1670 /*********************************************************************
1671 * _strnicmp_l (MSVCRT.@)
1673 int __cdecl
MSVCRT__strnicmp_l(const char *s1
, const char *s2
,
1674 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1676 MSVCRT_pthreadlocinfo locinfo
;
1679 if(s1
==NULL
|| s2
==NULL
)
1680 return MSVCRT__NLSCMPERROR
;
1686 locinfo
= get_locinfo();
1688 locinfo
= locale
->locinfo
;
1690 if(!locinfo
->lc_handle
[MSVCRT_LC_CTYPE
])
1691 return strncasecmp(s1
, s2
, count
);
1694 c1
= MSVCRT__tolower_l(*s1
++, locale
);
1695 c2
= MSVCRT__tolower_l(*s2
++, locale
);
1696 }while(--count
&& c1
&& c1
==c2
);
1701 /*********************************************************************
1702 * _stricmp_l (MSVCRT.@)
1704 int __cdecl
MSVCRT__stricmp_l(const char *s1
, const char *s2
, MSVCRT__locale_t locale
)
1706 return MSVCRT__strnicmp_l(s1
, s2
, -1, locale
);
1709 /*********************************************************************
1710 * _strnicmp (MSVCRT.@)
1712 int __cdecl
MSVCRT__strnicmp(const char *s1
, const char *s2
, MSVCRT_size_t count
)
1714 return MSVCRT__strnicmp_l(s1
, s2
, count
, NULL
);
1717 /*********************************************************************
1718 * _stricmp (MSVCRT.@)
1720 int __cdecl
MSVCRT__stricmp(const char *s1
, const char *s2
)
1722 return MSVCRT__strnicmp_l(s1
, s2
, -1, NULL
);
1725 /*********************************************************************
1728 char* __cdecl
MSVCRT_strstr(const char *haystack
, const char *needle
)
1730 return strstr(haystack
, needle
);