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
;
172 BOOL found_digit
= FALSE
;
175 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
176 *MSVCRT__errno() = MSVCRT_EINVAL
;
181 locale
= get_locale();
183 /* FIXME: use *_l functions */
196 hlp
= d
*10+*(p
++)-'0';
197 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
) {
208 if(*p
== *locale
->locinfo
->lconv
->decimal_point
)
213 hlp
= d
*10+*(p
++)-'0';
214 if(d
>MSVCRT_UI64_MAX
/10 || hlp
<d
)
229 if(*p
=='e' || *p
=='E' || *p
=='d' || *p
=='D') {
241 if(e
>INT_MAX
/10 || (e
=e
*10+*p
-'0')<0)
247 if(exp
<0 && e
<0 && exp
+e
>=0) exp
= INT_MIN
;
248 else if(exp
>0 && e
>0 && exp
+e
<0) exp
= INT_MAX
;
251 if(*p
=='-' || *p
=='+')
257 fpcontrol
= _control87(0, 0);
258 _control87(MSVCRT__EM_DENORMAL
|MSVCRT__EM_INVALID
|MSVCRT__EM_ZERODIVIDE
259 |MSVCRT__EM_OVERFLOW
|MSVCRT__EM_UNDERFLOW
|MSVCRT__EM_INEXACT
, 0xffffffff);
262 ret
= (double)sign
*d
*pow(10, exp
);
264 ret
= (double)sign
*d
/pow(10, -exp
);
266 _control87(fpcontrol
, 0xffffffff);
268 if((d
&& ret
==0.0) || isinf(ret
))
269 *MSVCRT__errno() = MSVCRT_ERANGE
;
277 /*********************************************************************
280 double CDECL
MSVCRT_strtod( const char *str
, char **end
)
282 return MSVCRT_strtod_l( str
, end
, NULL
);
285 /*********************************************************************
288 double CDECL
MSVCRT_atof( const char *str
)
290 return MSVCRT_strtod_l(str
, NULL
, NULL
);
293 /*********************************************************************
296 double CDECL
MSVCRT__atof_l( const char *str
, MSVCRT__locale_t locale
)
298 return MSVCRT_strtod_l(str
, NULL
, locale
);
301 /*********************************************************************
304 int CDECL
MSVCRT_strcoll( const char* str1
, const char* str2
)
306 /* FIXME: handle Windows locale */
307 return strcoll( str1
, str2
);
310 /*********************************************************************
311 * strcpy_s (MSVCRT.@)
313 int CDECL
MSVCRT_strcpy_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
316 if(!elem
) return MSVCRT_EINVAL
;
317 if(!dst
) return MSVCRT_EINVAL
;
321 return MSVCRT_EINVAL
;
324 for(i
= 0; i
< elem
; i
++)
326 if((dst
[i
] = src
[i
]) == '\0') return 0;
329 return MSVCRT_ERANGE
;
332 /*********************************************************************
333 * strcat_s (MSVCRT.@)
335 int CDECL
MSVCRT_strcat_s( char* dst
, MSVCRT_size_t elem
, const char* src
)
338 if(!dst
) return MSVCRT_EINVAL
;
339 if(elem
== 0) return MSVCRT_EINVAL
;
343 return MSVCRT_EINVAL
;
346 for(i
= 0; i
< elem
; i
++)
350 for(j
= 0; (j
+ i
) < elem
; j
++)
352 if((dst
[j
+ i
] = src
[j
]) == '\0') return 0;
356 /* Set the first element to 0, not the first element after the skipped part */
358 return MSVCRT_ERANGE
;
361 /*********************************************************************
364 MSVCRT_size_t CDECL
MSVCRT_strxfrm( char *dest
, const char *src
, MSVCRT_size_t len
)
366 /* FIXME: handle Windows locale */
367 return strxfrm( dest
, src
, len
);
370 /*********************************************************************
371 * _stricoll (MSVCRT.@)
373 int CDECL
MSVCRT__stricoll( const char* str1
, const char* str2
)
375 /* FIXME: handle collates */
376 TRACE("str1 %s str2 %s\n", debugstr_a(str1
), debugstr_a(str2
));
377 return lstrcmpiA( str1
, str2
);
380 /********************************************************************
381 * _atoldbl (MSVCRT.@)
383 int CDECL
MSVCRT__atoldbl(MSVCRT__LDOUBLE
*value
, const char *str
)
385 /* FIXME needs error checking for huge/small values */
387 TRACE("str %s value %p\n",str
,value
);
388 value
->x
= strtold(str
,0);
390 FIXME("stub, str %s value %p\n",str
,value
);
395 /********************************************************************
396 * __STRINGTOLD (MSVCRT.@)
398 int CDECL
__STRINGTOLD( MSVCRT__LDOUBLE
*value
, char **endptr
, const char *str
, int flags
)
401 FIXME("%p %p %s %x partial stub\n", value
, endptr
, str
, flags
);
402 value
->x
= strtold(str
,endptr
);
404 FIXME("%p %p %s %x stub\n", value
, endptr
, str
, flags
);
409 /******************************************************************
412 MSVCRT_long CDECL
MSVCRT_strtol(const char* nptr
, char** end
, int base
)
414 /* wrapper to forward libc error code to msvcrt's error codes */
418 ret
= strtol(nptr
, end
, base
);
421 case ERANGE
: *MSVCRT__errno() = MSVCRT_ERANGE
; break;
422 case EINVAL
: *MSVCRT__errno() = MSVCRT_EINVAL
; break;
424 /* cope with the fact that we may use 64bit long integers on libc
425 * while msvcrt always uses 32bit long integers
427 if (ret
> MSVCRT_LONG_MAX
)
429 ret
= MSVCRT_LONG_MAX
;
430 *MSVCRT__errno() = MSVCRT_ERANGE
;
432 else if (ret
< -MSVCRT_LONG_MAX
- 1)
434 ret
= -MSVCRT_LONG_MAX
- 1;
435 *MSVCRT__errno() = MSVCRT_ERANGE
;
443 /******************************************************************
446 MSVCRT_ulong CDECL
MSVCRT_strtoul(const char* nptr
, char** end
, int base
)
448 /* wrapper to forward libc error code to msvcrt's error codes */
452 ret
= strtoul(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_ULONG_MAX
)
463 ret
= MSVCRT_ULONG_MAX
;
464 *MSVCRT__errno() = MSVCRT_ERANGE
;
472 /******************************************************************
475 MSVCRT_size_t CDECL
MSVCRT_strnlen(const char *s
, MSVCRT_size_t maxlen
)
479 for(i
=0; i
<maxlen
; i
++)
485 /*********************************************************************
486 * _strtoi64_l (MSVCRT.@)
488 * FIXME: locale parameter is ignored
490 __int64 CDECL
MSVCRT_strtoi64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
492 BOOL negative
= FALSE
;
495 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
497 if(!nptr
|| base
<0 || base
>36 || base
==1) {
498 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
502 while(isspace(*nptr
)) nptr
++;
507 } else if(*nptr
== '+')
510 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
523 char cur
= tolower(*nptr
);
531 if(cur
<'a' || cur
>='a'+base
-10)
541 if(!negative
&& (ret
>MSVCRT_I64_MAX
/base
|| ret
*base
>MSVCRT_I64_MAX
-v
)) {
542 ret
= MSVCRT_I64_MAX
;
543 *MSVCRT__errno() = MSVCRT_ERANGE
;
544 } else if(negative
&& (ret
<MSVCRT_I64_MIN
/base
|| ret
*base
<MSVCRT_I64_MIN
-v
)) {
545 ret
= MSVCRT_I64_MIN
;
546 *MSVCRT__errno() = MSVCRT_ERANGE
;
552 *endptr
= (char*)nptr
;
557 /*********************************************************************
558 * _strtoi64 (MSVCRT.@)
560 __int64 CDECL
MSVCRT_strtoi64(const char *nptr
, char **endptr
, int base
)
562 return MSVCRT_strtoi64_l(nptr
, endptr
, base
, NULL
);
565 /*********************************************************************
566 * _strtoui64_l (MSVCRT.@)
568 * FIXME: locale parameter is ignored
570 unsigned __int64 CDECL
MSVCRT_strtoui64_l(const char *nptr
, char **endptr
, int base
, MSVCRT__locale_t locale
)
572 BOOL negative
= FALSE
;
573 unsigned __int64 ret
= 0;
575 TRACE("(%s %p %d %p)\n", nptr
, endptr
, base
, locale
);
577 if(!nptr
|| base
<0 || base
>36 || base
==1) {
578 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
582 while(isspace(*nptr
)) nptr
++;
587 } else if(*nptr
== '+')
590 if((base
==0 || base
==16) && *nptr
=='0' && tolower(*(nptr
+1))=='x') {
603 char cur
= tolower(*nptr
);
611 if(cur
<'a' || cur
>='a'+base
-10)
618 if(ret
>MSVCRT_UI64_MAX
/base
|| ret
*base
>MSVCRT_UI64_MAX
-v
) {
619 ret
= MSVCRT_UI64_MAX
;
620 *MSVCRT__errno() = MSVCRT_ERANGE
;
626 *endptr
= (char*)nptr
;
628 return negative
? -ret
: ret
;
631 /*********************************************************************
632 * _strtoui64 (MSVCRT.@)
634 unsigned __int64 CDECL
MSVCRT_strtoui64(const char *nptr
, char **endptr
, int base
)
636 return MSVCRT_strtoui64_l(nptr
, endptr
, base
, NULL
);
639 /*********************************************************************
640 * _ui64toa_s (MSVCRT.@)
642 int CDECL
MSVCRT__ui64toa_s(unsigned __int64 value
, char *str
,
643 MSVCRT_size_t size
, int radix
)
645 char buffer
[65], *pos
;
648 if(!str
|| radix
<2 || radix
>36) {
649 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
650 *MSVCRT__errno() = MSVCRT_EINVAL
;
651 return MSVCRT_EINVAL
;
664 *--pos
= 'a'+digit
-10;
667 if(buffer
-pos
+65 > size
) {
668 MSVCRT__invalid_parameter(NULL
, NULL
, NULL
, 0, 0);
669 *MSVCRT__errno() = MSVCRT_EINVAL
;
670 return MSVCRT_EINVAL
;
673 memcpy(str
, pos
, buffer
-pos
+65);
677 #define I10_OUTPUT_MAX_PREC 21
678 /* Internal structure used by $I10_OUTPUT */
679 struct _I10_OUTPUT_DATA
{
683 char str
[I10_OUTPUT_MAX_PREC
+1]; /* add space for '\0' */
686 /*********************************************************************
687 * $I10_OUTPUT (MSVCRT.@)
688 * ld - long double to be printed to data
689 * prec - precision of part, we're interested in
690 * flag - 0 for first prec digits, 1 for fractional part
691 * data - data to be populated
694 * 0 if given double is NaN or INF
698 * Native sets last byte of data->str to '0' or '9', I don't know what
699 * it means. Current implementation sets it always to '0'.
701 int CDECL
MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld
, int prec
, int flag
, struct _I10_OUTPUT_DATA
*data
)
703 static const char inf_str
[] = "1#INF";
704 static const char nan_str
[] = "1#QNAN";
708 char buf
[I10_OUTPUT_MAX_PREC
+9]; /* 9 = strlen("0.e+0000") + '\0' */
711 TRACE("(%lf %d %x %p)\n", d
, prec
, flag
, data
);
722 memcpy(data
->str
, inf_str
, sizeof(inf_str
));
730 memcpy(data
->str
, nan_str
, sizeof(nan_str
));
736 int exp
= 1+floor(log10(d
));
744 if(prec
+1 > I10_OUTPUT_MAX_PREC
)
745 prec
= I10_OUTPUT_MAX_PREC
-1;
751 sprintf(format
, "%%.%dle", prec
);
752 sprintf(buf
, format
, d
);
755 data
->pos
= atoi(buf
+prec
+3);
759 for(p
= buf
+prec
+1; p
>buf
+1 && *p
=='0'; p
--);
762 memcpy(data
->str
, buf
+1, data
->len
);
763 data
->str
[data
->len
] = '\0';
765 if(buf
[1]!='0' && prec
-data
->len
+1>0)
766 memcpy(data
->str
+data
->len
+1, buf
+data
->len
+1, prec
-data
->len
+1);
770 #undef I10_OUTPUT_MAX_PREC