msvcrt: Added _strncoll_l implementation.
[wine/multimedia.git] / dlls / msvcrt / string.c
blobb6fe68b0bbda117fb660df7a0a27b536d40ea736
1 /*
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <math.h>
31 #include <limits.h>
32 #include <errno.h>
33 #include "msvcrt.h"
34 #include "winnls.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 /*********************************************************************
40 * _mbsdup (MSVCRT.@)
41 * _strdup (MSVCRT.@)
43 char* CDECL _strdup(const char* str)
45 if(str)
47 char * ret = MSVCRT_malloc(strlen(str)+1);
48 if (ret) strcpy( ret, str );
49 return ret;
51 else return 0;
54 /*********************************************************************
55 * _strlwr_s_l (MSVCRT.@)
57 int CDECL _strlwr_s_l(char *str, MSVCRT_size_t len, MSVCRT__locale_t locale)
59 char *ptr = str;
61 if(!locale)
62 locale = get_locale();
64 if (!str || !len)
66 *MSVCRT__errno() = MSVCRT_EINVAL;
67 return MSVCRT_EINVAL;
70 while (len && *ptr)
72 len--;
73 ptr++;
76 if (!len)
78 str[0] = '\0';
79 *MSVCRT__errno() = MSVCRT_EINVAL;
80 return MSVCRT_EINVAL;
83 while (*str)
85 *str = MSVCRT__tolower_l(*str, locale);
86 str++;
89 return 0;
92 /*********************************************************************
93 * _strlwr_s (MSVCRT.@)
95 int CDECL _strlwr_s(char *str, MSVCRT_size_t len)
97 return _strlwr_s_l(str, len, NULL);
100 /*********************************************************************
101 * _strlwr_l (MSVCRT.@)
103 int CDECL _strlwr_l(char *str, MSVCRT__locale_t locale)
105 return _strlwr_s_l(str, -1, locale);
108 /*********************************************************************
109 * _strlwr (MSVCRT.@)
111 int CDECL _strlwr(char *str)
113 return _strlwr_s_l(str, -1, NULL);
116 /*********************************************************************
117 * _strupr_s_l (MSVCRT.@)
119 int CDECL _strupr_s_l(char *str, MSVCRT_size_t len, MSVCRT__locale_t locale)
121 char *ptr = str;
123 if(!locale)
124 locale = get_locale();
126 if (!str || !len)
128 *MSVCRT__errno() = MSVCRT_EINVAL;
129 return MSVCRT_EINVAL;
132 while (len && *ptr)
134 len--;
135 ptr++;
138 if (!len)
140 str[0] = '\0';
141 *MSVCRT__errno() = MSVCRT_EINVAL;
142 return MSVCRT_EINVAL;
145 while (*str)
147 *str = MSVCRT__toupper_l(*str, locale);
148 str++;
151 return 0;
154 /*********************************************************************
155 * _strupr_s (MSVCRT.@)
157 int CDECL _strupr_s(char *str, MSVCRT_size_t len)
159 return _strupr_s_l(str, len, NULL);
162 /*********************************************************************
163 * _strupr_l (MSVCRT.@)
165 int CDECL _strupr_l(char *str, MSVCRT__locale_t locale)
167 return _strupr_s_l(str, -1, locale);
170 /*********************************************************************
171 * _strupr (MSVCRT.@)
173 int CDECL _strupr(char *str)
175 return _strupr_s_l(str, -1, NULL);
178 /*********************************************************************
179 * _strnset (MSVCRT.@)
181 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
183 if (len > 0 && str)
184 while (*str && len--)
185 *str++ = value;
186 return str;
189 /*********************************************************************
190 * _strrev (MSVCRT.@)
192 char* CDECL _strrev(char* str)
194 char * p1;
195 char * p2;
197 if (str && *str)
198 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
200 *p1 ^= *p2;
201 *p2 ^= *p1;
202 *p1 ^= *p2;
205 return str;
208 /*********************************************************************
209 * _strset (MSVCRT.@)
211 char* CDECL _strset(char* str, int value)
213 char *ptr = str;
214 while (*ptr)
215 *ptr++ = value;
217 return str;
220 /*********************************************************************
221 * strtok (MSVCRT.@)
223 char * CDECL MSVCRT_strtok( char *str, const char *delim )
225 thread_data_t *data = msvcrt_get_thread_data();
226 char *ret;
228 if (!str)
229 if (!(str = data->strtok_next)) return NULL;
231 while (*str && strchr( delim, *str )) str++;
232 if (!*str) return NULL;
233 ret = str++;
234 while (*str && !strchr( delim, *str )) str++;
235 if (*str) *str++ = 0;
236 data->strtok_next = str;
237 return ret;
240 /*********************************************************************
241 * strtok_s (MSVCRT.@)
243 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
245 if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(ctx != NULL) ||
246 !MSVCRT_CHECK_PMT(str != NULL || *ctx != NULL)) {
247 *MSVCRT__errno() = MSVCRT_EINVAL;
248 return NULL;
251 if(!str)
252 str = *ctx;
254 while(*str && strchr(delim, *str))
255 str++;
256 if(!*str)
257 return NULL;
259 *ctx = str+1;
260 while(**ctx && !strchr(delim, **ctx))
261 (*ctx)++;
262 if(**ctx)
263 *(*ctx)++ = 0;
265 return str;
268 /*********************************************************************
269 * _swab (MSVCRT.@)
271 void CDECL MSVCRT__swab(char* src, char* dst, int len)
273 if (len > 1)
275 len = (unsigned)len >> 1;
277 while (len--) {
278 char s0 = src[0];
279 char s1 = src[1];
280 *dst++ = s1;
281 *dst++ = s0;
282 src = src + 2;
287 /*********************************************************************
288 * strtod_l (MSVCRT.@)
290 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
292 unsigned __int64 d=0, hlp;
293 unsigned fpcontrol;
294 int exp=0, sign=1;
295 const char *p;
296 double ret;
297 BOOL found_digit = FALSE;
299 if (!MSVCRT_CHECK_PMT(str != NULL)) {
300 *MSVCRT__errno() = MSVCRT_EINVAL;
301 return 0;
304 if(!locale)
305 locale = get_locale();
307 /* FIXME: use *_l functions */
308 p = str;
309 while(isspace(*p))
310 p++;
312 if(*p == '-') {
313 sign = -1;
314 p++;
315 } else if(*p == '+')
316 p++;
318 while(isdigit(*p)) {
319 found_digit = TRUE;
320 hlp = d*10+*(p++)-'0';
321 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
322 exp++;
323 break;
324 } else
325 d = hlp;
327 while(isdigit(*p)) {
328 exp++;
329 p++;
332 if(*p == *locale->locinfo->lconv->decimal_point)
333 p++;
335 while(isdigit(*p)) {
336 found_digit = TRUE;
337 hlp = d*10+*(p++)-'0';
338 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
339 break;
341 d = hlp;
342 exp--;
344 while(isdigit(*p))
345 p++;
347 if(!found_digit) {
348 if(end)
349 *end = (char*)str;
350 return 0.0;
353 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
354 int e=0, s=1;
356 p++;
357 if(*p == '-') {
358 s = -1;
359 p++;
360 } else if(*p == '+')
361 p++;
363 if(isdigit(*p)) {
364 while(isdigit(*p)) {
365 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
366 e = INT_MAX;
367 p++;
369 e *= s;
371 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
372 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
373 else exp += e;
374 } else {
375 if(*p=='-' || *p=='+')
376 p--;
377 p--;
381 fpcontrol = _control87(0, 0);
382 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
383 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
385 if(exp>0)
386 ret = (double)sign*d*pow(10, exp);
387 else
388 ret = (double)sign*d/pow(10, -exp);
390 _control87(fpcontrol, 0xffffffff);
392 if((d && ret==0.0) || isinf(ret))
393 *MSVCRT__errno() = MSVCRT_ERANGE;
395 if(end)
396 *end = (char*)p;
398 return ret;
401 /*********************************************************************
402 * strtod (MSVCRT.@)
404 double CDECL MSVCRT_strtod( const char *str, char **end )
406 return MSVCRT_strtod_l( str, end, NULL );
409 /*********************************************************************
410 * atof (MSVCRT.@)
412 double CDECL MSVCRT_atof( const char *str )
414 return MSVCRT_strtod_l(str, NULL, NULL);
417 /*********************************************************************
418 * _atof_l (MSVCRT.@)
420 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
422 return MSVCRT_strtod_l(str, NULL, locale);
425 /*********************************************************************
426 * _atoflt_l (MSVCRT.@)
428 int CDECL MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT *value, char *str, MSVCRT__locale_t locale)
430 unsigned __int64 d=0, hlp;
431 unsigned fpcontrol;
432 int exp=0, sign=1;
433 const char *p;
434 int ret=0;
435 BOOL found_digit = FALSE;
437 if(!locale)
438 locale = get_locale();
440 /* FIXME: use *_l functions */
441 p = str;
442 while(isspace(*p))
443 p++;
445 if(*p == '-') {
446 sign = -1;
447 p++;
448 } else if(*p == '+')
449 p++;
451 while(isdigit(*p)) {
452 found_digit = TRUE;
453 hlp = d*10+*(p++)-'0';
454 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
455 exp++;
456 break;
457 } else
458 d = hlp;
460 while(isdigit(*p)) {
461 exp++;
462 p++;
465 if(*p == *locale->locinfo->lconv->decimal_point)
466 p++;
468 while(isdigit(*p)) {
469 found_digit = TRUE;
470 hlp = d*10+*(p++)-'0';
471 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
472 break;
474 d = hlp;
475 exp--;
477 while(isdigit(*p))
478 p++;
480 if(!found_digit) {
481 value->f = 0.0;
482 return 0;
485 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
486 int e=0, s=1;
488 p++;
489 if(*p == '-') {
490 s = -1;
491 p++;
492 } else if(*p == '+')
493 p++;
495 if(isdigit(*p)) {
496 while(isdigit(*p)) {
497 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
498 e = INT_MAX;
499 p++;
501 e *= s;
503 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
504 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
505 else exp += e;
506 } else {
507 if(*p=='-' || *p=='+')
508 p--;
509 p--;
513 fpcontrol = _control87(0, 0);
514 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
515 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
517 if(exp>0)
518 value->f = (float)sign*d*powf(10, exp);
519 else
520 value->f = (float)sign*d/powf(10, -exp);
522 _control87(fpcontrol, 0xffffffff);
524 if((d && value->f==0.0) || isinf(value->f))
525 ret = exp > 0 ? MSVCRT__OVERFLOW : MSVCRT__UNDERFLOW;
527 return ret;
530 /*********************************************************************
531 * _strcoll_l (MSVCRT.@)
533 int CDECL MSVCRT_strcoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
535 if(!locale)
536 locale = get_locale();
538 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, -1, str2, -1)-2;
541 /*********************************************************************
542 * strcoll (MSVCRT.@)
544 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
546 return MSVCRT_strcoll_l(str1, str2, NULL);
549 /*********************************************************************
550 * _stricoll_l (MSVCRT.@)
552 int CDECL MSVCRT__stricoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
554 if(!locale)
555 locale = get_locale();
557 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
558 str1, -1, str2, -1)-2;
561 /*********************************************************************
562 * _stricoll (MSVCRT.@)
564 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
566 return MSVCRT__stricoll_l(str1, str2, NULL);
569 /*********************************************************************
570 * _strncoll_l (MSVCRT.@)
572 int CDECL MSVCRT_strncoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
574 if(!locale)
575 locale = get_locale();
577 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, count, str2, count)-2;
580 /*********************************************************************
581 * strncoll (MSVCRT.@)
583 int CDECL MSVCRT_strncoll( const char* str1, const char* str2, MSVCRT_size_t count )
585 return MSVCRT_strncoll_l(str1, str2, count, NULL);
588 /*********************************************************************
589 * strcpy_s (MSVCRT.@)
591 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
593 MSVCRT_size_t i;
594 if(!elem) return MSVCRT_EINVAL;
595 if(!dst) return MSVCRT_EINVAL;
596 if(!src)
598 dst[0] = '\0';
599 return MSVCRT_EINVAL;
602 for(i = 0; i < elem; i++)
604 if((dst[i] = src[i]) == '\0') return 0;
606 dst[0] = '\0';
607 return MSVCRT_ERANGE;
610 /*********************************************************************
611 * strcat_s (MSVCRT.@)
613 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
615 MSVCRT_size_t i, j;
616 if(!dst) return MSVCRT_EINVAL;
617 if(elem == 0) return MSVCRT_EINVAL;
618 if(!src)
620 dst[0] = '\0';
621 return MSVCRT_EINVAL;
624 for(i = 0; i < elem; i++)
626 if(dst[i] == '\0')
628 for(j = 0; (j + i) < elem; j++)
630 if((dst[j + i] = src[j]) == '\0') return 0;
634 /* Set the first element to 0, not the first element after the skipped part */
635 dst[0] = '\0';
636 return MSVCRT_ERANGE;
639 /*********************************************************************
640 * strncat_s (MSVCRT.@)
642 int CDECL MSVCRT_strncat_s( char* dst, MSVCRT_size_t elem, const char* src, MSVCRT_size_t count )
644 MSVCRT_size_t i, j;
645 if(!MSVCRT_CHECK_PMT(dst != 0) || !MSVCRT_CHECK_PMT(elem != 0))
646 return MSVCRT_EINVAL;
647 if(!MSVCRT_CHECK_PMT(src != 0))
649 dst[0] = '\0';
650 return MSVCRT_EINVAL;
653 for(i = 0; i < elem; i++)
655 if(dst[i] == '\0')
657 for(j = 0; (j + i) < elem; j++)
659 if(count == MSVCRT__TRUNCATE && j + i == elem - 1)
661 dst[j + i] = '\0';
662 return MSVCRT_STRUNCATE;
664 if(j == count || (dst[j + i] = src[j]) == '\0')
666 dst[j + i] = '\0';
667 return 0;
672 /* Set the first element to 0, not the first element after the skipped part */
673 dst[0] = '\0';
674 return MSVCRT_ERANGE;
677 /*********************************************************************
678 * strxfrm (MSVCRT.@)
680 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
682 /* FIXME: handle Windows locale */
683 return strxfrm( dest, src, len );
686 /********************************************************************
687 * _atoldbl (MSVCRT.@)
689 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
691 /* FIXME needs error checking for huge/small values */
692 #ifdef HAVE_STRTOLD
693 long double ld;
694 TRACE("str %s value %p\n",str,value);
695 ld = strtold(str,0);
696 memcpy(value, &ld, 10);
697 #else
698 FIXME("stub, str %s value %p\n",str,value);
699 #endif
700 return 0;
703 /********************************************************************
704 * __STRINGTOLD (MSVCRT.@)
706 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
708 #ifdef HAVE_STRTOLD
709 long double ld;
710 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
711 ld = strtold(str,0);
712 memcpy(value, &ld, 10);
713 #else
714 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
715 #endif
716 return 0;
719 /******************************************************************
720 * strtol (MSVCRT.@)
722 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
724 /* wrapper to forward libc error code to msvcrt's error codes */
725 long ret;
727 errno = 0;
728 ret = strtol(nptr, end, base);
729 switch (errno)
731 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
732 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
733 default:
734 /* cope with the fact that we may use 64bit long integers on libc
735 * while msvcrt always uses 32bit long integers
737 if (ret > MSVCRT_LONG_MAX)
739 ret = MSVCRT_LONG_MAX;
740 *MSVCRT__errno() = MSVCRT_ERANGE;
742 else if (ret < -MSVCRT_LONG_MAX - 1)
744 ret = -MSVCRT_LONG_MAX - 1;
745 *MSVCRT__errno() = MSVCRT_ERANGE;
747 break;
750 return ret;
753 /******************************************************************
754 * strtoul (MSVCRT.@)
756 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
758 /* wrapper to forward libc error code to msvcrt's error codes */
759 unsigned long ret;
761 errno = 0;
762 ret = strtoul(nptr, end, base);
763 switch (errno)
765 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
766 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
767 default:
768 /* cope with the fact that we may use 64bit long integers on libc
769 * while msvcrt always uses 32bit long integers
771 if (ret > MSVCRT_ULONG_MAX)
773 ret = MSVCRT_ULONG_MAX;
774 *MSVCRT__errno() = MSVCRT_ERANGE;
776 break;
779 return ret;
782 /******************************************************************
783 * strnlen (MSVCRT.@)
785 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
787 MSVCRT_size_t i;
789 for(i=0; i<maxlen; i++)
790 if(!s[i]) break;
792 return i;
795 /*********************************************************************
796 * _strtoi64_l (MSVCRT.@)
798 * FIXME: locale parameter is ignored
800 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
802 BOOL negative = FALSE;
803 __int64 ret = 0;
805 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
807 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
808 !MSVCRT_CHECK_PMT(base <= 36)) {
809 return 0;
812 while(isspace(*nptr)) nptr++;
814 if(*nptr == '-') {
815 negative = TRUE;
816 nptr++;
817 } else if(*nptr == '+')
818 nptr++;
820 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
821 base = 16;
822 nptr += 2;
825 if(base == 0) {
826 if(*nptr=='0')
827 base = 8;
828 else
829 base = 10;
832 while(*nptr) {
833 char cur = tolower(*nptr);
834 int v;
836 if(isdigit(cur)) {
837 if(cur >= '0'+base)
838 break;
839 v = cur-'0';
840 } else {
841 if(cur<'a' || cur>='a'+base-10)
842 break;
843 v = cur-'a'+10;
846 if(negative)
847 v = -v;
849 nptr++;
851 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
852 ret = MSVCRT_I64_MAX;
853 *MSVCRT__errno() = MSVCRT_ERANGE;
854 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
855 ret = MSVCRT_I64_MIN;
856 *MSVCRT__errno() = MSVCRT_ERANGE;
857 } else
858 ret = ret*base + v;
861 if(endptr)
862 *endptr = (char*)nptr;
864 return ret;
867 /*********************************************************************
868 * _strtoi64 (MSVCRT.@)
870 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
872 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
875 /*********************************************************************
876 * _strtoui64_l (MSVCRT.@)
878 * FIXME: locale parameter is ignored
880 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
882 BOOL negative = FALSE;
883 unsigned __int64 ret = 0;
885 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
887 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
888 !MSVCRT_CHECK_PMT(base <= 36)) {
889 return 0;
892 while(isspace(*nptr)) nptr++;
894 if(*nptr == '-') {
895 negative = TRUE;
896 nptr++;
897 } else if(*nptr == '+')
898 nptr++;
900 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
901 base = 16;
902 nptr += 2;
905 if(base == 0) {
906 if(*nptr=='0')
907 base = 8;
908 else
909 base = 10;
912 while(*nptr) {
913 char cur = tolower(*nptr);
914 int v;
916 if(isdigit(cur)) {
917 if(cur >= '0'+base)
918 break;
919 v = *nptr-'0';
920 } else {
921 if(cur<'a' || cur>='a'+base-10)
922 break;
923 v = cur-'a'+10;
926 nptr++;
928 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
929 ret = MSVCRT_UI64_MAX;
930 *MSVCRT__errno() = MSVCRT_ERANGE;
931 } else
932 ret = ret*base + v;
935 if(endptr)
936 *endptr = (char*)nptr;
938 return negative ? -ret : ret;
941 /*********************************************************************
942 * _strtoui64 (MSVCRT.@)
944 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
946 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
949 /*********************************************************************
950 * _ltoa_s (MSVCRT.@)
952 int CDECL _ltoa_s(MSVCRT_long value, char *str, MSVCRT_size_t size, int radix)
954 MSVCRT_ulong val;
955 unsigned int digit;
956 int is_negative;
957 char buffer[33], *pos;
958 size_t len;
960 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
961 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
963 if (str && size)
964 str[0] = '\0';
966 *MSVCRT__errno() = MSVCRT_EINVAL;
967 return MSVCRT_EINVAL;
970 if (value < 0 && radix == 10)
972 is_negative = 1;
973 val = -value;
975 else
977 is_negative = 0;
978 val = value;
981 pos = buffer + 32;
982 *pos = '\0';
986 digit = val % radix;
987 val /= radix;
989 if (digit < 10)
990 *--pos = '0' + digit;
991 else
992 *--pos = 'a' + digit - 10;
994 while (val != 0);
996 if (is_negative)
997 *--pos = '-';
999 len = buffer + 33 - pos;
1000 if (len > size)
1002 size_t i;
1003 char *p = str;
1005 /* Copy the temporary buffer backwards up to the available number of
1006 * characters. Don't copy the negative sign if present. */
1008 if (is_negative)
1010 p++;
1011 size--;
1014 for (pos = buffer + 31, i = 0; i < size; i++)
1015 *p++ = *pos--;
1017 str[0] = '\0';
1018 MSVCRT_INVALID_PMT("str[size] is too small");
1019 *MSVCRT__errno() = MSVCRT_ERANGE;
1020 return MSVCRT_ERANGE;
1023 memcpy(str, pos, len);
1024 return 0;
1027 /*********************************************************************
1028 * _ltow_s (MSVCRT.@)
1030 int CDECL _ltow_s(MSVCRT_long value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1032 MSVCRT_ulong val;
1033 unsigned int digit;
1034 int is_negative;
1035 MSVCRT_wchar_t buffer[33], *pos;
1036 size_t len;
1038 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1039 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1041 if (str && size)
1042 str[0] = '\0';
1044 *MSVCRT__errno() = MSVCRT_EINVAL;
1045 return MSVCRT_EINVAL;
1048 if (value < 0 && radix == 10)
1050 is_negative = 1;
1051 val = -value;
1053 else
1055 is_negative = 0;
1056 val = value;
1059 pos = buffer + 32;
1060 *pos = '\0';
1064 digit = val % radix;
1065 val /= radix;
1067 if (digit < 10)
1068 *--pos = '0' + digit;
1069 else
1070 *--pos = 'a' + digit - 10;
1072 while (val != 0);
1074 if (is_negative)
1075 *--pos = '-';
1077 len = buffer + 33 - pos;
1078 if (len > size)
1080 size_t i;
1081 MSVCRT_wchar_t *p = str;
1083 /* Copy the temporary buffer backwards up to the available number of
1084 * characters. Don't copy the negative sign if present. */
1086 if (is_negative)
1088 p++;
1089 size--;
1092 for (pos = buffer + 31, i = 0; i < size; i++)
1093 *p++ = *pos--;
1095 MSVCRT_INVALID_PMT("str[size] is too small");
1096 str[0] = '\0';
1097 *MSVCRT__errno() = MSVCRT_ERANGE;
1098 return MSVCRT_ERANGE;
1101 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1102 return 0;
1105 /*********************************************************************
1106 * _itoa_s (MSVCRT.@)
1108 int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
1110 return _ltoa_s(value, str, size, radix);
1113 /*********************************************************************
1114 * _itow_s (MSVCRT.@)
1116 int CDECL _itow_s(int value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1118 return _ltow_s(value, str, size, radix);
1121 /*********************************************************************
1122 * _ui64toa_s (MSVCRT.@)
1124 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
1125 MSVCRT_size_t size, int radix)
1127 char buffer[65], *pos;
1128 int digit;
1130 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1131 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
1132 *MSVCRT__errno() = MSVCRT_EINVAL;
1133 return MSVCRT_EINVAL;
1136 pos = buffer+64;
1137 *pos = '\0';
1139 do {
1140 digit = value%radix;
1141 value /= radix;
1143 if(digit < 10)
1144 *--pos = '0'+digit;
1145 else
1146 *--pos = 'a'+digit-10;
1147 }while(value != 0);
1149 if(buffer-pos+65 > size) {
1150 MSVCRT_INVALID_PMT("str[size] is too small");
1151 *MSVCRT__errno() = MSVCRT_EINVAL;
1152 return MSVCRT_EINVAL;
1155 memcpy(str, pos, buffer-pos+65);
1156 return 0;
1159 /*********************************************************************
1160 * _ui64tow_s (MSVCRT.@)
1162 int CDECL MSVCRT__ui64tow_s( unsigned __int64 value, MSVCRT_wchar_t *str,
1163 MSVCRT_size_t size, int radix )
1165 MSVCRT_wchar_t buffer[65], *pos;
1166 int digit;
1168 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1169 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
1170 *MSVCRT__errno() = MSVCRT_EINVAL;
1171 return MSVCRT_EINVAL;
1174 pos = &buffer[64];
1175 *pos = '\0';
1177 do {
1178 digit = value % radix;
1179 value = value / radix;
1180 if (digit < 10)
1181 *--pos = '0' + digit;
1182 else
1183 *--pos = 'a' + digit - 10;
1184 } while (value != 0);
1186 if(buffer-pos+65 > size) {
1187 MSVCRT_INVALID_PMT("str[size] is too small");
1188 *MSVCRT__errno() = MSVCRT_EINVAL;
1189 return MSVCRT_EINVAL;
1192 memcpy(str, pos, buffer-pos+65);
1193 return 0;
1196 /*********************************************************************
1197 * _ultoa_s (MSVCRT.@)
1199 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
1201 MSVCRT_ulong digit;
1202 char buffer[33], *pos;
1203 size_t len;
1205 if (!str || !size || radix < 2 || radix > 36)
1207 if (str && size)
1208 str[0] = '\0';
1210 *MSVCRT__errno() = MSVCRT_EINVAL;
1211 return MSVCRT_EINVAL;
1214 pos = buffer + 32;
1215 *pos = '\0';
1219 digit = value % radix;
1220 value /= radix;
1222 if (digit < 10)
1223 *--pos = '0' + digit;
1224 else
1225 *--pos = 'a' + digit - 10;
1227 while (value != 0);
1229 len = buffer + 33 - pos;
1230 if (len > size)
1232 size_t i;
1233 char *p = str;
1235 /* Copy the temporary buffer backwards up to the available number of
1236 * characters. */
1238 for (pos = buffer + 31, i = 0; i < size; i++)
1239 *p++ = *pos--;
1241 str[0] = '\0';
1242 *MSVCRT__errno() = MSVCRT_ERANGE;
1243 return MSVCRT_ERANGE;
1246 memcpy(str, pos, len);
1247 return 0;
1250 /*********************************************************************
1251 * _i64toa_s (MSVCRT.@)
1253 int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix)
1255 unsigned __int64 val;
1256 unsigned int digit;
1257 int is_negative;
1258 char buffer[65], *pos;
1259 size_t len;
1261 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1262 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1264 if (str && size)
1265 str[0] = '\0';
1267 *MSVCRT__errno() = MSVCRT_EINVAL;
1268 return MSVCRT_EINVAL;
1271 if (value < 0 && radix == 10)
1273 is_negative = 1;
1274 val = -value;
1276 else
1278 is_negative = 0;
1279 val = value;
1282 pos = buffer + 64;
1283 *pos = '\0';
1287 digit = val % radix;
1288 val /= radix;
1290 if (digit < 10)
1291 *--pos = '0' + digit;
1292 else
1293 *--pos = 'a' + digit - 10;
1295 while (val != 0);
1297 if (is_negative)
1298 *--pos = '-';
1300 len = buffer + 65 - pos;
1301 if (len > size)
1303 size_t i;
1304 char *p = str;
1306 /* Copy the temporary buffer backwards up to the available number of
1307 * characters. Don't copy the negative sign if present. */
1309 if (is_negative)
1311 p++;
1312 size--;
1315 for (pos = buffer + 63, i = 0; i < size; i++)
1316 *p++ = *pos--;
1318 str[0] = '\0';
1319 MSVCRT_INVALID_PMT("str[size] is too small");
1320 *MSVCRT__errno() = MSVCRT_ERANGE;
1321 return MSVCRT_ERANGE;
1324 memcpy(str, pos, len);
1325 return 0;
1328 /*********************************************************************
1329 * _i64tow_s (MSVCRT.@)
1331 int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1333 unsigned __int64 val;
1334 unsigned int digit;
1335 int is_negative;
1336 MSVCRT_wchar_t buffer[65], *pos;
1337 size_t len;
1339 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1340 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1342 if (str && size)
1343 str[0] = '\0';
1345 *MSVCRT__errno() = MSVCRT_EINVAL;
1346 return MSVCRT_EINVAL;
1349 if (value < 0 && radix == 10)
1351 is_negative = 1;
1352 val = -value;
1354 else
1356 is_negative = 0;
1357 val = value;
1360 pos = buffer + 64;
1361 *pos = '\0';
1365 digit = val % radix;
1366 val /= radix;
1368 if (digit < 10)
1369 *--pos = '0' + digit;
1370 else
1371 *--pos = 'a' + digit - 10;
1373 while (val != 0);
1375 if (is_negative)
1376 *--pos = '-';
1378 len = buffer + 65 - pos;
1379 if (len > size)
1381 size_t i;
1382 MSVCRT_wchar_t *p = str;
1384 /* Copy the temporary buffer backwards up to the available number of
1385 * characters. Don't copy the negative sign if present. */
1387 if (is_negative)
1389 p++;
1390 size--;
1393 for (pos = buffer + 63, i = 0; i < size; i++)
1394 *p++ = *pos--;
1396 MSVCRT_INVALID_PMT("str[size] is too small");
1397 str[0] = '\0';
1398 *MSVCRT__errno() = MSVCRT_ERANGE;
1399 return MSVCRT_ERANGE;
1402 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1403 return 0;
1406 #define I10_OUTPUT_MAX_PREC 21
1407 /* Internal structure used by $I10_OUTPUT */
1408 struct _I10_OUTPUT_DATA {
1409 short pos;
1410 char sign;
1411 BYTE len;
1412 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
1415 /*********************************************************************
1416 * $I10_OUTPUT (MSVCRT.@)
1417 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1418 * prec - precision of part, we're interested in
1419 * flag - 0 for first prec digits, 1 for fractional part
1420 * data - data to be populated
1422 * return value
1423 * 0 if given double is NaN or INF
1424 * 1 otherwise
1426 * FIXME
1427 * Native sets last byte of data->str to '0' or '9', I don't know what
1428 * it means. Current implementation sets it always to '0'.
1430 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
1432 static const char inf_str[] = "1#INF";
1433 static const char nan_str[] = "1#QNAN";
1435 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1436 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1437 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1438 long double ld = 0;
1439 double d;
1440 char format[8];
1441 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
1442 char *p;
1444 memcpy(&ld, &ld80, 10);
1445 d = ld;
1446 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
1448 if(d<0) {
1449 data->sign = '-';
1450 d = -d;
1451 } else
1452 data->sign = ' ';
1454 if(isinf(d)) {
1455 data->pos = 1;
1456 data->len = 5;
1457 memcpy(data->str, inf_str, sizeof(inf_str));
1459 return 0;
1462 if(isnan(d)) {
1463 data->pos = 1;
1464 data->len = 6;
1465 memcpy(data->str, nan_str, sizeof(nan_str));
1467 return 0;
1470 if(flag&1) {
1471 int exp = 1+floor(log10(d));
1473 prec += exp;
1474 if(exp < 0)
1475 prec--;
1477 prec--;
1479 if(prec+1 > I10_OUTPUT_MAX_PREC)
1480 prec = I10_OUTPUT_MAX_PREC-1;
1481 else if(prec < 0) {
1482 d = 0.0;
1483 prec = 0;
1486 sprintf(format, "%%.%dle", prec);
1487 sprintf(buf, format, d);
1489 buf[1] = buf[0];
1490 data->pos = atoi(buf+prec+3);
1491 if(buf[1] != '0')
1492 data->pos++;
1494 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1495 data->len = p-buf;
1497 memcpy(data->str, buf+1, data->len);
1498 data->str[data->len] = '\0';
1500 if(buf[1]!='0' && prec-data->len+1>0)
1501 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1503 return 1;
1505 #undef I10_OUTPUT_MAX_PREC