wined3d: Support getdc with user memory.
[wine.git] / dlls / msvcrt / string.c
blob10fb35f2cc9cb1bd4826438febe7e7e4a8574fd7
1 /*
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <math.h>
31 #include <limits.h>
32 #include <errno.h>
33 #include "msvcrt.h"
34 #include "winnls.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
39 /*********************************************************************
40 * _mbsdup (MSVCRT.@)
41 * _strdup (MSVCRT.@)
43 char* CDECL MSVCRT__strdup(const char* str)
45 if(str)
47 char * ret = MSVCRT_malloc(strlen(str)+1);
48 if (ret) strcpy( ret, str );
49 return ret;
51 else return 0;
54 /*********************************************************************
55 * _strlwr_s_l (MSVCRT.@)
57 int CDECL _strlwr_s_l(char *str, MSVCRT_size_t len, MSVCRT__locale_t locale)
59 char *ptr = str;
61 if (!str || !len)
63 *MSVCRT__errno() = MSVCRT_EINVAL;
64 return MSVCRT_EINVAL;
67 while (len && *ptr)
69 len--;
70 ptr++;
73 if (!len)
75 str[0] = '\0';
76 *MSVCRT__errno() = MSVCRT_EINVAL;
77 return MSVCRT_EINVAL;
80 while (*str)
82 *str = MSVCRT__tolower_l((unsigned char)*str, locale);
83 str++;
86 return 0;
89 /*********************************************************************
90 * _strlwr_s (MSVCRT.@)
92 int CDECL _strlwr_s(char *str, MSVCRT_size_t len)
94 return _strlwr_s_l(str, len, NULL);
97 /*********************************************************************
98 * _strlwr_l (MSVCRT.@)
100 char* CDECL _strlwr_l(char *str, MSVCRT__locale_t locale)
102 _strlwr_s_l(str, -1, locale);
103 return str;
106 /*********************************************************************
107 * _strlwr (MSVCRT.@)
109 char* CDECL MSVCRT__strlwr(char *str)
111 _strlwr_s_l(str, -1, NULL);
112 return str;
115 /*********************************************************************
116 * _strupr_s_l (MSVCRT.@)
118 int CDECL _strupr_s_l(char *str, MSVCRT_size_t len, MSVCRT__locale_t locale)
120 char *ptr = str;
122 if (!str || !len)
124 *MSVCRT__errno() = MSVCRT_EINVAL;
125 return MSVCRT_EINVAL;
128 while (len && *ptr)
130 len--;
131 ptr++;
134 if (!len)
136 str[0] = '\0';
137 *MSVCRT__errno() = MSVCRT_EINVAL;
138 return MSVCRT_EINVAL;
141 while (*str)
143 *str = MSVCRT__toupper_l((unsigned char)*str, locale);
144 str++;
147 return 0;
150 /*********************************************************************
151 * _strupr_s (MSVCRT.@)
153 int CDECL _strupr_s(char *str, MSVCRT_size_t len)
155 return _strupr_s_l(str, len, NULL);
158 /*********************************************************************
159 * _strupr_l (MSVCRT.@)
161 char* CDECL MSVCRT__strupr_l(char *str, MSVCRT__locale_t locale)
163 _strupr_s_l(str, -1, locale);
164 return str;
167 /*********************************************************************
168 * _strupr (MSVCRT.@)
170 char* CDECL MSVCRT__strupr(char *str)
172 _strupr_s_l(str, -1, NULL);
173 return str;
176 /*********************************************************************
177 * _strnset (MSVCRT.@)
179 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
181 if (len > 0 && str)
182 while (*str && len--)
183 *str++ = value;
184 return str;
187 /*********************************************************************
188 * _strrev (MSVCRT.@)
190 char* CDECL MSVCRT__strrev(char* str)
192 char * p1;
193 char * p2;
195 if (str && *str)
196 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
198 *p1 ^= *p2;
199 *p2 ^= *p1;
200 *p1 ^= *p2;
203 return str;
206 /*********************************************************************
207 * _strset (MSVCRT.@)
209 char* CDECL _strset(char* str, int value)
211 char *ptr = str;
212 while (*ptr)
213 *ptr++ = value;
215 return str;
218 /*********************************************************************
219 * strtok (MSVCRT.@)
221 char * CDECL MSVCRT_strtok( char *str, const char *delim )
223 thread_data_t *data = msvcrt_get_thread_data();
224 char *ret;
226 if (!str)
227 if (!(str = data->strtok_next)) return NULL;
229 while (*str && strchr( delim, *str )) str++;
230 if (!*str) return NULL;
231 ret = str++;
232 while (*str && !strchr( delim, *str )) str++;
233 if (*str) *str++ = 0;
234 data->strtok_next = str;
235 return ret;
238 /*********************************************************************
239 * strtok_s (MSVCRT.@)
241 char * CDECL MSVCRT_strtok_s(char *str, const char *delim, char **ctx)
243 if (!MSVCRT_CHECK_PMT(delim != NULL)) return NULL;
244 if (!MSVCRT_CHECK_PMT(ctx != NULL)) return NULL;
245 if (!MSVCRT_CHECK_PMT(str != NULL || *ctx != NULL)) return NULL;
247 if(!str)
248 str = *ctx;
250 while(*str && strchr(delim, *str))
251 str++;
252 if(!*str)
253 return NULL;
255 *ctx = str+1;
256 while(**ctx && !strchr(delim, **ctx))
257 (*ctx)++;
258 if(**ctx)
259 *(*ctx)++ = 0;
261 return str;
264 /*********************************************************************
265 * _swab (MSVCRT.@)
267 void CDECL MSVCRT__swab(char* src, char* dst, int len)
269 if (len > 1)
271 len = (unsigned)len >> 1;
273 while (len--) {
274 char s0 = src[0];
275 char s1 = src[1];
276 *dst++ = s1;
277 *dst++ = s0;
278 src = src + 2;
283 static double strtod_helper(const char *str, char **end, MSVCRT__locale_t locale, int *err)
285 MSVCRT_pthreadlocinfo locinfo;
286 unsigned __int64 d=0, hlp;
287 unsigned fpcontrol;
288 int exp=0, sign=1;
289 const char *p;
290 double ret;
291 long double lret=1, expcnt = 10;
292 BOOL found_digit = FALSE, negexp;
294 if(err)
295 *err = 0;
296 else
297 if(!MSVCRT_CHECK_PMT(str != NULL)) return 0;
299 if(!locale)
300 locinfo = get_locinfo();
301 else
302 locinfo = locale->locinfo;
304 /* FIXME: use *_l functions */
305 p = str;
306 while(isspace(*p))
307 p++;
309 if(*p == '-') {
310 sign = -1;
311 p++;
312 } else if(*p == '+')
313 p++;
315 while(isdigit(*p)) {
316 found_digit = TRUE;
317 hlp = d*10+*(p++)-'0';
318 if(d>MSVCRT_UI64_MAX/10 || hlp<d) {
319 exp++;
320 break;
321 } else
322 d = hlp;
324 while(isdigit(*p)) {
325 exp++;
326 p++;
329 if(*p == *locinfo->lconv->decimal_point)
330 p++;
332 while(isdigit(*p)) {
333 found_digit = TRUE;
334 hlp = d*10+*(p++)-'0';
335 if(d>MSVCRT_UI64_MAX/10 || hlp<d)
336 break;
338 d = hlp;
339 exp--;
341 while(isdigit(*p))
342 p++;
344 if(!found_digit) {
345 if(end)
346 *end = (char*)str;
347 return 0.0;
350 if(*p=='e' || *p=='E' || *p=='d' || *p=='D') {
351 int e=0, s=1;
353 p++;
354 if(*p == '-') {
355 s = -1;
356 p++;
357 } else if(*p == '+')
358 p++;
360 if(isdigit(*p)) {
361 while(isdigit(*p)) {
362 if(e>INT_MAX/10 || (e=e*10+*p-'0')<0)
363 e = INT_MAX;
364 p++;
366 e *= s;
368 if(exp<0 && e<0 && exp+e>=0) exp = INT_MIN;
369 else if(exp>0 && e>0 && exp+e<0) exp = INT_MAX;
370 else exp += e;
371 } else {
372 if(*p=='-' || *p=='+')
373 p--;
374 p--;
378 fpcontrol = _control87(0, 0);
379 _control87(MSVCRT__EM_DENORMAL|MSVCRT__EM_INVALID|MSVCRT__EM_ZERODIVIDE
380 |MSVCRT__EM_OVERFLOW|MSVCRT__EM_UNDERFLOW|MSVCRT__EM_INEXACT, 0xffffffff);
382 negexp = (exp < 0);
383 if(negexp)
384 exp = -exp;
385 while(exp) {
386 if(exp & 1)
387 lret *= expcnt;
388 exp /= 2;
389 expcnt = expcnt*expcnt;
391 ret = (long double)sign * (negexp ? d/lret : d*lret);
393 _control87(fpcontrol, 0xffffffff);
395 if((d && ret==0.0) || isinf(ret)) {
396 if(err)
397 *err = MSVCRT_ERANGE;
398 else
399 *MSVCRT__errno() = MSVCRT_ERANGE;
402 if(end)
403 *end = (char*)p;
405 return ret;
408 /*********************************************************************
409 * strtod_l (MSVCRT.@)
411 double CDECL MSVCRT_strtod_l(const char *str, char **end, MSVCRT__locale_t locale)
413 return strtod_helper(str, end, locale, NULL);
416 /*********************************************************************
417 * strtod (MSVCRT.@)
419 double CDECL MSVCRT_strtod( const char *str, char **end )
421 return MSVCRT_strtod_l( str, end, NULL );
424 /*********************************************************************
425 * atof (MSVCRT.@)
427 double CDECL MSVCRT_atof( const char *str )
429 return MSVCRT_strtod_l(str, NULL, NULL);
432 /*********************************************************************
433 * _atof_l (MSVCRT.@)
435 double CDECL MSVCRT__atof_l( const char *str, MSVCRT__locale_t locale)
437 return MSVCRT_strtod_l(str, NULL, locale);
440 /*********************************************************************
441 * _atoflt_l (MSVCRT.@)
443 int CDECL MSVCRT__atoflt_l( MSVCRT__CRT_FLOAT *value, char *str, MSVCRT__locale_t locale)
445 double d;
446 int err;
448 d = strtod_helper(str, NULL, locale, &err);
449 value->f = d;
450 if(isinf(value->f))
451 return MSVCRT__OVERFLOW;
452 if((d!=0 || err) && value->f>-MSVCRT_FLT_MIN && value->f<MSVCRT_FLT_MIN)
453 return MSVCRT__UNDERFLOW;
454 return 0;
457 /*********************************************************************
458 * _atodbl_l (MSVCRT.@)
460 int CDECL MSVCRT__atodbl_l(MSVCRT__CRT_DOUBLE *value, char *str, MSVCRT__locale_t locale)
462 int err;
464 value->x = strtod_helper(str, NULL, locale, &err);
465 if(isinf(value->x))
466 return MSVCRT__OVERFLOW;
467 if((value->x!=0 || err) && value->x>-MSVCRT_DBL_MIN && value->x<MSVCRT_DBL_MIN)
468 return MSVCRT__UNDERFLOW;
469 return 0;
472 /*********************************************************************
473 * _atodbl (MSVCRT.@)
475 int CDECL MSVCRT__atodbl(MSVCRT__CRT_DOUBLE *value, char *str)
477 return MSVCRT__atodbl_l(value, str, NULL);
480 /*********************************************************************
481 * _strcoll_l (MSVCRT.@)
483 int CDECL MSVCRT_strcoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
485 MSVCRT_pthreadlocinfo locinfo;
487 if(!locale)
488 locinfo = get_locinfo();
489 else
490 locinfo = locale->locinfo;
492 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, -1, str2, -1)-CSTR_EQUAL;
495 /*********************************************************************
496 * strcoll (MSVCRT.@)
498 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
500 return MSVCRT_strcoll_l(str1, str2, NULL);
503 /*********************************************************************
504 * _stricoll_l (MSVCRT.@)
506 int CDECL MSVCRT__stricoll_l( const char* str1, const char* str2, MSVCRT__locale_t locale )
508 MSVCRT_pthreadlocinfo locinfo;
510 if(!locale)
511 locinfo = get_locinfo();
512 else
513 locinfo = locale->locinfo;
515 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
516 str1, -1, str2, -1)-CSTR_EQUAL;
519 /*********************************************************************
520 * _stricoll (MSVCRT.@)
522 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
524 return MSVCRT__stricoll_l(str1, str2, NULL);
527 /*********************************************************************
528 * _strncoll_l (MSVCRT.@)
530 int CDECL MSVCRT__strncoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
532 MSVCRT_pthreadlocinfo locinfo;
534 if(!locale)
535 locinfo = get_locinfo();
536 else
537 locinfo = locale->locinfo;
539 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_COLLATE], 0, str1, count, str2, count)-CSTR_EQUAL;
542 /*********************************************************************
543 * _strncoll (MSVCRT.@)
545 int CDECL MSVCRT__strncoll( const char* str1, const char* str2, MSVCRT_size_t count )
547 return MSVCRT__strncoll_l(str1, str2, count, NULL);
550 /*********************************************************************
551 * _strnicoll_l (MSVCRT.@)
553 int CDECL MSVCRT__strnicoll_l( const char* str1, const char* str2, MSVCRT_size_t count, MSVCRT__locale_t locale )
555 MSVCRT_pthreadlocinfo locinfo;
557 if(!locale)
558 locinfo = get_locinfo();
559 else
560 locinfo = locale->locinfo;
562 return CompareStringA(locinfo->lc_handle[MSVCRT_LC_COLLATE], NORM_IGNORECASE,
563 str1, count, str2, count)-CSTR_EQUAL;
566 /*********************************************************************
567 * _strnicoll (MSVCRT.@)
569 int CDECL MSVCRT__strnicoll( const char* str1, const char* str2, MSVCRT_size_t count )
571 return MSVCRT__strnicoll_l(str1, str2, count, NULL);
574 /*********************************************************************
575 * strncpy (MSVCRT.@)
577 char* __cdecl MSVCRT_strncpy(char *dst, const char *src, MSVCRT_size_t len)
579 MSVCRT_size_t i;
581 for(i=0; i<len; i++)
582 if((dst[i] = src[i]) == '\0') break;
584 while (i < len) dst[i++] = 0;
586 return dst;
589 /*********************************************************************
590 * strcpy_s (MSVCRT.@)
592 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
594 MSVCRT_size_t i;
595 if(!elem) return MSVCRT_EINVAL;
596 if(!dst) return MSVCRT_EINVAL;
597 if(!src)
599 dst[0] = '\0';
600 return MSVCRT_EINVAL;
603 for(i = 0; i < elem; i++)
605 if((dst[i] = src[i]) == '\0') return 0;
607 dst[0] = '\0';
608 return MSVCRT_ERANGE;
611 /*********************************************************************
612 * strcat_s (MSVCRT.@)
614 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
616 MSVCRT_size_t i, j;
617 if(!dst) return MSVCRT_EINVAL;
618 if(elem == 0) return MSVCRT_EINVAL;
619 if(!src)
621 dst[0] = '\0';
622 return MSVCRT_EINVAL;
625 for(i = 0; i < elem; i++)
627 if(dst[i] == '\0')
629 for(j = 0; (j + i) < elem; j++)
631 if((dst[j + i] = src[j]) == '\0') return 0;
635 /* Set the first element to 0, not the first element after the skipped part */
636 dst[0] = '\0';
637 return MSVCRT_ERANGE;
640 /*********************************************************************
641 * strncat_s (MSVCRT.@)
643 int CDECL MSVCRT_strncat_s( char* dst, MSVCRT_size_t elem, const char* src, MSVCRT_size_t count )
645 MSVCRT_size_t i, j;
647 if (!MSVCRT_CHECK_PMT(dst != 0)) return MSVCRT_EINVAL;
648 if (!MSVCRT_CHECK_PMT(elem != 0)) return MSVCRT_EINVAL;
649 if (!MSVCRT_CHECK_PMT(src != 0))
651 dst[0] = '\0';
652 return MSVCRT_EINVAL;
655 for(i = 0; i < elem; i++)
657 if(dst[i] == '\0')
659 for(j = 0; (j + i) < elem; j++)
661 if(count == MSVCRT__TRUNCATE && j + i == elem - 1)
663 dst[j + i] = '\0';
664 return MSVCRT_STRUNCATE;
666 if(j == count || (dst[j + i] = src[j]) == '\0')
668 dst[j + i] = '\0';
669 return 0;
674 /* Set the first element to 0, not the first element after the skipped part */
675 dst[0] = '\0';
676 return MSVCRT_ERANGE;
679 /*********************************************************************
680 * strncat (MSVCRT.@)
682 char* __cdecl MSVCRT_strncat(char *dst, const char *src, MSVCRT_size_t len)
684 return strncat(dst, src, len);
687 /*********************************************************************
688 * strxfrm (MSVCRT.@)
690 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
692 /* FIXME: handle Windows locale */
693 return strxfrm( dest, src, len );
696 /********************************************************************
697 * _atoldbl (MSVCRT.@)
699 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
701 /* FIXME needs error checking for huge/small values */
702 #ifdef HAVE_STRTOLD
703 long double ld;
704 TRACE("str %s value %p\n",str,value);
705 ld = strtold(str,0);
706 memcpy(value, &ld, 10);
707 #else
708 FIXME("stub, str %s value %p\n",str,value);
709 #endif
710 return 0;
713 /********************************************************************
714 * __STRINGTOLD (MSVCRT.@)
716 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
718 #ifdef HAVE_STRTOLD
719 long double ld;
720 FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
721 ld = strtold(str,0);
722 memcpy(value, &ld, 10);
723 #else
724 FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
725 #endif
726 return 0;
729 /*********************************************************************
730 * strlen (MSVCRT.@)
732 MSVCRT_size_t __cdecl MSVCRT_strlen(const char *str)
734 return strlen(str);
737 /******************************************************************
738 * strnlen (MSVCRT.@)
740 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
742 MSVCRT_size_t i;
744 for(i=0; i<maxlen; i++)
745 if(!s[i]) break;
747 return i;
750 /*********************************************************************
751 * _strtoi64_l (MSVCRT.@)
753 * FIXME: locale parameter is ignored
755 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
757 BOOL negative = FALSE;
758 __int64 ret = 0;
760 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
762 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
763 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
764 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
766 while(isspace(*nptr)) nptr++;
768 if(*nptr == '-') {
769 negative = TRUE;
770 nptr++;
771 } else if(*nptr == '+')
772 nptr++;
774 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
775 base = 16;
776 nptr += 2;
779 if(base == 0) {
780 if(*nptr=='0')
781 base = 8;
782 else
783 base = 10;
786 while(*nptr) {
787 char cur = tolower(*nptr);
788 int v;
790 if(isdigit(cur)) {
791 if(cur >= '0'+base)
792 break;
793 v = cur-'0';
794 } else {
795 if(cur<'a' || cur>='a'+base-10)
796 break;
797 v = cur-'a'+10;
800 if(negative)
801 v = -v;
803 nptr++;
805 if(!negative && (ret>MSVCRT_I64_MAX/base || ret*base>MSVCRT_I64_MAX-v)) {
806 ret = MSVCRT_I64_MAX;
807 *MSVCRT__errno() = MSVCRT_ERANGE;
808 } else if(negative && (ret<MSVCRT_I64_MIN/base || ret*base<MSVCRT_I64_MIN-v)) {
809 ret = MSVCRT_I64_MIN;
810 *MSVCRT__errno() = MSVCRT_ERANGE;
811 } else
812 ret = ret*base + v;
815 if(endptr)
816 *endptr = (char*)nptr;
818 return ret;
821 /*********************************************************************
822 * _strtoi64 (MSVCRT.@)
824 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
826 return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
829 /*********************************************************************
830 * _atoi_l (MSVCRT.@)
832 int __cdecl MSVCRT__atoi_l(const char *str, MSVCRT__locale_t locale)
834 __int64 ret = MSVCRT_strtoi64_l(str, NULL, 10, locale);
836 if(ret > INT_MAX) {
837 ret = INT_MAX;
838 *MSVCRT__errno() = MSVCRT_ERANGE;
839 } else if(ret < INT_MIN) {
840 ret = INT_MIN;
841 *MSVCRT__errno() = MSVCRT_ERANGE;
843 return ret;
846 /*********************************************************************
847 * atoi (MSVCRT.@)
849 int __cdecl MSVCRT_atoi(const char *str)
851 BOOL minus = FALSE;
852 int ret = 0;
854 if(!str)
855 return 0;
857 while(isspace(*str)) str++;
859 if(*str == '+') {
860 str++;
861 }else if(*str == '-') {
862 minus = TRUE;
863 str++;
866 while(*str>='0' && *str<='9') {
867 ret = ret*10+*str-'0';
868 str++;
871 return minus ? -ret : ret;
874 /******************************************************************
875 * strtol (MSVCRT.@)
877 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
879 __int64 ret = MSVCRT_strtoi64_l(nptr, end, base, NULL);
881 if(ret > MSVCRT_LONG_MAX) {
882 ret = MSVCRT_LONG_MAX;
883 *MSVCRT__errno() = MSVCRT_ERANGE;
884 } else if(ret < MSVCRT_LONG_MIN) {
885 ret = MSVCRT_LONG_MIN;
886 *MSVCRT__errno() = MSVCRT_ERANGE;
889 return ret;
892 /******************************************************************
893 * strtoul (MSVCRT.@)
895 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
897 __int64 ret = MSVCRT_strtoi64_l(nptr, end, base, NULL);
899 if(ret > MSVCRT_ULONG_MAX) {
900 ret = MSVCRT_ULONG_MAX;
901 *MSVCRT__errno() = MSVCRT_ERANGE;
902 }else if(ret < -(__int64)MSVCRT_ULONG_MAX) {
903 ret = 1;
904 *MSVCRT__errno() = MSVCRT_ERANGE;
907 return ret;
910 /*********************************************************************
911 * _strtoui64_l (MSVCRT.@)
913 * FIXME: locale parameter is ignored
915 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
917 BOOL negative = FALSE;
918 unsigned __int64 ret = 0;
920 TRACE("(%s %p %d %p)\n", debugstr_a(nptr), endptr, base, locale);
922 if (!MSVCRT_CHECK_PMT(nptr != NULL)) return 0;
923 if (!MSVCRT_CHECK_PMT(base == 0 || base >= 2)) return 0;
924 if (!MSVCRT_CHECK_PMT(base <= 36)) return 0;
926 while(isspace(*nptr)) nptr++;
928 if(*nptr == '-') {
929 negative = TRUE;
930 nptr++;
931 } else if(*nptr == '+')
932 nptr++;
934 if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
935 base = 16;
936 nptr += 2;
939 if(base == 0) {
940 if(*nptr=='0')
941 base = 8;
942 else
943 base = 10;
946 while(*nptr) {
947 char cur = tolower(*nptr);
948 int v;
950 if(isdigit(cur)) {
951 if(cur >= '0'+base)
952 break;
953 v = *nptr-'0';
954 } else {
955 if(cur<'a' || cur>='a'+base-10)
956 break;
957 v = cur-'a'+10;
960 nptr++;
962 if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
963 ret = MSVCRT_UI64_MAX;
964 *MSVCRT__errno() = MSVCRT_ERANGE;
965 } else
966 ret = ret*base + v;
969 if(endptr)
970 *endptr = (char*)nptr;
972 return negative ? -ret : ret;
975 /*********************************************************************
976 * _strtoui64 (MSVCRT.@)
978 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
980 return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
983 static int ltoa_helper(MSVCRT_long value, char *str, MSVCRT_size_t size, int radix)
985 MSVCRT_ulong val;
986 unsigned int digit;
987 BOOL is_negative;
988 char buffer[33], *pos;
989 size_t len;
991 if (value < 0 && radix == 10)
993 is_negative = TRUE;
994 val = -value;
996 else
998 is_negative = FALSE;
999 val = value;
1002 pos = buffer + 32;
1003 *pos = '\0';
1007 digit = val % radix;
1008 val /= radix;
1010 if (digit < 10)
1011 *--pos = '0' + digit;
1012 else
1013 *--pos = 'a' + digit - 10;
1015 while (val != 0);
1017 if (is_negative)
1018 *--pos = '-';
1020 len = buffer + 33 - pos;
1021 if (len > size)
1023 size_t i;
1024 char *p = str;
1026 /* Copy the temporary buffer backwards up to the available number of
1027 * characters. Don't copy the negative sign if present. */
1029 if (is_negative)
1031 p++;
1032 size--;
1035 for (pos = buffer + 31, i = 0; i < size; i++)
1036 *p++ = *pos--;
1038 str[0] = '\0';
1039 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1040 return MSVCRT_ERANGE;
1043 memcpy(str, pos, len);
1044 return 0;
1047 /*********************************************************************
1048 * _ltoa_s (MSVCRT.@)
1050 int CDECL _ltoa_s(MSVCRT_long value, char *str, MSVCRT_size_t size, int radix)
1052 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1053 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1054 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1056 str[0] = '\0';
1057 return MSVCRT_EINVAL;
1060 return ltoa_helper(value, str, size, radix);
1063 /*********************************************************************
1064 * _ltow_s (MSVCRT.@)
1066 int CDECL _ltow_s(MSVCRT_long value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1068 MSVCRT_ulong val;
1069 unsigned int digit;
1070 BOOL is_negative;
1071 MSVCRT_wchar_t buffer[33], *pos;
1072 size_t len;
1074 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1075 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1076 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1078 str[0] = '\0';
1079 return MSVCRT_EINVAL;
1082 if (value < 0 && radix == 10)
1084 is_negative = TRUE;
1085 val = -value;
1087 else
1089 is_negative = FALSE;
1090 val = value;
1093 pos = buffer + 32;
1094 *pos = '\0';
1098 digit = val % radix;
1099 val /= radix;
1101 if (digit < 10)
1102 *--pos = '0' + digit;
1103 else
1104 *--pos = 'a' + digit - 10;
1106 while (val != 0);
1108 if (is_negative)
1109 *--pos = '-';
1111 len = buffer + 33 - pos;
1112 if (len > size)
1114 size_t i;
1115 MSVCRT_wchar_t *p = str;
1117 /* Copy the temporary buffer backwards up to the available number of
1118 * characters. Don't copy the negative sign if present. */
1120 if (is_negative)
1122 p++;
1123 size--;
1126 for (pos = buffer + 31, i = 0; i < size; i++)
1127 *p++ = *pos--;
1129 str[0] = '\0';
1130 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1131 return MSVCRT_ERANGE;
1134 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1135 return 0;
1138 /*********************************************************************
1139 * _itoa_s (MSVCRT.@)
1141 int CDECL _itoa_s(int value, char *str, MSVCRT_size_t size, int radix)
1143 return _ltoa_s(value, str, size, radix);
1146 /*********************************************************************
1147 * _itoa (MSVCRT.@)
1149 char* CDECL _itoa(int value, char *str, int radix)
1151 return ltoa_helper(value, str, MSVCRT_SIZE_MAX, radix) ? NULL : str;
1154 /*********************************************************************
1155 * _itow_s (MSVCRT.@)
1157 int CDECL _itow_s(int value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1159 return _ltow_s(value, str, size, radix);
1162 /*********************************************************************
1163 * _ui64toa_s (MSVCRT.@)
1165 int CDECL MSVCRT__ui64toa_s(unsigned __int64 value, char *str,
1166 MSVCRT_size_t size, int radix)
1168 char buffer[65], *pos;
1169 int digit;
1171 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1172 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1173 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1175 str[0] = '\0';
1176 return MSVCRT_EINVAL;
1179 pos = buffer+64;
1180 *pos = '\0';
1182 do {
1183 digit = value%radix;
1184 value /= radix;
1186 if(digit < 10)
1187 *--pos = '0'+digit;
1188 else
1189 *--pos = 'a'+digit-10;
1190 }while(value != 0);
1192 if(buffer-pos+65 > size) {
1193 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL);
1194 return MSVCRT_EINVAL;
1197 memcpy(str, pos, buffer-pos+65);
1198 return 0;
1201 /*********************************************************************
1202 * _ui64tow_s (MSVCRT.@)
1204 int CDECL MSVCRT__ui64tow_s( unsigned __int64 value, MSVCRT_wchar_t *str,
1205 MSVCRT_size_t size, int radix )
1207 MSVCRT_wchar_t buffer[65], *pos;
1208 int digit;
1210 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1211 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1212 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1214 str[0] = '\0';
1215 return MSVCRT_EINVAL;
1218 pos = &buffer[64];
1219 *pos = '\0';
1221 do {
1222 digit = value % radix;
1223 value = value / radix;
1224 if (digit < 10)
1225 *--pos = '0' + digit;
1226 else
1227 *--pos = 'a' + digit - 10;
1228 } while (value != 0);
1230 if(buffer-pos+65 > size) {
1231 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_EINVAL);
1232 return MSVCRT_EINVAL;
1235 memcpy(str, pos, (buffer-pos+65)*sizeof(MSVCRT_wchar_t));
1236 return 0;
1239 /*********************************************************************
1240 * _ultoa_s (MSVCRT.@)
1242 int CDECL _ultoa_s(MSVCRT_ulong value, char *str, MSVCRT_size_t size, int radix)
1244 MSVCRT_ulong digit;
1245 char buffer[33], *pos;
1246 size_t len;
1248 if (!str || !size || radix < 2 || radix > 36)
1250 if (str && size)
1251 str[0] = '\0';
1253 *MSVCRT__errno() = MSVCRT_EINVAL;
1254 return MSVCRT_EINVAL;
1257 pos = buffer + 32;
1258 *pos = '\0';
1262 digit = value % radix;
1263 value /= radix;
1265 if (digit < 10)
1266 *--pos = '0' + digit;
1267 else
1268 *--pos = 'a' + digit - 10;
1270 while (value != 0);
1272 len = buffer + 33 - pos;
1273 if (len > size)
1275 size_t i;
1276 char *p = str;
1278 /* Copy the temporary buffer backwards up to the available number of
1279 * characters. */
1281 for (pos = buffer + 31, i = 0; i < size; i++)
1282 *p++ = *pos--;
1284 str[0] = '\0';
1285 *MSVCRT__errno() = MSVCRT_ERANGE;
1286 return MSVCRT_ERANGE;
1289 memcpy(str, pos, len);
1290 return 0;
1293 /*********************************************************************
1294 * _ultow_s (MSVCRT.@)
1296 int CDECL _ultow_s(MSVCRT_ulong value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1298 MSVCRT_ulong digit;
1299 WCHAR buffer[33], *pos;
1300 size_t len;
1302 if (!str || !size || radix < 2 || radix > 36)
1304 if (str && size)
1305 str[0] = '\0';
1307 *MSVCRT__errno() = MSVCRT_EINVAL;
1308 return MSVCRT_EINVAL;
1311 pos = buffer + 32;
1312 *pos = '\0';
1316 digit = value % radix;
1317 value /= radix;
1319 if (digit < 10)
1320 *--pos = '0' + digit;
1321 else
1322 *--pos = 'a' + digit - 10;
1324 while (value != 0);
1326 len = buffer + 33 - pos;
1327 if (len > size)
1329 size_t i;
1330 WCHAR *p = str;
1332 /* Copy the temporary buffer backwards up to the available number of
1333 * characters. */
1335 for (pos = buffer + 31, i = 0; i < size; i++)
1336 *p++ = *pos--;
1338 str[0] = '\0';
1339 *MSVCRT__errno() = MSVCRT_ERANGE;
1340 return MSVCRT_ERANGE;
1343 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1344 return 0;
1347 /*********************************************************************
1348 * _i64toa_s (MSVCRT.@)
1350 int CDECL _i64toa_s(__int64 value, char *str, MSVCRT_size_t size, int radix)
1352 unsigned __int64 val;
1353 unsigned int digit;
1354 BOOL is_negative;
1355 char buffer[65], *pos;
1356 size_t len;
1358 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1359 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1360 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1362 str[0] = '\0';
1363 return MSVCRT_EINVAL;
1366 if (value < 0 && radix == 10)
1368 is_negative = TRUE;
1369 val = -value;
1371 else
1373 is_negative = FALSE;
1374 val = value;
1377 pos = buffer + 64;
1378 *pos = '\0';
1382 digit = val % radix;
1383 val /= radix;
1385 if (digit < 10)
1386 *--pos = '0' + digit;
1387 else
1388 *--pos = 'a' + digit - 10;
1390 while (val != 0);
1392 if (is_negative)
1393 *--pos = '-';
1395 len = buffer + 65 - pos;
1396 if (len > size)
1398 size_t i;
1399 char *p = str;
1401 /* Copy the temporary buffer backwards up to the available number of
1402 * characters. Don't copy the negative sign if present. */
1404 if (is_negative)
1406 p++;
1407 size--;
1410 for (pos = buffer + 63, i = 0; i < size; i++)
1411 *p++ = *pos--;
1413 str[0] = '\0';
1414 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1415 return MSVCRT_ERANGE;
1418 memcpy(str, pos, len);
1419 return 0;
1422 /*********************************************************************
1423 * _i64tow_s (MSVCRT.@)
1425 int CDECL _i64tow_s(__int64 value, MSVCRT_wchar_t *str, MSVCRT_size_t size, int radix)
1427 unsigned __int64 val;
1428 unsigned int digit;
1429 BOOL is_negative;
1430 MSVCRT_wchar_t buffer[65], *pos;
1431 size_t len;
1433 if (!MSVCRT_CHECK_PMT(str != NULL)) return MSVCRT_EINVAL;
1434 if (!MSVCRT_CHECK_PMT(size > 0)) return MSVCRT_EINVAL;
1435 if (!MSVCRT_CHECK_PMT(radix >= 2 && radix <= 36))
1437 str[0] = '\0';
1438 return MSVCRT_EINVAL;
1441 if (value < 0 && radix == 10)
1443 is_negative = TRUE;
1444 val = -value;
1446 else
1448 is_negative = FALSE;
1449 val = value;
1452 pos = buffer + 64;
1453 *pos = '\0';
1457 digit = val % radix;
1458 val /= radix;
1460 if (digit < 10)
1461 *--pos = '0' + digit;
1462 else
1463 *--pos = 'a' + digit - 10;
1465 while (val != 0);
1467 if (is_negative)
1468 *--pos = '-';
1470 len = buffer + 65 - pos;
1471 if (len > size)
1473 size_t i;
1474 MSVCRT_wchar_t *p = str;
1476 /* Copy the temporary buffer backwards up to the available number of
1477 * characters. Don't copy the negative sign if present. */
1479 if (is_negative)
1481 p++;
1482 size--;
1485 for (pos = buffer + 63, i = 0; i < size; i++)
1486 *p++ = *pos--;
1488 str[0] = '\0';
1489 MSVCRT_INVALID_PMT("str[size] is too small", MSVCRT_ERANGE);
1490 return MSVCRT_ERANGE;
1493 memcpy(str, pos, len * sizeof(MSVCRT_wchar_t));
1494 return 0;
1497 #define I10_OUTPUT_MAX_PREC 21
1498 /* Internal structure used by $I10_OUTPUT */
1499 struct _I10_OUTPUT_DATA {
1500 short pos;
1501 char sign;
1502 BYTE len;
1503 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
1506 /*********************************************************************
1507 * $I10_OUTPUT (MSVCRT.@)
1508 * ld80 - long double (Intel 80 bit FP in 12 bytes) to be printed to data
1509 * prec - precision of part, we're interested in
1510 * flag - 0 for first prec digits, 1 for fractional part
1511 * data - data to be populated
1513 * return value
1514 * 0 if given double is NaN or INF
1515 * 1 otherwise
1517 * FIXME
1518 * Native sets last byte of data->str to '0' or '9', I don't know what
1519 * it means. Current implementation sets it always to '0'.
1521 int CDECL MSVCRT_I10_OUTPUT(MSVCRT__LDOUBLE ld80, int prec, int flag, struct _I10_OUTPUT_DATA *data)
1523 static const char inf_str[] = "1#INF";
1524 static const char nan_str[] = "1#QNAN";
1526 /* MS' long double type wants 12 bytes for Intel's 80 bit FP format.
1527 * Some UNIX have sizeof(long double) == 16, yet only 80 bit are used.
1528 * Assume long double uses 80 bit FP, never seen 128 bit FP. */
1529 long double ld = 0;
1530 double d;
1531 char format[8];
1532 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
1533 char *p;
1535 memcpy(&ld, &ld80, 10);
1536 d = ld;
1537 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
1539 if(d<0) {
1540 data->sign = '-';
1541 d = -d;
1542 } else
1543 data->sign = ' ';
1545 if(isinf(d)) {
1546 data->pos = 1;
1547 data->len = 5;
1548 memcpy(data->str, inf_str, sizeof(inf_str));
1550 return 0;
1553 if(isnan(d)) {
1554 data->pos = 1;
1555 data->len = 6;
1556 memcpy(data->str, nan_str, sizeof(nan_str));
1558 return 0;
1561 if(flag&1) {
1562 int exp = 1+floor(log10(d));
1564 prec += exp;
1565 if(exp < 0)
1566 prec--;
1568 prec--;
1570 if(prec+1 > I10_OUTPUT_MAX_PREC)
1571 prec = I10_OUTPUT_MAX_PREC-1;
1572 else if(prec < 0) {
1573 d = 0.0;
1574 prec = 0;
1577 sprintf(format, "%%.%dle", prec);
1578 sprintf(buf, format, d);
1580 buf[1] = buf[0];
1581 data->pos = atoi(buf+prec+3);
1582 if(buf[1] != '0')
1583 data->pos++;
1585 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
1586 data->len = p-buf;
1588 memcpy(data->str, buf+1, data->len);
1589 data->str[data->len] = '\0';
1591 if(buf[1]!='0' && prec-data->len+1>0)
1592 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
1594 return 1;
1596 #undef I10_OUTPUT_MAX_PREC
1598 /*********************************************************************
1599 * memcmp (MSVCRT.@)
1601 int __cdecl MSVCRT_memcmp(const void *ptr1, const void *ptr2, MSVCRT_size_t n)
1603 return memcmp(ptr1, ptr2, n);
1606 /*********************************************************************
1607 * memcpy (MSVCRT.@)
1609 void * __cdecl MSVCRT_memcpy(void *dst, const void *src, MSVCRT_size_t n)
1611 return memmove(dst, src, n);
1614 /*********************************************************************
1615 * memmove (MSVCRT.@)
1617 void * __cdecl MSVCRT_memmove(void *dst, const void *src, MSVCRT_size_t n)
1619 return memmove(dst, src, n);
1622 /*********************************************************************
1623 * memset (MSVCRT.@)
1625 void* __cdecl MSVCRT_memset(void *dst, int c, MSVCRT_size_t n)
1627 return memset(dst, c, n);
1630 /*********************************************************************
1631 * strchr (MSVCRT.@)
1633 char* __cdecl MSVCRT_strchr(const char *str, int c)
1635 return strchr(str, c);
1638 /*********************************************************************
1639 * strrchr (MSVCRT.@)
1641 char* __cdecl MSVCRT_strrchr(const char *str, int c)
1643 return strrchr(str, c);
1646 /*********************************************************************
1647 * memchr (MSVCRT.@)
1649 void* __cdecl MSVCRT_memchr(const void *ptr, int c, MSVCRT_size_t n)
1651 return memchr(ptr, c, n);
1654 /*********************************************************************
1655 * strcmp (MSVCRT.@)
1657 int __cdecl MSVCRT_strcmp(const char *str1, const char *str2)
1659 return strcmp(str1, str2);
1662 /*********************************************************************
1663 * strncmp (MSVCRT.@)
1665 int __cdecl MSVCRT_strncmp(const char *str1, const char *str2, MSVCRT_size_t len)
1667 return strncmp(str1, str2, len);
1670 /*********************************************************************
1671 * _strnicmp_l (MSVCRT.@)
1673 int __cdecl MSVCRT__strnicmp_l(const char *s1, const char *s2,
1674 MSVCRT_size_t count, MSVCRT__locale_t locale)
1676 MSVCRT_pthreadlocinfo locinfo;
1677 char c1, c2;
1679 if(s1==NULL || s2==NULL)
1680 return MSVCRT__NLSCMPERROR;
1682 if(!count)
1683 return 0;
1685 if(!locale)
1686 locinfo = get_locinfo();
1687 else
1688 locinfo = locale->locinfo;
1690 if(!locinfo->lc_handle[MSVCRT_LC_CTYPE])
1691 return strncasecmp(s1, s2, count);
1693 do {
1694 c1 = MSVCRT__tolower_l(*s1++, locale);
1695 c2 = MSVCRT__tolower_l(*s2++, locale);
1696 }while(--count && c1 && c1==c2);
1698 return c1-c2;
1701 /*********************************************************************
1702 * _stricmp_l (MSVCRT.@)
1704 int __cdecl MSVCRT__stricmp_l(const char *s1, const char *s2, MSVCRT__locale_t locale)
1706 return MSVCRT__strnicmp_l(s1, s2, -1, locale);
1709 /*********************************************************************
1710 * _strnicmp (MSVCRT.@)
1712 int __cdecl MSVCRT__strnicmp(const char *s1, const char *s2, MSVCRT_size_t count)
1714 return MSVCRT__strnicmp_l(s1, s2, count, NULL);
1717 /*********************************************************************
1718 * _stricmp (MSVCRT.@)
1720 int __cdecl MSVCRT__stricmp(const char *s1, const char *s2)
1722 return MSVCRT__strnicmp_l(s1, s2, -1, NULL);
1725 /*********************************************************************
1726 * strstr (MSVCRT.@)
1728 char* __cdecl MSVCRT_strstr(const char *haystack, const char *needle)
1730 return strstr(haystack, needle);