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
MSVCRT__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
MSVCRT__strlwr_s(char *str
, MSVCRT_size_t len
)
94 return MSVCRT__strlwr_s_l(str
, len
, NULL
);
97 /*********************************************************************
98 * _strlwr_l (MSVCRT.@)
100 char* CDECL
_strlwr_l(char *str
, MSVCRT__locale_t locale
)
102 MSVCRT__strlwr_s_l(str
, -1, locale
);
106 /*********************************************************************
109 char* CDECL
MSVCRT__strlwr(char *str
)
111 MSVCRT__strlwr_s_l(str
, -1, NULL
);
115 /*********************************************************************
116 * _strupr_s_l (MSVCRT.@)
118 int CDECL
MSVCRT__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
MSVCRT__strupr_s(char *str
, MSVCRT_size_t len
)
155 return MSVCRT__strupr_s_l(str
, len
, NULL
);
158 /*********************************************************************
159 * _strupr_l (MSVCRT.@)
161 char* CDECL
MSVCRT__strupr_l(char *str
, MSVCRT__locale_t locale
)
163 MSVCRT__strupr_s_l(str
, -1, locale
);
167 /*********************************************************************
170 char* CDECL
MSVCRT__strupr(char *str
)
172 MSVCRT__strupr_s_l(str
, -1, NULL
);
176 /*********************************************************************
177 * _strnset_s (MSVCRT.@)
179 int CDECL
MSVCRT__strnset_s(char *str
, MSVCRT_size_t size
, int c
, MSVCRT_size_t count
)
183 if(!str
&& !size
&& !count
) return 0;
184 if(!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
185 if(!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
187 for(i
=0; i
<size
-1 && i
<count
; i
++) {
188 if(!str
[i
]) return 0;
192 if(!str
[i
]) return 0;
195 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
196 *MSVCRT__errno() = MSVCRT_EINVAL
;
197 return MSVCRT_EINVAL
;
200 /*********************************************************************
201 * _strnset (MSVCRT.@)
203 char* CDECL
MSVCRT__strnset(char* str
, int value
, MSVCRT_size_t len
)
206 while (*str
&& len
--)
211 /*********************************************************************
214 char* CDECL
MSVCRT__strrev(char* str
)
220 for (p1
= str
, p2
= str
+ strlen(str
) - 1; p2
> p1
; ++p1
, --p2
)
230 /*********************************************************************
233 char* CDECL
_strset(char* str
, int value
)
242 /*********************************************************************
245 char * CDECL
MSVCRT_strtok( char *str
, const char *delim
)
247 thread_data_t
*data
= msvcrt_get_thread_data();
251 if (!(str
= data
->strtok_next
)) return NULL
;
253 while (*str
&& strchr( delim
, *str
)) str
++;
254 if (!*str
) return NULL
;
256 while (*str
&& !strchr( delim
, *str
)) str
++;
257 if (*str
) *str
++ = 0;
258 data
->strtok_next
= str
;
262 /*********************************************************************
263 * strtok_s (MSVCRT.@)
265 char * CDECL
MSVCRT_strtok_s(char *str
, const char *delim
, char **ctx
)
267 if (!MSVCRT_CHECK_PMT(delim
!= NULL
)) return NULL
;
268 if (!MSVCRT_CHECK_PMT(ctx
!= NULL
)) return NULL
;
269 if (!MSVCRT_CHECK_PMT(str
!= NULL
|| *ctx
!= NULL
)) return NULL
;
274 while(*str
&& strchr(delim
, *str
))
283 while(**ctx
&& !strchr(delim
, **ctx
))
291 /*********************************************************************
294 void CDECL
MSVCRT__swab(char* src
, char* dst
, int len
)
298 len
= (unsigned)len
>> 1;
310 static double strtod_helper(const char *str
, char **end
, MSVCRT__locale_t locale
, int *err
)
312 MSVCRT_pthreadlocinfo locinfo
;
313 unsigned __int64 d
=0, hlp
;
318 long double lret
=1, expcnt
= 10;
319 BOOL found_digit
= FALSE
, negexp
;
324 else if(!MSVCRT_CHECK_PMT(str
!= NULL
)) {
331 locinfo
= get_locinfo();
333 locinfo
= locale
->locinfo
;
335 /* FIXME: use *_l functions */
346 #if _MSVCR_VER >= 140
347 if(tolower(p
[0]) == 'i' && tolower(p
[1]) == 'n' && tolower(p
[2]) == 'f') {
349 *end
= (char*) &p
[3];
350 if(tolower(p
[3]) == 'i' && tolower(p
[4]) == 'n' && tolower(p
[5]) == 'i' &&
351 tolower(p
[6]) == 't' && tolower(p
[7]) == 'y' && end
)
352 *end
= (char*) &p
[8];
353 return sign
*INFINITY
;
355 if(tolower(p
[0]) == 'n' &&
356 tolower(p
[1]) == 'a' &&
357 tolower(p
[2]) == 'n') {
359 *end
= (char*) &p
[3];
363 if(p
[0] == '0' && tolower(p
[1]) == 'x') {
370 while((*p
>='0' && *p
<='9') ||
371 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F')))) {
375 if (c
>='0' && c
<='9')
377 else if (c
>= 'a' && c
<= 'f')
382 if(d
>MSVCRT_UI64_MAX
/base
|| hlp
<d
) {
388 while((*p
>='0' && *p
<='9') ||
389 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F')))) {
394 if(*p
== *locinfo
->lconv
->decimal_point
)
397 while((*p
>='0' && *p
<='9') ||
398 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F')))) {
402 if (c
>='0' && c
<='9')
404 else if (c
>= 'a' && c
<= 'f')
409 if(d
>MSVCRT_UI64_MAX
/base
|| hlp
<d
)
414 while((*p
>='0' && *p
<='9') ||
415 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F'))))
427 if((base
== 10 && (*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D')) ||
428 (base
== 16 && (*p
=='p' || *p
=='P'))) {
438 if(*p
>='0' && *p
<='9') {
439 while(*p
>='0' && *p
<='9') {
440 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
446 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
447 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
450 if(*p
=='-' || *p
=='+')
456 fpcontrol
= _control87(0, 0);
457 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
458 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
467 expcnt
= expcnt
*expcnt
;
469 ret
= (long double)sign
* (negexp
? d
/lret
: d
*lret
);
471 _control87(fpcontrol
, 0xffffffff);
473 if((d
&& ret
==0.0) || isinf(ret
)) {
475 *err
= MSVCRT_ERANGE
;
477 *MSVCRT__errno() = MSVCRT_ERANGE
;
486 /*********************************************************************
487 * strtod_l (MSVCRT.@)
489 double CDECL
MSVCRT_strtod_l(const char *str
, char **end
, MSVCRT__locale_t locale
)
491 return strtod_helper(str
, end
, locale
, NULL
);
494 /*********************************************************************
497 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
499 return MSVCRT_strtod_l( str
, end
, NULL
);
502 /*********************************************************************
503 * strtof_l (MSVCR120.@)
505 float CDECL
MSVCRT__strtof_l( const char *str
, char **end
, MSVCRT__locale_t locale
)
507 return MSVCRT_strtod_l(str
, end
, locale
);
510 /*********************************************************************
511 * strtof (MSVCR120.@)
513 float CDECL
MSVCRT_strtof( const char *str
, char **end
)
515 return MSVCRT__strtof_l(str
, end
, NULL
);
518 /*********************************************************************
521 double CDECL
MSVCRT_atof( const char *str
)
523 return MSVCRT_strtod_l(str
, NULL
, NULL
);
526 /*********************************************************************
529 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
531 return MSVCRT_strtod_l(str
, NULL
, locale
);
534 /*********************************************************************
535 * _atoflt_l (MSVCRT.@)
537 int CDECL
MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT
*value
, char *str
, MSVCRT__locale_t locale
)
542 d
= strtod_helper(str
, NULL
, locale
, &err
);
545 return MSVCRT__OVERFLOW
;
546 if((d
!=0 || err
) && value
->f
>-MSVCRT_FLT_MIN
&& value
->f
<MSVCRT_FLT_MIN
)
547 return MSVCRT__UNDERFLOW
;
551 /*********************************************************************
552 * _atoflt (MSVCR100.@)
554 int CDECL
MSVCRT__atoflt(MSVCRT__CRT_FLOAT
*value
, char *str
)
556 return MSVCRT__atoflt_l(value
, str
, NULL
);
559 /*********************************************************************
560 * _atodbl_l (MSVCRT.@)
562 int CDECL
MSVCRT__atodbl_l(MSVCRT__CRT_DOUBLE
*value
, char *str
, MSVCRT__locale_t locale
)
566 value
->x
= strtod_helper(str
, NULL
, locale
, &err
);
568 return MSVCRT__OVERFLOW
;
569 if((value
->x
!=0 || err
) && value
->x
>-MSVCRT_DBL_MIN
&& value
->x
<MSVCRT_DBL_MIN
)
570 return MSVCRT__UNDERFLOW
;
574 /*********************************************************************
577 int CDECL
MSVCRT__atodbl(MSVCRT__CRT_DOUBLE
*value
, char *str
)
579 return MSVCRT__atodbl_l(value
, str
, NULL
);
582 /*********************************************************************
583 * _strcoll_l (MSVCRT.@)
585 int CDECL
MSVCRT_strcoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
587 MSVCRT_pthreadlocinfo locinfo
;
590 locinfo
= get_locinfo();
592 locinfo
= locale
->locinfo
;
594 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
595 return strcmp(str1
, str2
);
596 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, -1, str2
, -1)-CSTR_EQUAL
;
599 /*********************************************************************
602 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
604 return MSVCRT_strcoll_l(str1
, str2
, NULL
);
607 /*********************************************************************
608 * _stricoll_l (MSVCRT.@)
610 int CDECL
MSVCRT__stricoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
612 MSVCRT_pthreadlocinfo locinfo
;
615 locinfo
= get_locinfo();
617 locinfo
= locale
->locinfo
;
619 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
620 return strcasecmp(str1
, str2
);
621 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
622 str1
, -1, str2
, -1)-CSTR_EQUAL
;
625 /*********************************************************************
626 * _stricoll (MSVCRT.@)
628 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
630 return MSVCRT__stricoll_l(str1
, str2
, NULL
);
633 /*********************************************************************
634 * _strncoll_l (MSVCRT.@)
636 int CDECL
MSVCRT__strncoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
638 MSVCRT_pthreadlocinfo locinfo
;
641 locinfo
= get_locinfo();
643 locinfo
= locale
->locinfo
;
645 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
646 return strncmp(str1
, str2
, count
);
647 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, count
, str2
, count
)-CSTR_EQUAL
;
650 /*********************************************************************
651 * _strncoll (MSVCRT.@)
653 int CDECL
MSVCRT__strncoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
655 return MSVCRT__strncoll_l(str1
, str2
, count
, NULL
);
658 /*********************************************************************
659 * _strnicoll_l (MSVCRT.@)
661 int CDECL
MSVCRT__strnicoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
663 MSVCRT_pthreadlocinfo locinfo
;
666 locinfo
= get_locinfo();
668 locinfo
= locale
->locinfo
;
670 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
671 return strncasecmp(str1
, str2
, count
);
672 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
673 str1
, count
, str2
, count
)-CSTR_EQUAL
;
676 /*********************************************************************
677 * _strnicoll (MSVCRT.@)
679 int CDECL
MSVCRT__strnicoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
681 return MSVCRT__strnicoll_l(str1
, str2
, count
, NULL
);
684 /*********************************************************************
687 char* __cdecl
MSVCRT_strncpy(char *dst
, const char *src
, MSVCRT_size_t len
)
692 if((dst
[i
] = src
[i
]) == '\0') break;
694 while (i
< len
) dst
[i
++] = 0;
699 /*********************************************************************
702 char* CDECL
MSVCRT_strcpy(char *dst
, const char *src
)
705 while ((*dst
++ = *src
++));
709 /*********************************************************************
710 * strcpy_s (MSVCRT.@)
712 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
715 if(!elem
) return MSVCRT_EINVAL
;
716 if(!dst
) return MSVCRT_EINVAL
;
720 return MSVCRT_EINVAL
;
723 for(i
= 0; i
< elem
; i
++)
725 if((dst
[i
] = src
[i
]) == '\0') return 0;
728 return MSVCRT_ERANGE
;
731 /*********************************************************************
732 * strcat_s (MSVCRT.@)
734 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
737 if(!dst
) return MSVCRT_EINVAL
;
738 if(elem
== 0) return MSVCRT_EINVAL
;
742 return MSVCRT_EINVAL
;
745 for(i
= 0; i
< elem
; i
++)
749 for(j
= 0; (j
+ i
) < elem
; j
++)
751 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
755 /* Set the first element to 0, not the first element after the skipped part */
757 return MSVCRT_ERANGE
;
760 /*********************************************************************
761 * strncat_s (MSVCRT.@)
763 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
767 if (!MSVCRT_CHECK_PMT(dst
!= 0)) return MSVCRT_EINVAL
;
768 if (!MSVCRT_CHECK_PMT(elem
!= 0)) return MSVCRT_EINVAL
;
769 if (!MSVCRT_CHECK_PMT(src
!= 0))
772 return MSVCRT_EINVAL
;
775 for(i
= 0; i
< elem
; i
++)
779 for(j
= 0; (j
+ i
) < elem
; j
++)
781 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
784 return MSVCRT_STRUNCATE
;
786 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
794 /* Set the first element to 0, not the first element after the skipped part */
796 return MSVCRT_ERANGE
;
799 /*********************************************************************
802 char* __cdecl
MSVCRT_strncat(char *dst
, const char *src
, MSVCRT_size_t len
)
804 return strncat(dst
, src
, len
);
807 /*********************************************************************
808 * _strxfrm_l (MSVCRT.@)
810 MSVCRT_size_t CDECL
MSVCRT__strxfrm_l( char *dest
, const char *src
,
811 MSVCRT_size_t len
, MSVCRT__locale_t locale
)
813 MSVCRT_pthreadlocinfo locinfo
;
816 if(!MSVCRT_CHECK_PMT(src
)) return INT_MAX
;
817 if(!MSVCRT_CHECK_PMT(dest
|| !len
)) return INT_MAX
;
820 FIXME("len > INT_MAX not supported\n");
825 locinfo
= get_locinfo();
827 locinfo
= locale
->locinfo
;
829 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
]) {
830 MSVCRT_strncpy(dest
, src
, len
);
834 ret
= LCMapStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
],
835 LCMAP_SORTKEY
, src
, -1, NULL
, 0);
838 *MSVCRT__errno() = MSVCRT_EILSEQ
;
841 if(!len
) return ret
-1;
845 *MSVCRT__errno() = MSVCRT_ERANGE
;
849 return LCMapStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
],
850 LCMAP_SORTKEY
, src
, -1, dest
, len
) - 1;
853 /*********************************************************************
856 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
858 return MSVCRT__strxfrm_l(dest
, src
, len
, NULL
);
861 /********************************************************************
862 * _atoldbl (MSVCRT.@)
864 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
866 /* FIXME needs error checking for huge/small values */
869 TRACE("str %s value %p\n",str
,value
);
871 memcpy(value
, &ld
, 10);
873 FIXME("stub, str %s value %p\n",str
,value
);
878 /********************************************************************
879 * __STRINGTOLD (MSVCRT.@)
881 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
885 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
887 memcpy(value
, &ld
, 10);
889 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
894 /*********************************************************************
897 MSVCRT_size_t __cdecl
MSVCRT_strlen(const char *str
)
902 /******************************************************************
905 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
909 for(i
=0; i
<maxlen
; i
++)
915 /*********************************************************************
916 * _strtoi64_l (MSVCRT.@)
918 * FIXME: locale parameter is ignored
920 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
922 const char *p
= nptr
;
923 BOOL negative
= FALSE
;
924 BOOL got_digit
= FALSE
;
927 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
929 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
930 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
931 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
933 while(isspace(*nptr
)) nptr
++;
938 } else if(*nptr
== '+')
941 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
954 char cur
= tolower(*nptr
);
957 if(cur
>='0' && cur
<='9') {
962 if(cur
<'a' || cur
>='a'+base
-10)
973 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
974 ret
= MSVCRT_I64_MAX
;
975 *MSVCRT__errno() = MSVCRT_ERANGE
;
976 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
977 ret
= MSVCRT_I64_MIN
;
978 *MSVCRT__errno() = MSVCRT_ERANGE
;
984 *endptr
= (char*)(got_digit
? nptr
: p
);
989 /*********************************************************************
990 * _strtoi64 (MSVCRT.@)
992 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
994 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
997 /*********************************************************************
1000 int __cdecl
MSVCRT__atoi_l(const char *str
, MSVCRT__locale_t locale
)
1002 __int64 ret
= MSVCRT_strtoi64_l(str
, NULL
, 10, locale
);
1006 *MSVCRT__errno() = MSVCRT_ERANGE
;
1007 } else if(ret
< INT_MIN
) {
1009 *MSVCRT__errno() = MSVCRT_ERANGE
;
1014 /*********************************************************************
1018 int __cdecl
MSVCRT_atoi(const char *str
)
1026 while(isspace(*str
)) str
++;
1030 }else if(*str
== '-') {
1035 while(*str
>='0' && *str
<='9') {
1036 ret
= ret
*10+*str
-'0';
1040 return minus
? -ret
: ret
;
1043 int CDECL
MSVCRT_atoi(const char *str
)
1045 return MSVCRT__atoi_l(str
, NULL
);
1049 /******************************************************************
1050 * _atoll_l (MSVCR120.@)
1052 MSVCRT_longlong CDECL
MSVCRT__atoll_l(const char* str
, MSVCRT__locale_t locale
)
1054 return MSVCRT_strtoi64_l(str
, NULL
, 10, locale
);
1057 /******************************************************************
1058 * atoll (MSVCR120.@)
1060 MSVCRT_longlong CDECL
MSVCRT_atoll(const char* str
)
1062 return MSVCRT__atoll_l(str
, NULL
);
1065 /******************************************************************
1066 * _strtol_l (MSVCRT.@)
1068 MSVCRT_long CDECL
MSVCRT__strtol_l(const char* nptr
,
1069 char** end
, int base
, MSVCRT__locale_t locale
)
1071 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, locale
);
1073 if(ret
> MSVCRT_LONG_MAX
) {
1074 ret
= MSVCRT_LONG_MAX
;
1075 *MSVCRT__errno() = MSVCRT_ERANGE
;
1076 } else if(ret
< MSVCRT_LONG_MIN
) {
1077 ret
= MSVCRT_LONG_MIN
;
1078 *MSVCRT__errno() = MSVCRT_ERANGE
;
1084 /******************************************************************
1087 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
1089 return MSVCRT__strtol_l(nptr
, end
, base
, NULL
);
1092 /******************************************************************
1093 * _strtoul_l (MSVCRT.@)
1095 MSVCRT_ulong CDECL
MSVCRT_strtoul_l(const char* nptr
, char** end
, int base
, MSVCRT__locale_t locale
)
1097 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, locale
);
1099 if(ret
> MSVCRT_ULONG_MAX
) {
1100 ret
= MSVCRT_ULONG_MAX
;
1101 *MSVCRT__errno() = MSVCRT_ERANGE
;
1102 }else if(ret
< -(__int64
)MSVCRT_ULONG_MAX
) {
1104 *MSVCRT__errno() = MSVCRT_ERANGE
;
1110 /******************************************************************
1111 * strtoul (MSVCRT.@)
1113 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
1115 return MSVCRT_strtoul_l(nptr
, end
, base
, NULL
);
1118 /*********************************************************************
1119 * _strtoui64_l (MSVCRT.@)
1121 * FIXME: locale parameter is ignored
1123 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
1125 const char *p
= nptr
;
1126 BOOL negative
= FALSE
;
1127 BOOL got_digit
= FALSE
;
1128 unsigned __int64 ret
= 0;
1130 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
1132 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
1133 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
1134 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
1136 while(isspace(*nptr
)) nptr
++;
1141 } else if(*nptr
== '+')
1144 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
1157 char cur
= tolower(*nptr
);
1160 if(cur
>='0' && cur
<='9') {
1165 if(cur
<'a' || cur
>='a'+base
-10)
1173 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
1174 ret
= MSVCRT_UI64_MAX
;
1175 *MSVCRT__errno() = MSVCRT_ERANGE
;
1181 *endptr
= (char*)(got_digit
? nptr
: p
);
1183 return negative
? -ret
: ret
;
1186 /*********************************************************************
1187 * _strtoui64 (MSVCRT.@)
1189 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
1191 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
1194 static int ltoa_helper(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1199 char buffer
[33], *pos
;
1202 if (value
< 0 && radix
== 10)
1209 is_negative
= FALSE
;
1218 digit
= val
% radix
;
1222 *--pos
= '0' + digit
;
1224 *--pos
= 'a' + digit
- 10;
1231 len
= buffer
+ 33 - pos
;
1237 /* Copy the temporary buffer backwards up to the available number of
1238 * characters. Don't copy the negative sign if present. */
1246 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1250 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1251 return MSVCRT_ERANGE
;
1254 memcpy(str
, pos
, len
);
1258 /*********************************************************************
1259 * _ltoa_s (MSVCRT.@)
1261 int CDECL
MSVCRT__ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1263 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1264 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1265 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1268 return MSVCRT_EINVAL
;
1271 return ltoa_helper(value
, str
, size
, radix
);
1274 /*********************************************************************
1275 * _ltow_s (MSVCRT.@)
1277 int CDECL
MSVCRT__ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1282 MSVCRT_wchar_t buffer
[33], *pos
;
1285 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1286 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1287 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1290 return MSVCRT_EINVAL
;
1293 if (value
< 0 && radix
== 10)
1300 is_negative
= FALSE
;
1309 digit
= val
% radix
;
1313 *--pos
= '0' + digit
;
1315 *--pos
= 'a' + digit
- 10;
1322 len
= buffer
+ 33 - pos
;
1326 MSVCRT_wchar_t
*p
= str
;
1328 /* Copy the temporary buffer backwards up to the available number of
1329 * characters. Don't copy the negative sign if present. */
1337 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1341 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1342 return MSVCRT_ERANGE
;
1345 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1349 /*********************************************************************
1350 * _itoa_s (MSVCRT.@)
1352 int CDECL
MSVCRT__itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
1354 return MSVCRT__ltoa_s(value
, str
, size
, radix
);
1357 /*********************************************************************
1360 char* CDECL
MSVCRT__itoa(int value
, char *str
, int radix
)
1362 return ltoa_helper(value
, str
, MSVCRT_SIZE_MAX
, radix
) ? NULL
: str
;
1365 /*********************************************************************
1366 * _itow_s (MSVCRT.@)
1368 int CDECL
MSVCRT__itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1370 return MSVCRT__ltow_s(value
, str
, size
, radix
);
1373 /*********************************************************************
1374 * _ui64toa_s (MSVCRT.@)
1376 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
1377 MSVCRT_size_t size
, int radix
)
1379 char buffer
[65], *pos
;
1382 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1383 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1384 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1387 return MSVCRT_EINVAL
;
1394 digit
= value
%radix
;
1400 *--pos
= 'a'+digit
-10;
1403 if(buffer
-pos
+65 > size
) {
1404 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1405 return MSVCRT_EINVAL
;
1408 memcpy(str
, pos
, buffer
-pos
+65);
1412 /*********************************************************************
1413 * _ui64tow_s (MSVCRT.@)
1415 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1416 MSVCRT_size_t size
, int radix
)
1418 MSVCRT_wchar_t buffer
[65], *pos
;
1421 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1422 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1423 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1426 return MSVCRT_EINVAL
;
1433 digit
= value
% radix
;
1434 value
= value
/ radix
;
1436 *--pos
= '0' + digit
;
1438 *--pos
= 'a' + digit
- 10;
1439 } while (value
!= 0);
1441 if(buffer
-pos
+65 > size
) {
1442 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1443 return MSVCRT_EINVAL
;
1446 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(MSVCRT_wchar_t
));
1450 /*********************************************************************
1451 * _ultoa_s (MSVCRT.@)
1453 int CDECL
MSVCRT__ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1456 char buffer
[33], *pos
;
1459 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1464 *MSVCRT__errno() = MSVCRT_EINVAL
;
1465 return MSVCRT_EINVAL
;
1473 digit
= value
% radix
;
1477 *--pos
= '0' + digit
;
1479 *--pos
= 'a' + digit
- 10;
1483 len
= buffer
+ 33 - pos
;
1489 /* Copy the temporary buffer backwards up to the available number of
1492 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1496 *MSVCRT__errno() = MSVCRT_ERANGE
;
1497 return MSVCRT_ERANGE
;
1500 memcpy(str
, pos
, len
);
1504 /*********************************************************************
1505 * _ultow_s (MSVCRT.@)
1507 int CDECL
MSVCRT__ultow_s(MSVCRT_ulong value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1510 WCHAR buffer
[33], *pos
;
1513 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1518 *MSVCRT__errno() = MSVCRT_EINVAL
;
1519 return MSVCRT_EINVAL
;
1527 digit
= value
% radix
;
1531 *--pos
= '0' + digit
;
1533 *--pos
= 'a' + digit
- 10;
1537 len
= buffer
+ 33 - pos
;
1543 /* Copy the temporary buffer backwards up to the available number of
1546 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1550 *MSVCRT__errno() = MSVCRT_ERANGE
;
1551 return MSVCRT_ERANGE
;
1554 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1558 /*********************************************************************
1559 * _i64toa_s (MSVCRT.@)
1561 int CDECL
MSVCRT__i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1563 unsigned __int64 val
;
1566 char buffer
[65], *pos
;
1569 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1570 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1571 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1574 return MSVCRT_EINVAL
;
1577 if (value
< 0 && radix
== 10)
1584 is_negative
= FALSE
;
1593 digit
= val
% radix
;
1597 *--pos
= '0' + digit
;
1599 *--pos
= 'a' + digit
- 10;
1606 len
= buffer
+ 65 - pos
;
1612 /* Copy the temporary buffer backwards up to the available number of
1613 * characters. Don't copy the negative sign if present. */
1621 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1625 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1626 return MSVCRT_ERANGE
;
1629 memcpy(str
, pos
, len
);
1633 /*********************************************************************
1634 * _i64tow_s (MSVCRT.@)
1636 int CDECL
MSVCRT__i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1638 unsigned __int64 val
;
1641 MSVCRT_wchar_t buffer
[65], *pos
;
1644 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1645 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1646 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1649 return MSVCRT_EINVAL
;
1652 if (value
< 0 && radix
== 10)
1659 is_negative
= FALSE
;
1668 digit
= val
% radix
;
1672 *--pos
= '0' + digit
;
1674 *--pos
= 'a' + digit
- 10;
1681 len
= buffer
+ 65 - pos
;
1685 MSVCRT_wchar_t
*p
= str
;
1687 /* Copy the temporary buffer backwards up to the available number of
1688 * characters. Don't copy the negative sign if present. */
1696 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1700 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1701 return MSVCRT_ERANGE
;
1704 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1708 #define I10_OUTPUT_MAX_PREC 21
1709 /* Internal structure used by $I10_OUTPUT */
1710 struct _I10_OUTPUT_DATA
{
1714 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1717 /*********************************************************************
1718 * $I10_OUTPUT (MSVCRT.@)
1719 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1720 * prec - precision of part, we're interested in
1721 * flag - 0 for first prec digits, 1 for fractional part
1722 * data - data to be populated
1725 * 0 if given double is NaN or INF
1729 * Native sets last byte of data->str to '0' or '9', I don't know what
1730 * it means. Current implementation sets it always to '0'.
1732 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1734 static const char inf_str
[] = "1#INF";
1735 static const char nan_str
[] = "1#QNAN";
1737 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1738 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1739 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1743 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1746 memcpy(&ld
, &ld80
, 10);
1748 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1759 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1767 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1773 int exp
= 1+floor(log10(d
));
1781 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1782 prec
= I10_OUTPUT_MAX_PREC
-1;
1788 sprintf(format
, "%%.%dle", prec
);
1789 sprintf(buf
, format
, d
);
1792 data
->pos
= atoi(buf
+prec
+3);
1796 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1799 memcpy(data
->str
, buf
+1, data
->len
);
1800 data
->str
[data
->len
] = '\0';
1802 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1803 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1807 #undef I10_OUTPUT_MAX_PREC
1809 /*********************************************************************
1812 int __cdecl
MSVCRT_memcmp(const void *ptr1
, const void *ptr2
, MSVCRT_size_t n
)
1814 return memcmp(ptr1
, ptr2
, n
);
1817 /*********************************************************************
1820 void * __cdecl
MSVCRT_memcpy(void *dst
, const void *src
, MSVCRT_size_t n
)
1822 return memmove(dst
, src
, n
);
1825 /*********************************************************************
1826 * memmove (MSVCRT.@)
1828 void * __cdecl
MSVCRT_memmove(void *dst
, const void *src
, MSVCRT_size_t n
)
1830 return memmove(dst
, src
, n
);
1833 /*********************************************************************
1836 void* __cdecl
MSVCRT_memset(void *dst
, int c
, MSVCRT_size_t n
)
1838 return memset(dst
, c
, n
);
1841 /*********************************************************************
1844 char* __cdecl
MSVCRT_strchr(const char *str
, int c
)
1846 return strchr(str
, c
);
1849 /*********************************************************************
1850 * strrchr (MSVCRT.@)
1852 char* __cdecl
MSVCRT_strrchr(const char *str
, int c
)
1854 return strrchr(str
, c
);
1857 /*********************************************************************
1860 void* __cdecl
MSVCRT_memchr(const void *ptr
, int c
, MSVCRT_size_t n
)
1862 return memchr(ptr
, c
, n
);
1865 /*********************************************************************
1868 int __cdecl
MSVCRT_strcmp(const char *str1
, const char *str2
)
1870 return strcmp(str1
, str2
);
1873 /*********************************************************************
1874 * strncmp (MSVCRT.@)
1876 int __cdecl
MSVCRT_strncmp(const char *str1
, const char *str2
, MSVCRT_size_t len
)
1878 return strncmp(str1
, str2
, len
);
1881 /*********************************************************************
1882 * _strnicmp_l (MSVCRT.@)
1884 int __cdecl
MSVCRT__strnicmp_l(const char *s1
, const char *s2
,
1885 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1887 MSVCRT_pthreadlocinfo locinfo
;
1890 if(s1
==NULL
|| s2
==NULL
)
1891 return MSVCRT__NLSCMPERROR
;
1897 locinfo
= get_locinfo();
1899 locinfo
= locale
->locinfo
;
1901 if(!locinfo
->lc_handle
[MSVCRT_LC_CTYPE
])
1902 return strncasecmp(s1
, s2
, count
);
1905 c1
= MSVCRT__tolower_l(*s1
++, locale
);
1906 c2
= MSVCRT__tolower_l(*s2
++, locale
);
1907 }while(--count
&& c1
&& c1
==c2
);
1912 /*********************************************************************
1913 * _stricmp_l (MSVCRT.@)
1915 int __cdecl
MSVCRT__stricmp_l(const char *s1
, const char *s2
, MSVCRT__locale_t locale
)
1917 return MSVCRT__strnicmp_l(s1
, s2
, -1, locale
);
1920 /*********************************************************************
1921 * _strnicmp (MSVCRT.@)
1923 int __cdecl
MSVCRT__strnicmp(const char *s1
, const char *s2
, MSVCRT_size_t count
)
1925 return MSVCRT__strnicmp_l(s1
, s2
, count
, NULL
);
1928 /*********************************************************************
1929 * _stricmp (MSVCRT.@)
1931 int __cdecl
MSVCRT__stricmp(const char *s1
, const char *s2
)
1933 return MSVCRT__strnicmp_l(s1
, s2
, -1, NULL
);
1936 /*********************************************************************
1939 char* __cdecl
MSVCRT_strstr(const char *haystack
, const char *needle
)
1941 return strstr(haystack
, needle
);
1944 /*********************************************************************
1945 * _memicmp_l (MSVCRT.@)
1947 int __cdecl
MSVCRT__memicmp_l(const char *s1
, const char *s2
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
1951 #if _MSVCR_VER == 0 || _MSVCR_VER >= 80
1955 MSVCRT_INVALID_PMT(NULL
, EINVAL
);
1956 return len
? MSVCRT__NLSCMPERROR
: 0;
1962 if ((ret
= MSVCRT__tolower_l(*s1
, locale
) - MSVCRT__tolower_l(*s2
, locale
)))
1970 /*********************************************************************
1971 * _memicmp (MSVCRT.@)
1973 int __cdecl
MSVCRT__memicmp(const char *s1
, const char *s2
, MSVCRT_size_t len
)
1975 return MSVCRT__memicmp_l(s1
, s2
, len
, NULL
);