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 /*********************************************************************
398 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
400 /* FIXME: handle Windows locale */
401 return strxfrm( dest
, src
, len
);
404 /*********************************************************************
405 * _stricoll (MSVCRT.@)
407 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
409 /* FIXME: handle collates */
410 TRACE("str1 %s str2 %s\n", debugstr_a(str1
), debugstr_a(str2
));
411 return lstrcmpiA( str1
, str2
);
414 /********************************************************************
415 * _atoldbl (MSVCRT.@)
417 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
419 /* FIXME needs error checking for huge/small values */
421 TRACE("str %s value %p\n",str
,value
);
422 value
->x
= strtold(str
,0);
424 FIXME("stub, str %s value %p\n",str
,value
);
429 /********************************************************************
430 * __STRINGTOLD (MSVCRT.@)
432 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
435 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
436 value
->x
= strtold(str
,endptr
);
438 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
443 /******************************************************************
446 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
448 /* wrapper to forward libc error code to msvcrt's error codes */
452 ret
= strtol(nptr
, end
, base
);
455 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
456 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
458 /* cope with the fact that we may use 64bit long integers on libc
459 * while msvcrt always uses 32bit long integers
461 if (ret
> MSVCRT_LONG_MAX
)
463 ret
= MSVCRT_LONG_MAX
;
464 *MSVCRT__errno() = MSVCRT_ERANGE
;
466 else if (ret
< -MSVCRT_LONG_MAX
- 1)
468 ret
= -MSVCRT_LONG_MAX
- 1;
469 *MSVCRT__errno() = MSVCRT_ERANGE
;
477 /******************************************************************
480 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
482 /* wrapper to forward libc error code to msvcrt's error codes */
486 ret
= strtoul(nptr
, end
, base
);
489 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
490 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
492 /* cope with the fact that we may use 64bit long integers on libc
493 * while msvcrt always uses 32bit long integers
495 if (ret
> MSVCRT_ULONG_MAX
)
497 ret
= MSVCRT_ULONG_MAX
;
498 *MSVCRT__errno() = MSVCRT_ERANGE
;
506 /******************************************************************
509 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
513 for(i
=0; i
<maxlen
; i
++)
519 /*********************************************************************
520 * _strtoi64_l (MSVCRT.@)
522 * FIXME: locale parameter is ignored
524 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
526 BOOL negative
= FALSE
;
529 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
531 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
) || !MSVCRT_CHECK_PMT(base
== 0 || base
>= 2) ||
532 !MSVCRT_CHECK_PMT(base
<= 36)) {
536 while(isspace(*nptr
)) nptr
++;
541 } else if(*nptr
== '+')
544 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
557 char cur
= tolower(*nptr
);
565 if(cur
<'a' || cur
>='a'+base
-10)
575 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
576 ret
= MSVCRT_I64_MAX
;
577 *MSVCRT__errno() = MSVCRT_ERANGE
;
578 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
579 ret
= MSVCRT_I64_MIN
;
580 *MSVCRT__errno() = MSVCRT_ERANGE
;
586 *endptr
= (char*)nptr
;
591 /*********************************************************************
592 * _strtoi64 (MSVCRT.@)
594 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
596 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
599 /*********************************************************************
600 * _strtoui64_l (MSVCRT.@)
602 * FIXME: locale parameter is ignored
604 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
606 BOOL negative
= FALSE
;
607 unsigned __int64 ret
= 0;
609 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
611 if (!MSVCRT_CHECK_PMT(nptr
!= NULL
) || !MSVCRT_CHECK_PMT(base
== 0 || base
>= 2) ||
612 !MSVCRT_CHECK_PMT(base
<= 36)) {
616 while(isspace(*nptr
)) nptr
++;
621 } else if(*nptr
== '+')
624 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
637 char cur
= tolower(*nptr
);
645 if(cur
<'a' || cur
>='a'+base
-10)
652 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
653 ret
= MSVCRT_UI64_MAX
;
654 *MSVCRT__errno() = MSVCRT_ERANGE
;
660 *endptr
= (char*)nptr
;
662 return negative
? -ret
: ret
;
665 /*********************************************************************
666 * _strtoui64 (MSVCRT.@)
668 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
670 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
673 /*********************************************************************
676 int CDECL
_itoa_s(int value
, char *str
, MSVCRT_size_t size
, int radix
)
678 unsigned int val
, digit
;
680 char buffer
[33], *pos
;
683 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
684 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
689 *MSVCRT__errno() = MSVCRT_EINVAL
;
690 return MSVCRT_EINVAL
;
693 if (value
< 0 && radix
== 10)
713 *--pos
= '0' + digit
;
715 *--pos
= 'a' + digit
- 10;
722 len
= buffer
+ 33 - pos
;
728 /* Copy the temporary buffer backwards up to the available number of
729 * characters. Don't copy the negative sign if present. */
737 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
741 MSVCRT_INVALID_PMT("str[size] is too small");
742 *MSVCRT__errno() = MSVCRT_ERANGE
;
743 return MSVCRT_ERANGE
;
746 memcpy(str
, pos
, len
);
750 /*********************************************************************
753 int CDECL
_itow_s(int value
, MSVCRT_wchar_t
*str
, MSVCRT_size_t size
, int radix
)
755 unsigned int val
, digit
;
757 MSVCRT_wchar_t buffer
[33], *pos
;
760 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
761 !MSVCRT_CHECK_PMT(radix
>= 2) || !MSVCRT_CHECK_PMT(radix
<= 36))
766 *MSVCRT__errno() = MSVCRT_EINVAL
;
767 return MSVCRT_EINVAL
;
770 if (value
< 0 && radix
== 10)
790 *--pos
= '0' + digit
;
792 *--pos
= 'a' + digit
- 10;
799 len
= buffer
+ 33 - pos
;
803 MSVCRT_wchar_t
*p
= str
;
805 /* Copy the temporary buffer backwards up to the available number of
806 * characters. Don't copy the negative sign if present. */
814 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
817 MSVCRT_INVALID_PMT("str[size] is too small");
819 *MSVCRT__errno() = MSVCRT_ERANGE
;
820 return MSVCRT_ERANGE
;
823 memcpy(str
, pos
, len
* sizeof(MSVCRT_wchar_t
));
827 /*********************************************************************
828 * _ui64toa_s (MSVCRT.@)
830 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
831 MSVCRT_size_t size
, int radix
)
833 char buffer
[65], *pos
;
836 if (!MSVCRT_CHECK_PMT(str
!= NULL
) || !MSVCRT_CHECK_PMT(size
> 0) ||
837 !MSVCRT_CHECK_PMT(radix
>=2) || !MSVCRT_CHECK_PMT(radix
<=36)) {
838 *MSVCRT__errno() = MSVCRT_EINVAL
;
839 return MSVCRT_EINVAL
;
852 *--pos
= 'a'+digit
-10;
855 if(buffer
-pos
+65 > size
) {
856 MSVCRT_INVALID_PMT("str[size] is too small");
857 *MSVCRT__errno() = MSVCRT_EINVAL
;
858 return MSVCRT_EINVAL
;
861 memcpy(str
, pos
, buffer
-pos
+65);
865 /*********************************************************************
866 * _ultoa_s (MSVCRT.@)
868 int CDECL
_ultoa_s(MSVCRT_ulong value
, char *str
, MSVCRT_size_t size
, int radix
)
871 char buffer
[33], *pos
;
874 if (!str
|| !size
|| radix
< 2 || radix
> 36)
879 *MSVCRT__errno() = MSVCRT_EINVAL
;
880 return MSVCRT_EINVAL
;
888 digit
= value
% radix
;
892 *--pos
= '0' + digit
;
894 *--pos
= 'a' + digit
- 10;
898 len
= buffer
+ 33 - pos
;
904 /* Copy the temporary buffer backwards up to the available number of
907 for (pos
= buffer
+ 31, i
= 0; i
< size
; i
++)
911 *MSVCRT__errno() = MSVCRT_ERANGE
;
912 return MSVCRT_ERANGE
;
915 memcpy(str
, pos
, len
);
919 #define I10_OUTPUT_MAX_PREC 21
920 /* Internal structure used by $I10_OUTPUT */
921 struct _I10_OUTPUT_DATA
{
925 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
928 /*********************************************************************
929 * $I10_OUTPUT (MSVCRT.@)
930 * ld - long double to be printed to data
931 * prec - precision of part, we're interested in
932 * flag - 0 for first prec digits, 1 for fractional part
933 * data - data to be populated
936 * 0 if given double is NaN or INF
940 * Native sets last byte of data->str to '0' or '9', I don't know what
941 * it means. Current implementation sets it always to '0'.
943 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
945 static const char inf_str
[] = "1#INF";
946 static const char nan_str
[] = "1#QNAN";
950 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
953 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
964 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
972 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
978 int exp
= 1+floor(log10(d
));
986 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
987 prec
= I10_OUTPUT_MAX_PREC
-1;
993 sprintf(format
, "%%.%dle", prec
);
994 sprintf(buf
, format
, d
);
997 data
->pos
= atoi(buf
+prec
+3);
1001 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
1004 memcpy(data
->str
, buf
+1, data
->len
);
1005 data
->str
[data
->len
] = '\0';
1007 if(buf
[1]!='0' && prec
-data
->len
+1>0)
1008 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
1012 #undef I10_OUTPUT_MAX_PREC