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"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 /*********************************************************************
41 char* CDECL _strdup(const char* str)
45 char * ret = MSVCRT_malloc(strlen(str)+1);
46 if (ret) strcpy( ret, str );
52 /*********************************************************************
55 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
63 /*********************************************************************
66 char* CDECL _strrev(char* str)
72 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
82 /*********************************************************************
85 char* CDECL _strset(char* str, int value)
94 /*********************************************************************
97 char * CDECL MSVCRT_strtok( char *str, const char *delim )
99 thread_data_t *data = msvcrt_get_thread_data();
103 if (!(str = data->strtok_next)) return NULL;
105 while (*str && strchr( delim, *str )) str++;
106 if (!*str) return NULL;
108 while (*str && !strchr( delim, *str )) str++;
109 if (*str) *str++ = 0;
110 data->strtok_next = str;
114 /*********************************************************************
115 * strtok_s (MSVCRT.@)
117 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
119 if(!delim || !ctx || (!str && !*ctx)) {
120 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
121 *MSVCRT__errno() = MSVCRT_EINVAL;
128 while(*str && strchr(delim, *str))
134 while(**ctx && !strchr(delim, **ctx))
142 /*********************************************************************
145 void CDECL MSVCRT__swab(char* src, char* dst, int len)
149 len = (unsigned)len >> 1;
161 /*********************************************************************
162 * strtod_l (MSVCRT.@)
164 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
166 unsigned __int64 d=0, hlp;
173 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
174 *MSVCRT__errno() = MSVCRT_EINVAL;
179 locale = get_locale();
181 /* FIXME: use *_l functions */
193 hlp = d*10+*(p++)-'0';
194 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
205 if(*p == *locale->locinfo->lconv->decimal_point)
209 hlp = d*10+*(p++)-'0';
210 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
225 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
237 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
243 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
244 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
247 if(*p=='-' || *p=='+')
253 fpcontrol = _control87(0, 0);
254 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
255 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
258 ret = (double)sign*d*pow(10, exp);
260 ret = (double)sign*d/pow(10, -exp);
262 _control87(fpcontrol, 0xffffffff);
264 if((d && ret==0.0) || isinf(ret))
265 *MSVCRT__errno() = MSVCRT_ERANGE;
273 /*********************************************************************
276 double CDECL MSVCRT_strtod( const char *str, char **end )
278 return MSVCRT_strtod_l( str, end, NULL );
281 /*********************************************************************
284 double CDECL MSVCRT_atof( const char *str )
286 return MSVCRT_strtod_l(str, NULL, NULL);
289 /*********************************************************************
292 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
294 return MSVCRT_strtod_l(str, NULL, locale);
297 /*********************************************************************
300 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
302 /* FIXME: handle Windows locale */
303 return strcoll( str1, str2 );
306 /*********************************************************************
307 * strcpy_s (MSVCRT.@)
309 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
312 if(!elem) return MSVCRT_EINVAL;
313 if(!dst) return MSVCRT_EINVAL;
317 return MSVCRT_EINVAL;
320 for(i = 0; i < elem; i++)
322 if((dst[i] = src[i]) == '\0') return 0;
325 return MSVCRT_ERANGE;
328 /*********************************************************************
329 * strcat_s (MSVCRT.@)
331 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
334 if(!dst) return MSVCRT_EINVAL;
335 if(elem == 0) return MSVCRT_EINVAL;
339 return MSVCRT_EINVAL;
342 for(i = 0; i < elem; i++)
346 for(j = 0; (j + i) < elem; j++)
348 if((dst[j + i] = src[j]) == '\0') return 0;
352 /* Set the first element to 0, not the first element after the skipped part */
354 return MSVCRT_ERANGE;
357 /*********************************************************************
360 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
362 /* FIXME: handle Windows locale */
363 return strxfrm( dest, src, len );
366 /*********************************************************************
367 * _stricoll (MSVCRT.@)
369 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
371 /* FIXME: handle collates */
372 TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
373 return lstrcmpiA( str1, str2 );
376 /********************************************************************
377 * _atoldbl (MSVCRT.@)
379 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
381 /* FIXME needs error checking for huge/small values */
383 TRACE("str %s value %p\n",str,value);
384 value->x = strtold(str,0);
386 FIXME("stub, str %s value %p\n",str,value);
391 /********************************************************************
392 * __STRINGTOLD (MSVCRT.@)
394 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
397 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
398 value->x = strtold(str,endptr);
400 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
405 /******************************************************************
408 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
410 /* wrapper to forward libc error code to msvcrt's error codes */
414 ret = strtol(nptr, end, base);
417 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
418 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
420 /* cope with the fact that we may use 64bit long integers on libc
421 * while msvcrt always uses 32bit long integers
423 if (ret > MSVCRT_LONG_MAX)
425 ret = MSVCRT_LONG_MAX;
426 *MSVCRT__errno() = MSVCRT_ERANGE;
428 else if (ret < -MSVCRT_LONG_MAX - 1)
430 ret = -MSVCRT_LONG_MAX - 1;
431 *MSVCRT__errno() = MSVCRT_ERANGE;
439 /******************************************************************
442 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
444 /* wrapper to forward libc error code to msvcrt's error codes */
448 ret = strtoul(nptr, end, base);
451 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
452 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
454 /* cope with the fact that we may use 64bit long integers on libc
455 * while msvcrt always uses 32bit long integers
457 if (ret > MSVCRT_ULONG_MAX)
459 ret = MSVCRT_ULONG_MAX;
460 *MSVCRT__errno() = MSVCRT_ERANGE;
468 /******************************************************************
471 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
475 for(i=0; i<maxlen; i++)
481 /*********************************************************************
482 * _strtoi64_l (MSVCRT.@)
484 * FIXME: locale parameter is ignored
486 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
488 BOOL negative = FALSE;
491 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
493 if(!nptr || base<0 || base>36 || base==1) {
494 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
498 while(isspace(*nptr)) nptr++;
503 } else if(*nptr == '+')
506 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
519 char cur = tolower(*nptr);
527 if(cur<'a' || cur>='a'+base-10)
537 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
538 ret = MSVCRT_I64_MAX;
539 *MSVCRT__errno() = MSVCRT_ERANGE;
540 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
541 ret = MSVCRT_I64_MIN;
542 *MSVCRT__errno() = MSVCRT_ERANGE;
548 *endptr = (char*)nptr;
553 /*********************************************************************
554 * _strtoi64 (MSVCRT.@)
556 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
558 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
561 /*********************************************************************
562 * _strtoui64_l (MSVCRT.@)
564 * FIXME: locale parameter is ignored
566 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
568 BOOL negative = FALSE;
569 unsigned __int64 ret = 0;
571 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
573 if(!nptr || base<0 || base>36 || base==1) {
574 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
578 while(isspace(*nptr)) nptr++;
583 } else if(*nptr == '+')
586 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
599 char cur = tolower(*nptr);
607 if(cur<'a' || cur>='a'+base-10)
614 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
615 ret = MSVCRT_UI64_MAX;
616 *MSVCRT__errno() = MSVCRT_ERANGE;
622 *endptr = (char*)nptr;
624 return negative ? -ret : ret;
627 /*********************************************************************
628 * _strtoui64 (MSVCRT.@)
630 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
632 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
635 /*********************************************************************
636 * _ui64toa_s (MSVCRT.@)
638 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
639 MSVCRT_size_t size, int radix)
641 char buffer[65], *pos;
644 if(!str || radix<2 || radix>36) {
645 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
646 *MSVCRT__errno() = MSVCRT_EINVAL;
647 return MSVCRT_EINVAL;
660 *--pos = 'a'+digit-10;
663 if(buffer-pos+65 > size) {
664 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
665 *MSVCRT__errno() = MSVCRT_EINVAL;
666 return MSVCRT_EINVAL;
669 memcpy(str, pos, buffer-pos+65);