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)) return NULL;
244 if (!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
245 if (!MSVCRT_CHECK_PMT(str != NULL || *ctx != NULL)) return NULL;
250 while(*str && strchr(delim, *str))
256 while(**ctx && !strchr(delim, **ctx))
264 /*********************************************************************
267 void CDECL MSVCRT__swab(char* src, char* dst, int len)
271 len = (unsigned)len >> 1;
283 /*********************************************************************
284 * strtod_l (MSVCRT.@)
286 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
288 MSVCRT_pthreadlocinfo locinfo;
289 unsigned __int64 d=0, hlp;
294 long double lret=1, expcnt = 10;
295 BOOL found_digit = FALSE, negexp;
297 if (!MSVCRT_CHECK_PMT(str != NULL)) return 0;
300 locinfo = get_locinfo();
302 locinfo = locale->locinfo;
304 /* FIXME: use *_l functions */
317 hlp = d*10+*(p++)-'0';
318 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
329 if(*p == *locinfo->lconv->decimal_point)
334 hlp = d*10+*(p++)-'0';
335 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
350 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
362 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
368 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
369 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
372 if(*p=='-' || *p=='+')
378 fpcontrol = _control87(0, 0);
379 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
380 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
389 expcnt = expcnt*expcnt;
391 ret = (long double)sign * (negexp ? d/lret : d*lret);
393 _control87(fpcontrol, 0xffffffff);
395 if((d && ret==0.0) || isinf(ret))
396 *MSVCRT__errno() = MSVCRT_ERANGE;
404 /*********************************************************************
407 double CDECL MSVCRT_strtod( const char *str, char **end )
409 return MSVCRT_strtod_l( str, end, NULL );
412 /*********************************************************************
415 double CDECL MSVCRT_atof( const char *str )
417 return MSVCRT_strtod_l(str, NULL, NULL);
420 /*********************************************************************
423 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
425 return MSVCRT_strtod_l(str, NULL, locale);
428 /*********************************************************************
429 * _atoflt_l (MSVCRT.@)
431 int CDECL MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT *value, char *str, MSVCRT__locale_t locale)
433 MSVCRT_pthreadlocinfo locinfo;
434 unsigned __int64 d=0, hlp;
439 BOOL found_digit = FALSE;
442 locinfo = get_locinfo();
444 locinfo = locale->locinfo;
446 /* FIXME: use *_l functions */
459 hlp = d*10+*(p++)-'0';
460 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
471 if(*p == *locinfo->lconv->decimal_point)
476 hlp = d*10+*(p++)-'0';
477 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
491 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
503 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
509 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
510 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
513 if(*p=='-' || *p=='+')
519 fpcontrol = _control87(0, 0);
520 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
521 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
524 value->f = (double)sign*d*pow(10, exp);
526 value->f = (double)sign*d/pow(10, -exp);
528 _control87(fpcontrol, 0xffffffff);
530 if((d && value->f>-MSVCRT_FLT_MIN && value->f<MSVCRT_FLT_MIN) || isinf(value->f))
531 ret = exp > 0 ? MSVCRT__OVERFLOW : MSVCRT__UNDERFLOW;
536 /*********************************************************************
537 * _strcoll_l (MSVCRT.@)
539 int CDECL MSVCRT_strcoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
541 MSVCRT_pthreadlocinfo locinfo;
544 locinfo = get_locinfo();
546 locinfo = locale->locinfo;
548 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
551 /*********************************************************************
554 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
556 return MSVCRT_strcoll_l(str1, str2, NULL);
559 /*********************************************************************
560 * _stricoll_l (MSVCRT.@)
562 int CDECL MSVCRT__stricoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
564 MSVCRT_pthreadlocinfo locinfo;
567 locinfo = get_locinfo();
569 locinfo = locale->locinfo;
571 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
572 str1, -1, str2, -1)-CSTR_EQUAL;
575 /*********************************************************************
576 * _stricoll (MSVCRT.@)
578 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
580 return MSVCRT__stricoll_l(str1, str2, NULL);
583 /*********************************************************************
584 * _strncoll_l (MSVCRT.@)
586 int CDECL MSVCRT_strncoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
588 MSVCRT_pthreadlocinfo locinfo;
591 locinfo = get_locinfo();
593 locinfo = locale->locinfo;
595 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, count, str2, count)-CSTR_EQUAL;
598 /*********************************************************************
599 * strncoll (MSVCRT.@)
601 int CDECL MSVCRT_strncoll( const char* str1, const char* str2, MSVCRT_size_t count )
603 return MSVCRT_strncoll_l(str1, str2, count, NULL);
606 /*********************************************************************
607 * _strnicoll_l (MSVCRT.@)
609 int CDECL MSVCRT__strnicoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
611 MSVCRT_pthreadlocinfo locinfo;
614 locinfo = get_locinfo();
616 locinfo = locale->locinfo;
618 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
619 str1, count, str2, count)-CSTR_EQUAL;
622 /*********************************************************************
623 * _strnicoll (MSVCRT.@)
625 int CDECL MSVCRT__strnicoll( const char* str1, const char* str2, MSVCRT_size_t count )
627 return MSVCRT__strnicoll_l(str1, str2, count, NULL);
630 /*********************************************************************
631 * strcpy_s (MSVCRT.@)
633 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
636 if(!elem) return MSVCRT_EINVAL;
637 if(!dst) return MSVCRT_EINVAL;
641 return MSVCRT_EINVAL;
644 for(i = 0; i < elem; i++)
646 if((dst[i] = src[i]) == '\0') return 0;
649 return MSVCRT_ERANGE;
652 /*********************************************************************
653 * strcat_s (MSVCRT.@)
655 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
658 if(!dst) return MSVCRT_EINVAL;
659 if(elem == 0) return MSVCRT_EINVAL;
663 return MSVCRT_EINVAL;
666 for(i = 0; i < elem; i++)
670 for(j = 0; (j + i) < elem; j++)
672 if((dst[j + i] = src[j]) == '\0') return 0;
676 /* Set the first element to 0, not the first element after the skipped part */
678 return MSVCRT_ERANGE;
681 /*********************************************************************
682 * strncat_s (MSVCRT.@)
684 int CDECL MSVCRT_strncat_s( char* dst, MSVCRT_size_t elem, const char* src, MSVCRT_size_t count )
688 if (!MSVCRT_CHECK_PMT(dst != 0)) return MSVCRT_EINVAL;
689 if (!MSVCRT_CHECK_PMT(elem != 0)) return MSVCRT_EINVAL;
690 if (!MSVCRT_CHECK_PMT(src != 0))
693 return MSVCRT_EINVAL;
696 for(i = 0; i < elem; i++)
700 for(j = 0; (j + i) < elem; j++)
702 if(count == MSVCRT__TRUNCATE && j + i == elem - 1)
705 return MSVCRT_STRUNCATE;
707 if(j == count || (dst[j + i] = src[j]) == '\0')
715 /* Set the first element to 0, not the first element after the skipped part */
717 return MSVCRT_ERANGE;
720 /*********************************************************************
723 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
725 /* FIXME: handle Windows locale */
726 return strxfrm( dest, src, len );
729 /********************************************************************
730 * _atoldbl (MSVCRT.@)
732 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
734 /* FIXME needs error checking for huge/small values */
737 TRACE("str %s value %p\n",str,value);
739 memcpy(value, &ld, 10);
741 FIXME("stub, str %s value %p\n",str,value);
746 /********************************************************************
747 * __STRINGTOLD (MSVCRT.@)
749 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
753 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
755 memcpy(value, &ld, 10);
757 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
762 /******************************************************************
765 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
767 /* wrapper to forward libc error code to msvcrt's error codes */
771 ret = strtol(nptr, end, base);
774 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
775 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
777 /* cope with the fact that we may use 64bit long integers on libc
778 * while msvcrt always uses 32bit long integers
780 if (ret > MSVCRT_LONG_MAX)
782 ret = MSVCRT_LONG_MAX;
783 *MSVCRT__errno() = MSVCRT_ERANGE;
785 else if (ret < -MSVCRT_LONG_MAX - 1)
787 ret = -MSVCRT_LONG_MAX - 1;
788 *MSVCRT__errno() = MSVCRT_ERANGE;
796 /******************************************************************
799 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
801 /* wrapper to forward libc error code to msvcrt's error codes */
805 ret = strtoul(nptr, end, base);
808 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
809 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
811 /* cope with the fact that we may use 64bit long integers on libc
812 * while msvcrt always uses 32bit long integers
814 if (ret > MSVCRT_ULONG_MAX)
816 ret = MSVCRT_ULONG_MAX;
817 *MSVCRT__errno() = MSVCRT_ERANGE;
825 /******************************************************************
828 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
832 for(i=0; i<maxlen; i++)
838 /*********************************************************************
839 * _strtoi64_l (MSVCRT.@)
841 * FIXME: locale parameter is ignored
843 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
845 BOOL negative = FALSE;
848 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
850 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
851 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
852 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
854 while(isspace(*nptr)) nptr++;
859 } else if(*nptr == '+')
862 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
875 char cur = tolower(*nptr);
883 if(cur<'a' || cur>='a'+base-10)
893 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
894 ret = MSVCRT_I64_MAX;
895 *MSVCRT__errno() = MSVCRT_ERANGE;
896 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
897 ret = MSVCRT_I64_MIN;
898 *MSVCRT__errno() = MSVCRT_ERANGE;
904 *endptr = (char*)nptr;
909 /*********************************************************************
910 * _strtoi64 (MSVCRT.@)
912 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
914 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
917 /*********************************************************************
920 int MSVCRT__atoi_l(const char *str, MSVCRT__locale_t locale)
922 __int64 ret = MSVCRT_strtoi64_l(str, NULL, 10, locale);
926 *MSVCRT__errno() = MSVCRT_ERANGE;
927 } else if(ret < INT_MIN) {
929 *MSVCRT__errno() = MSVCRT_ERANGE;
934 /*********************************************************************
935 * _strtoui64_l (MSVCRT.@)
937 * FIXME: locale parameter is ignored
939 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
941 BOOL negative = FALSE;
942 unsigned __int64 ret = 0;
944 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
946 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
947 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
948 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
950 while(isspace(*nptr)) nptr++;
955 } else if(*nptr == '+')
958 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
971 char cur = tolower(*nptr);
979 if(cur<'a' || cur>='a'+base-10)
986 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
987 ret = MSVCRT_UI64_MAX;
988 *MSVCRT__errno() = MSVCRT_ERANGE;
994 *endptr = (char*)nptr;
996 return negative ? -ret : ret;
999 /*********************************************************************
1000 * _strtoui64 (MSVCRT.@)
1002 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
1004 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
1007 /*********************************************************************
1008 * _ltoa_s (MSVCRT.@)
1010 int CDECL _ltoa_s(MSVCRT_long value, char *str, MSVCRT_size_t size, int radix)
1015 char buffer[33], *pos;
1018 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1019 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1020 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1023 return MSVCRT_EINVAL;
1026 if (value < 0 && radix == 10)
1042 digit = val % radix;
1046 *--pos = '0' + digit;
1048 *--pos = 'a' + digit - 10;
1055 len = buffer + 33 - pos;
1061 /* Copy the temporary buffer backwards up to the available number of
1062 * characters. Don't copy the negative sign if present. */
1070 for (pos = buffer + 31, i = 0; i < size; i++)
1074 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1075 return MSVCRT_ERANGE;
1078 memcpy(str, pos, len);
1082 /*********************************************************************
1083 * _ltow_s (MSVCRT.@)
1085 int CDECL _ltow_s(MSVCRT_long value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1090 MSVCRT_wchar_t buffer[33], *pos;
1093 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1094 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1095 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1098 return MSVCRT_EINVAL;
1101 if (value < 0 && radix == 10)
1117 digit = val % radix;
1121 *--pos = '0' + digit;
1123 *--pos = 'a' + digit - 10;
1130 len = buffer + 33 - pos;
1134 MSVCRT_wchar_t *p = str;
1136 /* Copy the temporary buffer backwards up to the available number of
1137 * characters. Don't copy the negative sign if present. */
1145 for (pos = buffer + 31, i = 0; i < size; i++)
1149 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1150 return MSVCRT_ERANGE;
1153 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1157 /*********************************************************************
1158 * _itoa_s (MSVCRT.@)
1160 int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
1162 return _ltoa_s(value, str, size, radix);
1165 /*********************************************************************
1166 * _itow_s (MSVCRT.@)
1168 int CDECL _itow_s(int value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1170 return _ltow_s(value, str, size, radix);
1173 /*********************************************************************
1174 * _ui64toa_s (MSVCRT.@)
1176 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
1177 MSVCRT_size_t size, int radix)
1179 char buffer[65], *pos;
1182 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1183 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1184 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1187 return MSVCRT_EINVAL;
1194 digit = value%radix;
1200 *--pos = 'a'+digit-10;
1203 if(buffer-pos+65 > size) {
1204 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL);
1205 return MSVCRT_EINVAL;
1208 memcpy(str, pos, buffer-pos+65);
1212 /*********************************************************************
1213 * _ui64tow_s (MSVCRT.@)
1215 int CDECL MSVCRT__ui64tow_s( unsigned __int64 value, MSVCRT_wchar_t *str,
1216 MSVCRT_size_t size, int radix )
1218 MSVCRT_wchar_t buffer[65], *pos;
1221 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1222 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1223 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1226 return MSVCRT_EINVAL;
1233 digit = value % radix;
1234 value = value / radix;
1236 *--pos = '0' + digit;
1238 *--pos = 'a' + digit - 10;
1239 } while (value != 0);
1241 if(buffer-pos+65 > size) {
1242 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL);
1243 return MSVCRT_EINVAL;
1246 memcpy(str, pos, buffer-pos+65);
1250 /*********************************************************************
1251 * _ultoa_s (MSVCRT.@)
1253 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
1256 char buffer[33], *pos;
1259 if (!str || !size || radix < 2 || radix > 36)
1264 *MSVCRT__errno() = MSVCRT_EINVAL;
1265 return MSVCRT_EINVAL;
1273 digit = value % radix;
1277 *--pos = '0' + digit;
1279 *--pos = 'a' + digit - 10;
1283 len = buffer + 33 - pos;
1289 /* Copy the temporary buffer backwards up to the available number of
1292 for (pos = buffer + 31, i = 0; i < size; i++)
1296 *MSVCRT__errno() = MSVCRT_ERANGE;
1297 return MSVCRT_ERANGE;
1300 memcpy(str, pos, len);
1304 /*********************************************************************
1305 * _ultow_s (MSVCRT.@)
1307 int CDECL _ultow_s(MSVCRT_ulong value, WCHAR *str, MSVCRT_size_t size, int radix)
1310 WCHAR buffer[33], *pos;
1313 if (!str || !size || radix < 2 || radix > 36)
1318 *MSVCRT__errno() = MSVCRT_EINVAL;
1319 return MSVCRT_EINVAL;
1327 digit = value % radix;
1331 *--pos = '0' + digit;
1333 *--pos = 'a' + digit - 10;
1337 len = buffer + 33 - pos;
1343 /* Copy the temporary buffer backwards up to the available number of
1346 for (pos = buffer + 31, i = 0; i < size; i++)
1350 *MSVCRT__errno() = MSVCRT_ERANGE;
1351 return MSVCRT_ERANGE;
1354 memcpy(str, pos, len * sizeof(WCHAR));
1358 /*********************************************************************
1359 * _i64toa_s (MSVCRT.@)
1361 int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix)
1363 unsigned __int64 val;
1366 char buffer[65], *pos;
1369 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1370 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1371 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1374 return MSVCRT_EINVAL;
1377 if (value < 0 && radix == 10)
1393 digit = val % radix;
1397 *--pos = '0' + digit;
1399 *--pos = 'a' + digit - 10;
1406 len = buffer + 65 - pos;
1412 /* Copy the temporary buffer backwards up to the available number of
1413 * characters. Don't copy the negative sign if present. */
1421 for (pos = buffer + 63, i = 0; i < size; i++)
1425 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1426 return MSVCRT_ERANGE;
1429 memcpy(str, pos, len);
1433 /*********************************************************************
1434 * _i64tow_s (MSVCRT.@)
1436 int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1438 unsigned __int64 val;
1441 MSVCRT_wchar_t buffer[65], *pos;
1444 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1445 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1446 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1449 return MSVCRT_EINVAL;
1452 if (value < 0 && radix == 10)
1468 digit = val % radix;
1472 *--pos = '0' + digit;
1474 *--pos = 'a' + digit - 10;
1481 len = buffer + 65 - pos;
1485 MSVCRT_wchar_t *p = str;
1487 /* Copy the temporary buffer backwards up to the available number of
1488 * characters. Don't copy the negative sign if present. */
1496 for (pos = buffer + 63, i = 0; i < size; i++)
1500 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1501 return MSVCRT_ERANGE;
1504 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1508 #define I10_OUTPUT_MAX_PREC 21
1509 /* Internal structure used by $I10_OUTPUT */
1510 struct _I10_OUTPUT_DATA {
1514 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
1517 /*********************************************************************
1518 * $I10_OUTPUT (MSVCRT.@)
1519 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1520 * prec - precision of part, we're interested in
1521 * flag - 0 for first prec digits, 1 for fractional part
1522 * data - data to be populated
1525 * 0 if given double is NaN or INF
1529 * Native sets last byte of data->str to '0' or '9', I don't know what
1530 * it means. Current implementation sets it always to '0'.
1532 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
1534 static const char inf_str[] = "1#INF";
1535 static const char nan_str[] = "1#QNAN";
1537 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1538 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1539 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1543 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
1546 memcpy(&ld, &ld80, 10);
1548 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
1559 memcpy(data->str, inf_str, sizeof(inf_str));
1567 memcpy(data->str, nan_str, sizeof(nan_str));
1573 int exp = 1+floor(log10(d));
1581 if(prec+1 > I10_OUTPUT_MAX_PREC)
1582 prec = I10_OUTPUT_MAX_PREC-1;
1588 sprintf(format, "%%.%dle", prec);
1589 sprintf(buf, format, d);
1592 data->pos = atoi(buf+prec+3);
1596 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1599 memcpy(data->str, buf+1, data->len);
1600 data->str[data->len] = '\0';
1602 if(buf[1]!='0' && prec-data->len+1>0)
1603 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1607 #undef I10_OUTPUT_MAX_PREC