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
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34 /*********************************************************************
38 char* CDECL _strdup(const char* str)
42 char * ret = MSVCRT_malloc(strlen(str)+1);
43 if (ret) strcpy( ret, str );
49 /*********************************************************************
52 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
60 /*********************************************************************
63 char* CDECL _strrev(char* str)
69 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
79 /*********************************************************************
82 char* CDECL _strset(char* str, int value)
91 /*********************************************************************
94 char * CDECL MSVCRT_strtok( char *str, const char *delim )
96 thread_data_t *data = msvcrt_get_thread_data();
100 if (!(str = data->strtok_next)) return NULL;
102 while (*str && strchr( delim, *str )) str++;
103 if (!*str) return NULL;
105 while (*str && !strchr( delim, *str )) str++;
106 if (*str) *str++ = 0;
107 data->strtok_next = str;
112 /*********************************************************************
115 void CDECL MSVCRT__swab(char* src, char* dst, int len)
119 len = (unsigned)len >> 1;
131 /*********************************************************************
134 double CDECL MSVCRT_atof( const char *str )
139 /*********************************************************************
140 * strtod_l (MSVCRT.@)
142 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
144 const char *p, *dec_point=NULL, *exp=NULL;
150 locale = get_locale();
153 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
154 *MSVCRT__errno() = MSVCRT_EINVAL;
158 /* FIXME: use *_l functions */
162 if(*p=='+' || *p=='-')
166 if(*p == *locale->locinfo->lconv->decimal_point) {
173 if(*p=='d' || *p=='D')
176 /* FIXME: don't copy input string */
177 if((dec_point || exp) && (copy=_strdup(str))) {
179 copy[dec_point-str] = '.';
184 ret = strtod(copy, end);
186 *end = (char*)str+(*end-copy);
190 ret = strtod(str, end);
193 *MSVCRT__errno() = errno;
199 /*********************************************************************
202 double CDECL MSVCRT_strtod( const char *str, char **end )
204 return MSVCRT_strtod_l( str, end, NULL );
207 /*********************************************************************
210 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
212 /* FIXME: handle Windows locale */
213 return strcoll( str1, str2 );
216 /*********************************************************************
217 * strcpy_s (MSVCRT.@)
219 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
222 if(!elem) return MSVCRT_EINVAL;
223 if(!dst) return MSVCRT_EINVAL;
227 return MSVCRT_EINVAL;
230 for(i = 0; i < elem; i++)
232 if((dst[i] = src[i]) == '\0') return 0;
235 return MSVCRT_ERANGE;
238 /*********************************************************************
239 * strcat_s (MSVCRT.@)
241 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
244 if(!dst) return MSVCRT_EINVAL;
245 if(elem == 0) return MSVCRT_EINVAL;
249 return MSVCRT_EINVAL;
252 for(i = 0; i < elem; i++)
256 for(j = 0; (j + i) < elem; j++)
258 if((dst[j + i] = src[j]) == '\0') return 0;
262 /* Set the first element to 0, not the first element after the skipped part */
264 return MSVCRT_ERANGE;
267 /*********************************************************************
270 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
272 /* FIXME: handle Windows locale */
273 return strxfrm( dest, src, len );
276 /*********************************************************************
277 * _stricoll (MSVCRT.@)
279 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
281 /* FIXME: handle collates */
282 TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
283 return lstrcmpiA( str1, str2 );
286 /********************************************************************
287 * _atoldbl (MSVCRT.@)
289 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
291 /* FIXME needs error checking for huge/small values */
293 TRACE("str %s value %p\n",str,value);
294 value->x = strtold(str,0);
296 FIXME("stub, str %s value %p\n",str,value);
301 /********************************************************************
302 * __STRINGTOLD (MSVCRT.@)
304 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
307 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
308 value->x = strtold(str,endptr);
310 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
315 /******************************************************************
318 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
320 /* wrapper to forward libc error code to msvcrt's error codes */
324 ret = strtol(nptr, end, base);
327 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
328 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
330 /* cope with the fact that we may use 64bit long integers on libc
331 * while msvcrt always uses 32bit long integers
333 if (ret > MSVCRT_LONG_MAX)
335 ret = MSVCRT_LONG_MAX;
336 *MSVCRT__errno() = MSVCRT_ERANGE;
338 else if (ret < -MSVCRT_LONG_MAX - 1)
340 ret = -MSVCRT_LONG_MAX - 1;
341 *MSVCRT__errno() = MSVCRT_ERANGE;
349 /******************************************************************
352 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
354 /* wrapper to forward libc error code to msvcrt's error codes */
358 ret = strtoul(nptr, end, base);
361 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
362 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
364 /* cope with the fact that we may use 64bit long integers on libc
365 * while msvcrt always uses 32bit long integers
367 if (ret > MSVCRT_ULONG_MAX)
369 ret = MSVCRT_ULONG_MAX;
370 *MSVCRT__errno() = MSVCRT_ERANGE;
378 /******************************************************************
381 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
385 for(i=0; i<maxlen; i++)
391 /*********************************************************************
392 * _strtoi64_l (MSVCR90.@)
394 * FIXME: locale parameter is ignored
396 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
398 BOOL negative = FALSE;
401 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
403 if(!nptr || base<0 || base>36 || base==1) {
404 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
408 while(isspace(*nptr)) nptr++;
413 } else if(*nptr == '+')
416 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
429 char cur = tolower(*nptr);
437 if(cur<'a' || cur>='a'+base-10)
447 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
448 ret = MSVCRT_I64_MAX;
449 *MSVCRT__errno() = ERANGE;
450 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
451 ret = MSVCRT_I64_MIN;
452 *MSVCRT__errno() = ERANGE;
458 *endptr = (char*)nptr;
463 /*********************************************************************
464 * _strtoi64 (MSVCR90.@)
466 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
468 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
471 /*********************************************************************
472 * _strtoui64_l (MSVCR90.@)
474 * FIXME: locale parameter is ignored
476 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
478 BOOL negative = FALSE;
479 unsigned __int64 ret = 0;
481 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
483 if(!nptr || base<0 || base>36 || base==1) {
484 MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
488 while(isspace(*nptr)) nptr++;
493 } else if(*nptr == '+')
496 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
509 char cur = tolower(*nptr);
517 if(cur<'a' || cur>='a'+base-10)
524 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
525 ret = MSVCRT_UI64_MAX;
526 *MSVCRT__errno() = ERANGE;
532 *endptr = (char*)nptr;
534 return negative ? -ret : ret;
537 /*********************************************************************
538 * _strtoui64 (MSVCR90.@)
540 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
542 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);