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 /*********************************************************************
142 double CDECL MSVCRT_strtod( const char *str, char **end )
144 return strtod( str, end );
147 /*********************************************************************
150 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
152 /* FIXME: handle Windows locale */
153 return strcoll( str1, str2 );
156 /*********************************************************************
157 * strcpy_s (MSVCRT.@)
159 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
162 if(!elem) return MSVCRT_EINVAL;
163 if(!dst) return MSVCRT_EINVAL;
167 return MSVCRT_EINVAL;
170 for(i = 0; i < elem; i++)
172 if((dst[i] = src[i]) == '\0') return 0;
175 return MSVCRT_ERANGE;
178 /*********************************************************************
179 * strcat_s (MSVCRT.@)
181 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
184 if(!dst) return MSVCRT_EINVAL;
185 if(elem == 0) return MSVCRT_EINVAL;
189 return MSVCRT_EINVAL;
192 for(i = 0; i < elem; i++)
196 for(j = 0; (j + i) < elem; j++)
198 if((dst[j + i] = src[j]) == '\0') return 0;
202 /* Set the first element to 0, not the first element after the skipped part */
204 return MSVCRT_ERANGE;
207 /*********************************************************************
210 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
212 /* FIXME: handle Windows locale */
213 return strxfrm( dest, src, len );
216 /*********************************************************************
217 * _stricoll (MSVCRT.@)
219 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
221 /* FIXME: handle collates */
222 TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
223 return lstrcmpiA( str1, str2 );
226 /********************************************************************
227 * _atoldbl (MSVCRT.@)
229 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
231 /* FIXME needs error checking for huge/small values */
233 TRACE("str %s value %p\n",str,value);
234 value->x = strtold(str,0);
236 FIXME("stub, str %s value %p\n",str,value);
241 /********************************************************************
242 * __STRINGTOLD (MSVCRT.@)
244 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
247 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
248 value->x = strtold(str,endptr);
250 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
255 /******************************************************************
258 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
260 /* wrapper to forward libc error code to msvcrt's error codes */
264 ret = strtol(nptr, end, base);
267 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
268 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
270 /* cope with the fact that we may use 64bit long integers on libc
271 * while msvcrt always uses 32bit long integers
273 if (ret > MSVCRT_LONG_MAX)
275 ret = MSVCRT_LONG_MAX;
276 *MSVCRT__errno() = MSVCRT_ERANGE;
278 else if (ret < -MSVCRT_LONG_MAX - 1)
280 ret = -MSVCRT_LONG_MAX - 1;
281 *MSVCRT__errno() = MSVCRT_ERANGE;
289 /******************************************************************
292 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
294 /* wrapper to forward libc error code to msvcrt's error codes */
298 ret = strtoul(nptr, end, base);
301 case ERANGE: *MSVCRT__errno() = MSVCRT_ERANGE; break;
302 case EINVAL: *MSVCRT__errno() = MSVCRT_EINVAL; break;
304 /* cope with the fact that we may use 64bit long integers on libc
305 * while msvcrt always uses 32bit long integers
307 if (ret > MSVCRT_ULONG_MAX)
309 ret = MSVCRT_ULONG_MAX;
310 *MSVCRT__errno() = MSVCRT_ERANGE;
318 /******************************************************************
321 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
325 for(i=0; i<maxlen; i++)
331 /*********************************************************************
332 * _strtoi64_l (MSVCR90.@)
334 * FIXME: locale parameter is ignored
336 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
338 BOOL negative = FALSE;
341 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
343 if(!nptr || base<0 || base>36 || base==1) {
344 MSVCRT_invalid_parameter(NULL, NULL, NULL, 0, NULL);
348 while(isspace(*nptr)) nptr++;
353 } else if(*nptr == '+')
356 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
369 char cur = tolower(*nptr);
377 if(cur<'a' || cur>='a'+base-10)
387 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
388 ret = MSVCRT_I64_MAX;
389 *MSVCRT__errno() = ERANGE;
390 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
391 ret = MSVCRT_I64_MIN;
392 *MSVCRT__errno() = ERANGE;
398 *endptr = (char*)nptr;
403 /*********************************************************************
404 * _strtoi64 (MSVCR90.@)
406 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
408 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
411 /*********************************************************************
412 * _strtoui64_l (MSVCR90.@)
414 * FIXME: locale parameter is ignored
416 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
418 BOOL negative = FALSE;
419 unsigned __int64 ret = 0;
421 TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
423 if(!nptr || base<0 || base>36 || base==1) {
424 MSVCRT_invalid_parameter(NULL, NULL, NULL, 0, NULL);
428 while(isspace(*nptr)) nptr++;
433 } else if(*nptr == '+')
436 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
449 char cur = tolower(*nptr);
457 if(cur<'a' || cur>='a'+base-10)
464 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
465 ret = MSVCRT_UI64_MAX;
466 *MSVCRT__errno() = ERANGE;
472 *endptr = (char*)nptr;
474 return negative ? -ret : ret;
477 /*********************************************************************
478 * _strtoui64 (MSVCR90.@)
480 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
482 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);