2 * MSVCRT string functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define _ISOC99_SOURCE
26 #include "wine/port.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 /*********************************************************************
43 char* CDECL MSVCRT__strdup(const char* str)
47 char * ret = MSVCRT_malloc(strlen(str)+1);
48 if (ret) strcpy( ret, str );
54 /*********************************************************************
55 * _strlwr_s_l (MSVCRT.@)
57 int CDECL _strlwr_s_l(char *str, MSVCRT_size_t len, MSVCRT__locale_t locale)
63 *MSVCRT__errno() = MSVCRT_EINVAL;
76 *MSVCRT__errno() = MSVCRT_EINVAL;
82 *str = MSVCRT__tolower_l((unsigned char)*str, locale);
89 /*********************************************************************
90 * _strlwr_s (MSVCRT.@)
92 int CDECL _strlwr_s(char *str, MSVCRT_size_t len)
94 return _strlwr_s_l(str, len, NULL);
97 /*********************************************************************
98 * _strlwr_l (MSVCRT.@)
100 char* CDECL _strlwr_l(char *str, MSVCRT__locale_t locale)
102 _strlwr_s_l(str, -1, locale);
106 /*********************************************************************
109 char* CDECL MSVCRT__strlwr(char *str)
111 _strlwr_s_l(str, -1, NULL);
115 /*********************************************************************
116 * _strupr_s_l (MSVCRT.@)
118 int CDECL _strupr_s_l(char *str, MSVCRT_size_t len, MSVCRT__locale_t locale)
124 *MSVCRT__errno() = MSVCRT_EINVAL;
125 return MSVCRT_EINVAL;
137 *MSVCRT__errno() = MSVCRT_EINVAL;
138 return MSVCRT_EINVAL;
143 *str = MSVCRT__toupper_l((unsigned char)*str, locale);
150 /*********************************************************************
151 * _strupr_s (MSVCRT.@)
153 int CDECL _strupr_s(char *str, MSVCRT_size_t len)
155 return _strupr_s_l(str, len, NULL);
158 /*********************************************************************
159 * _strupr_l (MSVCRT.@)
161 char* CDECL MSVCRT__strupr_l(char *str, MSVCRT__locale_t locale)
163 _strupr_s_l(str, -1, locale);
167 /*********************************************************************
170 char* CDECL MSVCRT__strupr(char *str)
172 _strupr_s_l(str, -1, NULL);
176 /*********************************************************************
177 * _strnset (MSVCRT.@)
179 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
182 while (*str && len--)
187 /*********************************************************************
190 char* CDECL MSVCRT__strrev(char* str)
196 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
206 /*********************************************************************
209 char* CDECL _strset(char* str, int value)
218 /*********************************************************************
221 char * CDECL MSVCRT_strtok( char *str, const char *delim )
223 thread_data_t *data = msvcrt_get_thread_data();
227 if (!(str = data->strtok_next)) return NULL;
229 while (*str && strchr( delim, *str )) str++;
230 if (!*str) return NULL;
232 while (*str && !strchr( delim, *str )) str++;
233 if (*str) *str++ = 0;
234 data->strtok_next = str;
238 /*********************************************************************
239 * strtok_s (MSVCRT.@)
241 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
243 if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(ctx != NULL) ||
244 !MSVCRT_CHECK_PMT(str != NULL || *ctx != NULL)) {
245 *MSVCRT__errno() = MSVCRT_EINVAL;
252 while(*str && strchr(delim, *str))
258 while(**ctx && !strchr(delim, **ctx))
266 /*********************************************************************
269 void CDECL MSVCRT__swab(char* src, char* dst, int len)
273 len = (unsigned)len >> 1;
285 /*********************************************************************
286 * strtod_l (MSVCRT.@)
288 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
290 MSVCRT_pthreadlocinfo locinfo;
291 unsigned __int64 d=0, hlp;
296 BOOL found_digit = FALSE;
298 if (!MSVCRT_CHECK_PMT(str != NULL)) {
299 *MSVCRT__errno() = MSVCRT_EINVAL;
304 locinfo = get_locinfo();
306 locinfo = locale->locinfo;
308 /* FIXME: use *_l functions */
321 hlp = d*10+*(p++)-'0';
322 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
333 if(*p == *locinfo->lconv->decimal_point)
338 hlp = d*10+*(p++)-'0';
339 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
354 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
366 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
372 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
373 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
376 if(*p=='-' || *p=='+')
382 fpcontrol = _control87(0, 0);
383 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
384 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
387 ret = (double)sign*d*pow(10, exp);
389 ret = (double)sign*d/pow(10, -exp);
391 _control87(fpcontrol, 0xffffffff);
393 if((d && ret==0.0) || isinf(ret))
394 *MSVCRT__errno() = MSVCRT_ERANGE;
402 /*********************************************************************
405 double CDECL MSVCRT_strtod( const char *str, char **end )
407 return MSVCRT_strtod_l( str, end, NULL );
410 /*********************************************************************
413 double CDECL MSVCRT_atof( const char *str )
415 return MSVCRT_strtod_l(str, NULL, NULL);
418 /*********************************************************************
421 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
423 return MSVCRT_strtod_l(str, NULL, locale);
426 /*********************************************************************
427 * _atoflt_l (MSVCRT.@)
429 int CDECL MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT *value, char *str, MSVCRT__locale_t locale)
431 MSVCRT_pthreadlocinfo locinfo;
432 unsigned __int64 d=0, hlp;
437 BOOL found_digit = FALSE;
440 locinfo = get_locinfo();
442 locinfo = locale->locinfo;
444 /* FIXME: use *_l functions */
457 hlp = d*10+*(p++)-'0';
458 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
469 if(*p == *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 )
539 MSVCRT_pthreadlocinfo locinfo;
542 locinfo = get_locinfo();
544 locinfo = locale->locinfo;
546 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, -1, str2, -1)-2;
549 /*********************************************************************
552 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
554 return MSVCRT_strcoll_l(str1, str2, NULL);
557 /*********************************************************************
558 * _stricoll_l (MSVCRT.@)
560 int CDECL MSVCRT__stricoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
562 MSVCRT_pthreadlocinfo locinfo;
565 locinfo = get_locinfo();
567 locinfo = locale->locinfo;
569 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
570 str1, -1, str2, -1)-2;
573 /*********************************************************************
574 * _stricoll (MSVCRT.@)
576 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
578 return MSVCRT__stricoll_l(str1, str2, NULL);
581 /*********************************************************************
582 * _strncoll_l (MSVCRT.@)
584 int CDECL MSVCRT_strncoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
586 MSVCRT_pthreadlocinfo locinfo;
589 locinfo = get_locinfo();
591 locinfo = locale->locinfo;
593 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, count, str2, count)-2;
596 /*********************************************************************
597 * strncoll (MSVCRT.@)
599 int CDECL MSVCRT_strncoll( const char* str1, const char* str2, MSVCRT_size_t count )
601 return MSVCRT_strncoll_l(str1, str2, count, NULL);
604 /*********************************************************************
605 * _strnicoll_l (MSVCRT.@)
607 int CDECL MSVCRT__strnicoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
609 MSVCRT_pthreadlocinfo locinfo;
612 locinfo = get_locinfo();
614 locinfo = locale->locinfo;
616 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
617 str1, count, str2, count)-2;
620 /*********************************************************************
621 * _strnicoll (MSVCRT.@)
623 int CDECL MSVCRT__strnicoll( const char* str1, const char* str2, MSVCRT_size_t count )
625 return MSVCRT__strnicoll_l(str1, str2, count, NULL);
628 /*********************************************************************
629 * strcpy_s (MSVCRT.@)
631 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
634 if(!elem) return MSVCRT_EINVAL;
635 if(!dst) return MSVCRT_EINVAL;
639 return MSVCRT_EINVAL;
642 for(i = 0; i < elem; i++)
644 if((dst[i] = src[i]) == '\0') return 0;
647 return MSVCRT_ERANGE;
650 /*********************************************************************
651 * strcat_s (MSVCRT.@)
653 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
656 if(!dst) return MSVCRT_EINVAL;
657 if(elem == 0) return MSVCRT_EINVAL;
661 return MSVCRT_EINVAL;
664 for(i = 0; i < elem; i++)
668 for(j = 0; (j + i) < elem; j++)
670 if((dst[j + i] = src[j]) == '\0') return 0;
674 /* Set the first element to 0, not the first element after the skipped part */
676 return MSVCRT_ERANGE;
679 /*********************************************************************
680 * strncat_s (MSVCRT.@)
682 int CDECL MSVCRT_strncat_s( char* dst, MSVCRT_size_t elem, const char* src, MSVCRT_size_t count )
685 if(!MSVCRT_CHECK_PMT(dst != 0) || !MSVCRT_CHECK_PMT(elem != 0))
686 return MSVCRT_EINVAL;
687 if(!MSVCRT_CHECK_PMT(src != 0))
690 return MSVCRT_EINVAL;
693 for(i = 0; i < elem; i++)
697 for(j = 0; (j + i) < elem; j++)
699 if(count == MSVCRT__TRUNCATE && j + i == elem - 1)
702 return MSVCRT_STRUNCATE;
704 if(j == count || (dst[j + i] = src[j]) == '\0')
712 /* Set the first element to 0, not the first element after the skipped part */
714 return MSVCRT_ERANGE;
717 /*********************************************************************
720 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
722 /* FIXME: handle Windows locale */
723 return strxfrm( dest, src, len );
726 /********************************************************************
727 * _atoldbl (MSVCRT.@)
729 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
731 /* FIXME needs error checking for huge/small values */
734 TRACE("str %s value %p\n",str,value);
736 memcpy(value, &ld, 10);
738 FIXME("stub, str %s value %p\n",str,value);
743 /********************************************************************
744 * __STRINGTOLD (MSVCRT.@)
746 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
750 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
752 memcpy(value, &ld, 10);
754 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
759 /******************************************************************
762 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
764 /* wrapper to forward libc error code to msvcrt's error codes */
768 ret = strtol(nptr, end, base);
771 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
772 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
774 /* cope with the fact that we may use 64bit long integers on libc
775 * while msvcrt always uses 32bit long integers
777 if (ret > MSVCRT_LONG_MAX)
779 ret = MSVCRT_LONG_MAX;
780 *MSVCRT__errno() = MSVCRT_ERANGE;
782 else if (ret < -MSVCRT_LONG_MAX - 1)
784 ret = -MSVCRT_LONG_MAX - 1;
785 *MSVCRT__errno() = MSVCRT_ERANGE;
793 /******************************************************************
796 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
798 /* wrapper to forward libc error code to msvcrt's error codes */
802 ret = strtoul(nptr, end, base);
805 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
806 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
808 /* cope with the fact that we may use 64bit long integers on libc
809 * while msvcrt always uses 32bit long integers
811 if (ret > MSVCRT_ULONG_MAX)
813 ret = MSVCRT_ULONG_MAX;
814 *MSVCRT__errno() = MSVCRT_ERANGE;
822 /******************************************************************
825 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
829 for(i=0; i<maxlen; i++)
835 /*********************************************************************
836 * _strtoi64_l (MSVCRT.@)
838 * FIXME: locale parameter is ignored
840 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
842 BOOL negative = FALSE;
845 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
847 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
848 !MSVCRT_CHECK_PMT(base <= 36)) {
852 while(isspace(*nptr)) nptr++;
857 } else if(*nptr == '+')
860 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
873 char cur = tolower(*nptr);
881 if(cur<'a' || cur>='a'+base-10)
891 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
892 ret = MSVCRT_I64_MAX;
893 *MSVCRT__errno() = MSVCRT_ERANGE;
894 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
895 ret = MSVCRT_I64_MIN;
896 *MSVCRT__errno() = MSVCRT_ERANGE;
902 *endptr = (char*)nptr;
907 /*********************************************************************
908 * _strtoi64 (MSVCRT.@)
910 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
912 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
915 /*********************************************************************
916 * _strtoui64_l (MSVCRT.@)
918 * FIXME: locale parameter is ignored
920 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
922 BOOL negative = FALSE;
923 unsigned __int64 ret = 0;
925 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
927 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
928 !MSVCRT_CHECK_PMT(base <= 36)) {
932 while(isspace(*nptr)) nptr++;
937 } else if(*nptr == '+')
940 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
953 char cur = tolower(*nptr);
961 if(cur<'a' || cur>='a'+base-10)
968 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
969 ret = MSVCRT_UI64_MAX;
970 *MSVCRT__errno() = MSVCRT_ERANGE;
976 *endptr = (char*)nptr;
978 return negative ? -ret : ret;
981 /*********************************************************************
982 * _strtoui64 (MSVCRT.@)
984 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
986 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
989 /*********************************************************************
992 int CDECL _ltoa_s(MSVCRT_long value, char *str, MSVCRT_size_t size, int radix)
997 char buffer[33], *pos;
1000 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1001 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1006 *MSVCRT__errno() = MSVCRT_EINVAL;
1007 return MSVCRT_EINVAL;
1010 if (value < 0 && radix == 10)
1026 digit = val % radix;
1030 *--pos = '0' + digit;
1032 *--pos = 'a' + digit - 10;
1039 len = buffer + 33 - pos;
1045 /* Copy the temporary buffer backwards up to the available number of
1046 * characters. Don't copy the negative sign if present. */
1054 for (pos = buffer + 31, i = 0; i < size; i++)
1058 MSVCRT_INVALID_PMT("str[size] is too small");
1059 *MSVCRT__errno() = MSVCRT_ERANGE;
1060 return MSVCRT_ERANGE;
1063 memcpy(str, pos, len);
1067 /*********************************************************************
1068 * _ltow_s (MSVCRT.@)
1070 int CDECL _ltow_s(MSVCRT_long value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1075 MSVCRT_wchar_t buffer[33], *pos;
1078 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1079 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1084 *MSVCRT__errno() = MSVCRT_EINVAL;
1085 return MSVCRT_EINVAL;
1088 if (value < 0 && radix == 10)
1104 digit = val % radix;
1108 *--pos = '0' + digit;
1110 *--pos = 'a' + digit - 10;
1117 len = buffer + 33 - pos;
1121 MSVCRT_wchar_t *p = str;
1123 /* Copy the temporary buffer backwards up to the available number of
1124 * characters. Don't copy the negative sign if present. */
1132 for (pos = buffer + 31, i = 0; i < size; i++)
1135 MSVCRT_INVALID_PMT("str[size] is too small");
1137 *MSVCRT__errno() = MSVCRT_ERANGE;
1138 return MSVCRT_ERANGE;
1141 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1145 /*********************************************************************
1146 * _itoa_s (MSVCRT.@)
1148 int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
1150 return _ltoa_s(value, str, size, radix);
1153 /*********************************************************************
1154 * _itow_s (MSVCRT.@)
1156 int CDECL _itow_s(int value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1158 return _ltow_s(value, str, size, radix);
1161 /*********************************************************************
1162 * _ui64toa_s (MSVCRT.@)
1164 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
1165 MSVCRT_size_t size, int radix)
1167 char buffer[65], *pos;
1170 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1171 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
1172 *MSVCRT__errno() = MSVCRT_EINVAL;
1173 return MSVCRT_EINVAL;
1180 digit = value%radix;
1186 *--pos = 'a'+digit-10;
1189 if(buffer-pos+65 > size) {
1190 MSVCRT_INVALID_PMT("str[size] is too small");
1191 *MSVCRT__errno() = MSVCRT_EINVAL;
1192 return MSVCRT_EINVAL;
1195 memcpy(str, pos, buffer-pos+65);
1199 /*********************************************************************
1200 * _ui64tow_s (MSVCRT.@)
1202 int CDECL MSVCRT__ui64tow_s( unsigned __int64 value, MSVCRT_wchar_t *str,
1203 MSVCRT_size_t size, int radix )
1205 MSVCRT_wchar_t buffer[65], *pos;
1208 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1209 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
1210 *MSVCRT__errno() = MSVCRT_EINVAL;
1211 return MSVCRT_EINVAL;
1218 digit = value % radix;
1219 value = value / radix;
1221 *--pos = '0' + digit;
1223 *--pos = 'a' + digit - 10;
1224 } while (value != 0);
1226 if(buffer-pos+65 > size) {
1227 MSVCRT_INVALID_PMT("str[size] is too small");
1228 *MSVCRT__errno() = MSVCRT_EINVAL;
1229 return MSVCRT_EINVAL;
1232 memcpy(str, pos, buffer-pos+65);
1236 /*********************************************************************
1237 * _ultoa_s (MSVCRT.@)
1239 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
1242 char buffer[33], *pos;
1245 if (!str || !size || radix < 2 || radix > 36)
1250 *MSVCRT__errno() = MSVCRT_EINVAL;
1251 return MSVCRT_EINVAL;
1259 digit = value % radix;
1263 *--pos = '0' + digit;
1265 *--pos = 'a' + digit - 10;
1269 len = buffer + 33 - pos;
1275 /* Copy the temporary buffer backwards up to the available number of
1278 for (pos = buffer + 31, i = 0; i < size; i++)
1282 *MSVCRT__errno() = MSVCRT_ERANGE;
1283 return MSVCRT_ERANGE;
1286 memcpy(str, pos, len);
1290 /*********************************************************************
1291 * _i64toa_s (MSVCRT.@)
1293 int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix)
1295 unsigned __int64 val;
1298 char buffer[65], *pos;
1301 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1302 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1307 *MSVCRT__errno() = MSVCRT_EINVAL;
1308 return MSVCRT_EINVAL;
1311 if (value < 0 && radix == 10)
1327 digit = val % radix;
1331 *--pos = '0' + digit;
1333 *--pos = 'a' + digit - 10;
1340 len = buffer + 65 - pos;
1346 /* Copy the temporary buffer backwards up to the available number of
1347 * characters. Don't copy the negative sign if present. */
1355 for (pos = buffer + 63, i = 0; i < size; i++)
1359 MSVCRT_INVALID_PMT("str[size] is too small");
1360 *MSVCRT__errno() = MSVCRT_ERANGE;
1361 return MSVCRT_ERANGE;
1364 memcpy(str, pos, len);
1368 /*********************************************************************
1369 * _i64tow_s (MSVCRT.@)
1371 int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1373 unsigned __int64 val;
1376 MSVCRT_wchar_t buffer[65], *pos;
1379 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1380 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1385 *MSVCRT__errno() = MSVCRT_EINVAL;
1386 return MSVCRT_EINVAL;
1389 if (value < 0 && radix == 10)
1405 digit = val % radix;
1409 *--pos = '0' + digit;
1411 *--pos = 'a' + digit - 10;
1418 len = buffer + 65 - pos;
1422 MSVCRT_wchar_t *p = str;
1424 /* Copy the temporary buffer backwards up to the available number of
1425 * characters. Don't copy the negative sign if present. */
1433 for (pos = buffer + 63, i = 0; i < size; i++)
1436 MSVCRT_INVALID_PMT("str[size] is too small");
1438 *MSVCRT__errno() = MSVCRT_ERANGE;
1439 return MSVCRT_ERANGE;
1442 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1446 #define I10_OUTPUT_MAX_PREC 21
1447 /* Internal structure used by $I10_OUTPUT */
1448 struct _I10_OUTPUT_DATA {
1452 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
1455 /*********************************************************************
1456 * $I10_OUTPUT (MSVCRT.@)
1457 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1458 * prec - precision of part, we're interested in
1459 * flag - 0 for first prec digits, 1 for fractional part
1460 * data - data to be populated
1463 * 0 if given double is NaN or INF
1467 * Native sets last byte of data->str to '0' or '9', I don't know what
1468 * it means. Current implementation sets it always to '0'.
1470 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
1472 static const char inf_str[] = "1#INF";
1473 static const char nan_str[] = "1#QNAN";
1475 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1476 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1477 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1481 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
1484 memcpy(&ld, &ld80, 10);
1486 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
1497 memcpy(data->str, inf_str, sizeof(inf_str));
1505 memcpy(data->str, nan_str, sizeof(nan_str));
1511 int exp = 1+floor(log10(d));
1519 if(prec+1 > I10_OUTPUT_MAX_PREC)
1520 prec = I10_OUTPUT_MAX_PREC-1;
1526 sprintf(format, "%%.%dle", prec);
1527 sprintf(buf, format, d);
1530 data->pos = atoi(buf+prec+3);
1534 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1537 memcpy(data->str, buf+1, data->len);
1538 data->str[data->len] = '\0';
1540 if(buf[1]!='0' && prec-data->len+1>0)
1541 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1545 #undef I10_OUTPUT_MAX_PREC