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 BOOL found_digit = FALSE;
296 if (!MSVCRT_CHECK_PMT(str != NULL)) return 0;
299 locinfo = get_locinfo();
301 locinfo = locale->locinfo;
303 /* FIXME: use *_l functions */
316 hlp = d*10+*(p++)-'0';
317 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
328 if(*p == *locinfo->lconv->decimal_point)
333 hlp = d*10+*(p++)-'0';
334 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
349 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
361 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
367 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
368 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
371 if(*p=='-' || *p=='+')
377 fpcontrol = _control87(0, 0);
378 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
379 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
382 ret = (double)sign*d*pow(10, exp);
384 ret = (double)sign*d/pow(10, -exp);
386 _control87(fpcontrol, 0xffffffff);
388 if((d && ret==0.0) || isinf(ret))
389 *MSVCRT__errno() = MSVCRT_ERANGE;
397 /*********************************************************************
400 double CDECL MSVCRT_strtod( const char *str, char **end )
402 return MSVCRT_strtod_l( str, end, NULL );
405 /*********************************************************************
408 double CDECL MSVCRT_atof( const char *str )
410 return MSVCRT_strtod_l(str, NULL, NULL);
413 /*********************************************************************
416 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
418 return MSVCRT_strtod_l(str, NULL, locale);
421 /*********************************************************************
422 * _atoflt_l (MSVCRT.@)
424 int CDECL MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT *value, char *str, MSVCRT__locale_t locale)
426 MSVCRT_pthreadlocinfo locinfo;
427 unsigned __int64 d=0, hlp;
432 BOOL found_digit = FALSE;
435 locinfo = get_locinfo();
437 locinfo = locale->locinfo;
439 /* FIXME: use *_l functions */
452 hlp = d*10+*(p++)-'0';
453 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
464 if(*p == *locinfo->lconv->decimal_point)
469 hlp = d*10+*(p++)-'0';
470 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
484 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
496 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
502 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
503 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
506 if(*p=='-' || *p=='+')
512 fpcontrol = _control87(0, 0);
513 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
514 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
517 value->f = (float)sign*d*powf(10, exp);
519 value->f = (float)sign*d/powf(10, -exp);
521 _control87(fpcontrol, 0xffffffff);
523 if((d && value->f==0.0) || isinf(value->f))
524 ret = exp > 0 ? MSVCRT__OVERFLOW : MSVCRT__UNDERFLOW;
529 /*********************************************************************
530 * _strcoll_l (MSVCRT.@)
532 int CDECL MSVCRT_strcoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
534 MSVCRT_pthreadlocinfo locinfo;
537 locinfo = get_locinfo();
539 locinfo = locale->locinfo;
541 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
544 /*********************************************************************
547 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
549 return MSVCRT_strcoll_l(str1, str2, NULL);
552 /*********************************************************************
553 * _stricoll_l (MSVCRT.@)
555 int CDECL MSVCRT__stricoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
557 MSVCRT_pthreadlocinfo locinfo;
560 locinfo = get_locinfo();
562 locinfo = locale->locinfo;
564 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
565 str1, -1, str2, -1)-CSTR_EQUAL;
568 /*********************************************************************
569 * _stricoll (MSVCRT.@)
571 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
573 return MSVCRT__stricoll_l(str1, str2, NULL);
576 /*********************************************************************
577 * _strncoll_l (MSVCRT.@)
579 int CDECL MSVCRT_strncoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
581 MSVCRT_pthreadlocinfo locinfo;
584 locinfo = get_locinfo();
586 locinfo = locale->locinfo;
588 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, count, str2, count)-CSTR_EQUAL;
591 /*********************************************************************
592 * strncoll (MSVCRT.@)
594 int CDECL MSVCRT_strncoll( const char* str1, const char* str2, MSVCRT_size_t count )
596 return MSVCRT_strncoll_l(str1, str2, count, NULL);
599 /*********************************************************************
600 * _strnicoll_l (MSVCRT.@)
602 int CDECL MSVCRT__strnicoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
604 MSVCRT_pthreadlocinfo locinfo;
607 locinfo = get_locinfo();
609 locinfo = locale->locinfo;
611 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
612 str1, count, str2, count)-CSTR_EQUAL;
615 /*********************************************************************
616 * _strnicoll (MSVCRT.@)
618 int CDECL MSVCRT__strnicoll( const char* str1, const char* str2, MSVCRT_size_t count )
620 return MSVCRT__strnicoll_l(str1, str2, count, NULL);
623 /*********************************************************************
624 * strcpy_s (MSVCRT.@)
626 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
629 if(!elem) return MSVCRT_EINVAL;
630 if(!dst) return MSVCRT_EINVAL;
634 return MSVCRT_EINVAL;
637 for(i = 0; i < elem; i++)
639 if((dst[i] = src[i]) == '\0') return 0;
642 return MSVCRT_ERANGE;
645 /*********************************************************************
646 * strcat_s (MSVCRT.@)
648 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
651 if(!dst) return MSVCRT_EINVAL;
652 if(elem == 0) return MSVCRT_EINVAL;
656 return MSVCRT_EINVAL;
659 for(i = 0; i < elem; i++)
663 for(j = 0; (j + i) < elem; j++)
665 if((dst[j + i] = src[j]) == '\0') return 0;
669 /* Set the first element to 0, not the first element after the skipped part */
671 return MSVCRT_ERANGE;
674 /*********************************************************************
675 * strncat_s (MSVCRT.@)
677 int CDECL MSVCRT_strncat_s( char* dst, MSVCRT_size_t elem, const char* src, MSVCRT_size_t count )
681 if (!MSVCRT_CHECK_PMT(dst != 0)) return MSVCRT_EINVAL;
682 if (!MSVCRT_CHECK_PMT(elem != 0)) return MSVCRT_EINVAL;
683 if (!MSVCRT_CHECK_PMT(src != 0))
686 return MSVCRT_EINVAL;
689 for(i = 0; i < elem; i++)
693 for(j = 0; (j + i) < elem; j++)
695 if(count == MSVCRT__TRUNCATE && j + i == elem - 1)
698 return MSVCRT_STRUNCATE;
700 if(j == count || (dst[j + i] = src[j]) == '\0')
708 /* Set the first element to 0, not the first element after the skipped part */
710 return MSVCRT_ERANGE;
713 /*********************************************************************
716 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
718 /* FIXME: handle Windows locale */
719 return strxfrm( dest, src, len );
722 /********************************************************************
723 * _atoldbl (MSVCRT.@)
725 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
727 /* FIXME needs error checking for huge/small values */
730 TRACE("str %s value %p\n",str,value);
732 memcpy(value, &ld, 10);
734 FIXME("stub, str %s value %p\n",str,value);
739 /********************************************************************
740 * __STRINGTOLD (MSVCRT.@)
742 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
746 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
748 memcpy(value, &ld, 10);
750 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
755 /******************************************************************
758 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
760 /* wrapper to forward libc error code to msvcrt's error codes */
764 ret = strtol(nptr, end, base);
767 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
768 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
770 /* cope with the fact that we may use 64bit long integers on libc
771 * while msvcrt always uses 32bit long integers
773 if (ret > MSVCRT_LONG_MAX)
775 ret = MSVCRT_LONG_MAX;
776 *MSVCRT__errno() = MSVCRT_ERANGE;
778 else if (ret < -MSVCRT_LONG_MAX - 1)
780 ret = -MSVCRT_LONG_MAX - 1;
781 *MSVCRT__errno() = MSVCRT_ERANGE;
789 /******************************************************************
792 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
794 /* wrapper to forward libc error code to msvcrt's error codes */
798 ret = strtoul(nptr, end, base);
801 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
802 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
804 /* cope with the fact that we may use 64bit long integers on libc
805 * while msvcrt always uses 32bit long integers
807 if (ret > MSVCRT_ULONG_MAX)
809 ret = MSVCRT_ULONG_MAX;
810 *MSVCRT__errno() = MSVCRT_ERANGE;
818 /******************************************************************
821 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
825 for(i=0; i<maxlen; i++)
831 /*********************************************************************
832 * _strtoi64_l (MSVCRT.@)
834 * FIXME: locale parameter is ignored
836 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
838 BOOL negative = FALSE;
841 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
843 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
844 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
845 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
847 while(isspace(*nptr)) nptr++;
852 } else if(*nptr == '+')
855 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
868 char cur = tolower(*nptr);
876 if(cur<'a' || cur>='a'+base-10)
886 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
887 ret = MSVCRT_I64_MAX;
888 *MSVCRT__errno() = MSVCRT_ERANGE;
889 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
890 ret = MSVCRT_I64_MIN;
891 *MSVCRT__errno() = MSVCRT_ERANGE;
897 *endptr = (char*)nptr;
902 /*********************************************************************
903 * _strtoi64 (MSVCRT.@)
905 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
907 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
910 /*********************************************************************
911 * _strtoui64_l (MSVCRT.@)
913 * FIXME: locale parameter is ignored
915 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
917 BOOL negative = FALSE;
918 unsigned __int64 ret = 0;
920 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
922 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
923 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
924 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
926 while(isspace(*nptr)) nptr++;
931 } else if(*nptr == '+')
934 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
947 char cur = tolower(*nptr);
955 if(cur<'a' || cur>='a'+base-10)
962 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
963 ret = MSVCRT_UI64_MAX;
964 *MSVCRT__errno() = MSVCRT_ERANGE;
970 *endptr = (char*)nptr;
972 return negative ? -ret : ret;
975 /*********************************************************************
976 * _strtoui64 (MSVCRT.@)
978 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
980 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
983 /*********************************************************************
986 int CDECL _ltoa_s(MSVCRT_long value, char *str, MSVCRT_size_t size, int radix)
991 char buffer[33], *pos;
994 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
995 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
996 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
999 return MSVCRT_EINVAL;
1002 if (value < 0 && radix == 10)
1018 digit = val % radix;
1022 *--pos = '0' + digit;
1024 *--pos = 'a' + digit - 10;
1031 len = buffer + 33 - pos;
1037 /* Copy the temporary buffer backwards up to the available number of
1038 * characters. Don't copy the negative sign if present. */
1046 for (pos = buffer + 31, i = 0; i < size; i++)
1050 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1051 return MSVCRT_ERANGE;
1054 memcpy(str, pos, len);
1058 /*********************************************************************
1059 * _ltow_s (MSVCRT.@)
1061 int CDECL _ltow_s(MSVCRT_long value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1066 MSVCRT_wchar_t buffer[33], *pos;
1069 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1070 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1071 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1074 return MSVCRT_EINVAL;
1077 if (value < 0 && radix == 10)
1093 digit = val % radix;
1097 *--pos = '0' + digit;
1099 *--pos = 'a' + digit - 10;
1106 len = buffer + 33 - pos;
1110 MSVCRT_wchar_t *p = str;
1112 /* Copy the temporary buffer backwards up to the available number of
1113 * characters. Don't copy the negative sign if present. */
1121 for (pos = buffer + 31, i = 0; i < size; i++)
1125 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1126 return MSVCRT_ERANGE;
1129 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1133 /*********************************************************************
1134 * _itoa_s (MSVCRT.@)
1136 int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
1138 return _ltoa_s(value, str, size, radix);
1141 /*********************************************************************
1142 * _itow_s (MSVCRT.@)
1144 int CDECL _itow_s(int value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1146 return _ltow_s(value, str, size, radix);
1149 /*********************************************************************
1150 * _ui64toa_s (MSVCRT.@)
1152 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
1153 MSVCRT_size_t size, int radix)
1155 char buffer[65], *pos;
1158 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1159 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1160 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1163 return MSVCRT_EINVAL;
1170 digit = value%radix;
1176 *--pos = 'a'+digit-10;
1179 if(buffer-pos+65 > size) {
1180 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL);
1181 return MSVCRT_EINVAL;
1184 memcpy(str, pos, buffer-pos+65);
1188 /*********************************************************************
1189 * _ui64tow_s (MSVCRT.@)
1191 int CDECL MSVCRT__ui64tow_s( unsigned __int64 value, MSVCRT_wchar_t *str,
1192 MSVCRT_size_t size, int radix )
1194 MSVCRT_wchar_t buffer[65], *pos;
1197 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1198 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1199 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1202 return MSVCRT_EINVAL;
1209 digit = value % radix;
1210 value = value / radix;
1212 *--pos = '0' + digit;
1214 *--pos = 'a' + digit - 10;
1215 } while (value != 0);
1217 if(buffer-pos+65 > size) {
1218 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL);
1219 return MSVCRT_EINVAL;
1222 memcpy(str, pos, buffer-pos+65);
1226 /*********************************************************************
1227 * _ultoa_s (MSVCRT.@)
1229 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
1232 char buffer[33], *pos;
1235 if (!str || !size || radix < 2 || radix > 36)
1240 *MSVCRT__errno() = MSVCRT_EINVAL;
1241 return MSVCRT_EINVAL;
1249 digit = value % radix;
1253 *--pos = '0' + digit;
1255 *--pos = 'a' + digit - 10;
1259 len = buffer + 33 - pos;
1265 /* Copy the temporary buffer backwards up to the available number of
1268 for (pos = buffer + 31, i = 0; i < size; i++)
1272 *MSVCRT__errno() = MSVCRT_ERANGE;
1273 return MSVCRT_ERANGE;
1276 memcpy(str, pos, len);
1280 /*********************************************************************
1281 * _ultow_s (MSVCRT.@)
1283 int CDECL _ultow_s(MSVCRT_ulong value, WCHAR *str, MSVCRT_size_t size, int radix)
1286 WCHAR buffer[33], *pos;
1289 if (!str || !size || radix < 2 || radix > 36)
1294 *MSVCRT__errno() = MSVCRT_EINVAL;
1295 return MSVCRT_EINVAL;
1303 digit = value % radix;
1307 *--pos = '0' + digit;
1309 *--pos = 'a' + digit - 10;
1313 len = buffer + 33 - pos;
1319 /* Copy the temporary buffer backwards up to the available number of
1322 for (pos = buffer + 31, i = 0; i < size; i++)
1326 *MSVCRT__errno() = MSVCRT_ERANGE;
1327 return MSVCRT_ERANGE;
1330 memcpy(str, pos, len * sizeof(WCHAR));
1334 /*********************************************************************
1335 * _i64toa_s (MSVCRT.@)
1337 int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix)
1339 unsigned __int64 val;
1342 char buffer[65], *pos;
1345 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1346 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1347 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1350 return MSVCRT_EINVAL;
1353 if (value < 0 && radix == 10)
1369 digit = val % radix;
1373 *--pos = '0' + digit;
1375 *--pos = 'a' + digit - 10;
1382 len = buffer + 65 - pos;
1388 /* Copy the temporary buffer backwards up to the available number of
1389 * characters. Don't copy the negative sign if present. */
1397 for (pos = buffer + 63, i = 0; i < size; i++)
1401 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1402 return MSVCRT_ERANGE;
1405 memcpy(str, pos, len);
1409 /*********************************************************************
1410 * _i64tow_s (MSVCRT.@)
1412 int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1414 unsigned __int64 val;
1417 MSVCRT_wchar_t buffer[65], *pos;
1420 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1421 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1422 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1425 return MSVCRT_EINVAL;
1428 if (value < 0 && radix == 10)
1444 digit = val % radix;
1448 *--pos = '0' + digit;
1450 *--pos = 'a' + digit - 10;
1457 len = buffer + 65 - pos;
1461 MSVCRT_wchar_t *p = str;
1463 /* Copy the temporary buffer backwards up to the available number of
1464 * characters. Don't copy the negative sign if present. */
1472 for (pos = buffer + 63, i = 0; i < size; i++)
1476 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1477 return MSVCRT_ERANGE;
1480 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1484 #define I10_OUTPUT_MAX_PREC 21
1485 /* Internal structure used by $I10_OUTPUT */
1486 struct _I10_OUTPUT_DATA {
1490 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
1493 /*********************************************************************
1494 * $I10_OUTPUT (MSVCRT.@)
1495 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1496 * prec - precision of part, we're interested in
1497 * flag - 0 for first prec digits, 1 for fractional part
1498 * data - data to be populated
1501 * 0 if given double is NaN or INF
1505 * Native sets last byte of data->str to '0' or '9', I don't know what
1506 * it means. Current implementation sets it always to '0'.
1508 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
1510 static const char inf_str[] = "1#INF";
1511 static const char nan_str[] = "1#QNAN";
1513 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1514 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1515 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1519 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
1522 memcpy(&ld, &ld80, 10);
1524 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
1535 memcpy(data->str, inf_str, sizeof(inf_str));
1543 memcpy(data->str, nan_str, sizeof(nan_str));
1549 int exp = 1+floor(log10(d));
1557 if(prec+1 > I10_OUTPUT_MAX_PREC)
1558 prec = I10_OUTPUT_MAX_PREC-1;
1564 sprintf(format, "%%.%dle", prec);
1565 sprintf(buf, format, d);
1568 data->pos = atoi(buf+prec+3);
1572 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1575 memcpy(data->str, buf+1, data->len);
1576 data->str[data->len] = '\0';
1578 if(buf[1]!='0' && prec-data->len+1>0)
1579 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1583 #undef I10_OUTPUT_MAX_PREC