qmgr: Remove separate release helpers.
[wine] / dlls / msvcrt / scanf.c
1 /*
2  * general implementation of scanf used by scanf, sscanf, fscanf,
3  * _cscanf, wscanf, swscanf and fwscanf
4  *
5  * Copyright 1996,1998 Marcus Meissner
6  * Copyright 1996 Jukka Iivonen
7  * Copyright 1997,2000 Uwe Bonnes
8  * Copyright 2000 Jon Griffiths
9  * Copyright 2002 Daniel Gudbjartsson
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25
26 #include <stdarg.h>
27 #include <limits.h>
28 #include <math.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winternl.h"
33 #include "msvcrt.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37
38 extern MSVCRT_FILE MSVCRT__iob[];
39
40 /* helper function for *scanf.  Returns the value of character c in the
41  * given base, or -1 if the given character is not a digit of the base.
42  */
43 static int char2digit(char c, int base) {
44     if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
45     if (base<=10) return -1;
46     if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
47     if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
48     return -1;
49 }
50
51 /* helper function for *wscanf.  Returns the value of character c in the
52  * given base, or -1 if the given character is not a digit of the base.
53  */
54 static int wchar2digit(MSVCRT_wchar_t c, int base) {
55     if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
56     if (base<=10) return -1;
57     if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
58     if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
59     return -1;
60 }
61
62 /* vfscanf_l */
63 #undef WIDE_SCANF
64 #undef CONSOLE
65 #undef STRING
66 #undef SECURE
67 #include "scanf.h"
68
69 /* vfscanf_l */
70 #define SECURE 1
71 #include "scanf.h"
72
73 /* vfwscanf_l */
74 #define WIDE_SCANF 1
75 #undef CONSOLE
76 #undef STRING
77 #undef SECURE
78 #include "scanf.h"
79
80 /* vfwscanf_s_l */
81 #define SECURE 1
82 #include "scanf.h"
83
84 /* vsscanf_l */
85 #undef WIDE_SCANF
86 #undef CONSOLE
87 #define STRING 1
88 #undef SECURE
89 #include "scanf.h"
90
91 /* vsscanf_s_l */
92 #define SECURE 1
93 #include "scanf.h"
94
95 /* vsnscanf_l */
96 #undef SECURE
97 #define STRING_LEN 1
98 #include "scanf.h"
99
100 /* vsnscanf_s_l */
101 #define SECURE
102 #include "scanf.h"
103
104 /* vsnwscanf_l */
105 #define WIDE_SCANF 1
106 #undef SECURE
107 #include "scanf.h"
108
109 /* vsnwscanf_s_l */
110 #define SECURE 1
111 #include "scanf.h"
112 #undef STRING_LEN
113
114 /* vswscanf_l */
115 #define WIDE_SCANF 1
116 #undef CONSOLE
117 #define STRING 1
118 #undef SECURE
119 #include "scanf.h"
120
121 /* vswscanf_s_l */
122 #define SECURE 1
123 #include "scanf.h"
124
125 /* vcscanf_l */
126 #undef WIDE_SCANF
127 #define CONSOLE 1
128 #undef STRING
129 #undef SECURE
130 #include "scanf.h"
131
132 /* vcscanf_s_l */
133 #define SECURE 1
134 #include "scanf.h"
135
136 /* vcwscanf_l */
137 #define WIDE_SCANF 1
138 #define CONSOLE 1
139 #undef STRING
140 #undef SECURE
141 #include "scanf.h"
142
143 /* vcwscanf_s_l */
144 #define SECURE 1
145 #include "scanf.h"
146
147
148 /*********************************************************************
149  *              fscanf (MSVCRT.@)
150  */
151 int CDECL MSVCRT_fscanf(MSVCRT_FILE *file, const char *format, ...)
152 {
153     __ms_va_list valist;
154     int res;
155
156     __ms_va_start(valist, format);
157     res = MSVCRT_vfscanf_l(file, format, NULL, valist);
158     __ms_va_end(valist);
159     return res;
160 }
161
162 /*********************************************************************
163  *              _fscanf_l (MSVCRT.@)
164  */
165 int CDECL MSVCRT__fscanf_l(MSVCRT_FILE *file, const char *format,
166         MSVCRT__locale_t locale, ...)
167 {
168     __ms_va_list valist;
169     int res;
170
171     __ms_va_start(valist, locale);
172     res = MSVCRT_vfscanf_l(file, format, locale, valist);
173     __ms_va_end(valist);
174     return res;
175 }
176
177 /*********************************************************************
178  *              fscanf_s (MSVCRT.@)
179  */
180 int CDECL MSVCRT_fscanf_s(MSVCRT_FILE *file, const char *format, ...)
181 {
182     __ms_va_list valist;
183     int res;
184
185     __ms_va_start(valist, format);
186     res = MSVCRT_vfscanf_s_l(file, format, NULL, valist);
187     __ms_va_end(valist);
188     return res;
189 }
190
191 /*********************************************************************
192  *              _fscanf_s_l (MSVCRT.@)
193  */
194 int CDECL MSVCRT__fscanf_s_l(MSVCRT_FILE *file, const char *format,
195         MSVCRT__locale_t locale, ...)
196 {
197     __ms_va_list valist;
198     int res;
199
200     __ms_va_start(valist, locale);
201     res = MSVCRT_vfscanf_s_l(file, format, locale, valist);
202     __ms_va_end(valist);
203     return res;
204 }
205
206 /*********************************************************************
207  *              scanf (MSVCRT.@)
208  */
209 int CDECL MSVCRT_scanf(const char *format, ...)
210 {
211     __ms_va_list valist;
212     int res;
213
214     __ms_va_start(valist, format);
215     res = MSVCRT_vfscanf_l(MSVCRT_stdin, format, NULL, valist);
216     __ms_va_end(valist);
217     return res;
218 }
219
220 /*********************************************************************
221  *              _scanf_l (MSVCRT.@)
222  */
223 int CDECL MSVCRT__scanf_l(const char *format, MSVCRT__locale_t locale, ...)
224 {
225     __ms_va_list valist;
226     int res;
227
228     __ms_va_start(valist, locale);
229     res = MSVCRT_vfscanf_l(MSVCRT_stdin, format, locale, valist);
230     __ms_va_end(valist);
231     return res;
232 }
233
234 /*********************************************************************
235  *              scanf_s (MSVCRT.@)
236  */
237 int CDECL MSVCRT_scanf_s(const char *format, ...)
238 {
239     __ms_va_list valist;
240     int res;
241
242     __ms_va_start(valist, format);
243     res = MSVCRT_vfscanf_s_l(MSVCRT_stdin, format, NULL, valist);
244     __ms_va_end(valist);
245     return res;
246 }
247
248 /*********************************************************************
249  *              _scanf_s_l (MSVCRT.@)
250  */
251 int CDECL MSVCRT__scanf_s_l(const char *format, MSVCRT__locale_t locale, ...)
252 {
253     __ms_va_list valist;
254     int res;
255
256     __ms_va_start(valist, locale);
257     res = MSVCRT_vfscanf_s_l(MSVCRT_stdin, format, locale, valist);
258     __ms_va_end(valist);
259     return res;
260 }
261
262 /*********************************************************************
263  *              fwscanf (MSVCRT.@)
264  */
265 int CDECL MSVCRT_fwscanf(MSVCRT_FILE *file, const MSVCRT_wchar_t *format, ...)
266 {
267     __ms_va_list valist;
268     int res;
269
270     __ms_va_start(valist, format);
271     res = MSVCRT_vfwscanf_l(file, format, NULL, valist);
272     __ms_va_end(valist);
273     return res;
274 }
275
276 /*********************************************************************
277  *              _fwscanf_l (MSVCRT.@)
278  */
279 int CDECL MSVCRT__fwscanf_l(MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
280         MSVCRT__locale_t locale, ...)
281 {
282     __ms_va_list valist;
283     int res;
284
285     __ms_va_start(valist, locale);
286     res = MSVCRT_vfwscanf_l(file, format, locale, valist);
287     __ms_va_end(valist);
288     return res;
289 }
290
291 /*********************************************************************
292  *              fwscanf_s (MSVCRT.@)
293  */
294 int CDECL MSVCRT_fwscanf_s(MSVCRT_FILE *file, const MSVCRT_wchar_t *format, ...)
295 {
296     __ms_va_list valist;
297     int res;
298
299     __ms_va_start(valist, format);
300     res = MSVCRT_vfwscanf_s_l(file, format, NULL, valist);
301     __ms_va_end(valist);
302     return res;
303 }
304
305 /*********************************************************************
306  *              _fwscanf_s_l (MSVCRT.@)
307  */
308 int CDECL MSVCRT__fwscanf_s_l(MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
309         MSVCRT__locale_t locale, ...)
310 {
311     __ms_va_list valist;
312     int res;
313
314     __ms_va_start(valist, locale);
315     res = MSVCRT_vfwscanf_s_l(file, format, locale, valist);
316     __ms_va_end(valist);
317     return res;
318 }
319
320 /*********************************************************************
321  *              wscanf (MSVCRT.@)
322  */
323 int CDECL MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...)
324 {
325     __ms_va_list valist;
326     int res;
327
328     __ms_va_start(valist, format);
329     res = MSVCRT_vfwscanf_l(MSVCRT_stdin, format, NULL, valist);
330     __ms_va_end(valist);
331     return res;
332 }
333
334 /*********************************************************************
335  *              _wscanf_l (MSVCRT.@)
336  */
337 int CDECL MSVCRT__wscanf_l(const MSVCRT_wchar_t *format,
338         MSVCRT__locale_t locale, ...)
339 {
340     __ms_va_list valist;
341     int res;
342
343     __ms_va_start(valist, locale);
344     res = MSVCRT_vfwscanf_l(MSVCRT_stdin, format, locale, valist);
345     __ms_va_end(valist);
346     return res;
347 }
348
349 /*********************************************************************
350  *              wscanf_s (MSVCRT.@)
351  */
352 int CDECL MSVCRT_wscanf_s(const MSVCRT_wchar_t *format, ...)
353 {
354     __ms_va_list valist;
355     int res;
356
357     __ms_va_start(valist, format);
358     res = MSVCRT_vfwscanf_s_l(MSVCRT_stdin, format, NULL, valist);
359     __ms_va_end(valist);
360     return res;
361 }
362
363 /*********************************************************************
364  *              _wscanf_s_l (MSVCRT.@)
365  */
366 int CDECL MSVCRT__wscanf_s_l(const MSVCRT_wchar_t *format,
367         MSVCRT__locale_t locale, ...)
368 {
369     __ms_va_list valist;
370     int res;
371
372     __ms_va_start(valist, locale);
373     res = MSVCRT_vfwscanf_s_l(MSVCRT_stdin, format, locale, valist);
374     __ms_va_end(valist);
375     return res;
376 }
377
378 /*********************************************************************
379  *              sscanf (MSVCRT.@)
380  */
381 int CDECL MSVCRT_sscanf(const char *str, const char *format, ...)
382 {
383     __ms_va_list valist;
384     int res;
385
386     __ms_va_start(valist, format);
387     res = MSVCRT_vsscanf_l(str, format, NULL, valist);
388     __ms_va_end(valist);
389     return res;
390 }
391
392 /*********************************************************************
393  *              _sscanf_l (MSVCRT.@)
394  */
395 int CDECL MSVCRT__sscanf_l(const char *str, const char *format,
396         MSVCRT__locale_t locale, ...)
397 {
398     __ms_va_list valist;
399     int res;
400
401     __ms_va_start(valist, locale);
402     res = MSVCRT_vsscanf_l(str, format, locale, valist);
403     __ms_va_end(valist);
404     return res;
405 }
406
407 /*********************************************************************
408  *              sscanf_s (MSVCRT.@)
409  */
410 int CDECL MSVCRT_sscanf_s(const char *str, const char *format, ...)
411 {
412     __ms_va_list valist;
413     int res;
414
415     __ms_va_start(valist, format);
416     res = MSVCRT_vsscanf_s_l(str, format, NULL, valist);
417     __ms_va_end(valist);
418     return res;
419 }
420
421 /*********************************************************************
422  *              _sscanf_s_l (MSVCRT.@)
423  */
424 int CDECL MSVCRT__sscanf_s_l(const char *str, const char *format,
425         MSVCRT__locale_t locale, ...)
426 {
427     __ms_va_list valist;
428     int res;
429
430     __ms_va_start(valist, locale);
431     res = MSVCRT_vsscanf_s_l(str, format, locale, valist);
432     __ms_va_end(valist);
433     return res;
434 }
435
436 /*********************************************************************
437  *              swscanf (MSVCRT.@)
438  */
439 int CDECL MSVCRT_swscanf(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ...)
440 {
441     __ms_va_list valist;
442     int res;
443
444     __ms_va_start(valist, format);
445     res = MSVCRT_vswscanf_l(str, format, NULL, valist);
446     __ms_va_end(valist);
447     return res;
448 }
449
450 /*********************************************************************
451  *              _swscanf_l (MSVCRT.@)
452  */
453 int CDECL MSVCRT__swscanf_l(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
454         MSVCRT__locale_t locale, ...)
455 {
456     __ms_va_list valist;
457     int res;
458
459     __ms_va_start(valist, locale);
460     res = MSVCRT_vswscanf_l(str, format, locale, valist);
461     __ms_va_end(valist);
462     return res;
463 }
464
465 /*********************************************************************
466  *              swscanf_s (MSVCRT.@)
467  */
468 int CDECL MSVCRT_swscanf_s(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ...)
469 {
470     __ms_va_list valist;
471     int res;
472
473     __ms_va_start(valist, format);
474     res = MSVCRT_vswscanf_s_l(str, format, NULL, valist);
475     __ms_va_end(valist);
476     return res;
477 }
478
479 /*********************************************************************
480  *              _swscanf_s_l (MSVCRT.@)
481  */
482 int CDECL MSVCRT__swscanf_s_l(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
483         MSVCRT__locale_t locale, ...)
484 {
485     __ms_va_list valist;
486     int res;
487
488     __ms_va_start(valist, locale);
489     res = MSVCRT_vswscanf_s_l(str, format, locale, valist);
490     __ms_va_end(valist);
491     return res;
492 }
493
494 /*********************************************************************
495  *              _cscanf (MSVCRT.@)
496  */
497 int CDECL _cscanf(const char *format, ...)
498 {
499     __ms_va_list valist;
500     int res;
501
502     __ms_va_start(valist, format);
503     res = MSVCRT_vcscanf_l(format, NULL, valist);
504     __ms_va_end(valist);
505     return res;
506 }
507
508 /*********************************************************************
509  *              _cscanf_l (MSVCRT.@)
510  */
511 int CDECL _cscanf_l(const char *format, MSVCRT__locale_t locale, ...)
512 {
513     __ms_va_list valist;
514     int res;
515
516     __ms_va_start(valist, locale);
517     res = MSVCRT_vcscanf_l(format, locale, valist);
518     __ms_va_end(valist);
519     return res;
520 }
521
522 /*********************************************************************
523  *              _cscanf_s (MSVCRT.@)
524  */
525 int CDECL _cscanf_s(const char *format, ...)
526 {
527     __ms_va_list valist;
528     int res;
529
530     __ms_va_start(valist, format);
531     res = MSVCRT_vcscanf_s_l(format, NULL, valist);
532     __ms_va_end(valist);
533     return res;
534 }
535
536 /*********************************************************************
537  *              _cscanf_s_l (MSVCRT.@)
538  */
539 int CDECL _cscanf_s_l(const char *format, MSVCRT__locale_t locale, ...)
540 {
541     __ms_va_list valist;
542     int res;
543
544     __ms_va_start(valist, locale);
545     res = MSVCRT_vcscanf_s_l(format, locale, valist);
546     __ms_va_end(valist);
547     return res;
548 }
549
550 /*********************************************************************
551  *              _cwscanf (MSVCRT.@)
552  */
553 int CDECL _cwscanf(const char *format, ...)
554 {
555     __ms_va_list valist;
556     int res;
557
558     __ms_va_start(valist, format);
559     res = MSVCRT_vcwscanf_l(format, NULL, valist);
560     __ms_va_end(valist);
561     return res;
562 }
563
564 /*********************************************************************
565  *              _cwscanf_l (MSVCRT.@)
566  */
567 int CDECL _cwscanf_l(const char *format, MSVCRT__locale_t locale, ...)
568 {
569     __ms_va_list valist;
570     int res;
571
572     __ms_va_start(valist, locale);
573     res = MSVCRT_vcwscanf_l(format, locale, valist);
574     __ms_va_end(valist);
575     return res;
576 }
577
578 /*********************************************************************
579  *              _cwscanf_s (MSVCRT.@)
580  */
581 int CDECL _cwscanf_s(const char *format, ...)
582 {
583     __ms_va_list valist;
584     int res;
585
586     __ms_va_start(valist, format);
587     res = MSVCRT_vcwscanf_s_l(format, NULL, valist);
588     __ms_va_end(valist);
589     return res;
590 }
591
592 /*********************************************************************
593  *              _cwscanf_s_l (MSVCRT.@)
594  */
595 int CDECL _cwscanf_s_l(const char *format, MSVCRT__locale_t locale, ...)
596 {
597     __ms_va_list valist;
598     int res;
599
600     __ms_va_start(valist, locale);
601     res = MSVCRT_vcwscanf_s_l(format, locale, valist);
602     __ms_va_end(valist);
603     return res;
604 }
605
606 /*********************************************************************
607  *              _snscanf (MSVCRT.@)
608  */
609 int CDECL MSVCRT__snscanf(char *input, MSVCRT_size_t length, const char *format, ...)
610 {
611     __ms_va_list valist;
612     int res;
613
614     __ms_va_start(valist, format);
615     res = MSVCRT_vsnscanf_l(input, length, format, NULL, valist);
616     __ms_va_end(valist);
617     return res;
618 }
619
620 /*********************************************************************
621  *              _snscanf_l (MSVCRT.@)
622  */
623 int CDECL MSVCRT__snscanf_l(char *input, MSVCRT_size_t length,
624         const char *format, MSVCRT__locale_t locale, ...)
625 {
626     __ms_va_list valist;
627     int res;
628
629     __ms_va_start(valist, locale);
630     res = MSVCRT_vsnscanf_l(input, length, format, locale, valist);
631     __ms_va_end(valist);
632     return res;
633 }
634
635 /*********************************************************************
636  *              _snscanf_s (MSVCRT.@)
637  */
638 int CDECL MSVCRT__snscanf_s(char *input, MSVCRT_size_t length, const char *format, ...)
639 {
640     __ms_va_list valist;
641     int res;
642
643     __ms_va_start(valist, format);
644     res = MSVCRT_vsnscanf_s_l(input, length, format, NULL, valist);
645     __ms_va_end(valist);
646     return res;
647 }
648
649 /*********************************************************************
650  *              _snscanf_s_l (MSVCRT.@)
651  */
652 int CDECL MSVCRT__snscanf_s_l(char *input, MSVCRT_size_t length,
653         const char *format, MSVCRT__locale_t locale, ...)
654 {
655     __ms_va_list valist;
656     int res;
657
658     __ms_va_start(valist, locale);
659     res = MSVCRT_vsnscanf_s_l(input, length, format, locale, valist);
660     __ms_va_end(valist);
661     return res;
662 }
663
664 /*********************************************************************
665  *              _snwscanf (MSVCRT.@)
666  */
667 int CDECL MSVCRT__snwscanf(MSVCRT_wchar_t *input, MSVCRT_size_t length,
668         const MSVCRT_wchar_t *format, ...)
669 {
670     __ms_va_list valist;
671     int res;
672
673     __ms_va_start(valist, format);
674     res = MSVCRT_vsnwscanf_l(input, length, format, NULL, valist);
675     __ms_va_end(valist);
676     return res;
677 }
678
679 /*********************************************************************
680  *              _snwscanf_l (MSVCRT.@)
681  */
682 int CDECL MSVCRT__snwscanf_l(MSVCRT_wchar_t *input, MSVCRT_size_t length,
683         const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
684 {
685     __ms_va_list valist;
686     int res;
687
688     __ms_va_start(valist, locale);
689     res = MSVCRT_vsnwscanf_l(input, length, format, locale, valist);
690     __ms_va_end(valist);
691     return res;
692 }
693
694 /*********************************************************************
695  *              _snwscanf_s (MSVCRT.@)
696  */
697 int CDECL MSVCRT__snwscanf_s(MSVCRT_wchar_t *input, MSVCRT_size_t length,
698         const MSVCRT_wchar_t *format, ...)
699 {
700     __ms_va_list valist;
701     int res;
702
703     __ms_va_start(valist, format);
704     res = MSVCRT_vsnwscanf_s_l(input, length, format, NULL, valist);
705     __ms_va_end(valist);
706     return res;
707 }
708
709 /*********************************************************************
710  *              _snscanf_s_l (MSVCRT.@)
711  */
712 int CDECL MSVCRT__snwscanf_s_l(MSVCRT_wchar_t *input, MSVCRT_size_t length,
713         const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
714 {
715     __ms_va_list valist;
716     int res;
717
718     __ms_va_start(valist, locale);
719     res = MSVCRT_vsnwscanf_s_l(input, length, format, locale, valist);
720     __ms_va_end(valist);
721     return res;
722 }