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 /*********************************************************************
56 char* CDECL
MSVCRT__strnset(char* str
, int value
, MSVCRT_size_t len
)
64 /*********************************************************************
67 char* CDECL
_strrev(char* str
)
73 for (p1
= str
, p2
= str
+ strlen(str
) - 1; p2
> p1
; ++p1
, --p2
)
83 /*********************************************************************
86 char* CDECL
_strset(char* str
, int value
)
95 /*********************************************************************
98 char * CDECL
MSVCRT_strtok( char *str
, const char *delim
)
100 thread_data_t
*data
= msvcrt_get_thread_data();
104 if (!(str
= data
->strtok_next
)) return NULL
;
106 while (*str
&& strchr( delim
, *str
)) str
++;
107 if (!*str
) return NULL
;
109 while (*str
&& !strchr( delim
, *str
)) str
++;
110 if (*str
) *str
++ = 0;
111 data
->strtok_next
= str
;
115 /*********************************************************************
116 * strtok_s (MSVCRT.@)
118 char * CDECL
MSVCRT_strtok_s(char *str
, const char *delim
, char **ctx
)
120 if(!delim
|| !ctx
|| (!str
&& !*ctx
)) {
121 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
122 *MSVCRT__errno() = MSVCRT_EINVAL
;
129 while(*str
&& strchr(delim
, *str
))
135 while(**ctx
&& !strchr(delim
, **ctx
))
143 /*********************************************************************
146 void CDECL
MSVCRT__swab(char* src
, char* dst
, int len
)
150 len
= (unsigned)len
>> 1;
162 /*********************************************************************
163 * strtod_l (MSVCRT.@)
165 double CDECL
MSVCRT_strtod_l( const char *str
, char **end
, MSVCRT__locale_t locale
)
167 unsigned __int64 d
=0, hlp
;
174 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
175 *MSVCRT__errno() = MSVCRT_EINVAL
;
180 locale
= get_locale();
182 /* FIXME: use *_l functions */
194 hlp
= d
*10+*(p
++)-'0';
195 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
) {
206 if(*p
== *locale
->locinfo
->lconv
->decimal_point
)
210 hlp
= d
*10+*(p
++)-'0';
211 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
)
226 if(*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D') {
238 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
244 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
245 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
248 if(*p
=='-' || *p
=='+')
254 fpcontrol
= _control87(0, 0);
255 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
256 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
259 ret
= (double)sign
*d
*pow(10, exp
);
261 ret
= (double)sign
*d
/pow(10, -exp
);
263 _control87(fpcontrol
, 0xffffffff);
265 if((d
&& ret
==0.0) || isinf(ret
))
266 *MSVCRT__errno() = MSVCRT_ERANGE
;
274 /*********************************************************************
277 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
279 return MSVCRT_strtod_l( str
, end
, NULL
);
282 /*********************************************************************
285 double CDECL
MSVCRT_atof( const char *str
)
287 return MSVCRT_strtod_l(str
, NULL
, NULL
);
290 /*********************************************************************
293 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
295 return MSVCRT_strtod_l(str
, NULL
, locale
);
298 /*********************************************************************
301 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
303 /* FIXME: handle Windows locale */
304 return strcoll( str1
, str2
);
307 /*********************************************************************
308 * strcpy_s (MSVCRT.@)
310 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
313 if(!elem
) return MSVCRT_EINVAL
;
314 if(!dst
) return MSVCRT_EINVAL
;
318 return MSVCRT_EINVAL
;
321 for(i
= 0; i
< elem
; i
++)
323 if((dst
[i
] = src
[i
]) == '\0') return 0;
326 return MSVCRT_ERANGE
;
329 /*********************************************************************
330 * strcat_s (MSVCRT.@)
332 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
335 if(!dst
) return MSVCRT_EINVAL
;
336 if(elem
== 0) return MSVCRT_EINVAL
;
340 return MSVCRT_EINVAL
;
343 for(i
= 0; i
< elem
; i
++)
347 for(j
= 0; (j
+ i
) < elem
; j
++)
349 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
353 /* Set the first element to 0, not the first element after the skipped part */
355 return MSVCRT_ERANGE
;
358 /*********************************************************************
361 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
363 /* FIXME: handle Windows locale */
364 return strxfrm( dest
, src
, len
);
367 /*********************************************************************
368 * _stricoll (MSVCRT.@)
370 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
372 /* FIXME: handle collates */
373 TRACE("str1 %s str2 %s\n", debugstr_a(str1
), debugstr_a(str2
));
374 return lstrcmpiA( str1
, str2
);
377 /********************************************************************
378 * _atoldbl (MSVCRT.@)
380 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
382 /* FIXME needs error checking for huge/small values */
384 TRACE("str %s value %p\n",str
,value
);
385 value
->x
= strtold(str
,0);
387 FIXME("stub, str %s value %p\n",str
,value
);
392 /********************************************************************
393 * __STRINGTOLD (MSVCRT.@)
395 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
398 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
399 value
->x
= strtold(str
,endptr
);
401 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
406 /******************************************************************
409 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
411 /* wrapper to forward libc error code to msvcrt's error codes */
415 ret
= strtol(nptr
, end
, base
);
418 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
419 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
421 /* cope with the fact that we may use 64bit long integers on libc
422 * while msvcrt always uses 32bit long integers
424 if (ret
> MSVCRT_LONG_MAX
)
426 ret
= MSVCRT_LONG_MAX
;
427 *MSVCRT__errno() = MSVCRT_ERANGE
;
429 else if (ret
< -MSVCRT_LONG_MAX
- 1)
431 ret
= -MSVCRT_LONG_MAX
- 1;
432 *MSVCRT__errno() = MSVCRT_ERANGE
;
440 /******************************************************************
443 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
445 /* wrapper to forward libc error code to msvcrt's error codes */
449 ret
= strtoul(nptr
, end
, base
);
452 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
453 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
455 /* cope with the fact that we may use 64bit long integers on libc
456 * while msvcrt always uses 32bit long integers
458 if (ret
> MSVCRT_ULONG_MAX
)
460 ret
= MSVCRT_ULONG_MAX
;
461 *MSVCRT__errno() = MSVCRT_ERANGE
;
469 /******************************************************************
472 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
476 for(i
=0; i
<maxlen
; i
++)
482 /*********************************************************************
483 * _strtoi64_l (MSVCRT.@)
485 * FIXME: locale parameter is ignored
487 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
489 BOOL negative
= FALSE
;
492 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
494 if(!nptr
|| base
<0 || base
>36 || base
==1) {
495 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
499 while(isspace(*nptr
)) nptr
++;
504 } else if(*nptr
== '+')
507 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
520 char cur
= tolower(*nptr
);
528 if(cur
<'a' || cur
>='a'+base
-10)
538 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
539 ret
= MSVCRT_I64_MAX
;
540 *MSVCRT__errno() = MSVCRT_ERANGE
;
541 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
542 ret
= MSVCRT_I64_MIN
;
543 *MSVCRT__errno() = MSVCRT_ERANGE
;
549 *endptr
= (char*)nptr
;
554 /*********************************************************************
555 * _strtoi64 (MSVCRT.@)
557 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
559 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
562 /*********************************************************************
563 * _strtoui64_l (MSVCRT.@)
565 * FIXME: locale parameter is ignored
567 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
569 BOOL negative
= FALSE
;
570 unsigned __int64 ret
= 0;
572 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
574 if(!nptr
|| base
<0 || base
>36 || base
==1) {
575 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
579 while(isspace(*nptr
)) nptr
++;
584 } else if(*nptr
== '+')
587 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
600 char cur
= tolower(*nptr
);
608 if(cur
<'a' || cur
>='a'+base
-10)
615 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
616 ret
= MSVCRT_UI64_MAX
;
617 *MSVCRT__errno() = MSVCRT_ERANGE
;
623 *endptr
= (char*)nptr
;
625 return negative
? -ret
: ret
;
628 /*********************************************************************
629 * _strtoui64 (MSVCRT.@)
631 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
633 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
636 /*********************************************************************
637 * _ui64toa_s (MSVCRT.@)
639 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
640 MSVCRT_size_t size
, int radix
)
642 char buffer
[65], *pos
;
645 if(!str
|| radix
<2 || radix
>36) {
646 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
647 *MSVCRT__errno() = MSVCRT_EINVAL
;
648 return MSVCRT_EINVAL
;
661 *--pos
= 'a'+digit
-10;
664 if(buffer
-pos
+65 > size
) {
665 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
666 *MSVCRT__errno() = MSVCRT_EINVAL
;
667 return MSVCRT_EINVAL
;
670 memcpy(str
, pos
, buffer
-pos
+65);
674 #define I10_OUTPUT_MAX_PREC 21
675 /* Internal structure used by $I10_OUTPUT */
676 struct _I10_OUTPUT_DATA
{
680 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
683 /*********************************************************************
684 * $I10_OUTPUT (MSVCRT.@)
685 * ld - long double to be printed to data
686 * prec - precision of part, we're interested in
687 * flag - 0 for first prec digits, 1 for fractional part
688 * data - data to be populated
691 * 0 if given double is NaN or INF
695 * Native sets last byte of data->str to '0' or '9', I don't know what
696 * it means. Current implementation sets it always to '0'.
698 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
700 static const char inf_str
[] = "1#INF";
701 static const char nan_str
[] = "1#QNAN";
705 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
708 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
719 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
727 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
733 int exp
= 1+floor(log10(d
));
741 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
742 prec
= I10_OUTPUT_MAX_PREC
-1;
748 sprintf(format
, "%%.%dle", prec
);
749 sprintf(buf
, format
, d
);
752 data
->pos
= atoi(buf
+prec
+3);
756 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
759 memcpy(data
->str
, buf
+1, data
->len
);
760 data
->str
[data
->len
] = '\0';
762 if(buf
[1]!='0' && prec
-data
->len
+1>0)
763 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
767 #undef I10_OUTPUT_MAX_PREC