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 * strcpy_s (MSVCRT.@)
572 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
575 if(!elem
) return MSVCRT_EINVAL
;
576 if(!dst
) return MSVCRT_EINVAL
;
580 return MSVCRT_EINVAL
;
583 for(i
= 0; i
< elem
; i
++)
585 if((dst
[i
] = src
[i
]) == '\0') return 0;
588 return MSVCRT_ERANGE
;
591 /*********************************************************************
592 * strcat_s (MSVCRT.@)
594 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
597 if(!dst
) return MSVCRT_EINVAL
;
598 if(elem
== 0) return MSVCRT_EINVAL
;
602 return MSVCRT_EINVAL
;
605 for(i
= 0; i
< elem
; i
++)
609 for(j
= 0; (j
+ i
) < elem
; j
++)
611 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
615 /* Set the first element to 0, not the first element after the skipped part */
617 return MSVCRT_ERANGE
;
620 /*********************************************************************
621 * strncat_s (MSVCRT.@)
623 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
626 if(!MSVCRT_CHECK_PMT(dst
!= 0) || !MSVCRT_CHECK_PMT(elem
!= 0))
627 return MSVCRT_EINVAL
;
628 if(!MSVCRT_CHECK_PMT(src
!= 0))
631 return MSVCRT_EINVAL
;
634 for(i
= 0; i
< elem
; i
++)
638 for(j
= 0; (j
+ i
) < elem
; j
++)
640 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
643 return MSVCRT_STRUNCATE
;
645 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
653 /* Set the first element to 0, not the first element after the skipped part */
655 return MSVCRT_ERANGE
;
658 /*********************************************************************
661 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
663 /* FIXME: handle Windows locale */
664 return strxfrm( dest
, src
, len
);
667 /********************************************************************
668 * _atoldbl (MSVCRT.@)
670 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
672 /* FIXME needs error checking for huge/small values */
675 TRACE("str %s value %p\n",str
,value
);
677 memcpy(value
, &ld
, 10);
679 FIXME("stub, str %s value %p\n",str
,value
);
684 /********************************************************************
685 * __STRINGTOLD (MSVCRT.@)
687 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
691 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
693 memcpy(value
, &ld
, 10);
695 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
700 /******************************************************************
703 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
705 /* wrapper to forward libc error code to msvcrt's error codes */
709 ret
= strtol(nptr
, end
, base
);
712 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
713 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
715 /* cope with the fact that we may use 64bit long integers on libc
716 * while msvcrt always uses 32bit long integers
718 if (ret
> MSVCRT_LONG_MAX
)
720 ret
= MSVCRT_LONG_MAX
;
721 *MSVCRT__errno() = MSVCRT_ERANGE
;
723 else if (ret
< -MSVCRT_LONG_MAX
- 1)
725 ret
= -MSVCRT_LONG_MAX
- 1;
726 *MSVCRT__errno() = MSVCRT_ERANGE
;
734 /******************************************************************
737 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
739 /* wrapper to forward libc error code to msvcrt's error codes */
743 ret
= strtoul(nptr
, end
, base
);
746 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
747 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
749 /* cope with the fact that we may use 64bit long integers on libc
750 * while msvcrt always uses 32bit long integers
752 if (ret
> MSVCRT_ULONG_MAX
)
754 ret
= MSVCRT_ULONG_MAX
;
755 *MSVCRT__errno() = MSVCRT_ERANGE
;
763 /******************************************************************
766 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
770 for(i
=0; i
<maxlen
; i
++)
776 /*********************************************************************
777 * _strtoi64_l (MSVCRT.@)
779 * FIXME: locale parameter is ignored
781 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
783 BOOL negative
= FALSE
;
786 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
788 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
) || !MSVCRT_CHECK_PMT(base
== 0 || base
>= 2) ||
789 !MSVCRT_CHECK_PMT(base
<= 36)) {
793 while(isspace(*nptr
)) nptr
++;
798 } else if(*nptr
== '+')
801 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
814 char cur
= tolower(*nptr
);
822 if(cur
<'a' || cur
>='a'+base
-10)
832 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
833 ret
= MSVCRT_I64_MAX
;
834 *MSVCRT__errno() = MSVCRT_ERANGE
;
835 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
836 ret
= MSVCRT_I64_MIN
;
837 *MSVCRT__errno() = MSVCRT_ERANGE
;
843 *endptr
= (char*)nptr
;
848 /*********************************************************************
849 * _strtoi64 (MSVCRT.@)
851 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
853 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
856 /*********************************************************************
857 * _strtoui64_l (MSVCRT.@)
859 * FIXME: locale parameter is ignored
861 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
863 BOOL negative
= FALSE
;
864 unsigned __int64 ret
= 0;
866 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
868 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
) || !MSVCRT_CHECK_PMT(base
== 0 || base
>= 2) ||
869 !MSVCRT_CHECK_PMT(base
<= 36)) {
873 while(isspace(*nptr
)) nptr
++;
878 } else if(*nptr
== '+')
881 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
894 char cur
= tolower(*nptr
);
902 if(cur
<'a' || cur
>='a'+base
-10)
909 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
910 ret
= MSVCRT_UI64_MAX
;
911 *MSVCRT__errno() = MSVCRT_ERANGE
;
917 *endptr
= (char*)nptr
;
919 return negative
? -ret
: ret
;
922 /*********************************************************************
923 * _strtoui64 (MSVCRT.@)
925 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
927 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
930 /*********************************************************************
933 int CDECL
_ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
938 char buffer
[33], *pos
;
941 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
942 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
947 *MSVCRT__errno() = MSVCRT_EINVAL
;
948 return MSVCRT_EINVAL
;
951 if (value
< 0 && radix
== 10)
971 *--pos
= '0' + digit
;
973 *--pos
= 'a' + digit
- 10;
980 len
= buffer
+ 33 - pos
;
986 /* Copy the temporary buffer backwards up to the available number of
987 * characters. Don't copy the negative sign if present. */
995 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
999 MSVCRT_INVALID_PMT("str[size] is too small");
1000 *MSVCRT__errno() = MSVCRT_ERANGE
;
1001 return MSVCRT_ERANGE
;
1004 memcpy(str
, pos
, len
);
1008 /*********************************************************************
1009 * _ltow_s (MSVCRT.@)
1011 int CDECL
_ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1016 MSVCRT_wchar_t buffer
[33], *pos
;
1019 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1020 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
1025 *MSVCRT__errno() = MSVCRT_EINVAL
;
1026 return MSVCRT_EINVAL
;
1029 if (value
< 0 && radix
== 10)
1045 digit
= val
% radix
;
1049 *--pos
= '0' + digit
;
1051 *--pos
= 'a' + digit
- 10;
1058 len
= buffer
+ 33 - pos
;
1062 MSVCRT_wchar_t
*p
= str
;
1064 /* Copy the temporary buffer backwards up to the available number of
1065 * characters. Don't copy the negative sign if present. */
1073 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1076 MSVCRT_INVALID_PMT("str[size] is too small");
1078 *MSVCRT__errno() = MSVCRT_ERANGE
;
1079 return MSVCRT_ERANGE
;
1082 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1086 /*********************************************************************
1087 * _itoa_s (MSVCRT.@)
1089 int CDECL
_itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
1091 return _ltoa_s(value
, str
, size
, radix
);
1094 /*********************************************************************
1095 * _itow_s (MSVCRT.@)
1097 int CDECL
_itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1099 return _ltow_s(value
, str
, size
, radix
);
1102 /*********************************************************************
1103 * _ui64toa_s (MSVCRT.@)
1105 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
1106 MSVCRT_size_t size
, int radix
)
1108 char buffer
[65], *pos
;
1111 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1112 !MSVCRT_CHECK_PMT(radix
>=2) || !MSVCRT_CHECK_PMT(radix
<=36)) {
1113 *MSVCRT__errno() = MSVCRT_EINVAL
;
1114 return MSVCRT_EINVAL
;
1121 digit
= value
%radix
;
1127 *--pos
= 'a'+digit
-10;
1130 if(buffer
-pos
+65 > size
) {
1131 MSVCRT_INVALID_PMT("str[size] is too small");
1132 *MSVCRT__errno() = MSVCRT_EINVAL
;
1133 return MSVCRT_EINVAL
;
1136 memcpy(str
, pos
, buffer
-pos
+65);
1140 /*********************************************************************
1141 * _ui64tow_s (MSVCRT.@)
1143 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1144 MSVCRT_size_t size
, int radix
)
1146 MSVCRT_wchar_t buffer
[65], *pos
;
1149 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1150 !MSVCRT_CHECK_PMT(radix
>=2) || !MSVCRT_CHECK_PMT(radix
<=36)) {
1151 *MSVCRT__errno() = MSVCRT_EINVAL
;
1152 return MSVCRT_EINVAL
;
1159 digit
= value
% radix
;
1160 value
= value
/ radix
;
1162 *--pos
= '0' + digit
;
1164 *--pos
= 'a' + digit
- 10;
1165 } while (value
!= 0);
1167 if(buffer
-pos
+65 > size
) {
1168 MSVCRT_INVALID_PMT("str[size] is too small");
1169 *MSVCRT__errno() = MSVCRT_EINVAL
;
1170 return MSVCRT_EINVAL
;
1173 memcpy(str
, pos
, buffer
-pos
+65);
1177 /*********************************************************************
1178 * _ultoa_s (MSVCRT.@)
1180 int CDECL
_ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1183 char buffer
[33], *pos
;
1186 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1191 *MSVCRT__errno() = MSVCRT_EINVAL
;
1192 return MSVCRT_EINVAL
;
1200 digit
= value
% radix
;
1204 *--pos
= '0' + digit
;
1206 *--pos
= 'a' + digit
- 10;
1210 len
= buffer
+ 33 - pos
;
1216 /* Copy the temporary buffer backwards up to the available number of
1219 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1223 *MSVCRT__errno() = MSVCRT_ERANGE
;
1224 return MSVCRT_ERANGE
;
1227 memcpy(str
, pos
, len
);
1231 /*********************************************************************
1232 * _i64toa_s (MSVCRT.@)
1234 int CDECL
_i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1236 unsigned __int64 val
;
1239 char buffer
[65], *pos
;
1242 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1243 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
1248 *MSVCRT__errno() = MSVCRT_EINVAL
;
1249 return MSVCRT_EINVAL
;
1252 if (value
< 0 && radix
== 10)
1268 digit
= val
% radix
;
1272 *--pos
= '0' + digit
;
1274 *--pos
= 'a' + digit
- 10;
1281 len
= buffer
+ 65 - pos
;
1287 /* Copy the temporary buffer backwards up to the available number of
1288 * characters. Don't copy the negative sign if present. */
1296 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1300 MSVCRT_INVALID_PMT("str[size] is too small");
1301 *MSVCRT__errno() = MSVCRT_ERANGE
;
1302 return MSVCRT_ERANGE
;
1305 memcpy(str
, pos
, len
);
1309 /*********************************************************************
1310 * _i64tow_s (MSVCRT.@)
1312 int CDECL
_i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1314 unsigned __int64 val
;
1317 MSVCRT_wchar_t buffer
[65], *pos
;
1320 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1321 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
1326 *MSVCRT__errno() = MSVCRT_EINVAL
;
1327 return MSVCRT_EINVAL
;
1330 if (value
< 0 && radix
== 10)
1346 digit
= val
% radix
;
1350 *--pos
= '0' + digit
;
1352 *--pos
= 'a' + digit
- 10;
1359 len
= buffer
+ 65 - pos
;
1363 MSVCRT_wchar_t
*p
= str
;
1365 /* Copy the temporary buffer backwards up to the available number of
1366 * characters. Don't copy the negative sign if present. */
1374 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1377 MSVCRT_INVALID_PMT("str[size] is too small");
1379 *MSVCRT__errno() = MSVCRT_ERANGE
;
1380 return MSVCRT_ERANGE
;
1383 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1387 #define I10_OUTPUT_MAX_PREC 21
1388 /* Internal structure used by $I10_OUTPUT */
1389 struct _I10_OUTPUT_DATA
{
1393 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1396 /*********************************************************************
1397 * $I10_OUTPUT (MSVCRT.@)
1398 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1399 * prec - precision of part, we're interested in
1400 * flag - 0 for first prec digits, 1 for fractional part
1401 * data - data to be populated
1404 * 0 if given double is NaN or INF
1408 * Native sets last byte of data->str to '0' or '9', I don't know what
1409 * it means. Current implementation sets it always to '0'.
1411 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1413 static const char inf_str
[] = "1#INF";
1414 static const char nan_str
[] = "1#QNAN";
1416 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1417 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1418 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1422 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1425 memcpy(&ld
, &ld80
, 10);
1427 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1438 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1446 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1452 int exp
= 1+floor(log10(d
));
1460 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1461 prec
= I10_OUTPUT_MAX_PREC
-1;
1467 sprintf(format
, "%%.%dle", prec
);
1468 sprintf(buf
, format
, d
);
1471 data
->pos
= atoi(buf
+prec
+3);
1475 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1478 memcpy(data
->str
, buf
+1, data
->len
);
1479 data
->str
[data
->len
] = '\0';
1481 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1482 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1486 #undef I10_OUTPUT_MAX_PREC