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
_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
)
62 locale
= get_locale();
66 *MSVCRT__errno() = MSVCRT_EINVAL
;
79 *MSVCRT__errno() = MSVCRT_EINVAL
;
85 *str
= MSVCRT__tolower_l(*str
, locale
);
92 /*********************************************************************
93 * _strlwr_s (MSVCRT.@)
95 int CDECL
_strlwr_s(char *str
, MSVCRT_size_t len
)
97 return _strlwr_s_l(str
, len
, NULL
);
100 /*********************************************************************
101 * _strlwr_l (MSVCRT.@)
103 int CDECL
_strlwr_l(char *str
, MSVCRT__locale_t locale
)
105 return _strlwr_s_l(str
, -1, locale
);
108 /*********************************************************************
111 int CDECL
_strlwr(char *str
)
113 return _strlwr_s_l(str
, -1, NULL
);
116 /*********************************************************************
117 * _strupr_s_l (MSVCRT.@)
119 int CDECL
_strupr_s_l(char *str
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
124 locale
= get_locale();
128 *MSVCRT__errno() = MSVCRT_EINVAL
;
129 return MSVCRT_EINVAL
;
141 *MSVCRT__errno() = MSVCRT_EINVAL
;
142 return MSVCRT_EINVAL
;
147 *str
= MSVCRT__toupper_l(*str
, locale
);
154 /*********************************************************************
155 * _strupr_s (MSVCRT.@)
157 int CDECL
_strupr_s(char *str
, MSVCRT_size_t len
)
159 return _strupr_s_l(str
, len
, NULL
);
162 /*********************************************************************
163 * _strupr_l (MSVCRT.@)
165 int CDECL
_strupr_l(char *str
, MSVCRT__locale_t locale
)
167 return _strupr_s_l(str
, -1, locale
);
170 /*********************************************************************
173 int CDECL
_strupr(char *str
)
175 return _strupr_s_l(str
, -1, NULL
);
178 /*********************************************************************
179 * _strnset (MSVCRT.@)
181 char* CDECL
MSVCRT__strnset(char* str
, int value
, MSVCRT_size_t len
)
184 while (*str
&& len
--)
189 /*********************************************************************
192 char* CDECL
_strrev(char* str
)
198 for (p1
= str
, p2
= str
+ strlen(str
) - 1; p2
> p1
; ++p1
, --p2
)
208 /*********************************************************************
211 char* CDECL
_strset(char* str
, int value
)
220 /*********************************************************************
223 char * CDECL
MSVCRT_strtok( char *str
, const char *delim
)
225 thread_data_t
*data
= msvcrt_get_thread_data();
229 if (!(str
= data
->strtok_next
)) return NULL
;
231 while (*str
&& strchr( delim
, *str
)) str
++;
232 if (!*str
) return NULL
;
234 while (*str
&& !strchr( delim
, *str
)) str
++;
235 if (*str
) *str
++ = 0;
236 data
->strtok_next
= str
;
240 /*********************************************************************
241 * strtok_s (MSVCRT.@)
243 char * CDECL
MSVCRT_strtok_s(char *str
, const char *delim
, char **ctx
)
245 if (!MSVCRT_CHECK_PMT(delim
!= NULL
) || !MSVCRT_CHECK_PMT(ctx
!= NULL
) ||
246 !MSVCRT_CHECK_PMT(str
!= NULL
|| *ctx
!= NULL
)) {
247 *MSVCRT__errno() = MSVCRT_EINVAL
;
254 while(*str
&& strchr(delim
, *str
))
260 while(**ctx
&& !strchr(delim
, **ctx
))
268 /*********************************************************************
271 void CDECL
MSVCRT__swab(char* src
, char* dst
, int len
)
275 len
= (unsigned)len
>> 1;
287 /*********************************************************************
288 * strtod_l (MSVCRT.@)
290 double CDECL
MSVCRT_strtod_l( const char *str
, char **end
, MSVCRT__locale_t locale
)
292 unsigned __int64 d
=0, hlp
;
297 BOOL found_digit
= FALSE
;
299 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) {
300 *MSVCRT__errno() = MSVCRT_EINVAL
;
305 locale
= get_locale();
307 /* FIXME: use *_l functions */
320 hlp
= d
*10+*(p
++)-'0';
321 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
) {
332 if(*p
== *locale
->locinfo
->lconv
->decimal_point
)
337 hlp
= d
*10+*(p
++)-'0';
338 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
)
353 if(*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D') {
365 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
371 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
372 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
375 if(*p
=='-' || *p
=='+')
381 fpcontrol
= _control87(0, 0);
382 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
383 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
386 ret
= (double)sign
*d
*pow(10, exp
);
388 ret
= (double)sign
*d
/pow(10, -exp
);
390 _control87(fpcontrol
, 0xffffffff);
392 if((d
&& ret
==0.0) || isinf(ret
))
393 *MSVCRT__errno() = MSVCRT_ERANGE
;
401 /*********************************************************************
404 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
406 return MSVCRT_strtod_l( str
, end
, NULL
);
409 /*********************************************************************
412 double CDECL
MSVCRT_atof( const char *str
)
414 return MSVCRT_strtod_l(str
, NULL
, NULL
);
417 /*********************************************************************
420 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
422 return MSVCRT_strtod_l(str
, NULL
, locale
);
425 /*********************************************************************
426 * _atoflt_l (MSVCRT.@)
428 int CDECL
MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT
*value
, char *str
, MSVCRT__locale_t locale
)
430 unsigned __int64 d
=0, hlp
;
435 BOOL found_digit
= FALSE
;
438 locale
= get_locale();
440 /* FIXME: use *_l functions */
453 hlp
= d
*10+*(p
++)-'0';
454 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
) {
465 if(*p
== *locale
->locinfo
->lconv
->decimal_point
)
470 hlp
= d
*10+*(p
++)-'0';
471 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
)
485 if(*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D') {
497 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
503 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
504 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
507 if(*p
=='-' || *p
=='+')
513 fpcontrol
= _control87(0, 0);
514 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
515 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
518 value
->f
= (float)sign
*d
*powf(10, exp
);
520 value
->f
= (float)sign
*d
/powf(10, -exp
);
522 _control87(fpcontrol
, 0xffffffff);
524 if((d
&& value
->f
==0.0) || isinf(value
->f
))
525 ret
= exp
> 0 ? MSVCRT__OVERFLOW
: MSVCRT__UNDERFLOW
;
530 /*********************************************************************
531 * _strcoll_l (MSVCRT.@)
533 int CDECL
MSVCRT_strcoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
536 locale
= get_locale();
538 return CompareStringA(locale
->locinfo
->lc_handle
[MSVCRT_LC_CTYPE
], 0, str1
, -1, str2
, -1)-2;
541 /*********************************************************************
544 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
546 return MSVCRT_strcoll_l(str1
, str2
, NULL
);
549 /*********************************************************************
550 * _stricoll_l (MSVCRT.@)
552 int CDECL
MSVCRT__stricoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
555 locale
= get_locale();
557 return CompareStringA(locale
->locinfo
->lc_handle
[MSVCRT_LC_CTYPE
], NORM_IGNORECASE
,
558 str1
, -1, str2
, -1)-2;
561 /*********************************************************************
562 * _stricoll (MSVCRT.@)
564 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
566 return MSVCRT__stricoll_l(str1
, str2
, NULL
);
569 /*********************************************************************
570 * _strncoll_l (MSVCRT.@)
572 int CDECL
MSVCRT_strncoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
575 locale
= get_locale();
577 return CompareStringA(locale
->locinfo
->lc_handle
[MSVCRT_LC_CTYPE
], 0, str1
, count
, str2
, count
)-2;
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 * strcpy_s (MSVCRT.@)
591 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
594 if(!elem
) return MSVCRT_EINVAL
;
595 if(!dst
) return MSVCRT_EINVAL
;
599 return MSVCRT_EINVAL
;
602 for(i
= 0; i
< elem
; i
++)
604 if((dst
[i
] = src
[i
]) == '\0') return 0;
607 return MSVCRT_ERANGE
;
610 /*********************************************************************
611 * strcat_s (MSVCRT.@)
613 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
616 if(!dst
) return MSVCRT_EINVAL
;
617 if(elem
== 0) return MSVCRT_EINVAL
;
621 return MSVCRT_EINVAL
;
624 for(i
= 0; i
< elem
; i
++)
628 for(j
= 0; (j
+ i
) < elem
; j
++)
630 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
634 /* Set the first element to 0, not the first element after the skipped part */
636 return MSVCRT_ERANGE
;
639 /*********************************************************************
640 * strncat_s (MSVCRT.@)
642 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) || !MSVCRT_CHECK_PMT(elem
!= 0))
646 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", nptr
, endptr
, base
, locale
);
807 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
) || !MSVCRT_CHECK_PMT(base
== 0 || base
>= 2) ||
808 !MSVCRT_CHECK_PMT(base
<= 36)) {
812 while(isspace(*nptr
)) nptr
++;
817 } else if(*nptr
== '+')
820 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
833 char cur
= tolower(*nptr
);
841 if(cur
<'a' || cur
>='a'+base
-10)
851 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
852 ret
= MSVCRT_I64_MAX
;
853 *MSVCRT__errno() = MSVCRT_ERANGE
;
854 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
855 ret
= MSVCRT_I64_MIN
;
856 *MSVCRT__errno() = MSVCRT_ERANGE
;
862 *endptr
= (char*)nptr
;
867 /*********************************************************************
868 * _strtoi64 (MSVCRT.@)
870 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
872 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
875 /*********************************************************************
876 * _strtoui64_l (MSVCRT.@)
878 * FIXME: locale parameter is ignored
880 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
882 BOOL negative
= FALSE
;
883 unsigned __int64 ret
= 0;
885 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
887 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
) || !MSVCRT_CHECK_PMT(base
== 0 || base
>= 2) ||
888 !MSVCRT_CHECK_PMT(base
<= 36)) {
892 while(isspace(*nptr
)) nptr
++;
897 } else if(*nptr
== '+')
900 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
913 char cur
= tolower(*nptr
);
921 if(cur
<'a' || cur
>='a'+base
-10)
928 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
929 ret
= MSVCRT_UI64_MAX
;
930 *MSVCRT__errno() = MSVCRT_ERANGE
;
936 *endptr
= (char*)nptr
;
938 return negative
? -ret
: ret
;
941 /*********************************************************************
942 * _strtoui64 (MSVCRT.@)
944 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
946 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
949 /*********************************************************************
952 int CDECL
_ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
957 char buffer
[33], *pos
;
960 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
961 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
966 *MSVCRT__errno() = MSVCRT_EINVAL
;
967 return MSVCRT_EINVAL
;
970 if (value
< 0 && radix
== 10)
990 *--pos
= '0' + digit
;
992 *--pos
= 'a' + digit
- 10;
999 len
= buffer
+ 33 - pos
;
1005 /* Copy the temporary buffer backwards up to the available number of
1006 * characters. Don't copy the negative sign if present. */
1014 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1018 MSVCRT_INVALID_PMT("str[size] is too small");
1019 *MSVCRT__errno() = MSVCRT_ERANGE
;
1020 return MSVCRT_ERANGE
;
1023 memcpy(str
, pos
, len
);
1027 /*********************************************************************
1028 * _ltow_s (MSVCRT.@)
1030 int CDECL
_ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1035 MSVCRT_wchar_t buffer
[33], *pos
;
1038 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1039 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
1044 *MSVCRT__errno() = MSVCRT_EINVAL
;
1045 return MSVCRT_EINVAL
;
1048 if (value
< 0 && radix
== 10)
1064 digit
= val
% radix
;
1068 *--pos
= '0' + digit
;
1070 *--pos
= 'a' + digit
- 10;
1077 len
= buffer
+ 33 - pos
;
1081 MSVCRT_wchar_t
*p
= str
;
1083 /* Copy the temporary buffer backwards up to the available number of
1084 * characters. Don't copy the negative sign if present. */
1092 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1095 MSVCRT_INVALID_PMT("str[size] is too small");
1097 *MSVCRT__errno() = MSVCRT_ERANGE
;
1098 return MSVCRT_ERANGE
;
1101 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1105 /*********************************************************************
1106 * _itoa_s (MSVCRT.@)
1108 int CDECL
_itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
1110 return _ltoa_s(value
, str
, size
, radix
);
1113 /*********************************************************************
1114 * _itow_s (MSVCRT.@)
1116 int CDECL
_itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1118 return _ltow_s(value
, str
, size
, radix
);
1121 /*********************************************************************
1122 * _ui64toa_s (MSVCRT.@)
1124 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
1125 MSVCRT_size_t size
, int radix
)
1127 char buffer
[65], *pos
;
1130 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1131 !MSVCRT_CHECK_PMT(radix
>=2) || !MSVCRT_CHECK_PMT(radix
<=36)) {
1132 *MSVCRT__errno() = MSVCRT_EINVAL
;
1133 return MSVCRT_EINVAL
;
1140 digit
= value
%radix
;
1146 *--pos
= 'a'+digit
-10;
1149 if(buffer
-pos
+65 > size
) {
1150 MSVCRT_INVALID_PMT("str[size] is too small");
1151 *MSVCRT__errno() = MSVCRT_EINVAL
;
1152 return MSVCRT_EINVAL
;
1155 memcpy(str
, pos
, buffer
-pos
+65);
1159 /*********************************************************************
1160 * _ui64tow_s (MSVCRT.@)
1162 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1163 MSVCRT_size_t size
, int radix
)
1165 MSVCRT_wchar_t buffer
[65], *pos
;
1168 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1169 !MSVCRT_CHECK_PMT(radix
>=2) || !MSVCRT_CHECK_PMT(radix
<=36)) {
1170 *MSVCRT__errno() = MSVCRT_EINVAL
;
1171 return MSVCRT_EINVAL
;
1178 digit
= value
% radix
;
1179 value
= value
/ radix
;
1181 *--pos
= '0' + digit
;
1183 *--pos
= 'a' + digit
- 10;
1184 } while (value
!= 0);
1186 if(buffer
-pos
+65 > size
) {
1187 MSVCRT_INVALID_PMT("str[size] is too small");
1188 *MSVCRT__errno() = MSVCRT_EINVAL
;
1189 return MSVCRT_EINVAL
;
1192 memcpy(str
, pos
, buffer
-pos
+65);
1196 /*********************************************************************
1197 * _ultoa_s (MSVCRT.@)
1199 int CDECL
_ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1202 char buffer
[33], *pos
;
1205 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1210 *MSVCRT__errno() = MSVCRT_EINVAL
;
1211 return MSVCRT_EINVAL
;
1219 digit
= value
% radix
;
1223 *--pos
= '0' + digit
;
1225 *--pos
= 'a' + digit
- 10;
1229 len
= buffer
+ 33 - pos
;
1235 /* Copy the temporary buffer backwards up to the available number of
1238 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1242 *MSVCRT__errno() = MSVCRT_ERANGE
;
1243 return MSVCRT_ERANGE
;
1246 memcpy(str
, pos
, len
);
1250 /*********************************************************************
1251 * _i64toa_s (MSVCRT.@)
1253 int CDECL
_i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1255 unsigned __int64 val
;
1258 char buffer
[65], *pos
;
1261 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1262 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
1267 *MSVCRT__errno() = MSVCRT_EINVAL
;
1268 return MSVCRT_EINVAL
;
1271 if (value
< 0 && radix
== 10)
1287 digit
= val
% radix
;
1291 *--pos
= '0' + digit
;
1293 *--pos
= 'a' + digit
- 10;
1300 len
= buffer
+ 65 - pos
;
1306 /* Copy the temporary buffer backwards up to the available number of
1307 * characters. Don't copy the negative sign if present. */
1315 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1319 MSVCRT_INVALID_PMT("str[size] is too small");
1320 *MSVCRT__errno() = MSVCRT_ERANGE
;
1321 return MSVCRT_ERANGE
;
1324 memcpy(str
, pos
, len
);
1328 /*********************************************************************
1329 * _i64tow_s (MSVCRT.@)
1331 int CDECL
_i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1333 unsigned __int64 val
;
1336 MSVCRT_wchar_t buffer
[65], *pos
;
1339 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1340 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
1345 *MSVCRT__errno() = MSVCRT_EINVAL
;
1346 return MSVCRT_EINVAL
;
1349 if (value
< 0 && radix
== 10)
1365 digit
= val
% radix
;
1369 *--pos
= '0' + digit
;
1371 *--pos
= 'a' + digit
- 10;
1378 len
= buffer
+ 65 - pos
;
1382 MSVCRT_wchar_t
*p
= str
;
1384 /* Copy the temporary buffer backwards up to the available number of
1385 * characters. Don't copy the negative sign if present. */
1393 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1396 MSVCRT_INVALID_PMT("str[size] is too small");
1398 *MSVCRT__errno() = MSVCRT_ERANGE
;
1399 return MSVCRT_ERANGE
;
1402 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1406 #define I10_OUTPUT_MAX_PREC 21
1407 /* Internal structure used by $I10_OUTPUT */
1408 struct _I10_OUTPUT_DATA
{
1412 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1415 /*********************************************************************
1416 * $I10_OUTPUT (MSVCRT.@)
1417 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1418 * prec - precision of part, we're interested in
1419 * flag - 0 for first prec digits, 1 for fractional part
1420 * data - data to be populated
1423 * 0 if given double is NaN or INF
1427 * Native sets last byte of data->str to '0' or '9', I don't know what
1428 * it means. Current implementation sets it always to '0'.
1430 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1432 static const char inf_str
[] = "1#INF";
1433 static const char nan_str
[] = "1#QNAN";
1435 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1436 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1437 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1441 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1444 memcpy(&ld
, &ld80
, 10);
1446 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1457 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1465 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1471 int exp
= 1+floor(log10(d
));
1479 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1480 prec
= I10_OUTPUT_MAX_PREC
-1;
1486 sprintf(format
, "%%.%dle", prec
);
1487 sprintf(buf
, format
, d
);
1490 data
->pos
= atoi(buf
+prec
+3);
1494 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1497 memcpy(data
->str
, buf
+1, data
->len
);
1498 data
->str
[data
->len
] = '\0';
1500 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1501 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1505 #undef I10_OUTPUT_MAX_PREC