2 * MSVCRT string functions
4 * Copyright 1996,1998 Marcus Meissner
5 * Copyright 1996 Jukka Iivonen
6 * Copyright 1997,2000 Uwe Bonnes
7 * Copyright 2000 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define _ISOC99_SOURCE
26 #include "wine/port.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
39 /*********************************************************************
43 char* CDECL
MSVCRT__strdup(const char* str
)
47 char * ret
= MSVCRT_malloc(strlen(str
)+1);
48 if (ret
) strcpy( ret
, str
);
54 /*********************************************************************
55 * _strlwr_s_l (MSVCRT.@)
57 int CDECL
_strlwr_s_l(char *str
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
63 *MSVCRT__errno() = MSVCRT_EINVAL
;
76 *MSVCRT__errno() = MSVCRT_EINVAL
;
82 *str
= MSVCRT__tolower_l((unsigned char)*str
, locale
);
89 /*********************************************************************
90 * _strlwr_s (MSVCRT.@)
92 int CDECL
_strlwr_s(char *str
, MSVCRT_size_t len
)
94 return _strlwr_s_l(str
, len
, NULL
);
97 /*********************************************************************
98 * _strlwr_l (MSVCRT.@)
100 char* CDECL
_strlwr_l(char *str
, MSVCRT__locale_t locale
)
102 _strlwr_s_l(str
, -1, locale
);
106 /*********************************************************************
109 char* CDECL
MSVCRT__strlwr(char *str
)
111 _strlwr_s_l(str
, -1, NULL
);
115 /*********************************************************************
116 * _strupr_s_l (MSVCRT.@)
118 int CDECL
_strupr_s_l(char *str
, MSVCRT_size_t len
, MSVCRT__locale_t locale
)
124 *MSVCRT__errno() = MSVCRT_EINVAL
;
125 return MSVCRT_EINVAL
;
137 *MSVCRT__errno() = MSVCRT_EINVAL
;
138 return MSVCRT_EINVAL
;
143 *str
= MSVCRT__toupper_l((unsigned char)*str
, locale
);
150 /*********************************************************************
151 * _strupr_s (MSVCRT.@)
153 int CDECL
_strupr_s(char *str
, MSVCRT_size_t len
)
155 return _strupr_s_l(str
, len
, NULL
);
158 /*********************************************************************
159 * _strupr_l (MSVCRT.@)
161 char* CDECL
MSVCRT__strupr_l(char *str
, MSVCRT__locale_t locale
)
163 _strupr_s_l(str
, -1, locale
);
167 /*********************************************************************
170 char* CDECL
MSVCRT__strupr(char *str
)
172 _strupr_s_l(str
, -1, NULL
);
176 /*********************************************************************
177 * _strnset (MSVCRT.@)
179 char* CDECL
MSVCRT__strnset(char* str
, int value
, MSVCRT_size_t len
)
182 while (*str
&& len
--)
187 /*********************************************************************
190 char* CDECL
MSVCRT__strrev(char* str
)
196 for (p1
= str
, p2
= str
+ strlen(str
) - 1; p2
> p1
; ++p1
, --p2
)
206 /*********************************************************************
209 char* CDECL
_strset(char* str
, int value
)
218 /*********************************************************************
221 char * CDECL
MSVCRT_strtok( char *str
, const char *delim
)
223 thread_data_t
*data
= msvcrt_get_thread_data();
227 if (!(str
= data
->strtok_next
)) return NULL
;
229 while (*str
&& strchr( delim
, *str
)) str
++;
230 if (!*str
) return NULL
;
232 while (*str
&& !strchr( delim
, *str
)) str
++;
233 if (*str
) *str
++ = 0;
234 data
->strtok_next
= str
;
238 /*********************************************************************
239 * strtok_s (MSVCRT.@)
241 char * CDECL
MSVCRT_strtok_s(char *str
, const char *delim
, char **ctx
)
243 if (!MSVCRT_CHECK_PMT(delim
!= NULL
)) return NULL
;
244 if (!MSVCRT_CHECK_PMT(ctx
!= NULL
)) return NULL
;
245 if (!MSVCRT_CHECK_PMT(str
!= NULL
|| *ctx
!= NULL
)) return NULL
;
250 while(*str
&& strchr(delim
, *str
))
256 while(**ctx
&& !strchr(delim
, **ctx
))
264 /*********************************************************************
267 void CDECL
MSVCRT__swab(char* src
, char* dst
, int len
)
271 len
= (unsigned)len
>> 1;
283 static double strtod_helper(const char *str
, char **end
, MSVCRT__locale_t locale
, int *err
)
285 MSVCRT_pthreadlocinfo locinfo
;
286 unsigned __int64 d
=0, hlp
;
291 long double lret
=1, expcnt
= 10;
292 BOOL found_digit
= FALSE
, negexp
;
297 if(!MSVCRT_CHECK_PMT(str
!= NULL
)) return 0;
300 locinfo
= get_locinfo();
302 locinfo
= locale
->locinfo
;
304 /* FIXME: use *_l functions */
317 hlp
= d
*10+*(p
++)-'0';
318 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
) {
329 if(*p
== *locinfo
->lconv
->decimal_point
)
334 hlp
= d
*10+*(p
++)-'0';
335 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
)
350 if(*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D') {
362 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
368 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
369 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
372 if(*p
=='-' || *p
=='+')
378 fpcontrol
= _control87(0, 0);
379 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
380 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
389 expcnt
= expcnt
*expcnt
;
391 ret
= (long double)sign
* (negexp
? d
/lret
: d
*lret
);
393 _control87(fpcontrol
, 0xffffffff);
395 if((d
&& ret
==0.0) || isinf(ret
)) {
397 *err
= MSVCRT_ERANGE
;
399 *MSVCRT__errno() = MSVCRT_ERANGE
;
408 /*********************************************************************
409 * strtod_l (MSVCRT.@)
411 double CDECL
MSVCRT_strtod_l(const char *str
, char **end
, MSVCRT__locale_t locale
)
413 return strtod_helper(str
, end
, locale
, NULL
);
416 /*********************************************************************
419 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
421 return MSVCRT_strtod_l( str
, end
, NULL
);
424 /*********************************************************************
427 double CDECL
MSVCRT_atof( const char *str
)
429 return MSVCRT_strtod_l(str
, NULL
, NULL
);
432 /*********************************************************************
435 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
437 return MSVCRT_strtod_l(str
, NULL
, locale
);
440 /*********************************************************************
441 * _atoflt_l (MSVCRT.@)
443 int CDECL
MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT
*value
, char *str
, MSVCRT__locale_t locale
)
448 d
= strtod_helper(str
, NULL
, locale
, &err
);
451 return MSVCRT__OVERFLOW
;
452 if((d
!=0 || err
) && value
->f
>-MSVCRT_FLT_MIN
&& value
->f
<MSVCRT_FLT_MIN
)
453 return MSVCRT__UNDERFLOW
;
457 /*********************************************************************
458 * _atodbl_l (MSVCRT.@)
460 int CDECL
MSVCRT__atodbl_l(MSVCRT__CRT_DOUBLE
*value
, char *str
, MSVCRT__locale_t locale
)
464 value
->x
= strtod_helper(str
, NULL
, locale
, &err
);
466 return MSVCRT__OVERFLOW
;
467 if((value
->x
!=0 || err
) && value
->x
>-MSVCRT_DBL_MIN
&& value
->x
<MSVCRT_DBL_MIN
)
468 return MSVCRT__UNDERFLOW
;
472 /*********************************************************************
475 int CDECL
MSVCRT__atodbl(MSVCRT__CRT_DOUBLE
*value
, char *str
)
477 return MSVCRT__atodbl_l(value
, str
, NULL
);
480 /*********************************************************************
481 * _strcoll_l (MSVCRT.@)
483 int CDECL
MSVCRT_strcoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
485 MSVCRT_pthreadlocinfo locinfo
;
488 locinfo
= get_locinfo();
490 locinfo
= locale
->locinfo
;
492 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, -1, str2
, -1)-CSTR_EQUAL
;
495 /*********************************************************************
498 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
500 return MSVCRT_strcoll_l(str1
, str2
, NULL
);
503 /*********************************************************************
504 * _stricoll_l (MSVCRT.@)
506 int CDECL
MSVCRT__stricoll_l( const char* str1
, const char* str2
, MSVCRT__locale_t locale
)
508 MSVCRT_pthreadlocinfo locinfo
;
511 locinfo
= get_locinfo();
513 locinfo
= locale
->locinfo
;
515 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
516 str1
, -1, str2
, -1)-CSTR_EQUAL
;
519 /*********************************************************************
520 * _stricoll (MSVCRT.@)
522 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
524 return MSVCRT__stricoll_l(str1
, str2
, NULL
);
527 /*********************************************************************
528 * _strncoll_l (MSVCRT.@)
530 int CDECL
MSVCRT__strncoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
532 MSVCRT_pthreadlocinfo locinfo
;
535 locinfo
= get_locinfo();
537 locinfo
= locale
->locinfo
;
539 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], 0, str1
, count
, str2
, count
)-CSTR_EQUAL
;
542 /*********************************************************************
543 * _strncoll (MSVCRT.@)
545 int CDECL
MSVCRT__strncoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
547 return MSVCRT__strncoll_l(str1
, str2
, count
, NULL
);
550 /*********************************************************************
551 * _strnicoll_l (MSVCRT.@)
553 int CDECL
MSVCRT__strnicoll_l( const char* str1
, const char* str2
, MSVCRT_size_t count
, MSVCRT__locale_t locale
)
555 MSVCRT_pthreadlocinfo locinfo
;
558 locinfo
= get_locinfo();
560 locinfo
= locale
->locinfo
;
562 return CompareStringA(locinfo
->lc_handle
[MSVCRT_LC_COLLATE
], NORM_IGNORECASE
,
563 str1
, count
, str2
, count
)-CSTR_EQUAL
;
566 /*********************************************************************
567 * _strnicoll (MSVCRT.@)
569 int CDECL
MSVCRT__strnicoll( const char* str1
, const char* str2
, MSVCRT_size_t count
)
571 return MSVCRT__strnicoll_l(str1
, str2
, count
, NULL
);
574 /*********************************************************************
577 char* __cdecl
MSVCRT_strncpy(char *dst
, const char *src
, MSVCRT_size_t len
)
582 if((dst
[i
] = src
[i
]) == '\0') break;
584 while (i
< len
) dst
[i
++] = 0;
589 /*********************************************************************
590 * strcpy_s (MSVCRT.@)
592 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
595 if(!elem
) return MSVCRT_EINVAL
;
596 if(!dst
) return MSVCRT_EINVAL
;
600 return MSVCRT_EINVAL
;
603 for(i
= 0; i
< elem
; i
++)
605 if((dst
[i
] = src
[i
]) == '\0') return 0;
608 return MSVCRT_ERANGE
;
611 /*********************************************************************
612 * strcat_s (MSVCRT.@)
614 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
617 if(!dst
) return MSVCRT_EINVAL
;
618 if(elem
== 0) return MSVCRT_EINVAL
;
622 return MSVCRT_EINVAL
;
625 for(i
= 0; i
< elem
; i
++)
629 for(j
= 0; (j
+ i
) < elem
; j
++)
631 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
635 /* Set the first element to 0, not the first element after the skipped part */
637 return MSVCRT_ERANGE
;
640 /*********************************************************************
641 * strncat_s (MSVCRT.@)
643 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
647 if (!MSVCRT_CHECK_PMT(dst
!= 0)) return MSVCRT_EINVAL
;
648 if (!MSVCRT_CHECK_PMT(elem
!= 0)) return MSVCRT_EINVAL
;
649 if (!MSVCRT_CHECK_PMT(src
!= 0))
652 return MSVCRT_EINVAL
;
655 for(i
= 0; i
< elem
; i
++)
659 for(j
= 0; (j
+ i
) < elem
; j
++)
661 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
664 return MSVCRT_STRUNCATE
;
666 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
674 /* Set the first element to 0, not the first element after the skipped part */
676 return MSVCRT_ERANGE
;
679 /*********************************************************************
682 char* __cdecl
MSVCRT_strncat(char *dst
, const char *src
, MSVCRT_size_t len
)
684 return strncat(dst
, src
, len
);
687 /*********************************************************************
690 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
692 /* FIXME: handle Windows locale */
693 return strxfrm( dest
, src
, len
);
696 /********************************************************************
697 * _atoldbl (MSVCRT.@)
699 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
701 /* FIXME needs error checking for huge/small values */
704 TRACE("str %s value %p\n",str
,value
);
706 memcpy(value
, &ld
, 10);
708 FIXME("stub, str %s value %p\n",str
,value
);
713 /********************************************************************
714 * __STRINGTOLD (MSVCRT.@)
716 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
720 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
722 memcpy(value
, &ld
, 10);
724 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
729 /******************************************************************
732 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
734 /* wrapper to forward libc error code to msvcrt's error codes */
738 ret
= strtol(nptr
, end
, base
);
741 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
742 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
744 /* cope with the fact that we may use 64bit long integers on libc
745 * while msvcrt always uses 32bit long integers
747 if (ret
> MSVCRT_LONG_MAX
)
749 ret
= MSVCRT_LONG_MAX
;
750 *MSVCRT__errno() = MSVCRT_ERANGE
;
752 else if (ret
< -MSVCRT_LONG_MAX
- 1)
754 ret
= -MSVCRT_LONG_MAX
- 1;
755 *MSVCRT__errno() = MSVCRT_ERANGE
;
763 /******************************************************************
766 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
768 /* wrapper to forward libc error code to msvcrt's error codes */
772 ret
= strtoul(nptr
, end
, base
);
775 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
776 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
778 /* cope with the fact that we may use 64bit long integers on libc
779 * while msvcrt always uses 32bit long integers
781 if (ret
> MSVCRT_ULONG_MAX
)
783 ret
= MSVCRT_ULONG_MAX
;
784 *MSVCRT__errno() = MSVCRT_ERANGE
;
792 /*********************************************************************
795 MSVCRT_size_t __cdecl
MSVCRT_strlen(const char *str
)
800 /******************************************************************
803 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
807 for(i
=0; i
<maxlen
; i
++)
813 /*********************************************************************
814 * _strtoi64_l (MSVCRT.@)
816 * FIXME: locale parameter is ignored
818 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
820 BOOL negative
= FALSE
;
823 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
825 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
826 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
827 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
829 while(isspace(*nptr
)) nptr
++;
834 } else if(*nptr
== '+')
837 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
850 char cur
= tolower(*nptr
);
858 if(cur
<'a' || cur
>='a'+base
-10)
868 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
869 ret
= MSVCRT_I64_MAX
;
870 *MSVCRT__errno() = MSVCRT_ERANGE
;
871 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
872 ret
= MSVCRT_I64_MIN
;
873 *MSVCRT__errno() = MSVCRT_ERANGE
;
879 *endptr
= (char*)nptr
;
884 /*********************************************************************
885 * _strtoi64 (MSVCRT.@)
887 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
889 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
892 /*********************************************************************
895 int __cdecl
MSVCRT__atoi_l(const char *str
, MSVCRT__locale_t locale
)
897 __int64 ret
= MSVCRT_strtoi64_l(str
, NULL
, 10, locale
);
901 *MSVCRT__errno() = MSVCRT_ERANGE
;
902 } else if(ret
< INT_MIN
) {
904 *MSVCRT__errno() = MSVCRT_ERANGE
;
909 /*********************************************************************
912 int __cdecl
MSVCRT_atoi(const char *str
)
920 while(isspace(*str
)) str
++;
924 }else if(*str
== '-') {
929 while(*str
>='0' && *str
<='9') {
930 ret
= ret
*10+*str
-'0';
934 return minus
? -ret
: ret
;
937 /*********************************************************************
938 * _strtoui64_l (MSVCRT.@)
940 * FIXME: locale parameter is ignored
942 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
944 BOOL negative
= FALSE
;
945 unsigned __int64 ret
= 0;
947 TRACE("(%s %p %d %p)\n", debugstr_a(nptr
), endptr
, base
, locale
);
949 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
)) return 0;
950 if (!MSVCRT_CHECK_PMT(base
== 0 || base
>= 2)) return 0;
951 if (!MSVCRT_CHECK_PMT(base
<= 36)) return 0;
953 while(isspace(*nptr
)) nptr
++;
958 } else if(*nptr
== '+')
961 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
974 char cur
= tolower(*nptr
);
982 if(cur
<'a' || cur
>='a'+base
-10)
989 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
990 ret
= MSVCRT_UI64_MAX
;
991 *MSVCRT__errno() = MSVCRT_ERANGE
;
997 *endptr
= (char*)nptr
;
999 return negative
? -ret
: ret
;
1002 /*********************************************************************
1003 * _strtoui64 (MSVCRT.@)
1005 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
1007 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
1010 /*********************************************************************
1011 * _ltoa_s (MSVCRT.@)
1013 int CDECL
_ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
1018 char buffer
[33], *pos
;
1021 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1022 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1023 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1026 return MSVCRT_EINVAL
;
1029 if (value
< 0 && radix
== 10)
1045 digit
= val
% radix
;
1049 *--pos
= '0' + digit
;
1051 *--pos
= 'a' + digit
- 10;
1058 len
= buffer
+ 33 - pos
;
1064 /* Copy the temporary buffer backwards up to the available number of
1065 * characters. Don't copy the negative sign if present. */
1073 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1077 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1078 return MSVCRT_ERANGE
;
1081 memcpy(str
, pos
, len
);
1085 /*********************************************************************
1086 * _ltow_s (MSVCRT.@)
1088 int CDECL
_ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1093 MSVCRT_wchar_t buffer
[33], *pos
;
1096 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1097 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1098 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1101 return MSVCRT_EINVAL
;
1104 if (value
< 0 && radix
== 10)
1120 digit
= val
% radix
;
1124 *--pos
= '0' + digit
;
1126 *--pos
= 'a' + digit
- 10;
1133 len
= buffer
+ 33 - pos
;
1137 MSVCRT_wchar_t
*p
= str
;
1139 /* Copy the temporary buffer backwards up to the available number of
1140 * characters. Don't copy the negative sign if present. */
1148 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1152 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1153 return MSVCRT_ERANGE
;
1156 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1160 /*********************************************************************
1161 * _itoa_s (MSVCRT.@)
1163 int CDECL
_itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
1165 return _ltoa_s(value
, str
, size
, radix
);
1168 /*********************************************************************
1169 * _itow_s (MSVCRT.@)
1171 int CDECL
_itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1173 return _ltow_s(value
, str
, size
, radix
);
1176 /*********************************************************************
1177 * _ui64toa_s (MSVCRT.@)
1179 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
1180 MSVCRT_size_t size
, int radix
)
1182 char buffer
[65], *pos
;
1185 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1186 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1187 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1190 return MSVCRT_EINVAL
;
1197 digit
= value
%radix
;
1203 *--pos
= 'a'+digit
-10;
1206 if(buffer
-pos
+65 > size
) {
1207 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1208 return MSVCRT_EINVAL
;
1211 memcpy(str
, pos
, buffer
-pos
+65);
1215 /*********************************************************************
1216 * _ui64tow_s (MSVCRT.@)
1218 int CDECL
MSVCRT__ui64tow_s( unsigned __int64 value
, MSVCRT_wchar_t
*str
,
1219 MSVCRT_size_t size
, int radix
)
1221 MSVCRT_wchar_t buffer
[65], *pos
;
1224 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1225 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1226 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1229 return MSVCRT_EINVAL
;
1236 digit
= value
% radix
;
1237 value
= value
/ radix
;
1239 *--pos
= '0' + digit
;
1241 *--pos
= 'a' + digit
- 10;
1242 } while (value
!= 0);
1244 if(buffer
-pos
+65 > size
) {
1245 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL
);
1246 return MSVCRT_EINVAL
;
1249 memcpy(str
, pos
, (buffer
-pos
+65)*sizeof(MSVCRT_wchar_t
));
1253 /*********************************************************************
1254 * _ultoa_s (MSVCRT.@)
1256 int CDECL
_ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
1259 char buffer
[33], *pos
;
1262 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1267 *MSVCRT__errno() = MSVCRT_EINVAL
;
1268 return MSVCRT_EINVAL
;
1276 digit
= value
% radix
;
1280 *--pos
= '0' + digit
;
1282 *--pos
= 'a' + digit
- 10;
1286 len
= buffer
+ 33 - pos
;
1292 /* Copy the temporary buffer backwards up to the available number of
1295 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1299 *MSVCRT__errno() = MSVCRT_ERANGE
;
1300 return MSVCRT_ERANGE
;
1303 memcpy(str
, pos
, len
);
1307 /*********************************************************************
1308 * _ultow_s (MSVCRT.@)
1310 int CDECL
_ultow_s(MSVCRT_ulong value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1313 WCHAR buffer
[33], *pos
;
1316 if (!str
|| !size
|| radix
< 2 || radix
> 36)
1321 *MSVCRT__errno() = MSVCRT_EINVAL
;
1322 return MSVCRT_EINVAL
;
1330 digit
= value
% radix
;
1334 *--pos
= '0' + digit
;
1336 *--pos
= 'a' + digit
- 10;
1340 len
= buffer
+ 33 - pos
;
1346 /* Copy the temporary buffer backwards up to the available number of
1349 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
1353 *MSVCRT__errno() = MSVCRT_ERANGE
;
1354 return MSVCRT_ERANGE
;
1357 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1361 /*********************************************************************
1362 * _i64toa_s (MSVCRT.@)
1364 int CDECL
_i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
1366 unsigned __int64 val
;
1369 char buffer
[65], *pos
;
1372 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1373 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1374 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1377 return MSVCRT_EINVAL
;
1380 if (value
< 0 && radix
== 10)
1396 digit
= val
% radix
;
1400 *--pos
= '0' + digit
;
1402 *--pos
= 'a' + digit
- 10;
1409 len
= buffer
+ 65 - pos
;
1415 /* Copy the temporary buffer backwards up to the available number of
1416 * characters. Don't copy the negative sign if present. */
1424 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1428 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1429 return MSVCRT_ERANGE
;
1432 memcpy(str
, pos
, len
);
1436 /*********************************************************************
1437 * _i64tow_s (MSVCRT.@)
1439 int CDECL
_i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1441 unsigned __int64 val
;
1444 MSVCRT_wchar_t buffer
[65], *pos
;
1447 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) return MSVCRT_EINVAL
;
1448 if (!MSVCRT_CHECK_PMT(size
> 0)) return MSVCRT_EINVAL
;
1449 if (!MSVCRT_CHECK_PMT(radix
>= 2 && radix
<= 36))
1452 return MSVCRT_EINVAL
;
1455 if (value
< 0 && radix
== 10)
1471 digit
= val
% radix
;
1475 *--pos
= '0' + digit
;
1477 *--pos
= 'a' + digit
- 10;
1484 len
= buffer
+ 65 - pos
;
1488 MSVCRT_wchar_t
*p
= str
;
1490 /* Copy the temporary buffer backwards up to the available number of
1491 * characters. Don't copy the negative sign if present. */
1499 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1503 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE
);
1504 return MSVCRT_ERANGE
;
1507 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1511 #define I10_OUTPUT_MAX_PREC 21
1512 /* Internal structure used by $I10_OUTPUT */
1513 struct _I10_OUTPUT_DATA
{
1517 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1520 /*********************************************************************
1521 * $I10_OUTPUT (MSVCRT.@)
1522 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1523 * prec - precision of part, we're interested in
1524 * flag - 0 for first prec digits, 1 for fractional part
1525 * data - data to be populated
1528 * 0 if given double is NaN or INF
1532 * Native sets last byte of data->str to '0' or '9', I don't know what
1533 * it means. Current implementation sets it always to '0'.
1535 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1537 static const char inf_str
[] = "1#INF";
1538 static const char nan_str
[] = "1#QNAN";
1540 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1541 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1542 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1546 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1549 memcpy(&ld
, &ld80
, 10);
1551 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1562 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1570 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1576 int exp
= 1+floor(log10(d
));
1584 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1585 prec
= I10_OUTPUT_MAX_PREC
-1;
1591 sprintf(format
, "%%.%dle", prec
);
1592 sprintf(buf
, format
, d
);
1595 data
->pos
= atoi(buf
+prec
+3);
1599 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1602 memcpy(data
->str
, buf
+1, data
->len
);
1603 data
->str
[data
->len
] = '\0';
1605 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1606 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1610 #undef I10_OUTPUT_MAX_PREC
1612 /*********************************************************************
1615 int __cdecl
MSVCRT_memcmp(const void *ptr1
, const void *ptr2
, MSVCRT_size_t n
)
1617 return memcmp(ptr1
, ptr2
, n
);
1620 /*********************************************************************
1623 void * __cdecl
MSVCRT_memcpy(void *dst
, const void *src
, MSVCRT_size_t n
)
1625 return memmove(dst
, src
, n
);
1628 /*********************************************************************
1629 * memmove (MSVCRT.@)
1631 void * __cdecl
MSVCRT_memmove(void *dst
, const void *src
, MSVCRT_size_t n
)
1633 return memmove(dst
, src
, n
);
1636 /*********************************************************************
1639 void* __cdecl
MSVCRT_memset(void *dst
, int c
, MSVCRT_size_t n
)
1641 return memset(dst
, c
, n
);
1644 /*********************************************************************
1647 char* __cdecl
MSVCRT_strchr(const char *str
, int c
)
1649 return strchr(str
, c
);
1652 /*********************************************************************
1653 * strrchr (MSVCRT.@)
1655 char* __cdecl
MSVCRT_strrchr(const char *str
, int c
)
1657 return strrchr(str
, c
);
1660 /*********************************************************************
1663 void* __cdecl
MSVCRT_memchr(const void *ptr
, int c
, MSVCRT_size_t n
)
1665 return memchr(ptr
, c
, n
);
1668 /*********************************************************************
1671 int __cdecl
MSVCRT_strcmp(const char *str1
, const char *str2
)
1673 return strcmp(str1
, str2
);
1676 /*********************************************************************
1677 * strncmp (MSVCRT.@)
1679 int __cdecl
MSVCRT_strncmp(const char *str1
, const char *str2
, MSVCRT_size_t len
)
1681 return strncmp(str1
, str2
, len
);
1684 /*********************************************************************
1685 * _strnicmp_l (MSVCRT.@)
1687 int __cdecl
MSVCRT__strnicmp_l(const char *s1
, const char *s2
,
1688 MSVCRT_size_t count
, MSVCRT__locale_t locale
)
1690 MSVCRT_pthreadlocinfo locinfo
;
1693 if(s1
==NULL
|| s2
==NULL
)
1694 return MSVCRT__NLSCMPERROR
;
1700 locinfo
= get_locinfo();
1702 locinfo
= locale
->locinfo
;
1704 if(!locinfo
->lc_handle
[MSVCRT_LC_CTYPE
])
1705 return strncasecmp(s1
, s2
, count
);
1708 c1
= MSVCRT__tolower_l(*s1
++, locale
);
1709 c2
= MSVCRT__tolower_l(*s2
++, locale
);
1710 }while(--count
&& c1
&& c1
==c2
);
1715 /*********************************************************************
1716 * _stricmp_l (MSVCRT.@)
1718 int __cdecl
MSVCRT__stricmp_l(const char *s1
, const char *s2
, MSVCRT__locale_t locale
)
1720 return MSVCRT__strnicmp_l(s1
, s2
, -1, locale
);
1723 /*********************************************************************
1724 * _strnicmp (MSVCRT.@)
1726 int __cdecl
MSVCRT__strnicmp(const char *s1
, const char *s2
, MSVCRT_size_t count
)
1728 return MSVCRT__strnicmp_l(s1
, s2
, count
, NULL
);
1731 /*********************************************************************
1732 * _stricmp (MSVCRT.@)
1734 int __cdecl
MSVCRT__stricmp(const char *s1
, const char *s2
)
1736 return MSVCRT__strnicmp_l(s1
, s2
, -1, NULL
);
1739 /*********************************************************************
1742 char* __cdecl
MSVCRT_strstr(const char *haystack
, const char *needle
)
1744 return strstr(haystack
, needle
);