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"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
38 /*********************************************************************
42 char* CDECL
_strdup(const char* str
)
46 char * ret
= MSVCRT_malloc(strlen(str
)+1);
47 if (ret
) strcpy( ret
, str
);
53 /*********************************************************************
54 * _strlwr_s (MSVCRT.@)
56 int CDECL
_strlwr_s(char *str
, MSVCRT_size_t len
)
62 *MSVCRT__errno() = MSVCRT_EINVAL
;
75 *MSVCRT__errno() = MSVCRT_EINVAL
;
88 /*********************************************************************
91 char* CDECL
MSVCRT__strnset(char* str
, int value
, MSVCRT_size_t len
)
99 /*********************************************************************
102 char* CDECL
_strrev(char* str
)
108 for (p1
= str
, p2
= str
+ strlen(str
) - 1; p2
> p1
; ++p1
, --p2
)
118 /*********************************************************************
121 char* CDECL
_strset(char* str
, int value
)
130 /*********************************************************************
133 char * CDECL
MSVCRT_strtok( char *str
, const char *delim
)
135 thread_data_t
*data
= msvcrt_get_thread_data();
139 if (!(str
= data
->strtok_next
)) return NULL
;
141 while (*str
&& strchr( delim
, *str
)) str
++;
142 if (!*str
) return NULL
;
144 while (*str
&& !strchr( delim
, *str
)) str
++;
145 if (*str
) *str
++ = 0;
146 data
->strtok_next
= str
;
150 /*********************************************************************
151 * strtok_s (MSVCRT.@)
153 char * CDECL
MSVCRT_strtok_s(char *str
, const char *delim
, char **ctx
)
155 if (!MSVCRT_CHECK_PMT(delim
!= NULL
) || !MSVCRT_CHECK_PMT(ctx
!= NULL
) ||
156 !MSVCRT_CHECK_PMT(str
!= NULL
|| *ctx
!= NULL
)) {
157 *MSVCRT__errno() = MSVCRT_EINVAL
;
164 while(*str
&& strchr(delim
, *str
))
170 while(**ctx
&& !strchr(delim
, **ctx
))
178 /*********************************************************************
181 void CDECL
MSVCRT__swab(char* src
, char* dst
, int len
)
185 len
= (unsigned)len
>> 1;
197 /*********************************************************************
198 * strtod_l (MSVCRT.@)
200 double CDECL
MSVCRT_strtod_l( const char *str
, char **end
, MSVCRT__locale_t locale
)
202 unsigned __int64 d
=0, hlp
;
207 BOOL found_digit
= FALSE
;
209 if (!MSVCRT_CHECK_PMT(str
!= NULL
)) {
210 *MSVCRT__errno() = MSVCRT_EINVAL
;
215 locale
= get_locale();
217 /* FIXME: use *_l functions */
230 hlp
= d
*10+*(p
++)-'0';
231 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
) {
242 if(*p
== *locale
->locinfo
->lconv
->decimal_point
)
247 hlp
= d
*10+*(p
++)-'0';
248 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
)
263 if(*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D') {
275 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
281 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
282 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
285 if(*p
=='-' || *p
=='+')
291 fpcontrol
= _control87(0, 0);
292 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
293 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
296 ret
= (double)sign
*d
*pow(10, exp
);
298 ret
= (double)sign
*d
/pow(10, -exp
);
300 _control87(fpcontrol
, 0xffffffff);
302 if((d
&& ret
==0.0) || isinf(ret
))
303 *MSVCRT__errno() = MSVCRT_ERANGE
;
311 /*********************************************************************
314 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
316 return MSVCRT_strtod_l( str
, end
, NULL
);
319 /*********************************************************************
322 double CDECL
MSVCRT_atof( const char *str
)
324 return MSVCRT_strtod_l(str
, NULL
, NULL
);
327 /*********************************************************************
330 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
332 return MSVCRT_strtod_l(str
, NULL
, locale
);
335 /*********************************************************************
338 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
340 /* FIXME: handle Windows locale */
341 return strcoll( str1
, str2
);
344 /*********************************************************************
345 * strcpy_s (MSVCRT.@)
347 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
350 if(!elem
) return MSVCRT_EINVAL
;
351 if(!dst
) return MSVCRT_EINVAL
;
355 return MSVCRT_EINVAL
;
358 for(i
= 0; i
< elem
; i
++)
360 if((dst
[i
] = src
[i
]) == '\0') return 0;
363 return MSVCRT_ERANGE
;
366 /*********************************************************************
367 * strcat_s (MSVCRT.@)
369 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
372 if(!dst
) return MSVCRT_EINVAL
;
373 if(elem
== 0) return MSVCRT_EINVAL
;
377 return MSVCRT_EINVAL
;
380 for(i
= 0; i
< elem
; i
++)
384 for(j
= 0; (j
+ i
) < elem
; j
++)
386 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
390 /* Set the first element to 0, not the first element after the skipped part */
392 return MSVCRT_ERANGE
;
395 /*********************************************************************
396 * strncat_s (MSVCRT.@)
398 int CDECL
MSVCRT_strncat_s( char* dst
, MSVCRT_size_t elem
, const char* src
, MSVCRT_size_t count
)
401 if(!MSVCRT_CHECK_PMT(dst
!= 0) || !MSVCRT_CHECK_PMT(elem
!= 0))
402 return MSVCRT_EINVAL
;
403 if(!MSVCRT_CHECK_PMT(src
!= 0))
406 return MSVCRT_EINVAL
;
409 for(i
= 0; i
< elem
; i
++)
413 for(j
= 0; (j
+ i
) < elem
; j
++)
415 if(count
== MSVCRT__TRUNCATE
&& j
+ i
== elem
- 1)
418 return MSVCRT_STRUNCATE
;
420 if(j
== count
|| (dst
[j
+ i
] = src
[j
]) == '\0')
428 /* Set the first element to 0, not the first element after the skipped part */
430 return MSVCRT_ERANGE
;
433 /*********************************************************************
436 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
438 /* FIXME: handle Windows locale */
439 return strxfrm( dest
, src
, len
);
442 /*********************************************************************
443 * _stricoll (MSVCRT.@)
445 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
447 /* FIXME: handle collates */
448 TRACE("str1 %s str2 %s\n", debugstr_a(str1
), debugstr_a(str2
));
449 return lstrcmpiA( str1
, str2
);
452 /********************************************************************
453 * _atoldbl (MSVCRT.@)
455 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
457 /* FIXME needs error checking for huge/small values */
459 TRACE("str %s value %p\n",str
,value
);
460 value
->x
= strtold(str
,0);
462 FIXME("stub, str %s value %p\n",str
,value
);
467 /********************************************************************
468 * __STRINGTOLD (MSVCRT.@)
470 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
473 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
474 value
->x
= strtold(str
,endptr
);
476 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
481 /******************************************************************
484 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
486 /* wrapper to forward libc error code to msvcrt's error codes */
490 ret
= strtol(nptr
, end
, base
);
493 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
494 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
496 /* cope with the fact that we may use 64bit long integers on libc
497 * while msvcrt always uses 32bit long integers
499 if (ret
> MSVCRT_LONG_MAX
)
501 ret
= MSVCRT_LONG_MAX
;
502 *MSVCRT__errno() = MSVCRT_ERANGE
;
504 else if (ret
< -MSVCRT_LONG_MAX
- 1)
506 ret
= -MSVCRT_LONG_MAX
- 1;
507 *MSVCRT__errno() = MSVCRT_ERANGE
;
515 /******************************************************************
518 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
520 /* wrapper to forward libc error code to msvcrt's error codes */
524 ret
= strtoul(nptr
, end
, base
);
527 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
528 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
530 /* cope with the fact that we may use 64bit long integers on libc
531 * while msvcrt always uses 32bit long integers
533 if (ret
> MSVCRT_ULONG_MAX
)
535 ret
= MSVCRT_ULONG_MAX
;
536 *MSVCRT__errno() = MSVCRT_ERANGE
;
544 /******************************************************************
547 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
551 for(i
=0; i
<maxlen
; i
++)
557 /*********************************************************************
558 * _strtoi64_l (MSVCRT.@)
560 * FIXME: locale parameter is ignored
562 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
564 BOOL negative
= FALSE
;
567 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
569 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
) || !MSVCRT_CHECK_PMT(base
== 0 || base
>= 2) ||
570 !MSVCRT_CHECK_PMT(base
<= 36)) {
574 while(isspace(*nptr
)) nptr
++;
579 } else if(*nptr
== '+')
582 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
595 char cur
= tolower(*nptr
);
603 if(cur
<'a' || cur
>='a'+base
-10)
613 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
614 ret
= MSVCRT_I64_MAX
;
615 *MSVCRT__errno() = MSVCRT_ERANGE
;
616 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
617 ret
= MSVCRT_I64_MIN
;
618 *MSVCRT__errno() = MSVCRT_ERANGE
;
624 *endptr
= (char*)nptr
;
629 /*********************************************************************
630 * _strtoi64 (MSVCRT.@)
632 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
634 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
637 /*********************************************************************
638 * _strtoui64_l (MSVCRT.@)
640 * FIXME: locale parameter is ignored
642 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
644 BOOL negative
= FALSE
;
645 unsigned __int64 ret
= 0;
647 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
649 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
) || !MSVCRT_CHECK_PMT(base
== 0 || base
>= 2) ||
650 !MSVCRT_CHECK_PMT(base
<= 36)) {
654 while(isspace(*nptr
)) nptr
++;
659 } else if(*nptr
== '+')
662 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
675 char cur
= tolower(*nptr
);
683 if(cur
<'a' || cur
>='a'+base
-10)
690 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
691 ret
= MSVCRT_UI64_MAX
;
692 *MSVCRT__errno() = MSVCRT_ERANGE
;
698 *endptr
= (char*)nptr
;
700 return negative
? -ret
: ret
;
703 /*********************************************************************
704 * _strtoui64 (MSVCRT.@)
706 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
708 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
711 /*********************************************************************
714 int CDECL
_ltoa_s(MSVCRT_long value
, char *str
, MSVCRT_size_t size
, int radix
)
719 char buffer
[33], *pos
;
722 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
723 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
728 *MSVCRT__errno() = MSVCRT_EINVAL
;
729 return MSVCRT_EINVAL
;
732 if (value
< 0 && radix
== 10)
752 *--pos
= '0' + digit
;
754 *--pos
= 'a' + digit
- 10;
761 len
= buffer
+ 33 - pos
;
767 /* Copy the temporary buffer backwards up to the available number of
768 * characters. Don't copy the negative sign if present. */
776 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
780 MSVCRT_INVALID_PMT("str[size] is too small");
781 *MSVCRT__errno() = MSVCRT_ERANGE
;
782 return MSVCRT_ERANGE
;
785 memcpy(str
, pos
, len
);
789 /*********************************************************************
792 int CDECL
_ltow_s(MSVCRT_long value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
797 MSVCRT_wchar_t buffer
[33], *pos
;
800 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
801 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
806 *MSVCRT__errno() = MSVCRT_EINVAL
;
807 return MSVCRT_EINVAL
;
810 if (value
< 0 && radix
== 10)
830 *--pos
= '0' + digit
;
832 *--pos
= 'a' + digit
- 10;
839 len
= buffer
+ 33 - pos
;
843 MSVCRT_wchar_t
*p
= str
;
845 /* Copy the temporary buffer backwards up to the available number of
846 * characters. Don't copy the negative sign if present. */
854 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
857 MSVCRT_INVALID_PMT("str[size] is too small");
859 *MSVCRT__errno() = MSVCRT_ERANGE
;
860 return MSVCRT_ERANGE
;
863 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
867 /*********************************************************************
870 int CDECL
_itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
872 return _ltoa_s(value
, str
, size
, radix
);
875 /*********************************************************************
878 int CDECL
_itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
880 return _ltow_s(value
, str
, size
, radix
);
883 /*********************************************************************
884 * _ui64toa_s (MSVCRT.@)
886 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
887 MSVCRT_size_t size
, int radix
)
889 char buffer
[65], *pos
;
892 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
893 !MSVCRT_CHECK_PMT(radix
>=2) || !MSVCRT_CHECK_PMT(radix
<=36)) {
894 *MSVCRT__errno() = MSVCRT_EINVAL
;
895 return MSVCRT_EINVAL
;
908 *--pos
= 'a'+digit
-10;
911 if(buffer
-pos
+65 > size
) {
912 MSVCRT_INVALID_PMT("str[size] is too small");
913 *MSVCRT__errno() = MSVCRT_EINVAL
;
914 return MSVCRT_EINVAL
;
917 memcpy(str
, pos
, buffer
-pos
+65);
921 /*********************************************************************
922 * _ultoa_s (MSVCRT.@)
924 int CDECL
_ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
927 char buffer
[33], *pos
;
930 if (!str
|| !size
|| radix
< 2 || radix
> 36)
935 *MSVCRT__errno() = MSVCRT_EINVAL
;
936 return MSVCRT_EINVAL
;
944 digit
= value
% radix
;
948 *--pos
= '0' + digit
;
950 *--pos
= 'a' + digit
- 10;
954 len
= buffer
+ 33 - pos
;
960 /* Copy the temporary buffer backwards up to the available number of
963 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
967 *MSVCRT__errno() = MSVCRT_ERANGE
;
968 return MSVCRT_ERANGE
;
971 memcpy(str
, pos
, len
);
975 /*********************************************************************
976 * _i64toa_s (MSVCRT.@)
978 int CDECL
_i64toa_s(__int64 value
, char *str
, MSVCRT_size_t size
, int radix
)
980 unsigned __int64 val
;
983 char buffer
[65], *pos
;
986 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
987 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
992 *MSVCRT__errno() = MSVCRT_EINVAL
;
993 return MSVCRT_EINVAL
;
996 if (value
< 0 && radix
== 10)
1012 digit
= val
% radix
;
1016 *--pos
= '0' + digit
;
1018 *--pos
= 'a' + digit
- 10;
1025 len
= buffer
+ 65 - pos
;
1031 /* Copy the temporary buffer backwards up to the available number of
1032 * characters. Don't copy the negative sign if present. */
1040 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1044 MSVCRT_INVALID_PMT("str[size] is too small");
1045 *MSVCRT__errno() = MSVCRT_ERANGE
;
1046 return MSVCRT_ERANGE
;
1049 memcpy(str
, pos
, len
);
1053 /*********************************************************************
1054 * _i64tow_s (MSVCRT.@)
1056 int CDECL
_i64tow_s(__int64 value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
1058 unsigned __int64 val
;
1061 MSVCRT_wchar_t buffer
[65], *pos
;
1064 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
1065 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
1070 *MSVCRT__errno() = MSVCRT_EINVAL
;
1071 return MSVCRT_EINVAL
;
1074 if (value
< 0 && radix
== 10)
1090 digit
= val
% radix
;
1094 *--pos
= '0' + digit
;
1096 *--pos
= 'a' + digit
- 10;
1103 len
= buffer
+ 65 - pos
;
1107 MSVCRT_wchar_t
*p
= str
;
1109 /* Copy the temporary buffer backwards up to the available number of
1110 * characters. Don't copy the negative sign if present. */
1118 for (pos
= buffer
+ 63, i
= 0; i
< size
; i
++)
1121 MSVCRT_INVALID_PMT("str[size] is too small");
1123 *MSVCRT__errno() = MSVCRT_ERANGE
;
1124 return MSVCRT_ERANGE
;
1127 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
1131 #define I10_OUTPUT_MAX_PREC 21
1132 /* Internal structure used by $I10_OUTPUT */
1133 struct _I10_OUTPUT_DATA
{
1137 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
1140 /*********************************************************************
1141 * $I10_OUTPUT (MSVCRT.@)
1142 * ld - long double to be printed to data
1143 * prec - precision of part, we're interested in
1144 * flag - 0 for first prec digits, 1 for fractional part
1145 * data - data to be populated
1148 * 0 if given double is NaN or INF
1152 * Native sets last byte of data->str to '0' or '9', I don't know what
1153 * it means. Current implementation sets it always to '0'.
1155 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
1157 static const char inf_str
[] = "1#INF";
1158 static const char nan_str
[] = "1#QNAN";
1162 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
1165 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
1176 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
1184 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
1190 int exp
= 1+floor(log10(d
));
1198 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
1199 prec
= I10_OUTPUT_MAX_PREC
-1;
1205 sprintf(format
, "%%.%dle", prec
);
1206 sprintf(buf
, format
, d
);
1209 data
->pos
= atoi(buf
+prec
+3);
1213 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1216 memcpy(data
->str
, buf
+1, data
->len
);
1217 data
->str
[data
->len
] = '\0';
1219 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1220 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1224 #undef I10_OUTPUT_MAX_PREC