cmd: del /a: test deleting readonly files, with fix.
[wine.git] / dlls / msvcrt / string.c
blob0246b2f910214a9093f8b100898229ec8616a704
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 char* CDECL _strlwr_l(char *str, MSVCRT__locale_t locale)
105 _strlwr_s_l(str, -1, locale);
106 return str;
109 /*********************************************************************
110 * _strlwr (MSVCRT.@)
112 char* CDECL _strlwr(char *str)
114 _strlwr_s_l(str, -1, NULL);
115 return str;
118 /*********************************************************************
119 * _strupr_s_l (MSVCRT.@)
121 int CDECL _strupr_s_l(char *str, MSVCRT_size_t len, MSVCRT__locale_t locale)
123 char *ptr = str;
125 if(!locale)
126 locale = get_locale();
128 if (!str || !len)
130 *MSVCRT__errno() = MSVCRT_EINVAL;
131 return MSVCRT_EINVAL;
134 while (len && *ptr)
136 len--;
137 ptr++;
140 if (!len)
142 str[0] = '\0';
143 *MSVCRT__errno() = MSVCRT_EINVAL;
144 return MSVCRT_EINVAL;
147 while (*str)
149 *str = MSVCRT__toupper_l(*str, locale);
150 str++;
153 return 0;
156 /*********************************************************************
157 * _strupr_s (MSVCRT.@)
159 int CDECL _strupr_s(char *str, MSVCRT_size_t len)
161 return _strupr_s_l(str, len, NULL);
164 /*********************************************************************
165 * _strupr_l (MSVCRT.@)
167 char* CDECL _strupr_l(char *str, MSVCRT__locale_t locale)
169 _strupr_s_l(str, -1, locale);
170 return str;
173 /*********************************************************************
174 * _strupr (MSVCRT.@)
176 char* CDECL _strupr(char *str)
178 _strupr_s_l(str, -1, NULL);
179 return str;
182 /*********************************************************************
183 * _strnset (MSVCRT.@)
185 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
187 if (len > 0 && str)
188 while (*str && len--)
189 *str++ = value;
190 return str;
193 /*********************************************************************
194 * _strrev (MSVCRT.@)
196 char* CDECL _strrev(char* str)
198 char * p1;
199 char * p2;
201 if (str && *str)
202 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
204 *p1 ^= *p2;
205 *p2 ^= *p1;
206 *p1 ^= *p2;
209 return str;
212 /*********************************************************************
213 * _strset (MSVCRT.@)
215 char* CDECL _strset(char* str, int value)
217 char *ptr = str;
218 while (*ptr)
219 *ptr++ = value;
221 return str;
224 /*********************************************************************
225 * strtok (MSVCRT.@)
227 char * CDECL MSVCRT_strtok( char *str, const char *delim )
229 thread_data_t *data = msvcrt_get_thread_data();
230 char *ret;
232 if (!str)
233 if (!(str = data->strtok_next)) return NULL;
235 while (*str && strchr( delim, *str )) str++;
236 if (!*str) return NULL;
237 ret = str++;
238 while (*str && !strchr( delim, *str )) str++;
239 if (*str) *str++ = 0;
240 data->strtok_next = str;
241 return ret;
244 /*********************************************************************
245 * strtok_s (MSVCRT.@)
247 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
249 if (!MSVCRT_CHECK_PMT(delim != NULL) || !MSVCRT_CHECK_PMT(ctx != NULL) ||
250 !MSVCRT_CHECK_PMT(str != NULL || *ctx != NULL)) {
251 *MSVCRT__errno() = MSVCRT_EINVAL;
252 return NULL;
255 if(!str)
256 str = *ctx;
258 while(*str && strchr(delim, *str))
259 str++;
260 if(!*str)
261 return NULL;
263 *ctx = str+1;
264 while(**ctx && !strchr(delim, **ctx))
265 (*ctx)++;
266 if(**ctx)
267 *(*ctx)++ = 0;
269 return str;
272 /*********************************************************************
273 * _swab (MSVCRT.@)
275 void CDECL MSVCRT__swab(char* src, char* dst, int len)
277 if (len > 1)
279 len = (unsigned)len >> 1;
281 while (len--) {
282 char s0 = src[0];
283 char s1 = src[1];
284 *dst++ = s1;
285 *dst++ = s0;
286 src = src + 2;
291 /*********************************************************************
292 * strtod_l (MSVCRT.@)
294 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
296 unsigned __int64 d=0, hlp;
297 unsigned fpcontrol;
298 int exp=0, sign=1;
299 const char *p;
300 double ret;
301 BOOL found_digit = FALSE;
303 if (!MSVCRT_CHECK_PMT(str != NULL)) {
304 *MSVCRT__errno() = MSVCRT_EINVAL;
305 return 0;
308 if(!locale)
309 locale = get_locale();
311 /* FIXME: use *_l functions */
312 p = str;
313 while(isspace(*p))
314 p++;
316 if(*p == '-') {
317 sign = -1;
318 p++;
319 } else if(*p == '+')
320 p++;
322 while(isdigit(*p)) {
323 found_digit = TRUE;
324 hlp = d*10+*(p++)-'0';
325 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
326 exp++;
327 break;
328 } else
329 d = hlp;
331 while(isdigit(*p)) {
332 exp++;
333 p++;
336 if(*p == *locale->locinfo->lconv->decimal_point)
337 p++;
339 while(isdigit(*p)) {
340 found_digit = TRUE;
341 hlp = d*10+*(p++)-'0';
342 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
343 break;
345 d = hlp;
346 exp--;
348 while(isdigit(*p))
349 p++;
351 if(!found_digit) {
352 if(end)
353 *end = (char*)str;
354 return 0.0;
357 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
358 int e=0, s=1;
360 p++;
361 if(*p == '-') {
362 s = -1;
363 p++;
364 } else if(*p == '+')
365 p++;
367 if(isdigit(*p)) {
368 while(isdigit(*p)) {
369 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
370 e = INT_MAX;
371 p++;
373 e *= s;
375 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
376 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
377 else exp += e;
378 } else {
379 if(*p=='-' || *p=='+')
380 p--;
381 p--;
385 fpcontrol = _control87(0, 0);
386 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
387 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
389 if(exp>0)
390 ret = (double)sign*d*pow(10, exp);
391 else
392 ret = (double)sign*d/pow(10, -exp);
394 _control87(fpcontrol, 0xffffffff);
396 if((d && ret==0.0) || isinf(ret))
397 *MSVCRT__errno() = MSVCRT_ERANGE;
399 if(end)
400 *end = (char*)p;
402 return ret;
405 /*********************************************************************
406 * strtod (MSVCRT.@)
408 double CDECL MSVCRT_strtod( const char *str, char **end )
410 return MSVCRT_strtod_l( str, end, NULL );
413 /*********************************************************************
414 * atof (MSVCRT.@)
416 double CDECL MSVCRT_atof( const char *str )
418 return MSVCRT_strtod_l(str, NULL, NULL);
421 /*********************************************************************
422 * _atof_l (MSVCRT.@)
424 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
426 return MSVCRT_strtod_l(str, NULL, locale);
429 /*********************************************************************
430 * _atoflt_l (MSVCRT.@)
432 int CDECL MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT *value, char *str, MSVCRT__locale_t locale)
434 unsigned __int64 d=0, hlp;
435 unsigned fpcontrol;
436 int exp=0, sign=1;
437 const char *p;
438 int ret=0;
439 BOOL found_digit = FALSE;
441 if(!locale)
442 locale = get_locale();
444 /* FIXME: use *_l functions */
445 p = str;
446 while(isspace(*p))
447 p++;
449 if(*p == '-') {
450 sign = -1;
451 p++;
452 } else if(*p == '+')
453 p++;
455 while(isdigit(*p)) {
456 found_digit = TRUE;
457 hlp = d*10+*(p++)-'0';
458 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
459 exp++;
460 break;
461 } else
462 d = hlp;
464 while(isdigit(*p)) {
465 exp++;
466 p++;
469 if(*p == *locale->locinfo->lconv->decimal_point)
470 p++;
472 while(isdigit(*p)) {
473 found_digit = TRUE;
474 hlp = d*10+*(p++)-'0';
475 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
476 break;
478 d = hlp;
479 exp--;
481 while(isdigit(*p))
482 p++;
484 if(!found_digit) {
485 value->f = 0.0;
486 return 0;
489 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
490 int e=0, s=1;
492 p++;
493 if(*p == '-') {
494 s = -1;
495 p++;
496 } else if(*p == '+')
497 p++;
499 if(isdigit(*p)) {
500 while(isdigit(*p)) {
501 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
502 e = INT_MAX;
503 p++;
505 e *= s;
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;
509 else exp += e;
510 } else {
511 if(*p=='-' || *p=='+')
512 p--;
513 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);
521 if(exp>0)
522 value->f = (float)sign*d*powf(10, exp);
523 else
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;
531 return ret;
534 /*********************************************************************
535 * _strcoll_l (MSVCRT.@)
537 int CDECL MSVCRT_strcoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
539 if(!locale)
540 locale = get_locale();
542 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, -1, str2, -1)-2;
545 /*********************************************************************
546 * strcoll (MSVCRT.@)
548 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
550 return MSVCRT_strcoll_l(str1, str2, NULL);
553 /*********************************************************************
554 * _stricoll_l (MSVCRT.@)
556 int CDECL MSVCRT__stricoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
558 if(!locale)
559 locale = get_locale();
561 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
562 str1, -1, str2, -1)-2;
565 /*********************************************************************
566 * _stricoll (MSVCRT.@)
568 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
570 return MSVCRT__stricoll_l(str1, str2, NULL);
573 /*********************************************************************
574 * _strncoll_l (MSVCRT.@)
576 int CDECL MSVCRT_strncoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
578 if(!locale)
579 locale = get_locale();
581 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], 0, str1, count, str2, count)-2;
584 /*********************************************************************
585 * strncoll (MSVCRT.@)
587 int CDECL MSVCRT_strncoll( const char* str1, const char* str2, MSVCRT_size_t count )
589 return MSVCRT_strncoll_l(str1, str2, count, NULL);
592 /*********************************************************************
593 * _strnicoll_l (MSVCRT.@)
595 int CDECL MSVCRT__strnicoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
597 if(!locale)
598 locale = get_locale();
600 return CompareStringA(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE], NORM_IGNORECASE,
601 str1, count, str2, count)-2;
604 /*********************************************************************
605 * _strnicoll (MSVCRT.@)
607 int CDECL MSVCRT__strnicoll( const char* str1, const char* str2, MSVCRT_size_t count )
609 return MSVCRT__strnicoll_l(str1, str2, count, NULL);
612 /*********************************************************************
613 * strcpy_s (MSVCRT.@)
615 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
617 MSVCRT_size_t i;
618 if(!elem) return MSVCRT_EINVAL;
619 if(!dst) return MSVCRT_EINVAL;
620 if(!src)
622 dst[0] = '\0';
623 return MSVCRT_EINVAL;
626 for(i = 0; i < elem; i++)
628 if((dst[i] = src[i]) == '\0') return 0;
630 dst[0] = '\0';
631 return MSVCRT_ERANGE;
634 /*********************************************************************
635 * strcat_s (MSVCRT.@)
637 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
639 MSVCRT_size_t i, j;
640 if(!dst) return MSVCRT_EINVAL;
641 if(elem == 0) return MSVCRT_EINVAL;
642 if(!src)
644 dst[0] = '\0';
645 return MSVCRT_EINVAL;
648 for(i = 0; i < elem; i++)
650 if(dst[i] == '\0')
652 for(j = 0; (j + i) < elem; j++)
654 if((dst[j + i] = src[j]) == '\0') return 0;
658 /* Set the first element to 0, not the first element after the skipped part */
659 dst[0] = '\0';
660 return MSVCRT_ERANGE;
663 /*********************************************************************
664 * strncat_s (MSVCRT.@)
666 int CDECL MSVCRT_strncat_s( char* dst, MSVCRT_size_t elem, const char* src, MSVCRT_size_t count )
668 MSVCRT_size_t i, j;
669 if(!MSVCRT_CHECK_PMT(dst != 0) || !MSVCRT_CHECK_PMT(elem != 0))
670 return MSVCRT_EINVAL;
671 if(!MSVCRT_CHECK_PMT(src != 0))
673 dst[0] = '\0';
674 return MSVCRT_EINVAL;
677 for(i = 0; i < elem; i++)
679 if(dst[i] == '\0')
681 for(j = 0; (j + i) < elem; j++)
683 if(count == MSVCRT__TRUNCATE && j + i == elem - 1)
685 dst[j + i] = '\0';
686 return MSVCRT_STRUNCATE;
688 if(j == count || (dst[j + i] = src[j]) == '\0')
690 dst[j + i] = '\0';
691 return 0;
696 /* Set the first element to 0, not the first element after the skipped part */
697 dst[0] = '\0';
698 return MSVCRT_ERANGE;
701 /*********************************************************************
702 * strxfrm (MSVCRT.@)
704 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
706 /* FIXME: handle Windows locale */
707 return strxfrm( dest, src, len );
710 /********************************************************************
711 * _atoldbl (MSVCRT.@)
713 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
715 /* FIXME needs error checking for huge/small values */
716 #ifdef HAVE_STRTOLD
717 long double ld;
718 TRACE("str %s value %p\n",str,value);
719 ld = strtold(str,0);
720 memcpy(value, &ld, 10);
721 #else
722 FIXME("stub, str %s value %p\n",str,value);
723 #endif
724 return 0;
727 /********************************************************************
728 * __STRINGTOLD (MSVCRT.@)
730 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
732 #ifdef HAVE_STRTOLD
733 long double ld;
734 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
735 ld = strtold(str,0);
736 memcpy(value, &ld, 10);
737 #else
738 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
739 #endif
740 return 0;
743 /******************************************************************
744 * strtol (MSVCRT.@)
746 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
748 /* wrapper to forward libc error code to msvcrt's error codes */
749 long ret;
751 errno = 0;
752 ret = strtol(nptr, end, base);
753 switch (errno)
755 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
756 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
757 default:
758 /* cope with the fact that we may use 64bit long integers on libc
759 * while msvcrt always uses 32bit long integers
761 if (ret > MSVCRT_LONG_MAX)
763 ret = MSVCRT_LONG_MAX;
764 *MSVCRT__errno() = MSVCRT_ERANGE;
766 else if (ret < -MSVCRT_LONG_MAX - 1)
768 ret = -MSVCRT_LONG_MAX - 1;
769 *MSVCRT__errno() = MSVCRT_ERANGE;
771 break;
774 return ret;
777 /******************************************************************
778 * strtoul (MSVCRT.@)
780 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
782 /* wrapper to forward libc error code to msvcrt's error codes */
783 unsigned long ret;
785 errno = 0;
786 ret = strtoul(nptr, end, base);
787 switch (errno)
789 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
790 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
791 default:
792 /* cope with the fact that we may use 64bit long integers on libc
793 * while msvcrt always uses 32bit long integers
795 if (ret > MSVCRT_ULONG_MAX)
797 ret = MSVCRT_ULONG_MAX;
798 *MSVCRT__errno() = MSVCRT_ERANGE;
800 break;
803 return ret;
806 /******************************************************************
807 * strnlen (MSVCRT.@)
809 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
811 MSVCRT_size_t i;
813 for(i=0; i<maxlen; i++)
814 if(!s[i]) break;
816 return i;
819 /*********************************************************************
820 * _strtoi64_l (MSVCRT.@)
822 * FIXME: locale parameter is ignored
824 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
826 BOOL negative = FALSE;
827 __int64 ret = 0;
829 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
831 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
832 !MSVCRT_CHECK_PMT(base <= 36)) {
833 return 0;
836 while(isspace(*nptr)) nptr++;
838 if(*nptr == '-') {
839 negative = TRUE;
840 nptr++;
841 } else if(*nptr == '+')
842 nptr++;
844 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
845 base = 16;
846 nptr += 2;
849 if(base == 0) {
850 if(*nptr=='0')
851 base = 8;
852 else
853 base = 10;
856 while(*nptr) {
857 char cur = tolower(*nptr);
858 int v;
860 if(isdigit(cur)) {
861 if(cur >= '0'+base)
862 break;
863 v = cur-'0';
864 } else {
865 if(cur<'a' || cur>='a'+base-10)
866 break;
867 v = cur-'a'+10;
870 if(negative)
871 v = -v;
873 nptr++;
875 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
876 ret = MSVCRT_I64_MAX;
877 *MSVCRT__errno() = MSVCRT_ERANGE;
878 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
879 ret = MSVCRT_I64_MIN;
880 *MSVCRT__errno() = MSVCRT_ERANGE;
881 } else
882 ret = ret*base + v;
885 if(endptr)
886 *endptr = (char*)nptr;
888 return ret;
891 /*********************************************************************
892 * _strtoi64 (MSVCRT.@)
894 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
896 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
899 /*********************************************************************
900 * _strtoui64_l (MSVCRT.@)
902 * FIXME: locale parameter is ignored
904 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
906 BOOL negative = FALSE;
907 unsigned __int64 ret = 0;
909 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
911 if (!MSVCRT_CHECK_PMT(nptr != NULL) || !MSVCRT_CHECK_PMT(base == 0 || base >= 2) ||
912 !MSVCRT_CHECK_PMT(base <= 36)) {
913 return 0;
916 while(isspace(*nptr)) nptr++;
918 if(*nptr == '-') {
919 negative = TRUE;
920 nptr++;
921 } else if(*nptr == '+')
922 nptr++;
924 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
925 base = 16;
926 nptr += 2;
929 if(base == 0) {
930 if(*nptr=='0')
931 base = 8;
932 else
933 base = 10;
936 while(*nptr) {
937 char cur = tolower(*nptr);
938 int v;
940 if(isdigit(cur)) {
941 if(cur >= '0'+base)
942 break;
943 v = *nptr-'0';
944 } else {
945 if(cur<'a' || cur>='a'+base-10)
946 break;
947 v = cur-'a'+10;
950 nptr++;
952 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
953 ret = MSVCRT_UI64_MAX;
954 *MSVCRT__errno() = MSVCRT_ERANGE;
955 } else
956 ret = ret*base + v;
959 if(endptr)
960 *endptr = (char*)nptr;
962 return negative ? -ret : ret;
965 /*********************************************************************
966 * _strtoui64 (MSVCRT.@)
968 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
970 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
973 /*********************************************************************
974 * _ltoa_s (MSVCRT.@)
976 int CDECL _ltoa_s(MSVCRT_long value, char *str, MSVCRT_size_t size, int radix)
978 MSVCRT_ulong val;
979 unsigned int digit;
980 int is_negative;
981 char buffer[33], *pos;
982 size_t len;
984 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
985 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
987 if (str && size)
988 str[0] = '\0';
990 *MSVCRT__errno() = MSVCRT_EINVAL;
991 return MSVCRT_EINVAL;
994 if (value < 0 && radix == 10)
996 is_negative = 1;
997 val = -value;
999 else
1001 is_negative = 0;
1002 val = value;
1005 pos = buffer + 32;
1006 *pos = '\0';
1010 digit = val % radix;
1011 val /= radix;
1013 if (digit < 10)
1014 *--pos = '0' + digit;
1015 else
1016 *--pos = 'a' + digit - 10;
1018 while (val != 0);
1020 if (is_negative)
1021 *--pos = '-';
1023 len = buffer + 33 - pos;
1024 if (len > size)
1026 size_t i;
1027 char *p = str;
1029 /* Copy the temporary buffer backwards up to the available number of
1030 * characters. Don't copy the negative sign if present. */
1032 if (is_negative)
1034 p++;
1035 size--;
1038 for (pos = buffer + 31, i = 0; i < size; i++)
1039 *p++ = *pos--;
1041 str[0] = '\0';
1042 MSVCRT_INVALID_PMT("str[size] is too small");
1043 *MSVCRT__errno() = MSVCRT_ERANGE;
1044 return MSVCRT_ERANGE;
1047 memcpy(str, pos, len);
1048 return 0;
1051 /*********************************************************************
1052 * _ltow_s (MSVCRT.@)
1054 int CDECL _ltow_s(MSVCRT_long value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1056 MSVCRT_ulong val;
1057 unsigned int digit;
1058 int is_negative;
1059 MSVCRT_wchar_t buffer[33], *pos;
1060 size_t len;
1062 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1063 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1065 if (str && size)
1066 str[0] = '\0';
1068 *MSVCRT__errno() = MSVCRT_EINVAL;
1069 return MSVCRT_EINVAL;
1072 if (value < 0 && radix == 10)
1074 is_negative = 1;
1075 val = -value;
1077 else
1079 is_negative = 0;
1080 val = value;
1083 pos = buffer + 32;
1084 *pos = '\0';
1088 digit = val % radix;
1089 val /= radix;
1091 if (digit < 10)
1092 *--pos = '0' + digit;
1093 else
1094 *--pos = 'a' + digit - 10;
1096 while (val != 0);
1098 if (is_negative)
1099 *--pos = '-';
1101 len = buffer + 33 - pos;
1102 if (len > size)
1104 size_t i;
1105 MSVCRT_wchar_t *p = str;
1107 /* Copy the temporary buffer backwards up to the available number of
1108 * characters. Don't copy the negative sign if present. */
1110 if (is_negative)
1112 p++;
1113 size--;
1116 for (pos = buffer + 31, i = 0; i < size; i++)
1117 *p++ = *pos--;
1119 MSVCRT_INVALID_PMT("str[size] is too small");
1120 str[0] = '\0';
1121 *MSVCRT__errno() = MSVCRT_ERANGE;
1122 return MSVCRT_ERANGE;
1125 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1126 return 0;
1129 /*********************************************************************
1130 * _itoa_s (MSVCRT.@)
1132 int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
1134 return _ltoa_s(value, str, size, radix);
1137 /*********************************************************************
1138 * _itow_s (MSVCRT.@)
1140 int CDECL _itow_s(int value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1142 return _ltow_s(value, str, size, radix);
1145 /*********************************************************************
1146 * _ui64toa_s (MSVCRT.@)
1148 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
1149 MSVCRT_size_t size, int radix)
1151 char buffer[65], *pos;
1152 int digit;
1154 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1155 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
1156 *MSVCRT__errno() = MSVCRT_EINVAL;
1157 return MSVCRT_EINVAL;
1160 pos = buffer+64;
1161 *pos = '\0';
1163 do {
1164 digit = value%radix;
1165 value /= radix;
1167 if(digit < 10)
1168 *--pos = '0'+digit;
1169 else
1170 *--pos = 'a'+digit-10;
1171 }while(value != 0);
1173 if(buffer-pos+65 > size) {
1174 MSVCRT_INVALID_PMT("str[size] is too small");
1175 *MSVCRT__errno() = MSVCRT_EINVAL;
1176 return MSVCRT_EINVAL;
1179 memcpy(str, pos, buffer-pos+65);
1180 return 0;
1183 /*********************************************************************
1184 * _ui64tow_s (MSVCRT.@)
1186 int CDECL MSVCRT__ui64tow_s( unsigned __int64 value, MSVCRT_wchar_t *str,
1187 MSVCRT_size_t size, int radix )
1189 MSVCRT_wchar_t buffer[65], *pos;
1190 int digit;
1192 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1193 !MSVCRT_CHECK_PMT(radix>=2) || !MSVCRT_CHECK_PMT(radix<=36)) {
1194 *MSVCRT__errno() = MSVCRT_EINVAL;
1195 return MSVCRT_EINVAL;
1198 pos = &buffer[64];
1199 *pos = '\0';
1201 do {
1202 digit = value % radix;
1203 value = value / radix;
1204 if (digit < 10)
1205 *--pos = '0' + digit;
1206 else
1207 *--pos = 'a' + digit - 10;
1208 } while (value != 0);
1210 if(buffer-pos+65 > size) {
1211 MSVCRT_INVALID_PMT("str[size] is too small");
1212 *MSVCRT__errno() = MSVCRT_EINVAL;
1213 return MSVCRT_EINVAL;
1216 memcpy(str, pos, buffer-pos+65);
1217 return 0;
1220 /*********************************************************************
1221 * _ultoa_s (MSVCRT.@)
1223 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
1225 MSVCRT_ulong digit;
1226 char buffer[33], *pos;
1227 size_t len;
1229 if (!str || !size || radix < 2 || radix > 36)
1231 if (str && size)
1232 str[0] = '\0';
1234 *MSVCRT__errno() = MSVCRT_EINVAL;
1235 return MSVCRT_EINVAL;
1238 pos = buffer + 32;
1239 *pos = '\0';
1243 digit = value % radix;
1244 value /= radix;
1246 if (digit < 10)
1247 *--pos = '0' + digit;
1248 else
1249 *--pos = 'a' + digit - 10;
1251 while (value != 0);
1253 len = buffer + 33 - pos;
1254 if (len > size)
1256 size_t i;
1257 char *p = str;
1259 /* Copy the temporary buffer backwards up to the available number of
1260 * characters. */
1262 for (pos = buffer + 31, i = 0; i < size; i++)
1263 *p++ = *pos--;
1265 str[0] = '\0';
1266 *MSVCRT__errno() = MSVCRT_ERANGE;
1267 return MSVCRT_ERANGE;
1270 memcpy(str, pos, len);
1271 return 0;
1274 /*********************************************************************
1275 * _i64toa_s (MSVCRT.@)
1277 int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix)
1279 unsigned __int64 val;
1280 unsigned int digit;
1281 int is_negative;
1282 char buffer[65], *pos;
1283 size_t len;
1285 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1286 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1288 if (str && size)
1289 str[0] = '\0';
1291 *MSVCRT__errno() = MSVCRT_EINVAL;
1292 return MSVCRT_EINVAL;
1295 if (value < 0 && radix == 10)
1297 is_negative = 1;
1298 val = -value;
1300 else
1302 is_negative = 0;
1303 val = value;
1306 pos = buffer + 64;
1307 *pos = '\0';
1311 digit = val % radix;
1312 val /= radix;
1314 if (digit < 10)
1315 *--pos = '0' + digit;
1316 else
1317 *--pos = 'a' + digit - 10;
1319 while (val != 0);
1321 if (is_negative)
1322 *--pos = '-';
1324 len = buffer + 65 - pos;
1325 if (len > size)
1327 size_t i;
1328 char *p = str;
1330 /* Copy the temporary buffer backwards up to the available number of
1331 * characters. Don't copy the negative sign if present. */
1333 if (is_negative)
1335 p++;
1336 size--;
1339 for (pos = buffer + 63, i = 0; i < size; i++)
1340 *p++ = *pos--;
1342 str[0] = '\0';
1343 MSVCRT_INVALID_PMT("str[size] is too small");
1344 *MSVCRT__errno() = MSVCRT_ERANGE;
1345 return MSVCRT_ERANGE;
1348 memcpy(str, pos, len);
1349 return 0;
1352 /*********************************************************************
1353 * _i64tow_s (MSVCRT.@)
1355 int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1357 unsigned __int64 val;
1358 unsigned int digit;
1359 int is_negative;
1360 MSVCRT_wchar_t buffer[65], *pos;
1361 size_t len;
1363 if (!MSVCRT_CHECK_PMT(str != NULL) || !MSVCRT_CHECK_PMT(size > 0) ||
1364 !MSVCRT_CHECK_PMT(radix >= 2) || !MSVCRT_CHECK_PMT(radix <= 36))
1366 if (str && size)
1367 str[0] = '\0';
1369 *MSVCRT__errno() = MSVCRT_EINVAL;
1370 return MSVCRT_EINVAL;
1373 if (value < 0 && radix == 10)
1375 is_negative = 1;
1376 val = -value;
1378 else
1380 is_negative = 0;
1381 val = value;
1384 pos = buffer + 64;
1385 *pos = '\0';
1389 digit = val % radix;
1390 val /= radix;
1392 if (digit < 10)
1393 *--pos = '0' + digit;
1394 else
1395 *--pos = 'a' + digit - 10;
1397 while (val != 0);
1399 if (is_negative)
1400 *--pos = '-';
1402 len = buffer + 65 - pos;
1403 if (len > size)
1405 size_t i;
1406 MSVCRT_wchar_t *p = str;
1408 /* Copy the temporary buffer backwards up to the available number of
1409 * characters. Don't copy the negative sign if present. */
1411 if (is_negative)
1413 p++;
1414 size--;
1417 for (pos = buffer + 63, i = 0; i < size; i++)
1418 *p++ = *pos--;
1420 MSVCRT_INVALID_PMT("str[size] is too small");
1421 str[0] = '\0';
1422 *MSVCRT__errno() = MSVCRT_ERANGE;
1423 return MSVCRT_ERANGE;
1426 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1427 return 0;
1430 #define I10_OUTPUT_MAX_PREC 21
1431 /* Internal structure used by $I10_OUTPUT */
1432 struct _I10_OUTPUT_DATA {
1433 short pos;
1434 char sign;
1435 BYTE len;
1436 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
1439 /*********************************************************************
1440 * $I10_OUTPUT (MSVCRT.@)
1441 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1442 * prec - precision of part, we're interested in
1443 * flag - 0 for first prec digits, 1 for fractional part
1444 * data - data to be populated
1446 * return value
1447 * 0 if given double is NaN or INF
1448 * 1 otherwise
1450 * FIXME
1451 * Native sets last byte of data->str to '0' or '9', I don't know what
1452 * it means. Current implementation sets it always to '0'.
1454 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
1456 static const char inf_str[] = "1#INF";
1457 static const char nan_str[] = "1#QNAN";
1459 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1460 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1461 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1462 long double ld = 0;
1463 double d;
1464 char format[8];
1465 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
1466 char *p;
1468 memcpy(&ld, &ld80, 10);
1469 d = ld;
1470 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
1472 if(d<0) {
1473 data->sign = '-';
1474 d = -d;
1475 } else
1476 data->sign = ' ';
1478 if(isinf(d)) {
1479 data->pos = 1;
1480 data->len = 5;
1481 memcpy(data->str, inf_str, sizeof(inf_str));
1483 return 0;
1486 if(isnan(d)) {
1487 data->pos = 1;
1488 data->len = 6;
1489 memcpy(data->str, nan_str, sizeof(nan_str));
1491 return 0;
1494 if(flag&1) {
1495 int exp = 1+floor(log10(d));
1497 prec += exp;
1498 if(exp < 0)
1499 prec--;
1501 prec--;
1503 if(prec+1 > I10_OUTPUT_MAX_PREC)
1504 prec = I10_OUTPUT_MAX_PREC-1;
1505 else if(prec < 0) {
1506 d = 0.0;
1507 prec = 0;
1510 sprintf(format, "%%.%dle", prec);
1511 sprintf(buf, format, d);
1513 buf[1] = buf[0];
1514 data->pos = atoi(buf+prec+3);
1515 if(buf[1] != '0')
1516 data->pos++;
1518 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1519 data->len = p-buf;
1521 memcpy(data->str, buf+1, data->len);
1522 data->str[data->len] = '\0';
1524 if(buf[1]!='0' && prec-data->len+1>0)
1525 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1527 return 1;
1529 #undef I10_OUTPUT_MAX_PREC