msvcrt: Added _fwscanf_s(_l) implementation.
[wine] / dlls / msvcrt / string.c
1 /*
2  * MSVCRT string functions
3  *
4  * Copyright 1996,1998 Marcus Meissner
5  * Copyright 1996 Jukka Iivonen
6  * Copyright 1997,2000 Uwe Bonnes
7  * Copyright 2000 Jon Griffiths
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #define _ISOC99_SOURCE
25 #include "config.h"
26
27 #include <stdlib.h>
28 #include <errno.h>
29 #include "msvcrt.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
33
34 /*********************************************************************
35  *              _mbsdup (MSVCRT.@)
36  *              _strdup (MSVCRT.@)
37  */
38 char* CDECL _strdup(const char* str)
39 {
40     if(str)
41     {
42       char * ret = MSVCRT_malloc(strlen(str)+1);
43       if (ret) strcpy( ret, str );
44       return ret;
45     }
46     else return 0;
47 }
48
49 /*********************************************************************
50  *              _strnset (MSVCRT.@)
51  */
52 char* CDECL MSVCRT__strnset(char* str, int value, MSVCRT_size_t len)
53 {
54   if (len > 0 && str)
55     while (*str && len--)
56       *str++ = value;
57   return str;
58 }
59
60 /*********************************************************************
61  *              _strrev (MSVCRT.@)
62  */
63 char* CDECL _strrev(char* str)
64 {
65   char * p1;
66   char * p2;
67
68   if (str && *str)
69     for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
70     {
71       *p1 ^= *p2;
72       *p2 ^= *p1;
73       *p1 ^= *p2;
74     }
75
76   return str;
77 }
78
79 /*********************************************************************
80  *              _strset (MSVCRT.@)
81  */
82 char* CDECL _strset(char* str, int value)
83 {
84   char *ptr = str;
85   while (*ptr)
86     *ptr++ = value;
87
88   return str;
89 }
90
91 /*********************************************************************
92  *              strtok  (MSVCRT.@)
93  */
94 char * CDECL MSVCRT_strtok( char *str, const char *delim )
95 {
96     thread_data_t *data = msvcrt_get_thread_data();
97     char *ret;
98
99     if (!str)
100         if (!(str = data->strtok_next)) return NULL;
101
102     while (*str && strchr( delim, *str )) str++;
103     if (!*str) return NULL;
104     ret = str++;
105     while (*str && !strchr( delim, *str )) str++;
106     if (*str) *str++ = 0;
107     data->strtok_next = str;
108     return ret;
109 }
110
111
112 /*********************************************************************
113  *              _swab (MSVCRT.@)
114  */
115 void CDECL MSVCRT__swab(char* src, char* dst, int len)
116 {
117   if (len > 1)
118   {
119     len = (unsigned)len >> 1;
120
121     while (len--) {
122       char s0 = src[0];
123       char s1 = src[1];
124       *dst++ = s1;
125       *dst++ = s0;
126       src = src + 2;
127     }
128   }
129 }
130
131 /*********************************************************************
132  *              atof  (MSVCRT.@)
133  */
134 double CDECL MSVCRT_atof( const char *str )
135 {
136     return atof( str );
137 }
138
139 /*********************************************************************
140  *              strtod_l  (MSVCRT.@)
141  */
142 double CDECL MSVCRT_strtod_l( const char *str, char **end, MSVCRT__locale_t locale)
143 {
144     const char *p, *dec_point=NULL, *exp=NULL;
145     char *copy;
146     double ret;
147     int err = errno;
148
149     if(!locale)
150         locale = get_locale();
151
152     if(!str) {
153         MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
154         *MSVCRT__errno() = MSVCRT_EINVAL;
155         return 0;
156     }
157
158     /* FIXME: use *_l functions */
159     p = str;
160     while(isspace(*p))
161         p++;
162     if(*p=='+' || *p=='-')
163         p++;
164     while(isdigit(*p))
165         p++;
166     if(*p == *locale->locinfo->lconv->decimal_point) {
167         if(*p!='.')
168             dec_point = p;
169         p++;
170     }
171     while(isdigit(*p))
172         p++;
173     if(*p=='d' || *p=='D')
174         exp = p;
175
176     /* FIXME: don't copy input string */
177     if((dec_point || exp) && (copy=_strdup(str))) {
178         if(dec_point)
179             copy[dec_point-str] = '.';
180
181         if(exp)
182             copy[exp-str] = 'e';
183
184         ret = strtod(copy, end);
185         if(end)
186             *end = (char*)str+(*end-copy);
187
188         MSVCRT_free(copy);
189     } else
190         ret = strtod(str, end);
191
192     if(err != errno)
193         *MSVCRT__errno() = errno;
194
195     return ret;
196
197 }
198
199 /*********************************************************************
200  *              strtod  (MSVCRT.@)
201  */
202 double CDECL MSVCRT_strtod( const char *str, char **end )
203 {
204     return MSVCRT_strtod_l( str, end, NULL );
205 }
206
207 /*********************************************************************
208  *              strcoll (MSVCRT.@)
209  */
210 int CDECL MSVCRT_strcoll( const char* str1, const char* str2 )
211 {
212     /* FIXME: handle Windows locale */
213     return strcoll( str1, str2 );
214 }
215
216 /*********************************************************************
217  *      strcpy_s (MSVCRT.@)
218  */
219 int CDECL MSVCRT_strcpy_s( char* dst, MSVCRT_size_t elem, const char* src )
220 {
221     MSVCRT_size_t i;
222     if(!elem) return MSVCRT_EINVAL;
223     if(!dst) return MSVCRT_EINVAL;
224     if(!src)
225     {
226         dst[0] = '\0';
227         return MSVCRT_EINVAL;
228     }
229
230     for(i = 0; i < elem; i++)
231     {
232         if((dst[i] = src[i]) == '\0') return 0;
233     }
234     dst[0] = '\0';
235     return MSVCRT_ERANGE;
236 }
237
238 /*********************************************************************
239  *      strcat_s (MSVCRT.@)
240  */
241 int CDECL MSVCRT_strcat_s( char* dst, MSVCRT_size_t elem, const char* src )
242 {
243     MSVCRT_size_t i, j;
244     if(!dst) return MSVCRT_EINVAL;
245     if(elem == 0) return MSVCRT_EINVAL;
246     if(!src)
247     {
248         dst[0] = '\0';
249         return MSVCRT_EINVAL;
250     }
251
252     for(i = 0; i < elem; i++)
253     {
254         if(dst[i] == '\0')
255         {
256             for(j = 0; (j + i) < elem; j++)
257             {
258                 if((dst[j + i] = src[j]) == '\0') return 0;
259             }
260         }
261     }
262     /* Set the first element to 0, not the first element after the skipped part */
263     dst[0] = '\0';
264     return MSVCRT_ERANGE;
265 }
266
267 /*********************************************************************
268  *              strxfrm (MSVCRT.@)
269  */
270 MSVCRT_size_t CDECL MSVCRT_strxfrm( char *dest, const char *src, MSVCRT_size_t len )
271 {
272     /* FIXME: handle Windows locale */
273     return strxfrm( dest, src, len );
274 }
275
276 /*********************************************************************
277  *              _stricoll (MSVCRT.@)
278  */
279 int CDECL MSVCRT__stricoll( const char* str1, const char* str2 )
280 {
281   /* FIXME: handle collates */
282   TRACE("str1 %s str2 %s\n", debugstr_a(str1), debugstr_a(str2));
283   return lstrcmpiA( str1, str2 );
284 }
285
286 /********************************************************************
287  *              _atoldbl (MSVCRT.@)
288  */
289 int CDECL MSVCRT__atoldbl(MSVCRT__LDOUBLE *value, const char *str)
290 {
291   /* FIXME needs error checking for huge/small values */
292 #ifdef HAVE_STRTOLD
293   TRACE("str %s value %p\n",str,value);
294   value->x = strtold(str,0);
295 #else
296   FIXME("stub, str %s value %p\n",str,value);
297 #endif
298   return 0;
299 }
300
301 /********************************************************************
302  *              __STRINGTOLD (MSVCRT.@)
303  */
304 int CDECL __STRINGTOLD( MSVCRT__LDOUBLE *value, char **endptr, const char *str, int flags )
305 {
306 #ifdef HAVE_STRTOLD
307     FIXME("%p %p %s %x partial stub\n", value, endptr, str, flags );
308     value->x = strtold(str,endptr);
309 #else
310     FIXME("%p %p %s %x stub\n", value, endptr, str, flags );
311 #endif
312     return 0;
313 }
314
315 /******************************************************************
316  *              strtol (MSVCRT.@)
317  */
318 MSVCRT_long CDECL MSVCRT_strtol(const char* nptr, char** end, int base)
319 {
320     /* wrapper to forward libc error code to msvcrt's error codes */
321     long ret;
322
323     errno = 0;
324     ret = strtol(nptr, end, base);
325     switch (errno)
326     {
327     case ERANGE:        *MSVCRT__errno() = MSVCRT_ERANGE;       break;
328     case EINVAL:        *MSVCRT__errno() = MSVCRT_EINVAL;       break;
329     default:
330         /* cope with the fact that we may use 64bit long integers on libc
331          * while msvcrt always uses 32bit long integers
332          */
333         if (ret > MSVCRT_LONG_MAX)
334         {
335             ret = MSVCRT_LONG_MAX;
336             *MSVCRT__errno() = MSVCRT_ERANGE;
337         }
338         else if (ret < -MSVCRT_LONG_MAX - 1)
339         {
340             ret = -MSVCRT_LONG_MAX - 1;
341             *MSVCRT__errno() = MSVCRT_ERANGE;
342         }
343         break;
344     }
345
346     return ret;
347 }
348
349 /******************************************************************
350  *              strtoul (MSVCRT.@)
351  */
352 MSVCRT_ulong CDECL MSVCRT_strtoul(const char* nptr, char** end, int base)
353 {
354     /* wrapper to forward libc error code to msvcrt's error codes */
355     unsigned long ret;
356
357     errno = 0;
358     ret = strtoul(nptr, end, base);
359     switch (errno)
360     {
361     case ERANGE:        *MSVCRT__errno() = MSVCRT_ERANGE;       break;
362     case EINVAL:        *MSVCRT__errno() = MSVCRT_EINVAL;       break;
363     default:
364         /* cope with the fact that we may use 64bit long integers on libc
365          * while msvcrt always uses 32bit long integers
366          */
367         if (ret > MSVCRT_ULONG_MAX)
368         {
369             ret = MSVCRT_ULONG_MAX;
370             *MSVCRT__errno() = MSVCRT_ERANGE;
371         }
372         break;
373     }
374
375     return ret;
376 }
377
378 /******************************************************************
379  *              strnlen (MSVCRT.@)
380  */
381 MSVCRT_size_t CDECL MSVCRT_strnlen(const char *s, MSVCRT_size_t maxlen)
382 {
383     MSVCRT_size_t i;
384
385     for(i=0; i<maxlen; i++)
386         if(!s[i]) break;
387
388     return i;
389 }
390
391 /*********************************************************************
392  *  _strtoi64_l (MSVCR90.@)
393  *
394  * FIXME: locale parameter is ignored
395  */
396 __int64 CDECL MSVCRT_strtoi64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
397 {
398     BOOL negative = FALSE;
399     __int64 ret = 0;
400
401     TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
402
403     if(!nptr || base<0 || base>36 || base==1) {
404         MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
405         return 0;
406     }
407
408     while(isspace(*nptr)) nptr++;
409
410     if(*nptr == '-') {
411         negative = TRUE;
412         nptr++;
413     } else if(*nptr == '+')
414         nptr++;
415
416     if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
417         base = 16;
418         nptr += 2;
419     }
420
421     if(base == 0) {
422         if(*nptr=='0')
423             base = 8;
424         else
425             base = 10;
426     }
427
428     while(*nptr) {
429         char cur = tolower(*nptr);
430         int v;
431
432         if(isdigit(cur)) {
433             if(cur >= '0'+base)
434                 break;
435             v = cur-'0';
436         } else {
437             if(cur<'a' || cur>='a'+base-10)
438                 break;
439             v = cur-'a'+10;
440         }
441
442         if(negative)
443             v = -v;
444
445         nptr++;
446
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;
453         } else
454             ret = ret*base + v;
455     }
456
457     if(endptr)
458         *endptr = (char*)nptr;
459
460     return ret;
461 }
462
463 /*********************************************************************
464  *  _strtoi64 (MSVCR90.@)
465  */
466 __int64 CDECL MSVCRT_strtoi64(const char *nptr, char **endptr, int base)
467 {
468     return MSVCRT_strtoi64_l(nptr, endptr, base, NULL);
469 }
470
471 /*********************************************************************
472  *  _strtoui64_l (MSVCR90.@)
473  *
474  * FIXME: locale parameter is ignored
475  */
476 unsigned __int64 CDECL MSVCRT_strtoui64_l(const char *nptr, char **endptr, int base, MSVCRT__locale_t locale)
477 {
478     BOOL negative = FALSE;
479     unsigned __int64 ret = 0;
480
481     TRACE("(%s %p %d %p)\n", nptr, endptr, base, locale);
482
483     if(!nptr || base<0 || base>36 || base==1) {
484         MSVCRT__invalid_parameter(NULL, NULL, NULL, 0, 0);
485         return 0;
486     }
487
488     while(isspace(*nptr)) nptr++;
489
490     if(*nptr == '-') {
491         negative = TRUE;
492         nptr++;
493     } else if(*nptr == '+')
494         nptr++;
495
496     if((base==0 || base==16) && *nptr=='0' && tolower(*(nptr+1))=='x') {
497         base = 16;
498         nptr += 2;
499     }
500
501     if(base == 0) {
502         if(*nptr=='0')
503             base = 8;
504         else
505             base = 10;
506     }
507
508     while(*nptr) {
509         char cur = tolower(*nptr);
510         int v;
511
512         if(isdigit(cur)) {
513             if(cur >= '0'+base)
514                 break;
515             v = *nptr-'0';
516         } else {
517             if(cur<'a' || cur>='a'+base-10)
518                 break;
519             v = cur-'a'+10;
520         }
521
522         nptr++;
523
524         if(ret>MSVCRT_UI64_MAX/base || ret*base>MSVCRT_UI64_MAX-v) {
525             ret = MSVCRT_UI64_MAX;
526             *MSVCRT__errno() = ERANGE;
527         } else
528             ret = ret*base + v;
529     }
530
531     if(endptr)
532         *endptr = (char*)nptr;
533
534     return negative ? -ret : ret;
535 }
536
537 /*********************************************************************
538  *  _strtoui64 (MSVCR90.@)
539  */
540 unsigned __int64 CDECL MSVCRT_strtoui64(const char *nptr, char **endptr, int base)
541 {
542     return MSVCRT_strtoui64_l(nptr, endptr, base, NULL);
543 }