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;
587 /*********************************************************************
588 * strcpy_s (MSVCRT.@)
590 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
593 if(!elem
) return MSVCRT_EINVAL
;
594 if(!dst
) return MSVCRT_EINVAL
;
598 return MSVCRT_EINVAL
;
601 for(i
= 0; i
< elem
; i
++)
603 if((dst
[i
] = src
[i
]) == '\0') return 0;
606 return MSVCRT_ERANGE
;
609 /*********************************************************************
610 * strcat_s (MSVCRT.@)
612 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
615 if(!dst
) return MSVCRT_EINVAL
;
616 if(elem
== 0) return MSVCRT_EINVAL
;
620 return MSVCRT_EINVAL
;
623 for(i
= 0; i
< elem
; i
++)
627 for(j
= 0; (j
+ i
) < elem
; j
++)
629 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
633 /* Set the first element to 0, not the first element after the skipped part */
635 return MSVCRT_ERANGE
;
638 /*********************************************************************
639 * strncat_s (MSVCRT.@)
641 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
645 if (!MSVCRT_CHECK_PMT(dst
!= 0)) return MSVCRT_EINVAL
;
646 if (!MSVCRT_CHECK_PMT(elem
!= 0)) return MSVCRT_EINVAL
;
647 if (!MSVCRT_CHECK_PMT(src
!= 0))
650 return MSVCRT_EINVAL
;
653 for(i
= 0; i
< elem
; i
++)
657 for(j
= 0; (j
+ i
) < elem
; j
++)
659 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
662 return MSVCRT_STRUNCATE
;
664 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
672 /* Set the first element to 0, not the first element after the skipped part */
674 return MSVCRT_ERANGE
;
677 /*********************************************************************
680 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
682 /* FIXME: handle Windows locale */
683 return strxfrm( dest
, src
, len
);
686 /********************************************************************
687 * _atoldbl (MSVCRT.@)
689 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
691 /* FIXME needs error checking for huge/small values */
694 TRACE("str %s value %p\n",str
,value
);
696 memcpy(value
, &ld
, 10);
698 FIXME("stub, str %s value %p\n",str
,value
);
703 /********************************************************************
704 * __STRINGTOLD (MSVCRT.@)
706 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
710 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
712 memcpy(value
, &ld
, 10);
714 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
719 /******************************************************************
722 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
724 /* wrapper to forward libc error code to msvcrt's error codes */
728 ret
= strtol(nptr
, end
, base
);
731 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
732 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
734 /* cope with the fact that we may use 64bit long integers on libc
735 * while msvcrt always uses 32bit long integers
737 if (ret
> MSVCRT_LONG_MAX
)
739 ret
= MSVCRT_LONG_MAX
;
740 *MSVCRT__errno() = MSVCRT_ERANGE
;
742 else if (ret
< -MSVCRT_LONG_MAX
- 1)
744 ret
= -MSVCRT_LONG_MAX
- 1;
745 *MSVCRT__errno() = MSVCRT_ERANGE
;
753 /******************************************************************
756 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
758 /* wrapper to forward libc error code to msvcrt's error codes */
762 ret
= strtoul(nptr
, end
, base
);
765 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
766 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
768 /* cope with the fact that we may use 64bit long integers on libc
769 * while msvcrt always uses 32bit long integers
771 if (ret
> MSVCRT_ULONG_MAX
)
773 ret
= MSVCRT_ULONG_MAX
;
774 *MSVCRT__errno() = MSVCRT_ERANGE
;
782 /******************************************************************
785 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
789 for(i
=0; i
<maxlen
; i
++)
795 /*********************************************************************
796 * _strtoi64_l (MSVCRT.@)
798 * FIXME: locale parameter is ignored
800 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
802 BOOL negative
= FALSE
;
805 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
807 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
808 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
809 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
811 while(isspace(*nptr
)) nptr
++;
816 } else if(*nptr
== '+')
819 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
832 char cur
= tolower(*nptr
);
840 if(cur
<'a' || cur
>='a'+base
-10)
850 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
851 ret
= MSVCRT_I64_MAX
;
852 *MSVCRT__errno() = MSVCRT_ERANGE
;
853 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
854 ret
= MSVCRT_I64_MIN
;
855 *MSVCRT__errno() = MSVCRT_ERANGE
;
861 *endptr
= (char*)nptr
;
866 /*********************************************************************
867 * _strtoi64 (MSVCRT.@)
869 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
871 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
874 /*********************************************************************
877 int __cdecl
MSVCRT__atoi_l(const char *str
, MSVCRT__locale_t locale
)
879 __int64 ret
= MSVCRT_strtoi64_l(str
, NULL
, 10, locale
);
883 *MSVCRT__errno() = MSVCRT_ERANGE
;
884 } else if(ret
< INT_MIN
) {
886 *MSVCRT__errno() = MSVCRT_ERANGE
;
891 /*********************************************************************
894 int __cdecl
MSVCRT_atoi(const char *str
)
896 return MSVCRT__atoi_l(str
, NULL
);
899 /*********************************************************************
900 * _strtoui64_l (MSVCRT.@)
902 * FIXME: locale parameter is ignored
904 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
906 BOOL negative
= FALSE
;
907 unsigned __int64 ret
= 0;
909 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
911 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
912 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
913 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
915 while(isspace(*nptr
)) nptr
++;
920 } else if(*nptr
== '+')
923 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
936 char cur
= tolower(*nptr
);
944 if(cur
<'a' || cur
>='a'+base
-10)
951 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
952 ret
= MSVCRT_UI64_MAX
;
953 *MSVCRT__errno() = MSVCRT_ERANGE
;
959 *endptr
= (char*)nptr
;
961 return negative
? -ret
: ret
;
964 /*********************************************************************
965 * _strtoui64 (MSVCRT.@)
967 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
969 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
972 /*********************************************************************
975 int CDECL
_ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
980 char buffer
[33], *pos
;
983 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
984 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
985 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
988 return MSVCRT_EINVAL
;
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 * _ltow_s (MSVCRT.@)
1050 int CDECL
_ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1055 MSVCRT_wchar_t buffer
[33], *pos
;
1058 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1059 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1060 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1063 return MSVCRT_EINVAL
;
1066 if (value
< 0 && radix
== 10)
1082 digit
= val
% radix
;
1086 *--pos
= '0' + digit
;
1088 *--pos
= 'a' + digit
- 10;
1095 len
= buffer
+ 33 - pos
;
1099 MSVCRT_wchar_t
*p
= str
;
1101 /* Copy the temporary buffer backwards up to the available number of
1102 * characters. Don't copy the negative sign if present. */
1110 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1114 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1115 return MSVCRT_ERANGE
;
1118 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1122 /*********************************************************************
1123 * _itoa_s (MSVCRT.@)
1125 int CDECL
_itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
1127 return _ltoa_s(value
, str
, size
, radix
);
1130 /*********************************************************************
1131 * _itow_s (MSVCRT.@)
1133 int CDECL
_itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1135 return _ltow_s(value
, str
, size
, radix
);
1138 /*********************************************************************
1139 * _ui64toa_s (MSVCRT.@)
1141 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
1142 MSVCRT_size_t size
, int radix
)
1144 char buffer
[65], *pos
;
1147 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1148 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1149 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1152 return MSVCRT_EINVAL
;
1159 digit
= value
%radix
;
1165 *--pos
= 'a'+digit
-10;
1168 if(buffer
-pos
+65 > size
) {
1169 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1170 return MSVCRT_EINVAL
;
1173 memcpy(str
, pos
, buffer
-pos
+65);
1177 /*********************************************************************
1178 * _ui64tow_s (MSVCRT.@)
1180 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1181 MSVCRT_size_t size
, int radix
)
1183 MSVCRT_wchar_t buffer
[65], *pos
;
1186 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1187 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1188 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1191 return MSVCRT_EINVAL
;
1198 digit
= value
% radix
;
1199 value
= value
/ radix
;
1201 *--pos
= '0' + digit
;
1203 *--pos
= 'a' + digit
- 10;
1204 } while (value
!= 0);
1206 if(buffer
-pos
+65 > size
) {
1207 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1208 return MSVCRT_EINVAL
;
1211 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(MSVCRT_wchar_t
));
1215 /*********************************************************************
1216 * _ultoa_s (MSVCRT.@)
1218 int CDECL
_ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1221 char buffer
[33], *pos
;
1224 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1229 *MSVCRT__errno() = MSVCRT_EINVAL
;
1230 return MSVCRT_EINVAL
;
1238 digit
= value
% radix
;
1242 *--pos
= '0' + digit
;
1244 *--pos
= 'a' + digit
- 10;
1248 len
= buffer
+ 33 - pos
;
1254 /* Copy the temporary buffer backwards up to the available number of
1257 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1261 *MSVCRT__errno() = MSVCRT_ERANGE
;
1262 return MSVCRT_ERANGE
;
1265 memcpy(str
, pos
, len
);
1269 /*********************************************************************
1270 * _ultow_s (MSVCRT.@)
1272 int CDECL
_ultow_s(MSVCRT_ulong value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1275 WCHAR buffer
[33], *pos
;
1278 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1283 *MSVCRT__errno() = MSVCRT_EINVAL
;
1284 return MSVCRT_EINVAL
;
1292 digit
= value
% radix
;
1296 *--pos
= '0' + digit
;
1298 *--pos
= 'a' + digit
- 10;
1302 len
= buffer
+ 33 - pos
;
1308 /* Copy the temporary buffer backwards up to the available number of
1311 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1315 *MSVCRT__errno() = MSVCRT_ERANGE
;
1316 return MSVCRT_ERANGE
;
1319 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1323 /*********************************************************************
1324 * _i64toa_s (MSVCRT.@)
1326 int CDECL
_i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1328 unsigned __int64 val
;
1331 char buffer
[65], *pos
;
1334 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1335 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1336 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1339 return MSVCRT_EINVAL
;
1342 if (value
< 0 && radix
== 10)
1358 digit
= val
% radix
;
1362 *--pos
= '0' + digit
;
1364 *--pos
= 'a' + digit
- 10;
1371 len
= buffer
+ 65 - pos
;
1377 /* Copy the temporary buffer backwards up to the available number of
1378 * characters. Don't copy the negative sign if present. */
1386 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1390 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1391 return MSVCRT_ERANGE
;
1394 memcpy(str
, pos
, len
);
1398 /*********************************************************************
1399 * _i64tow_s (MSVCRT.@)
1401 int CDECL
_i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1403 unsigned __int64 val
;
1406 MSVCRT_wchar_t buffer
[65], *pos
;
1409 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1410 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1411 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1414 return MSVCRT_EINVAL
;
1417 if (value
< 0 && radix
== 10)
1433 digit
= val
% radix
;
1437 *--pos
= '0' + digit
;
1439 *--pos
= 'a' + digit
- 10;
1446 len
= buffer
+ 65 - pos
;
1450 MSVCRT_wchar_t
*p
= str
;
1452 /* Copy the temporary buffer backwards up to the available number of
1453 * characters. Don't copy the negative sign if present. */
1461 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1465 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1466 return MSVCRT_ERANGE
;
1469 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1473 #define I10_OUTPUT_MAX_PREC 21
1474 /* Internal structure used by $I10_OUTPUT */
1475 struct _I10_OUTPUT_DATA
{
1479 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1482 /*********************************************************************
1483 * $I10_OUTPUT (MSVCRT.@)
1484 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1485 * prec - precision of part, we're interested in
1486 * flag - 0 for first prec digits, 1 for fractional part
1487 * data - data to be populated
1490 * 0 if given double is NaN or INF
1494 * Native sets last byte of data->str to '0' or '9', I don't know what
1495 * it means. Current implementation sets it always to '0'.
1497 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1499 static const char inf_str
[] = "1#INF";
1500 static const char nan_str
[] = "1#QNAN";
1502 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1503 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1504 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1508 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1511 memcpy(&ld
, &ld80
, 10);
1513 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1524 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1532 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1538 int exp
= 1+floor(log10(d
));
1546 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1547 prec
= I10_OUTPUT_MAX_PREC
-1;
1553 sprintf(format
, "%%.%dle", prec
);
1554 sprintf(buf
, format
, d
);
1557 data
->pos
= atoi(buf
+prec
+3);
1561 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1564 memcpy(data
->str
, buf
+1, data
->len
);
1565 data
->str
[data
->len
] = '\0';
1567 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1568 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1572 #undef I10_OUTPUT_MAX_PREC
1574 /*********************************************************************
1577 void * __cdecl
MSVCRT_memcpy( void *dst
, const void *src
, size_t n
)
1579 return memmove( dst
, src
, n
);
1582 /*********************************************************************
1585 void* __cdecl
MSVCRT_memset(void *dst
, int c
, MSVCRT_size_t n
)
1587 return memset(dst
, c
, n
);
1590 /*********************************************************************
1593 char* __cdecl
MSVCRT_strchr(const char *str
, int c
)
1595 return strchr(str
, c
);
1598 /*********************************************************************
1599 * strncmp (MSVCRT.@)
1601 int __cdecl
MSVCRT_strncmp(const char *str1
, const char *str2
, MSVCRT_size_t len
)
1603 return strncmp(str1
, str2
, len
);
1606 /*********************************************************************
1607 * _strnicmp_l (MSVCRT.@)
1609 int __cdecl
MSVCRT__strnicmp_l(const char *s1
, const char *s2
,
1610 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1612 MSVCRT_pthreadlocinfo locinfo
;
1615 if(s1
==NULL
|| s2
==NULL
)
1616 return MSVCRT__NLSCMPERROR
;
1622 locinfo
= get_locinfo();
1624 locinfo
= locale
->locinfo
;
1626 if(!locinfo
->lc_handle
[MSVCRT_LC_CTYPE
])
1627 return strncasecmp(s1
, s2
, count
);
1630 c1
= MSVCRT__tolower_l(*s1
++, locale
);
1631 c2
= MSVCRT__tolower_l(*s2
++, locale
);
1632 }while(--count
&& c1
&& c1
==c2
);
1637 /*********************************************************************
1638 * _stricmp_l (MSVCRT.@)
1640 int __cdecl
MSVCRT__stricmp_l(const char *s1
, const char *s2
, MSVCRT__locale_t locale
)
1642 return MSVCRT__strnicmp_l(s1
, s2
, -1, locale
);
1645 /*********************************************************************
1646 * _strnicmp (MSVCRT.@)
1648 int __cdecl
MSVCRT__strnicmp(const char *s1
, const char *s2
, MSVCRT_size_t count
)
1650 return MSVCRT__strnicmp_l(s1
, s2
, count
, NULL
);
1653 /*********************************************************************
1654 * _stricmp (MSVCRT.@)
1656 int __cdecl
MSVCRT__stricmp(const char *s1
, const char *s2
)
1658 return MSVCRT__strnicmp_l(s1
, s2
, -1, NULL
);