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
))
280 while(**ctx
&& !strchr(delim
, **ctx
))
288 /*********************************************************************
291 void CDECL
MSVCRT__swab(char* src
, char* dst
, int len
)
295 len
= (unsigned)len
>> 1;
307 static double strtod_helper(const char *str
, char **end
, MSVCRT__locale_t locale
, int *err
)
309 MSVCRT_pthreadlocinfo locinfo
;
310 unsigned __int64 d
=0, hlp
;
315 long double lret
=1, expcnt
= 10;
316 BOOL found_digit
= FALSE
, negexp
;
321 else if(!MSVCRT_CHECK_PMT(str
!= NULL
)) {
328 locinfo
= get_locinfo();
330 locinfo
= locale
->locinfo
;
332 /* FIXME: use *_l functions */
343 #if _MSVCR_VER >= 140
344 if(tolower(p
[0]) == 'i' && tolower(p
[1]) == 'n' && tolower(p
[2]) == 'f') {
346 *end
= (char*) &p
[3];
347 if(tolower(p
[3]) == 'i' && tolower(p
[4]) == 'n' && tolower(p
[5]) == 'i' &&
348 tolower(p
[6]) == 't' && tolower(p
[7]) == 'y' && end
)
349 *end
= (char*) &p
[8];
350 return sign
*INFINITY
;
352 if(tolower(p
[0]) == 'n' &&
353 tolower(p
[1]) == 'a' &&
354 tolower(p
[2]) == 'n') {
356 *end
= (char*) &p
[3];
360 if(p
[0] == '0' && tolower(p
[1]) == 'x') {
367 while((*p
>='0' && *p
<='9') ||
368 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F')))) {
372 if (c
>='0' && c
<='9')
374 else if (c
>= 'a' && c
<= 'f')
379 if(d
>MSVCRT_UI64_MAX
/base
|| hlp
<d
) {
385 while((*p
>='0' && *p
<='9') ||
386 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F')))) {
391 if(*p
== *locinfo
->lconv
->decimal_point
)
394 while((*p
>='0' && *p
<='9') ||
395 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F')))) {
399 if (c
>='0' && c
<='9')
401 else if (c
>= 'a' && c
<= 'f')
406 if(d
>MSVCRT_UI64_MAX
/base
|| hlp
<d
)
411 while((*p
>='0' && *p
<='9') ||
412 (base
== 16 && ((*p
>= 'a' && *p
<= 'f') || (*p
>= 'A' && *p
<= 'F'))))
424 if((base
== 10 && (*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D')) ||
425 (base
== 16 && (*p
=='p' || *p
=='P'))) {
435 if(*p
>='0' && *p
<='9') {
436 while(*p
>='0' && *p
<='9') {
437 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
443 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
444 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
447 if(*p
=='-' || *p
=='+')
453 fpcontrol
= _control87(0, 0);
454 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
455 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
464 expcnt
= expcnt
*expcnt
;
466 ret
= (long double)sign
* (negexp
? d
/lret
: d
*lret
);
468 _control87(fpcontrol
, 0xffffffff);
470 if((d
&& ret
==0.0) || isinf(ret
)) {
472 *err
= MSVCRT_ERANGE
;
474 *MSVCRT__errno() = MSVCRT_ERANGE
;
483 /*********************************************************************
484 * strtod_l (MSVCRT.@)
486 double CDECL
MSVCRT_strtod_l(const char *str
, char **end
, MSVCRT__locale_t locale
)
488 return strtod_helper(str
, end
, locale
, NULL
);
491 /*********************************************************************
494 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
496 return MSVCRT_strtod_l( str
, end
, NULL
);
499 /*********************************************************************
500 * strtof_l (MSVCR120.@)
502 float CDECL
MSVCRT__strtof_l( const char *str
, char **end
, MSVCRT__locale_t locale
)
504 return MSVCRT_strtod_l(str
, end
, locale
);
507 /*********************************************************************
508 * strtof (MSVCR120.@)
510 float CDECL
MSVCRT_strtof( const char *str
, char **end
)
512 return MSVCRT__strtof_l(str
, end
, NULL
);
515 /*********************************************************************
518 double CDECL
MSVCRT_atof( const char *str
)
520 return MSVCRT_strtod_l(str
, NULL
, NULL
);
523 /*********************************************************************
526 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
528 return MSVCRT_strtod_l(str
, NULL
, locale
);
531 /*********************************************************************
532 * _atoflt_l (MSVCRT.@)
534 int CDECL
MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT
*value
, char *str
, MSVCRT__locale_t locale
)
539 d
= strtod_helper(str
, NULL
, locale
, &err
);
542 return MSVCRT__OVERFLOW
;
543 if((d
!=0 || err
) && value
->f
>-MSVCRT_FLT_MIN
&& value
->f
<MSVCRT_FLT_MIN
)
544 return MSVCRT__UNDERFLOW
;
548 /*********************************************************************
549 * _atoflt (MSVCR100.@)
551 int CDECL
MSVCRT__atoflt(MSVCRT__CRT_FLOAT
*value
, char *str
)
553 return MSVCRT__atoflt_l(value
, str
, NULL
);
556 /*********************************************************************
557 * _atodbl_l (MSVCRT.@)
559 int CDECL
MSVCRT__atodbl_l(MSVCRT__CRT_DOUBLE
*value
, char *str
, MSVCRT__locale_t locale
)
563 value
->x
= strtod_helper(str
, NULL
, locale
, &err
);
565 return MSVCRT__OVERFLOW
;
566 if((value
->x
!=0 || err
) && value
->x
>-MSVCRT_DBL_MIN
&& value
->x
<MSVCRT_DBL_MIN
)
567 return MSVCRT__UNDERFLOW
;
571 /*********************************************************************
574 int CDECL
MSVCRT__atodbl(MSVCRT__CRT_DOUBLE
*value
, char *str
)
576 return MSVCRT__atodbl_l(value
, str
, NULL
);
579 /*********************************************************************
580 * _strcoll_l (MSVCRT.@)
582 int CDECL
MSVCRT_strcoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
584 MSVCRT_pthreadlocinfo locinfo
;
587 locinfo
= get_locinfo();
589 locinfo
= locale
->locinfo
;
591 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
592 return strcmp(str1
, str2
);
593 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, -1, str2
, -1)-CSTR_EQUAL
;
596 /*********************************************************************
599 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
601 return MSVCRT_strcoll_l(str1
, str2
, NULL
);
604 /*********************************************************************
605 * _stricoll_l (MSVCRT.@)
607 int CDECL
MSVCRT__stricoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
609 MSVCRT_pthreadlocinfo locinfo
;
612 locinfo
= get_locinfo();
614 locinfo
= locale
->locinfo
;
616 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
617 return strcasecmp(str1
, str2
);
618 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
619 str1
, -1, str2
, -1)-CSTR_EQUAL
;
622 /*********************************************************************
623 * _stricoll (MSVCRT.@)
625 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
627 return MSVCRT__stricoll_l(str1
, str2
, NULL
);
630 /*********************************************************************
631 * _strncoll_l (MSVCRT.@)
633 int CDECL
MSVCRT__strncoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
635 MSVCRT_pthreadlocinfo locinfo
;
638 locinfo
= get_locinfo();
640 locinfo
= locale
->locinfo
;
642 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
643 return strncmp(str1
, str2
, count
);
644 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, count
, str2
, count
)-CSTR_EQUAL
;
647 /*********************************************************************
648 * _strncoll (MSVCRT.@)
650 int CDECL
MSVCRT__strncoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
652 return MSVCRT__strncoll_l(str1
, str2
, count
, NULL
);
655 /*********************************************************************
656 * _strnicoll_l (MSVCRT.@)
658 int CDECL
MSVCRT__strnicoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
660 MSVCRT_pthreadlocinfo locinfo
;
663 locinfo
= get_locinfo();
665 locinfo
= locale
->locinfo
;
667 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
])
668 return strncasecmp(str1
, str2
, count
);
669 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
670 str1
, count
, str2
, count
)-CSTR_EQUAL
;
673 /*********************************************************************
674 * _strnicoll (MSVCRT.@)
676 int CDECL
MSVCRT__strnicoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
678 return MSVCRT__strnicoll_l(str1
, str2
, count
, NULL
);
681 /*********************************************************************
684 char* __cdecl
MSVCRT_strncpy(char *dst
, const char *src
, MSVCRT_size_t len
)
689 if((dst
[i
] = src
[i
]) == '\0') break;
691 while (i
< len
) dst
[i
++] = 0;
696 /*********************************************************************
697 * strcpy_s (MSVCRT.@)
699 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
702 if(!elem
) return MSVCRT_EINVAL
;
703 if(!dst
) return MSVCRT_EINVAL
;
707 return MSVCRT_EINVAL
;
710 for(i
= 0; i
< elem
; i
++)
712 if((dst
[i
] = src
[i
]) == '\0') return 0;
715 return MSVCRT_ERANGE
;
718 /*********************************************************************
719 * strcat_s (MSVCRT.@)
721 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
724 if(!dst
) return MSVCRT_EINVAL
;
725 if(elem
== 0) return MSVCRT_EINVAL
;
729 return MSVCRT_EINVAL
;
732 for(i
= 0; i
< elem
; i
++)
736 for(j
= 0; (j
+ i
) < elem
; j
++)
738 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
742 /* Set the first element to 0, not the first element after the skipped part */
744 return MSVCRT_ERANGE
;
747 /*********************************************************************
748 * strncat_s (MSVCRT.@)
750 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
754 if (!MSVCRT_CHECK_PMT(dst
!= 0)) return MSVCRT_EINVAL
;
755 if (!MSVCRT_CHECK_PMT(elem
!= 0)) return MSVCRT_EINVAL
;
756 if (!MSVCRT_CHECK_PMT(src
!= 0))
759 return MSVCRT_EINVAL
;
762 for(i
= 0; i
< elem
; i
++)
766 for(j
= 0; (j
+ i
) < elem
; j
++)
768 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
771 return MSVCRT_STRUNCATE
;
773 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
781 /* Set the first element to 0, not the first element after the skipped part */
783 return MSVCRT_ERANGE
;
786 /*********************************************************************
789 char* __cdecl
MSVCRT_strncat(char *dst
, const char *src
, MSVCRT_size_t len
)
791 return strncat(dst
, src
, len
);
794 /*********************************************************************
795 * _strxfrm_l (MSVCRT.@)
797 MSVCRT_size_t CDECL
MSVCRT__strxfrm_l( char *dest
, const char *src
,
798 MSVCRT_size_t len
, MSVCRT__locale_t locale
)
800 MSVCRT_pthreadlocinfo locinfo
;
803 if(!MSVCRT_CHECK_PMT(src
)) return INT_MAX
;
804 if(!MSVCRT_CHECK_PMT(dest
|| !len
)) return INT_MAX
;
807 FIXME("len > INT_MAX not supported\n");
812 locinfo
= get_locinfo();
814 locinfo
= locale
->locinfo
;
816 if(!locinfo
->lc_handle
[MSVCRT_LC_COLLATE
]) {
817 MSVCRT_strncpy(dest
, src
, len
);
821 ret
= LCMapStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
],
822 LCMAP_SORTKEY
, src
, -1, NULL
, 0);
825 *MSVCRT__errno() = MSVCRT_EILSEQ
;
828 if(!len
) return ret
-1;
832 *MSVCRT__errno() = MSVCRT_ERANGE
;
836 return LCMapStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
],
837 LCMAP_SORTKEY
, src
, -1, dest
, len
) - 1;
840 /*********************************************************************
843 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
845 return MSVCRT__strxfrm_l(dest
, src
, len
, NULL
);
848 /********************************************************************
849 * _atoldbl (MSVCRT.@)
851 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
853 /* FIXME needs error checking for huge/small values */
856 TRACE("str %s value %p\n",str
,value
);
858 memcpy(value
, &ld
, 10);
860 FIXME("stub, str %s value %p\n",str
,value
);
865 /********************************************************************
866 * __STRINGTOLD (MSVCRT.@)
868 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
872 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
874 memcpy(value
, &ld
, 10);
876 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
881 /*********************************************************************
884 MSVCRT_size_t __cdecl
MSVCRT_strlen(const char *str
)
889 /******************************************************************
892 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
896 for(i
=0; i
<maxlen
; i
++)
902 /*********************************************************************
903 * _strtoi64_l (MSVCRT.@)
905 * FIXME: locale parameter is ignored
907 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
909 const char *p
= nptr
;
910 BOOL negative
= FALSE
;
911 BOOL got_digit
= FALSE
;
914 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
916 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
917 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
918 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
920 while(isspace(*nptr
)) nptr
++;
925 } else if(*nptr
== '+')
928 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
941 char cur
= tolower(*nptr
);
944 if(cur
>='0' && cur
<='9') {
949 if(cur
<'a' || cur
>='a'+base
-10)
960 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
961 ret
= MSVCRT_I64_MAX
;
962 *MSVCRT__errno() = MSVCRT_ERANGE
;
963 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
964 ret
= MSVCRT_I64_MIN
;
965 *MSVCRT__errno() = MSVCRT_ERANGE
;
971 *endptr
= (char*)(got_digit
? nptr
: p
);
976 /*********************************************************************
977 * _strtoi64 (MSVCRT.@)
979 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
981 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
984 /*********************************************************************
987 int __cdecl
MSVCRT__atoi_l(const char *str
, MSVCRT__locale_t locale
)
989 __int64 ret
= MSVCRT_strtoi64_l(str
, NULL
, 10, locale
);
993 *MSVCRT__errno() = MSVCRT_ERANGE
;
994 } else if(ret
< INT_MIN
) {
996 *MSVCRT__errno() = MSVCRT_ERANGE
;
1001 /*********************************************************************
1005 int __cdecl
MSVCRT_atoi(const char *str
)
1013 while(isspace(*str
)) str
++;
1017 }else if(*str
== '-') {
1022 while(*str
>='0' && *str
<='9') {
1023 ret
= ret
*10+*str
-'0';
1027 return minus
? -ret
: ret
;
1030 int CDECL
MSVCRT_atoi(const char *str
)
1032 return MSVCRT__atoi_l(str
, NULL
);
1036 /******************************************************************
1037 * _atoll_l (MSVCR120.@)
1039 MSVCRT_longlong CDECL
MSVCRT__atoll_l(const char* str
, MSVCRT__locale_t locale
)
1041 return MSVCRT_strtoi64_l(str
, NULL
, 10, locale
);
1044 /******************************************************************
1045 * atoll (MSVCR120.@)
1047 MSVCRT_longlong CDECL
MSVCRT_atoll(const char* str
)
1049 return MSVCRT__atoll_l(str
, NULL
);
1052 /******************************************************************
1053 * _strtol_l (MSVCRT.@)
1055 MSVCRT_long CDECL
MSVCRT__strtol_l(const char* nptr
,
1056 char** end
, int base
, MSVCRT__locale_t locale
)
1058 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, locale
);
1060 if(ret
> MSVCRT_LONG_MAX
) {
1061 ret
= MSVCRT_LONG_MAX
;
1062 *MSVCRT__errno() = MSVCRT_ERANGE
;
1063 } else if(ret
< MSVCRT_LONG_MIN
) {
1064 ret
= MSVCRT_LONG_MIN
;
1065 *MSVCRT__errno() = MSVCRT_ERANGE
;
1071 /******************************************************************
1074 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
1076 return MSVCRT__strtol_l(nptr
, end
, base
, NULL
);
1079 /******************************************************************
1080 * _strtoul_l (MSVCRT.@)
1082 MSVCRT_ulong CDECL
MSVCRT_strtoul_l(const char* nptr
, char** end
, int base
, MSVCRT__locale_t locale
)
1084 __int64 ret
= MSVCRT_strtoi64_l(nptr
, end
, base
, locale
);
1086 if(ret
> MSVCRT_ULONG_MAX
) {
1087 ret
= MSVCRT_ULONG_MAX
;
1088 *MSVCRT__errno() = MSVCRT_ERANGE
;
1089 }else if(ret
< -(__int64
)MSVCRT_ULONG_MAX
) {
1091 *MSVCRT__errno() = MSVCRT_ERANGE
;
1097 /******************************************************************
1098 * strtoul (MSVCRT.@)
1100 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
1102 return MSVCRT_strtoul_l(nptr
, end
, base
, NULL
);
1105 /*********************************************************************
1106 * _strtoui64_l (MSVCRT.@)
1108 * FIXME: locale parameter is ignored
1110 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
1112 const char *p
= nptr
;
1113 BOOL negative
= FALSE
;
1114 BOOL got_digit
= FALSE
;
1115 unsigned __int64 ret
= 0;
1117 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
1119 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
1120 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
1121 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
1123 while(isspace(*nptr
)) nptr
++;
1128 } else if(*nptr
== '+')
1131 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
1144 char cur
= tolower(*nptr
);
1147 if(cur
>='0' && cur
<='9') {
1152 if(cur
<'a' || cur
>='a'+base
-10)
1160 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
1161 ret
= MSVCRT_UI64_MAX
;
1162 *MSVCRT__errno() = MSVCRT_ERANGE
;
1168 *endptr
= (char*)(got_digit
? nptr
: p
);
1170 return negative
? -ret
: ret
;
1173 /*********************************************************************
1174 * _strtoui64 (MSVCRT.@)
1176 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
1178 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
1181 static int ltoa_helper(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1186 char buffer
[33], *pos
;
1189 if (value
< 0 && radix
== 10)
1196 is_negative
= FALSE
;
1205 digit
= val
% radix
;
1209 *--pos
= '0' + digit
;
1211 *--pos
= 'a' + digit
- 10;
1218 len
= buffer
+ 33 - pos
;
1224 /* Copy the temporary buffer backwards up to the available number of
1225 * characters. Don't copy the negative sign if present. */
1233 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1237 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1238 return MSVCRT_ERANGE
;
1241 memcpy(str
, pos
, len
);
1245 /*********************************************************************
1246 * _ltoa_s (MSVCRT.@)
1248 int CDECL
MSVCRT__ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1250 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1251 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1252 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1255 return MSVCRT_EINVAL
;
1258 return ltoa_helper(value
, str
, size
, radix
);
1261 /*********************************************************************
1262 * _ltow_s (MSVCRT.@)
1264 int CDECL
MSVCRT__ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1269 MSVCRT_wchar_t buffer
[33], *pos
;
1272 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1273 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1274 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1277 return MSVCRT_EINVAL
;
1280 if (value
< 0 && radix
== 10)
1287 is_negative
= FALSE
;
1296 digit
= val
% radix
;
1300 *--pos
= '0' + digit
;
1302 *--pos
= 'a' + digit
- 10;
1309 len
= buffer
+ 33 - pos
;
1313 MSVCRT_wchar_t
*p
= str
;
1315 /* Copy the temporary buffer backwards up to the available number of
1316 * characters. Don't copy the negative sign if present. */
1324 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1328 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1329 return MSVCRT_ERANGE
;
1332 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1336 /*********************************************************************
1337 * _itoa_s (MSVCRT.@)
1339 int CDECL
MSVCRT__itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
1341 return MSVCRT__ltoa_s(value
, str
, size
, radix
);
1344 /*********************************************************************
1347 char* CDECL
MSVCRT__itoa(int value
, char *str
, int radix
)
1349 return ltoa_helper(value
, str
, MSVCRT_SIZE_MAX
, radix
) ? NULL
: str
;
1352 /*********************************************************************
1353 * _itow_s (MSVCRT.@)
1355 int CDECL
MSVCRT__itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1357 return MSVCRT__ltow_s(value
, str
, size
, radix
);
1360 /*********************************************************************
1361 * _ui64toa_s (MSVCRT.@)
1363 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
1364 MSVCRT_size_t size
, int radix
)
1366 char buffer
[65], *pos
;
1369 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1370 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1371 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1374 return MSVCRT_EINVAL
;
1381 digit
= value
%radix
;
1387 *--pos
= 'a'+digit
-10;
1390 if(buffer
-pos
+65 > size
) {
1391 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1392 return MSVCRT_EINVAL
;
1395 memcpy(str
, pos
, buffer
-pos
+65);
1399 /*********************************************************************
1400 * _ui64tow_s (MSVCRT.@)
1402 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1403 MSVCRT_size_t size
, int radix
)
1405 MSVCRT_wchar_t buffer
[65], *pos
;
1408 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1409 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1410 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1413 return MSVCRT_EINVAL
;
1420 digit
= value
% radix
;
1421 value
= value
/ radix
;
1423 *--pos
= '0' + digit
;
1425 *--pos
= 'a' + digit
- 10;
1426 } while (value
!= 0);
1428 if(buffer
-pos
+65 > size
) {
1429 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1430 return MSVCRT_EINVAL
;
1433 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(MSVCRT_wchar_t
));
1437 /*********************************************************************
1438 * _ultoa_s (MSVCRT.@)
1440 int CDECL
MSVCRT__ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1443 char buffer
[33], *pos
;
1446 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1451 *MSVCRT__errno() = MSVCRT_EINVAL
;
1452 return MSVCRT_EINVAL
;
1460 digit
= value
% radix
;
1464 *--pos
= '0' + digit
;
1466 *--pos
= 'a' + digit
- 10;
1470 len
= buffer
+ 33 - pos
;
1476 /* Copy the temporary buffer backwards up to the available number of
1479 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1483 *MSVCRT__errno() = MSVCRT_ERANGE
;
1484 return MSVCRT_ERANGE
;
1487 memcpy(str
, pos
, len
);
1491 /*********************************************************************
1492 * _ultow_s (MSVCRT.@)
1494 int CDECL
MSVCRT__ultow_s(MSVCRT_ulong value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1497 WCHAR buffer
[33], *pos
;
1500 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1505 *MSVCRT__errno() = MSVCRT_EINVAL
;
1506 return MSVCRT_EINVAL
;
1514 digit
= value
% radix
;
1518 *--pos
= '0' + digit
;
1520 *--pos
= 'a' + digit
- 10;
1524 len
= buffer
+ 33 - pos
;
1530 /* Copy the temporary buffer backwards up to the available number of
1533 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1537 *MSVCRT__errno() = MSVCRT_ERANGE
;
1538 return MSVCRT_ERANGE
;
1541 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1545 /*********************************************************************
1546 * _i64toa_s (MSVCRT.@)
1548 int CDECL
MSVCRT__i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1550 unsigned __int64 val
;
1553 char buffer
[65], *pos
;
1556 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1557 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1558 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1561 return MSVCRT_EINVAL
;
1564 if (value
< 0 && radix
== 10)
1571 is_negative
= FALSE
;
1580 digit
= val
% radix
;
1584 *--pos
= '0' + digit
;
1586 *--pos
= 'a' + digit
- 10;
1593 len
= buffer
+ 65 - pos
;
1599 /* Copy the temporary buffer backwards up to the available number of
1600 * characters. Don't copy the negative sign if present. */
1608 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1612 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1613 return MSVCRT_ERANGE
;
1616 memcpy(str
, pos
, len
);
1620 /*********************************************************************
1621 * _i64tow_s (MSVCRT.@)
1623 int CDECL
MSVCRT__i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1625 unsigned __int64 val
;
1628 MSVCRT_wchar_t buffer
[65], *pos
;
1631 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1632 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1633 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1636 return MSVCRT_EINVAL
;
1639 if (value
< 0 && radix
== 10)
1646 is_negative
= FALSE
;
1655 digit
= val
% radix
;
1659 *--pos
= '0' + digit
;
1661 *--pos
= 'a' + digit
- 10;
1668 len
= buffer
+ 65 - pos
;
1672 MSVCRT_wchar_t
*p
= str
;
1674 /* Copy the temporary buffer backwards up to the available number of
1675 * characters. Don't copy the negative sign if present. */
1683 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1687 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1688 return MSVCRT_ERANGE
;
1691 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1695 #define I10_OUTPUT_MAX_PREC 21
1696 /* Internal structure used by $I10_OUTPUT */
1697 struct _I10_OUTPUT_DATA
{
1701 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1704 /*********************************************************************
1705 * $I10_OUTPUT (MSVCRT.@)
1706 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1707 * prec - precision of part, we're interested in
1708 * flag - 0 for first prec digits, 1 for fractional part
1709 * data - data to be populated
1712 * 0 if given double is NaN or INF
1716 * Native sets last byte of data->str to '0' or '9', I don't know what
1717 * it means. Current implementation sets it always to '0'.
1719 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1721 static const char inf_str
[] = "1#INF";
1722 static const char nan_str
[] = "1#QNAN";
1724 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1725 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1726 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1730 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1733 memcpy(&ld
, &ld80
, 10);
1735 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1746 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1754 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1760 int exp
= 1+floor(log10(d
));
1768 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1769 prec
= I10_OUTPUT_MAX_PREC
-1;
1775 sprintf(format
, "%%.%dle", prec
);
1776 sprintf(buf
, format
, d
);
1779 data
->pos
= atoi(buf
+prec
+3);
1783 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1786 memcpy(data
->str
, buf
+1, data
->len
);
1787 data
->str
[data
->len
] = '\0';
1789 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1790 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1794 #undef I10_OUTPUT_MAX_PREC
1796 /*********************************************************************
1799 int __cdecl
MSVCRT_memcmp(const void *ptr1
, const void *ptr2
, MSVCRT_size_t n
)
1801 return memcmp(ptr1
, ptr2
, n
);
1804 /*********************************************************************
1807 void * __cdecl
MSVCRT_memcpy(void *dst
, const void *src
, MSVCRT_size_t n
)
1809 return memmove(dst
, src
, n
);
1812 /*********************************************************************
1813 * memmove (MSVCRT.@)
1815 void * __cdecl
MSVCRT_memmove(void *dst
, const void *src
, MSVCRT_size_t n
)
1817 return memmove(dst
, src
, n
);
1820 /*********************************************************************
1823 void* __cdecl
MSVCRT_memset(void *dst
, int c
, MSVCRT_size_t n
)
1825 return memset(dst
, c
, n
);
1828 /*********************************************************************
1831 char* __cdecl
MSVCRT_strchr(const char *str
, int c
)
1833 return strchr(str
, c
);
1836 /*********************************************************************
1837 * strrchr (MSVCRT.@)
1839 char* __cdecl
MSVCRT_strrchr(const char *str
, int c
)
1841 return strrchr(str
, c
);
1844 /*********************************************************************
1847 void* __cdecl
MSVCRT_memchr(const void *ptr
, int c
, MSVCRT_size_t n
)
1849 return memchr(ptr
, c
, n
);
1852 /*********************************************************************
1855 int __cdecl
MSVCRT_strcmp(const char *str1
, const char *str2
)
1857 return strcmp(str1
, str2
);
1860 /*********************************************************************
1861 * strncmp (MSVCRT.@)
1863 int __cdecl
MSVCRT_strncmp(const char *str1
, const char *str2
, MSVCRT_size_t len
)
1865 return strncmp(str1
, str2
, len
);
1868 /*********************************************************************
1869 * _strnicmp_l (MSVCRT.@)
1871 int __cdecl
MSVCRT__strnicmp_l(const char *s1
, const char *s2
,
1872 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1874 MSVCRT_pthreadlocinfo locinfo
;
1877 if(s1
==NULL
|| s2
==NULL
)
1878 return MSVCRT__NLSCMPERROR
;
1884 locinfo
= get_locinfo();
1886 locinfo
= locale
->locinfo
;
1888 if(!locinfo
->lc_handle
[MSVCRT_LC_CTYPE
])
1889 return strncasecmp(s1
, s2
, count
);
1892 c1
= MSVCRT__tolower_l(*s1
++, locale
);
1893 c2
= MSVCRT__tolower_l(*s2
++, locale
);
1894 }while(--count
&& c1
&& c1
==c2
);
1899 /*********************************************************************
1900 * _stricmp_l (MSVCRT.@)
1902 int __cdecl
MSVCRT__stricmp_l(const char *s1
, const char *s2
, MSVCRT__locale_t locale
)
1904 return MSVCRT__strnicmp_l(s1
, s2
, -1, locale
);
1907 /*********************************************************************
1908 * _strnicmp (MSVCRT.@)
1910 int __cdecl
MSVCRT__strnicmp(const char *s1
, const char *s2
, MSVCRT_size_t count
)
1912 return MSVCRT__strnicmp_l(s1
, s2
, count
, NULL
);
1915 /*********************************************************************
1916 * _stricmp (MSVCRT.@)
1918 int __cdecl
MSVCRT__stricmp(const char *s1
, const char *s2
)
1920 return MSVCRT__strnicmp_l(s1
, s2
, -1, NULL
);
1923 /*********************************************************************
1926 char* __cdecl
MSVCRT_strstr(const char *haystack
, const char *needle
)
1928 return strstr(haystack
, needle
);