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 char* CDECL _strlwr_l(char *str, MSVCRT__locale_t locale)
105 _strlwr_s_l(str, -1, locale);
109 /*********************************************************************
112 char* CDECL _strlwr(char *str)
114 _strlwr_s_l(str, -1, NULL);
118 /*********************************************************************
119 * _strupr_s_l (MSVCRT.@)
121 int CDECL _strupr_s_l(char *str, MSVCRT_size_t len, MSVCRT__locale_t locale)
126 locale = get_locale();
130 *MSVCRT__errno() = MSVCRT_EINVAL;
131 return MSVCRT_EINVAL;
143 *MSVCRT__errno() = MSVCRT_EINVAL;
144 return MSVCRT_EINVAL;
149 *str = MSVCRT__toupper_l(*str, locale);
156 /*********************************************************************
157 * _strupr_s (MSVCRT.@)
159 int CDECL _strupr_s(char *str, MSVCRT_size_t len)
161 return _strupr_s_l(str, len, NULL);
164 /*********************************************************************
165 * _strupr_l (MSVCRT.@)
167 char* CDECL _strupr_l(char *str, MSVCRT__locale_t locale)
169 _strupr_s_l(str, -1, locale);
173 /*********************************************************************
176 char* CDECL _strupr(char *str)
178 _strupr_s_l(str, -1, NULL);
182 /*********************************************************************
183 * _strnset (MSVCRT.@)
185 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
188 while (*str && len--)
193 /*********************************************************************
196 char* CDECL _strrev(char* str)
202 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
212 /*********************************************************************
215 char* CDECL _strset(char* str, int value)
224 /*********************************************************************
227 char * CDECL MSVCRT_strtok( char *str, const char *delim )
229 thread_data_t *data = msvcrt_get_thread_data();
233 if (!(str = data->strtok_next)) return NULL;
235 while (*str && strchr( delim, *str )) str++;
236 if (!*str) return NULL;
238 while (*str && !strchr( delim, *str )) str++;
239 if (*str) *str++ = 0;
240 data->strtok_next = str;
244 /*********************************************************************
245 * strtok_s (MSVCRT.@)
247 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
249 if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(ctx != NULL) ||
250 !MSVCRT_CHECK_PMT(str != NULL || *ctx != NULL)) {
251 *MSVCRT__errno() = MSVCRT_EINVAL;
258 while(*str && strchr(delim, *str))
264 while(**ctx && !strchr(delim, **ctx))
272 /*********************************************************************
275 void CDECL MSVCRT__swab(char* src, char* dst, int len)
279 len = (unsigned)len >> 1;
291 /*********************************************************************
292 * strtod_l (MSVCRT.@)
294 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
296 unsigned __int64 d=0, hlp;
301 BOOL found_digit = FALSE;
303 if (!MSVCRT_CHECK_PMT(str != NULL)) {
304 *MSVCRT__errno() = MSVCRT_EINVAL;
309 locale = get_locale();
311 /* FIXME: use *_l functions */
324 hlp = d*10+*(p++)-'0';
325 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
336 if(*p == *locale->locinfo->lconv->decimal_point)
341 hlp = d*10+*(p++)-'0';
342 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
357 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
369 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
375 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
376 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
379 if(*p=='-' || *p=='+')
385 fpcontrol = _control87(0, 0);
386 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
387 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
390 ret = (double)sign*d*pow(10, exp);
392 ret = (double)sign*d/pow(10, -exp);
394 _control87(fpcontrol, 0xffffffff);
396 if((d && ret==0.0) || isinf(ret))
397 *MSVCRT__errno() = MSVCRT_ERANGE;
405 /*********************************************************************
408 double CDECL MSVCRT_strtod( const char *str, char **end )
410 return MSVCRT_strtod_l( str, end, NULL );
413 /*********************************************************************
416 double CDECL MSVCRT_atof( const char *str )
418 return MSVCRT_strtod_l(str, NULL, NULL);
421 /*********************************************************************
424 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
426 return MSVCRT_strtod_l(str, NULL, locale);
429 /*********************************************************************
430 * _atoflt_l (MSVCRT.@)
432 int CDECL MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT *value, char *str, MSVCRT__locale_t locale)
434 unsigned __int64 d=0, hlp;
439 BOOL found_digit = FALSE;
442 locale = get_locale();
444 /* FIXME: use *_l functions */
457 hlp = d*10+*(p++)-'0';
458 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
469 if(*p == *locale->locinfo->lconv->decimal_point)
474 hlp = d*10+*(p++)-'0';
475 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
489 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
501 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
507 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
508 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
511 if(*p=='-' || *p=='+')
517 fpcontrol = _control87(0, 0);
518 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
519 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
522 value->f = (float)sign*d*powf(10, exp);
524 value->f = (float)sign*d/powf(10, -exp);
526 _control87(fpcontrol, 0xffffffff);
528 if((d && value->f==0.0) || isinf(value->f))
529 ret = exp > 0 ? MSVCRT__OVERFLOW : MSVCRT__UNDERFLOW;
534 /*********************************************************************
535 * _strcoll_l (MSVCRT.@)
537 int CDECL MSVCRT_strcoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
540 locale = get_locale();
542 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, -1, str2, -1)-2;
545 /*********************************************************************
548 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
550 return MSVCRT_strcoll_l(str1, str2, NULL);
553 /*********************************************************************
554 * _stricoll_l (MSVCRT.@)
556 int CDECL MSVCRT__stricoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
559 locale = get_locale();
561 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
562 str1, -1, str2, -1)-2;
565 /*********************************************************************
566 * _stricoll (MSVCRT.@)
568 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
570 return MSVCRT__stricoll_l(str1, str2, NULL);
573 /*********************************************************************
574 * _strncoll_l (MSVCRT.@)
576 int CDECL MSVCRT_strncoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
579 locale = get_locale();
581 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, count, str2, count)-2;
584 /*********************************************************************
585 * strncoll (MSVCRT.@)
587 int CDECL MSVCRT_strncoll( const char* str1, const char* str2, MSVCRT_size_t count )
589 return MSVCRT_strncoll_l(str1, str2, count, NULL);
592 /*********************************************************************
593 * _strnicoll_l (MSVCRT.@)
595 int CDECL MSVCRT__strnicoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
598 locale = get_locale();
600 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
601 str1, count, str2, count)-2;
604 /*********************************************************************
605 * _strnicoll (MSVCRT.@)
607 int CDECL MSVCRT__strnicoll( const char* str1, const char* str2, MSVCRT_size_t count )
609 return MSVCRT__strnicoll_l(str1, str2, count, NULL);
612 /*********************************************************************
613 * strcpy_s (MSVCRT.@)
615 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
618 if(!elem) return MSVCRT_EINVAL;
619 if(!dst) return MSVCRT_EINVAL;
623 return MSVCRT_EINVAL;
626 for(i = 0; i < elem; i++)
628 if((dst[i] = src[i]) == '\0') return 0;
631 return MSVCRT_ERANGE;
634 /*********************************************************************
635 * strcat_s (MSVCRT.@)
637 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
640 if(!dst) return MSVCRT_EINVAL;
641 if(elem == 0) return MSVCRT_EINVAL;
645 return MSVCRT_EINVAL;
648 for(i = 0; i < elem; i++)
652 for(j = 0; (j + i) < elem; j++)
654 if((dst[j + i] = src[j]) == '\0') return 0;
658 /* Set the first element to 0, not the first element after the skipped part */
660 return MSVCRT_ERANGE;
663 /*********************************************************************
664 * strncat_s (MSVCRT.@)
666 int CDECL MSVCRT_strncat_s( char* dst, MSVCRT_size_t elem, const char* src, MSVCRT_size_t count )
669 if(!MSVCRT_CHECK_PMT(dst != 0) || !MSVCRT_CHECK_PMT(elem != 0))
670 return MSVCRT_EINVAL;
671 if(!MSVCRT_CHECK_PMT(src != 0))
674 return MSVCRT_EINVAL;
677 for(i = 0; i < elem; i++)
681 for(j = 0; (j + i) < elem; j++)
683 if(count == MSVCRT__TRUNCATE && j + i == elem - 1)
686 return MSVCRT_STRUNCATE;
688 if(j == count || (dst[j + i] = src[j]) == '\0')
696 /* Set the first element to 0, not the first element after the skipped part */
698 return MSVCRT_ERANGE;
701 /*********************************************************************
704 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
706 /* FIXME: handle Windows locale */
707 return strxfrm( dest, src, len );
710 /********************************************************************
711 * _atoldbl (MSVCRT.@)
713 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
715 /* FIXME needs error checking for huge/small values */
718 TRACE("str %s value %p\n",str,value);
720 memcpy(value, &ld, 10);
722 FIXME("stub, str %s value %p\n",str,value);
727 /********************************************************************
728 * __STRINGTOLD (MSVCRT.@)
730 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
734 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
736 memcpy(value, &ld, 10);
738 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
743 /******************************************************************
746 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
748 /* wrapper to forward libc error code to msvcrt's error codes */
752 ret = strtol(nptr, end, base);
755 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
756 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
758 /* cope with the fact that we may use 64bit long integers on libc
759 * while msvcrt always uses 32bit long integers
761 if (ret > MSVCRT_LONG_MAX)
763 ret = MSVCRT_LONG_MAX;
764 *MSVCRT__errno() = MSVCRT_ERANGE;
766 else if (ret < -MSVCRT_LONG_MAX - 1)
768 ret = -MSVCRT_LONG_MAX - 1;
769 *MSVCRT__errno() = MSVCRT_ERANGE;
777 /******************************************************************
780 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
782 /* wrapper to forward libc error code to msvcrt's error codes */
786 ret = strtoul(nptr, end, base);
789 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
790 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
792 /* cope with the fact that we may use 64bit long integers on libc
793 * while msvcrt always uses 32bit long integers
795 if (ret > MSVCRT_ULONG_MAX)
797 ret = MSVCRT_ULONG_MAX;
798 *MSVCRT__errno() = MSVCRT_ERANGE;
806 /******************************************************************
809 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
813 for(i=0; i<maxlen; i++)
819 /*********************************************************************
820 * _strtoi64_l (MSVCRT.@)
822 * FIXME: locale parameter is ignored
824 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
826 BOOL negative = FALSE;
829 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
831 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
832 !MSVCRT_CHECK_PMT(base <= 36)) {
836 while(isspace(*nptr)) nptr++;
841 } else if(*nptr == '+')
844 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
857 char cur = tolower(*nptr);
865 if(cur<'a' || cur>='a'+base-10)
875 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
876 ret = MSVCRT_I64_MAX;
877 *MSVCRT__errno() = MSVCRT_ERANGE;
878 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
879 ret = MSVCRT_I64_MIN;
880 *MSVCRT__errno() = MSVCRT_ERANGE;
886 *endptr = (char*)nptr;
891 /*********************************************************************
892 * _strtoi64 (MSVCRT.@)
894 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
896 return MSVCRT_strtoi64_l(nptr, endptr, base, 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", nptr, endptr, base, locale);
911 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
912 !MSVCRT_CHECK_PMT(base <= 36)) {
916 while(isspace(*nptr)) nptr++;
921 } else if(*nptr == '+')
924 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
937 char cur = tolower(*nptr);
945 if(cur<'a' || cur>='a'+base-10)
952 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
953 ret = MSVCRT_UI64_MAX;
954 *MSVCRT__errno() = MSVCRT_ERANGE;
960 *endptr = (char*)nptr;
962 return negative ? -ret : ret;
965 /*********************************************************************
966 * _strtoui64 (MSVCRT.@)
968 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
970 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
973 /*********************************************************************
976 int CDECL _ltoa_s(MSVCRT_long value, char *str, MSVCRT_size_t size, int radix)
981 char buffer[33], *pos;
984 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
985 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
990 *MSVCRT__errno() = MSVCRT_EINVAL;
991 return MSVCRT_EINVAL;
994 if (value < 0 && radix == 10)
1010 digit = val % radix;
1014 *--pos = '0' + digit;
1016 *--pos = 'a' + digit - 10;
1023 len = buffer + 33 - pos;
1029 /* Copy the temporary buffer backwards up to the available number of
1030 * characters. Don't copy the negative sign if present. */
1038 for (pos = buffer + 31, i = 0; i < size; i++)
1042 MSVCRT_INVALID_PMT("str[size] is too small");
1043 *MSVCRT__errno() = MSVCRT_ERANGE;
1044 return MSVCRT_ERANGE;
1047 memcpy(str, pos, len);
1051 /*********************************************************************
1052 * _ltow_s (MSVCRT.@)
1054 int CDECL _ltow_s(MSVCRT_long value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1059 MSVCRT_wchar_t buffer[33], *pos;
1062 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1063 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1068 *MSVCRT__errno() = MSVCRT_EINVAL;
1069 return MSVCRT_EINVAL;
1072 if (value < 0 && radix == 10)
1088 digit = val % radix;
1092 *--pos = '0' + digit;
1094 *--pos = 'a' + digit - 10;
1101 len = buffer + 33 - pos;
1105 MSVCRT_wchar_t *p = str;
1107 /* Copy the temporary buffer backwards up to the available number of
1108 * characters. Don't copy the negative sign if present. */
1116 for (pos = buffer + 31, i = 0; i < size; i++)
1119 MSVCRT_INVALID_PMT("str[size] is too small");
1121 *MSVCRT__errno() = MSVCRT_ERANGE;
1122 return MSVCRT_ERANGE;
1125 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1129 /*********************************************************************
1130 * _itoa_s (MSVCRT.@)
1132 int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
1134 return _ltoa_s(value, str, size, radix);
1137 /*********************************************************************
1138 * _itow_s (MSVCRT.@)
1140 int CDECL _itow_s(int value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1142 return _ltow_s(value, str, size, radix);
1145 /*********************************************************************
1146 * _ui64toa_s (MSVCRT.@)
1148 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
1149 MSVCRT_size_t size, int radix)
1151 char buffer[65], *pos;
1154 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1155 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
1156 *MSVCRT__errno() = MSVCRT_EINVAL;
1157 return MSVCRT_EINVAL;
1164 digit = value%radix;
1170 *--pos = 'a'+digit-10;
1173 if(buffer-pos+65 > size) {
1174 MSVCRT_INVALID_PMT("str[size] is too small");
1175 *MSVCRT__errno() = MSVCRT_EINVAL;
1176 return MSVCRT_EINVAL;
1179 memcpy(str, pos, buffer-pos+65);
1183 /*********************************************************************
1184 * _ui64tow_s (MSVCRT.@)
1186 int CDECL MSVCRT__ui64tow_s( unsigned __int64 value, MSVCRT_wchar_t *str,
1187 MSVCRT_size_t size, int radix )
1189 MSVCRT_wchar_t buffer[65], *pos;
1192 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1193 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
1194 *MSVCRT__errno() = MSVCRT_EINVAL;
1195 return MSVCRT_EINVAL;
1202 digit = value % radix;
1203 value = value / radix;
1205 *--pos = '0' + digit;
1207 *--pos = 'a' + digit - 10;
1208 } while (value != 0);
1210 if(buffer-pos+65 > size) {
1211 MSVCRT_INVALID_PMT("str[size] is too small");
1212 *MSVCRT__errno() = MSVCRT_EINVAL;
1213 return MSVCRT_EINVAL;
1216 memcpy(str, pos, buffer-pos+65);
1220 /*********************************************************************
1221 * _ultoa_s (MSVCRT.@)
1223 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
1226 char buffer[33], *pos;
1229 if (!str || !size || radix < 2 || radix > 36)
1234 *MSVCRT__errno() = MSVCRT_EINVAL;
1235 return MSVCRT_EINVAL;
1243 digit = value % radix;
1247 *--pos = '0' + digit;
1249 *--pos = 'a' + digit - 10;
1253 len = buffer + 33 - pos;
1259 /* Copy the temporary buffer backwards up to the available number of
1262 for (pos = buffer + 31, i = 0; i < size; i++)
1266 *MSVCRT__errno() = MSVCRT_ERANGE;
1267 return MSVCRT_ERANGE;
1270 memcpy(str, pos, len);
1274 /*********************************************************************
1275 * _i64toa_s (MSVCRT.@)
1277 int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix)
1279 unsigned __int64 val;
1282 char buffer[65], *pos;
1285 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1286 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1291 *MSVCRT__errno() = MSVCRT_EINVAL;
1292 return MSVCRT_EINVAL;
1295 if (value < 0 && radix == 10)
1311 digit = val % radix;
1315 *--pos = '0' + digit;
1317 *--pos = 'a' + digit - 10;
1324 len = buffer + 65 - pos;
1330 /* Copy the temporary buffer backwards up to the available number of
1331 * characters. Don't copy the negative sign if present. */
1339 for (pos = buffer + 63, i = 0; i < size; i++)
1343 MSVCRT_INVALID_PMT("str[size] is too small");
1344 *MSVCRT__errno() = MSVCRT_ERANGE;
1345 return MSVCRT_ERANGE;
1348 memcpy(str, pos, len);
1352 /*********************************************************************
1353 * _i64tow_s (MSVCRT.@)
1355 int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1357 unsigned __int64 val;
1360 MSVCRT_wchar_t buffer[65], *pos;
1363 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1364 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1369 *MSVCRT__errno() = MSVCRT_EINVAL;
1370 return MSVCRT_EINVAL;
1373 if (value < 0 && radix == 10)
1389 digit = val % radix;
1393 *--pos = '0' + digit;
1395 *--pos = 'a' + digit - 10;
1402 len = buffer + 65 - pos;
1406 MSVCRT_wchar_t *p = str;
1408 /* Copy the temporary buffer backwards up to the available number of
1409 * characters. Don't copy the negative sign if present. */
1417 for (pos = buffer + 63, i = 0; i < size; i++)
1420 MSVCRT_INVALID_PMT("str[size] is too small");
1422 *MSVCRT__errno() = MSVCRT_ERANGE;
1423 return MSVCRT_ERANGE;
1426 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1430 #define I10_OUTPUT_MAX_PREC 21
1431 /* Internal structure used by $I10_OUTPUT */
1432 struct _I10_OUTPUT_DATA {
1436 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
1439 /*********************************************************************
1440 * $I10_OUTPUT (MSVCRT.@)
1441 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1442 * prec - precision of part, we're interested in
1443 * flag - 0 for first prec digits, 1 for fractional part
1444 * data - data to be populated
1447 * 0 if given double is NaN or INF
1451 * Native sets last byte of data->str to '0' or '9', I don't know what
1452 * it means. Current implementation sets it always to '0'.
1454 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
1456 static const char inf_str[] = "1#INF";
1457 static const char nan_str[] = "1#QNAN";
1459 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1460 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1461 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1465 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
1468 memcpy(&ld, &ld80, 10);
1470 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
1481 memcpy(data->str, inf_str, sizeof(inf_str));
1489 memcpy(data->str, nan_str, sizeof(nan_str));
1495 int exp = 1+floor(log10(d));
1503 if(prec+1 > I10_OUTPUT_MAX_PREC)
1504 prec = I10_OUTPUT_MAX_PREC-1;
1510 sprintf(format, "%%.%dle", prec);
1511 sprintf(buf, format, d);
1514 data->pos = atoi(buf+prec+3);
1518 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1521 memcpy(data->str, buf+1, data->len);
1522 data->str[data->len] = '\0';
1524 if(buf[1]!='0' && prec-data->len+1>0)
1525 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1529 #undef I10_OUTPUT_MAX_PREC