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"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
38 /*********************************************************************
42 char* CDECL
_strdup(const char* str
)
46 char * ret
= MSVCRT_malloc(strlen(str
)+1);
47 if (ret
) strcpy( ret
, str
);
53 /*********************************************************************
54 * _strlwr_s (MSVCRT.@)
56 int CDECL
_strlwr_s(char *str
, MSVCRT_size_t len
)
62 *MSVCRT__errno() = MSVCRT_EINVAL
;
75 *MSVCRT__errno() = MSVCRT_EINVAL
;
88 /*********************************************************************
91 char* CDECL
MSVCRT__strnset(char* str
, int value
, MSVCRT_size_t len
)
99 /*********************************************************************
102 char* CDECL
_strrev(char* str
)
108 for (p1
= str
, p2
= str
+ strlen(str
) - 1; p2
> p1
; ++p1
, --p2
)
118 /*********************************************************************
121 char* CDECL
_strset(char* str
, int value
)
130 /*********************************************************************
133 char * CDECL
MSVCRT_strtok( char *str
, const char *delim
)
135 thread_data_t
*data
= msvcrt_get_thread_data();
139 if (!(str
= data
->strtok_next
)) return NULL
;
141 while (*str
&& strchr( delim
, *str
)) str
++;
142 if (!*str
) return NULL
;
144 while (*str
&& !strchr( delim
, *str
)) str
++;
145 if (*str
) *str
++ = 0;
146 data
->strtok_next
= str
;
150 /*********************************************************************
151 * strtok_s (MSVCRT.@)
153 char * CDECL
MSVCRT_strtok_s(char *str
, const char *delim
, char **ctx
)
155 if (!MSVCRT_CHECK_PMT(delim
!= NULL
) || !MSVCRT_CHECK_PMT(ctx
!= NULL
) ||
156 !MSVCRT_CHECK_PMT(str
!= NULL
|| *ctx
!= NULL
)) {
157 *MSVCRT__errno() = MSVCRT_EINVAL
;
164 while(*str
&& strchr(delim
, *str
))
170 while(**ctx
&& !strchr(delim
, **ctx
))
178 /*********************************************************************
181 void CDECL
MSVCRT__swab(char* src
, char* dst
, int len
)
185 len
= (unsigned)len
>> 1;
197 /*********************************************************************
198 * strtod_l (MSVCRT.@)
200 double CDECL
MSVCRT_strtod_l( const char *str
, char **end
, MSVCRT__locale_t locale
)
202 unsigned __int64 d
=0, hlp
;
207 BOOL found_digit
= FALSE
;
209 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) {
210 *MSVCRT__errno() = MSVCRT_EINVAL
;
215 locale
= get_locale();
217 /* FIXME: use *_l functions */
230 hlp
= d
*10+*(p
++)-'0';
231 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
) {
242 if(*p
== *locale
->locinfo
->lconv
->decimal_point
)
247 hlp
= d
*10+*(p
++)-'0';
248 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
)
263 if(*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D') {
275 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
281 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
282 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
285 if(*p
=='-' || *p
=='+')
291 fpcontrol
= _control87(0, 0);
292 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
293 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
296 ret
= (double)sign
*d
*pow(10, exp
);
298 ret
= (double)sign
*d
/pow(10, -exp
);
300 _control87(fpcontrol
, 0xffffffff);
302 if((d
&& ret
==0.0) || isinf(ret
))
303 *MSVCRT__errno() = MSVCRT_ERANGE
;
311 /*********************************************************************
314 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
316 return MSVCRT_strtod_l( str
, end
, NULL
);
319 /*********************************************************************
322 double CDECL
MSVCRT_atof( const char *str
)
324 return MSVCRT_strtod_l(str
, NULL
, NULL
);
327 /*********************************************************************
330 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
332 return MSVCRT_strtod_l(str
, NULL
, locale
);
335 /*********************************************************************
336 * _atoflt_l (MSVCRT.@)
338 int CDECL
MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT
*value
, char *str
, MSVCRT__locale_t locale
)
340 unsigned __int64 d
=0, hlp
;
345 BOOL found_digit
= FALSE
;
348 locale
= get_locale();
350 /* FIXME: use *_l functions */
363 hlp
= d
*10+*(p
++)-'0';
364 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
) {
375 if(*p
== *locale
->locinfo
->lconv
->decimal_point
)
380 hlp
= d
*10+*(p
++)-'0';
381 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
)
395 if(*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D') {
407 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
413 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
414 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
417 if(*p
=='-' || *p
=='+')
423 fpcontrol
= _control87(0, 0);
424 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
425 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
428 value
->f
= (float)sign
*d
*powf(10, exp
);
430 value
->f
= (float)sign
*d
/powf(10, -exp
);
432 _control87(fpcontrol
, 0xffffffff);
434 if((d
&& value
->f
==0.0) || isinf(value
->f
))
435 ret
= exp
> 0 ? MSVCRT__OVERFLOW
: MSVCRT__UNDERFLOW
;
440 /*********************************************************************
443 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
445 /* FIXME: handle Windows locale */
446 return strcoll( str1
, str2
);
449 /*********************************************************************
450 * strcpy_s (MSVCRT.@)
452 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
455 if(!elem
) return MSVCRT_EINVAL
;
456 if(!dst
) return MSVCRT_EINVAL
;
460 return MSVCRT_EINVAL
;
463 for(i
= 0; i
< elem
; i
++)
465 if((dst
[i
] = src
[i
]) == '\0') return 0;
468 return MSVCRT_ERANGE
;
471 /*********************************************************************
472 * strcat_s (MSVCRT.@)
474 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
477 if(!dst
) return MSVCRT_EINVAL
;
478 if(elem
== 0) return MSVCRT_EINVAL
;
482 return MSVCRT_EINVAL
;
485 for(i
= 0; i
< elem
; i
++)
489 for(j
= 0; (j
+ i
) < elem
; j
++)
491 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
495 /* Set the first element to 0, not the first element after the skipped part */
497 return MSVCRT_ERANGE
;
500 /*********************************************************************
501 * strncat_s (MSVCRT.@)
503 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
506 if(!MSVCRT_CHECK_PMT(dst
!= 0) || !MSVCRT_CHECK_PMT(elem
!= 0))
507 return MSVCRT_EINVAL
;
508 if(!MSVCRT_CHECK_PMT(src
!= 0))
511 return MSVCRT_EINVAL
;
514 for(i
= 0; i
< elem
; i
++)
518 for(j
= 0; (j
+ i
) < elem
; j
++)
520 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
523 return MSVCRT_STRUNCATE
;
525 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
533 /* Set the first element to 0, not the first element after the skipped part */
535 return MSVCRT_ERANGE
;
538 /*********************************************************************
541 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
543 /* FIXME: handle Windows locale */
544 return strxfrm( dest
, src
, len
);
547 /*********************************************************************
548 * _stricoll (MSVCRT.@)
550 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
552 /* FIXME: handle collates */
553 TRACE("str1 %s str2 %s\n", debugstr_a(str1
), debugstr_a(str2
));
554 return lstrcmpiA( str1
, str2
);
557 /********************************************************************
558 * _atoldbl (MSVCRT.@)
560 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
562 /* FIXME needs error checking for huge/small values */
565 TRACE("str %s value %p\n",str
,value
);
567 memcpy(value
, &ld
, 10);
569 FIXME("stub, str %s value %p\n",str
,value
);
574 /********************************************************************
575 * __STRINGTOLD (MSVCRT.@)
577 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
581 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
583 memcpy(value
, &ld
, 10);
585 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
590 /******************************************************************
593 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
595 /* wrapper to forward libc error code to msvcrt's error codes */
599 ret
= strtol(nptr
, end
, base
);
602 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
603 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
605 /* cope with the fact that we may use 64bit long integers on libc
606 * while msvcrt always uses 32bit long integers
608 if (ret
> MSVCRT_LONG_MAX
)
610 ret
= MSVCRT_LONG_MAX
;
611 *MSVCRT__errno() = MSVCRT_ERANGE
;
613 else if (ret
< -MSVCRT_LONG_MAX
- 1)
615 ret
= -MSVCRT_LONG_MAX
- 1;
616 *MSVCRT__errno() = MSVCRT_ERANGE
;
624 /******************************************************************
627 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
629 /* wrapper to forward libc error code to msvcrt's error codes */
633 ret
= strtoul(nptr
, end
, base
);
636 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
637 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
639 /* cope with the fact that we may use 64bit long integers on libc
640 * while msvcrt always uses 32bit long integers
642 if (ret
> MSVCRT_ULONG_MAX
)
644 ret
= MSVCRT_ULONG_MAX
;
645 *MSVCRT__errno() = MSVCRT_ERANGE
;
653 /******************************************************************
656 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
660 for(i
=0; i
<maxlen
; i
++)
666 /*********************************************************************
667 * _strtoi64_l (MSVCRT.@)
669 * FIXME: locale parameter is ignored
671 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
673 BOOL negative
= FALSE
;
676 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
678 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
) || !MSVCRT_CHECK_PMT(base
== 0 || base
>= 2) ||
679 !MSVCRT_CHECK_PMT(base
<= 36)) {
683 while(isspace(*nptr
)) nptr
++;
688 } else if(*nptr
== '+')
691 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
704 char cur
= tolower(*nptr
);
712 if(cur
<'a' || cur
>='a'+base
-10)
722 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
723 ret
= MSVCRT_I64_MAX
;
724 *MSVCRT__errno() = MSVCRT_ERANGE
;
725 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
726 ret
= MSVCRT_I64_MIN
;
727 *MSVCRT__errno() = MSVCRT_ERANGE
;
733 *endptr
= (char*)nptr
;
738 /*********************************************************************
739 * _strtoi64 (MSVCRT.@)
741 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
743 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
746 /*********************************************************************
747 * _strtoui64_l (MSVCRT.@)
749 * FIXME: locale parameter is ignored
751 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
753 BOOL negative
= FALSE
;
754 unsigned __int64 ret
= 0;
756 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
758 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
) || !MSVCRT_CHECK_PMT(base
== 0 || base
>= 2) ||
759 !MSVCRT_CHECK_PMT(base
<= 36)) {
763 while(isspace(*nptr
)) nptr
++;
768 } else if(*nptr
== '+')
771 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
784 char cur
= tolower(*nptr
);
792 if(cur
<'a' || cur
>='a'+base
-10)
799 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
800 ret
= MSVCRT_UI64_MAX
;
801 *MSVCRT__errno() = MSVCRT_ERANGE
;
807 *endptr
= (char*)nptr
;
809 return negative
? -ret
: ret
;
812 /*********************************************************************
813 * _strtoui64 (MSVCRT.@)
815 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
817 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
820 /*********************************************************************
823 int CDECL
_ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
828 char buffer
[33], *pos
;
831 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
832 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
837 *MSVCRT__errno() = MSVCRT_EINVAL
;
838 return MSVCRT_EINVAL
;
841 if (value
< 0 && radix
== 10)
861 *--pos
= '0' + digit
;
863 *--pos
= 'a' + digit
- 10;
870 len
= buffer
+ 33 - pos
;
876 /* Copy the temporary buffer backwards up to the available number of
877 * characters. Don't copy the negative sign if present. */
885 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
889 MSVCRT_INVALID_PMT("str[size] is too small");
890 *MSVCRT__errno() = MSVCRT_ERANGE
;
891 return MSVCRT_ERANGE
;
894 memcpy(str
, pos
, len
);
898 /*********************************************************************
901 int CDECL
_ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
906 MSVCRT_wchar_t buffer
[33], *pos
;
909 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
910 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
915 *MSVCRT__errno() = MSVCRT_EINVAL
;
916 return MSVCRT_EINVAL
;
919 if (value
< 0 && radix
== 10)
939 *--pos
= '0' + digit
;
941 *--pos
= 'a' + digit
- 10;
948 len
= buffer
+ 33 - pos
;
952 MSVCRT_wchar_t
*p
= str
;
954 /* Copy the temporary buffer backwards up to the available number of
955 * characters. Don't copy the negative sign if present. */
963 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
966 MSVCRT_INVALID_PMT("str[size] is too small");
968 *MSVCRT__errno() = MSVCRT_ERANGE
;
969 return MSVCRT_ERANGE
;
972 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
976 /*********************************************************************
979 int CDECL
_itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
981 return _ltoa_s(value
, str
, size
, radix
);
984 /*********************************************************************
987 int CDECL
_itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
989 return _ltow_s(value
, str
, size
, radix
);
992 /*********************************************************************
993 * _ui64toa_s (MSVCRT.@)
995 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
996 MSVCRT_size_t size
, int radix
)
998 char buffer
[65], *pos
;
1001 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1002 !MSVCRT_CHECK_PMT(radix
>=2) || !MSVCRT_CHECK_PMT(radix
<=36)) {
1003 *MSVCRT__errno() = MSVCRT_EINVAL
;
1004 return MSVCRT_EINVAL
;
1011 digit
= value
%radix
;
1017 *--pos
= 'a'+digit
-10;
1020 if(buffer
-pos
+65 > size
) {
1021 MSVCRT_INVALID_PMT("str[size] is too small");
1022 *MSVCRT__errno() = MSVCRT_EINVAL
;
1023 return MSVCRT_EINVAL
;
1026 memcpy(str
, pos
, buffer
-pos
+65);
1030 /*********************************************************************
1031 * _ui64tow_s (MSVCRT.@)
1033 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1034 MSVCRT_size_t size
, int radix
)
1036 MSVCRT_wchar_t buffer
[65], *pos
;
1039 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1040 !MSVCRT_CHECK_PMT(radix
>=2) || !MSVCRT_CHECK_PMT(radix
<=36)) {
1041 *MSVCRT__errno() = MSVCRT_EINVAL
;
1042 return MSVCRT_EINVAL
;
1049 digit
= value
% radix
;
1050 value
= value
/ radix
;
1052 *--pos
= '0' + digit
;
1054 *--pos
= 'a' + digit
- 10;
1055 } while (value
!= 0);
1057 if(buffer
-pos
+65 > size
) {
1058 MSVCRT_INVALID_PMT("str[size] is too small");
1059 *MSVCRT__errno() = MSVCRT_EINVAL
;
1060 return MSVCRT_EINVAL
;
1063 memcpy(str
, pos
, buffer
-pos
+65);
1067 /*********************************************************************
1068 * _ultoa_s (MSVCRT.@)
1070 int CDECL
_ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1073 char buffer
[33], *pos
;
1076 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1081 *MSVCRT__errno() = MSVCRT_EINVAL
;
1082 return MSVCRT_EINVAL
;
1090 digit
= value
% radix
;
1094 *--pos
= '0' + digit
;
1096 *--pos
= 'a' + digit
- 10;
1100 len
= buffer
+ 33 - pos
;
1106 /* Copy the temporary buffer backwards up to the available number of
1109 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1113 *MSVCRT__errno() = MSVCRT_ERANGE
;
1114 return MSVCRT_ERANGE
;
1117 memcpy(str
, pos
, len
);
1121 /*********************************************************************
1122 * _i64toa_s (MSVCRT.@)
1124 int CDECL
_i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1126 unsigned __int64 val
;
1129 char buffer
[65], *pos
;
1132 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1133 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
1138 *MSVCRT__errno() = MSVCRT_EINVAL
;
1139 return MSVCRT_EINVAL
;
1142 if (value
< 0 && radix
== 10)
1158 digit
= val
% radix
;
1162 *--pos
= '0' + digit
;
1164 *--pos
= 'a' + digit
- 10;
1171 len
= buffer
+ 65 - pos
;
1177 /* Copy the temporary buffer backwards up to the available number of
1178 * characters. Don't copy the negative sign if present. */
1186 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1190 MSVCRT_INVALID_PMT("str[size] is too small");
1191 *MSVCRT__errno() = MSVCRT_ERANGE
;
1192 return MSVCRT_ERANGE
;
1195 memcpy(str
, pos
, len
);
1199 /*********************************************************************
1200 * _i64tow_s (MSVCRT.@)
1202 int CDECL
_i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1204 unsigned __int64 val
;
1207 MSVCRT_wchar_t buffer
[65], *pos
;
1210 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1211 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
1216 *MSVCRT__errno() = MSVCRT_EINVAL
;
1217 return MSVCRT_EINVAL
;
1220 if (value
< 0 && radix
== 10)
1236 digit
= val
% radix
;
1240 *--pos
= '0' + digit
;
1242 *--pos
= 'a' + digit
- 10;
1249 len
= buffer
+ 65 - pos
;
1253 MSVCRT_wchar_t
*p
= str
;
1255 /* Copy the temporary buffer backwards up to the available number of
1256 * characters. Don't copy the negative sign if present. */
1264 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1267 MSVCRT_INVALID_PMT("str[size] is too small");
1269 *MSVCRT__errno() = MSVCRT_ERANGE
;
1270 return MSVCRT_ERANGE
;
1273 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1277 #define I10_OUTPUT_MAX_PREC 21
1278 /* Internal structure used by $I10_OUTPUT */
1279 struct _I10_OUTPUT_DATA
{
1283 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1286 /*********************************************************************
1287 * $I10_OUTPUT (MSVCRT.@)
1288 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1289 * prec - precision of part, we're interested in
1290 * flag - 0 for first prec digits, 1 for fractional part
1291 * data - data to be populated
1294 * 0 if given double is NaN or INF
1298 * Native sets last byte of data->str to '0' or '9', I don't know what
1299 * it means. Current implementation sets it always to '0'.
1301 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1303 static const char inf_str
[] = "1#INF";
1304 static const char nan_str
[] = "1#QNAN";
1306 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1307 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1308 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1312 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1315 memcpy(&ld
, &ld80
, 10);
1317 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1328 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1336 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1342 int exp
= 1+floor(log10(d
));
1350 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1351 prec
= I10_OUTPUT_MAX_PREC
-1;
1357 sprintf(format
, "%%.%dle", prec
);
1358 sprintf(buf
, format
, d
);
1361 data
->pos
= atoi(buf
+prec
+3);
1365 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1368 memcpy(data
->str
, buf
+1, data
->len
);
1369 data
->str
[data
->len
] = '\0';
1371 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1372 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1376 #undef I10_OUTPUT_MAX_PREC