Release 1.5.13.
[wine.git] / dlls / msvcrt / scanf.c
blobb998400b0ef5df0c72d7130a4d91309c8629ec8e
1 /*
2 * general implementation of scanf used by scanf, sscanf, fscanf,
3 * _cscanf, wscanf, swscanf and fwscanf
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
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.
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.
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
26 #include <stdarg.h>
27 #include <limits.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winternl.h"
32 #include "msvcrt.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
37 extern MSVCRT_FILE MSVCRT__iob[];
39 /* helper function for *scanf. Returns the value of character c in the
40 * given base, or -1 if the given character is not a digit of the base.
42 static int char2digit(char c, int base) {
43 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
44 if (base<=10) return -1;
45 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
46 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
47 return -1;
50 /* helper function for *wscanf. Returns the value of character c in the
51 * given base, or -1 if the given character is not a digit of the base.
53 static int wchar2digit(MSVCRT_wchar_t c, int base) {
54 if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
55 if (base<=10) return -1;
56 if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
57 if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
58 return -1;
61 /* vfscanf_l */
62 #undef WIDE_SCANF
63 #undef CONSOLE
64 #undef STRING
65 #undef SECURE
66 #include "scanf.h"
68 /* vfscanf_l */
69 #define SECURE 1
70 #include "scanf.h"
72 /* vfwscanf_l */
73 #define WIDE_SCANF 1
74 #undef CONSOLE
75 #undef STRING
76 #undef SECURE
77 #include "scanf.h"
79 /* vfwscanf_s_l */
80 #define SECURE 1
81 #include "scanf.h"
83 /* vsscanf_l */
84 #undef WIDE_SCANF
85 #undef CONSOLE
86 #define STRING 1
87 #undef SECURE
88 #include "scanf.h"
90 /* vsscanf_s_l */
91 #define SECURE 1
92 #include "scanf.h"
94 /* vsnscanf_l */
95 #undef SECURE
96 #define STRING_LEN 1
97 #include "scanf.h"
99 /* vsnscanf_s_l */
100 #define SECURE
101 #include "scanf.h"
103 /* vsnwscanf_l */
104 #define WIDE_SCANF 1
105 #undef SECURE
106 #include "scanf.h"
108 /* vsnwscanf_s_l */
109 #define SECURE 1
110 #include "scanf.h"
111 #undef STRING_LEN
113 /* vswscanf_l */
114 #define WIDE_SCANF 1
115 #undef CONSOLE
116 #define STRING 1
117 #undef SECURE
118 #include "scanf.h"
120 /* vswscanf_s_l */
121 #define SECURE 1
122 #include "scanf.h"
124 /* vcscanf_l */
125 #undef WIDE_SCANF
126 #define CONSOLE 1
127 #undef STRING
128 #undef SECURE
129 #include "scanf.h"
131 /* vcscanf_s_l */
132 #define SECURE 1
133 #include "scanf.h"
135 /* vcwscanf_l */
136 #define WIDE_SCANF 1
137 #define CONSOLE 1
138 #undef STRING
139 #undef SECURE
140 #include "scanf.h"
142 /* vcwscanf_s_l */
143 #define SECURE 1
144 #include "scanf.h"
147 /*********************************************************************
148 * fscanf (MSVCRT.@)
150 int CDECL MSVCRT_fscanf(MSVCRT_FILE *file, const char *format, ...)
152 __ms_va_list valist;
153 int res;
155 __ms_va_start(valist, format);
156 res = MSVCRT_vfscanf_l(file, format, NULL, valist);
157 __ms_va_end(valist);
158 return res;
161 /*********************************************************************
162 * _fscanf_l (MSVCRT.@)
164 int CDECL MSVCRT__fscanf_l(MSVCRT_FILE *file, const char *format,
165 MSVCRT__locale_t locale, ...)
167 __ms_va_list valist;
168 int res;
170 __ms_va_start(valist, locale);
171 res = MSVCRT_vfscanf_l(file, format, locale, valist);
172 __ms_va_end(valist);
173 return res;
176 /*********************************************************************
177 * fscanf_s (MSVCRT.@)
179 int CDECL MSVCRT_fscanf_s(MSVCRT_FILE *file, const char *format, ...)
181 __ms_va_list valist;
182 int res;
184 __ms_va_start(valist, format);
185 res = MSVCRT_vfscanf_s_l(file, format, NULL, valist);
186 __ms_va_end(valist);
187 return res;
190 /*********************************************************************
191 * _fscanf_s_l (MSVCRT.@)
193 int CDECL MSVCRT__fscanf_s_l(MSVCRT_FILE *file, const char *format,
194 MSVCRT__locale_t locale, ...)
196 __ms_va_list valist;
197 int res;
199 __ms_va_start(valist, locale);
200 res = MSVCRT_vfscanf_s_l(file, format, locale, valist);
201 __ms_va_end(valist);
202 return res;
205 /*********************************************************************
206 * scanf (MSVCRT.@)
208 int CDECL MSVCRT_scanf(const char *format, ...)
210 __ms_va_list valist;
211 int res;
213 __ms_va_start(valist, format);
214 res = MSVCRT_vfscanf_l(MSVCRT_stdin, format, NULL, valist);
215 __ms_va_end(valist);
216 return res;
219 /*********************************************************************
220 * _scanf_l (MSVCRT.@)
222 int CDECL MSVCRT__scanf_l(const char *format, MSVCRT__locale_t locale, ...)
224 __ms_va_list valist;
225 int res;
227 __ms_va_start(valist, locale);
228 res = MSVCRT_vfscanf_l(MSVCRT_stdin, format, locale, valist);
229 __ms_va_end(valist);
230 return res;
233 /*********************************************************************
234 * scanf_s (MSVCRT.@)
236 int CDECL MSVCRT_scanf_s(const char *format, ...)
238 __ms_va_list valist;
239 int res;
241 __ms_va_start(valist, format);
242 res = MSVCRT_vfscanf_s_l(MSVCRT_stdin, format, NULL, valist);
243 __ms_va_end(valist);
244 return res;
247 /*********************************************************************
248 * _scanf_s_l (MSVCRT.@)
250 int CDECL MSVCRT__scanf_s_l(const char *format, MSVCRT__locale_t locale, ...)
252 __ms_va_list valist;
253 int res;
255 __ms_va_start(valist, locale);
256 res = MSVCRT_vfscanf_s_l(MSVCRT_stdin, format, locale, valist);
257 __ms_va_end(valist);
258 return res;
261 /*********************************************************************
262 * fwscanf (MSVCRT.@)
264 int CDECL MSVCRT_fwscanf(MSVCRT_FILE *file, const MSVCRT_wchar_t *format, ...)
266 __ms_va_list valist;
267 int res;
269 __ms_va_start(valist, format);
270 res = MSVCRT_vfwscanf_l(file, format, NULL, valist);
271 __ms_va_end(valist);
272 return res;
275 /*********************************************************************
276 * _fwscanf_l (MSVCRT.@)
278 int CDECL MSVCRT__fwscanf_l(MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
279 MSVCRT__locale_t locale, ...)
281 __ms_va_list valist;
282 int res;
284 __ms_va_start(valist, locale);
285 res = MSVCRT_vfwscanf_l(file, format, locale, valist);
286 __ms_va_end(valist);
287 return res;
290 /*********************************************************************
291 * fwscanf_s (MSVCRT.@)
293 int CDECL MSVCRT_fwscanf_s(MSVCRT_FILE *file, const MSVCRT_wchar_t *format, ...)
295 __ms_va_list valist;
296 int res;
298 __ms_va_start(valist, format);
299 res = MSVCRT_vfwscanf_s_l(file, format, NULL, valist);
300 __ms_va_end(valist);
301 return res;
304 /*********************************************************************
305 * _fwscanf_s_l (MSVCRT.@)
307 int CDECL MSVCRT__fwscanf_s_l(MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
308 MSVCRT__locale_t locale, ...)
310 __ms_va_list valist;
311 int res;
313 __ms_va_start(valist, locale);
314 res = MSVCRT_vfwscanf_s_l(file, format, locale, valist);
315 __ms_va_end(valist);
316 return res;
319 /*********************************************************************
320 * wscanf (MSVCRT.@)
322 int CDECL MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...)
324 __ms_va_list valist;
325 int res;
327 __ms_va_start(valist, format);
328 res = MSVCRT_vfwscanf_l(MSVCRT_stdin, format, NULL, valist);
329 __ms_va_end(valist);
330 return res;
333 /*********************************************************************
334 * _wscanf_l (MSVCRT.@)
336 int CDECL MSVCRT__wscanf_l(const MSVCRT_wchar_t *format,
337 MSVCRT__locale_t locale, ...)
339 __ms_va_list valist;
340 int res;
342 __ms_va_start(valist, locale);
343 res = MSVCRT_vfwscanf_l(MSVCRT_stdin, format, locale, valist);
344 __ms_va_end(valist);
345 return res;
348 /*********************************************************************
349 * wscanf_s (MSVCRT.@)
351 int CDECL MSVCRT_wscanf_s(const MSVCRT_wchar_t *format, ...)
353 __ms_va_list valist;
354 int res;
356 __ms_va_start(valist, format);
357 res = MSVCRT_vfwscanf_s_l(MSVCRT_stdin, format, NULL, valist);
358 __ms_va_end(valist);
359 return res;
362 /*********************************************************************
363 * _wscanf_s_l (MSVCRT.@)
365 int CDECL MSVCRT__wscanf_s_l(const MSVCRT_wchar_t *format,
366 MSVCRT__locale_t locale, ...)
368 __ms_va_list valist;
369 int res;
371 __ms_va_start(valist, locale);
372 res = MSVCRT_vfwscanf_s_l(MSVCRT_stdin, format, locale, valist);
373 __ms_va_end(valist);
374 return res;
377 /*********************************************************************
378 * sscanf (MSVCRT.@)
380 int CDECL MSVCRT_sscanf(const char *str, const char *format, ...)
382 __ms_va_list valist;
383 int res;
385 __ms_va_start(valist, format);
386 res = MSVCRT_vsscanf_l(str, format, NULL, valist);
387 __ms_va_end(valist);
388 return res;
391 /*********************************************************************
392 * _sscanf_l (MSVCRT.@)
394 int CDECL MSVCRT__sscanf_l(const char *str, const char *format,
395 MSVCRT__locale_t locale, ...)
397 __ms_va_list valist;
398 int res;
400 __ms_va_start(valist, locale);
401 res = MSVCRT_vsscanf_l(str, format, locale, valist);
402 __ms_va_end(valist);
403 return res;
406 /*********************************************************************
407 * sscanf_s (MSVCRT.@)
409 int CDECL MSVCRT_sscanf_s(const char *str, const char *format, ...)
411 __ms_va_list valist;
412 int res;
414 __ms_va_start(valist, format);
415 res = MSVCRT_vsscanf_s_l(str, format, NULL, valist);
416 __ms_va_end(valist);
417 return res;
420 /*********************************************************************
421 * _sscanf_s_l (MSVCRT.@)
423 int CDECL MSVCRT__sscanf_s_l(const char *str, const char *format,
424 MSVCRT__locale_t locale, ...)
426 __ms_va_list valist;
427 int res;
429 __ms_va_start(valist, locale);
430 res = MSVCRT_vsscanf_s_l(str, format, locale, valist);
431 __ms_va_end(valist);
432 return res;
435 /*********************************************************************
436 * swscanf (MSVCRT.@)
438 int CDECL MSVCRT_swscanf(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ...)
440 __ms_va_list valist;
441 int res;
443 __ms_va_start(valist, format);
444 res = MSVCRT_vswscanf_l(str, format, NULL, valist);
445 __ms_va_end(valist);
446 return res;
449 /*********************************************************************
450 * _swscanf_l (MSVCRT.@)
452 int CDECL MSVCRT__swscanf_l(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
453 MSVCRT__locale_t locale, ...)
455 __ms_va_list valist;
456 int res;
458 __ms_va_start(valist, locale);
459 res = MSVCRT_vswscanf_l(str, format, locale, valist);
460 __ms_va_end(valist);
461 return res;
464 /*********************************************************************
465 * swscanf_s (MSVCRT.@)
467 int CDECL MSVCRT_swscanf_s(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ...)
469 __ms_va_list valist;
470 int res;
472 __ms_va_start(valist, format);
473 res = MSVCRT_vswscanf_s_l(str, format, NULL, valist);
474 __ms_va_end(valist);
475 return res;
478 /*********************************************************************
479 * _swscanf_s_l (MSVCRT.@)
481 int CDECL MSVCRT__swscanf_s_l(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
482 MSVCRT__locale_t locale, ...)
484 __ms_va_list valist;
485 int res;
487 __ms_va_start(valist, locale);
488 res = MSVCRT_vswscanf_s_l(str, format, locale, valist);
489 __ms_va_end(valist);
490 return res;
493 /*********************************************************************
494 * _cscanf (MSVCRT.@)
496 int CDECL _cscanf(const char *format, ...)
498 __ms_va_list valist;
499 int res;
501 __ms_va_start(valist, format);
502 res = MSVCRT_vcscanf_l(format, NULL, valist);
503 __ms_va_end(valist);
504 return res;
507 /*********************************************************************
508 * _cscanf_l (MSVCRT.@)
510 int CDECL _cscanf_l(const char *format, MSVCRT__locale_t locale, ...)
512 __ms_va_list valist;
513 int res;
515 __ms_va_start(valist, locale);
516 res = MSVCRT_vcscanf_l(format, locale, valist);
517 __ms_va_end(valist);
518 return res;
521 /*********************************************************************
522 * _cscanf_s (MSVCRT.@)
524 int CDECL _cscanf_s(const char *format, ...)
526 __ms_va_list valist;
527 int res;
529 __ms_va_start(valist, format);
530 res = MSVCRT_vcscanf_s_l(format, NULL, valist);
531 __ms_va_end(valist);
532 return res;
535 /*********************************************************************
536 * _cscanf_s_l (MSVCRT.@)
538 int CDECL _cscanf_s_l(const char *format, MSVCRT__locale_t locale, ...)
540 __ms_va_list valist;
541 int res;
543 __ms_va_start(valist, locale);
544 res = MSVCRT_vcscanf_s_l(format, locale, valist);
545 __ms_va_end(valist);
546 return res;
549 /*********************************************************************
550 * _cwscanf (MSVCRT.@)
552 int CDECL _cwscanf(const char *format, ...)
554 __ms_va_list valist;
555 int res;
557 __ms_va_start(valist, format);
558 res = MSVCRT_vcwscanf_l(format, NULL, valist);
559 __ms_va_end(valist);
560 return res;
563 /*********************************************************************
564 * _cwscanf_l (MSVCRT.@)
566 int CDECL _cwscanf_l(const char *format, MSVCRT__locale_t locale, ...)
568 __ms_va_list valist;
569 int res;
571 __ms_va_start(valist, locale);
572 res = MSVCRT_vcwscanf_l(format, locale, valist);
573 __ms_va_end(valist);
574 return res;
577 /*********************************************************************
578 * _cwscanf_s (MSVCRT.@)
580 int CDECL _cwscanf_s(const char *format, ...)
582 __ms_va_list valist;
583 int res;
585 __ms_va_start(valist, format);
586 res = MSVCRT_vcwscanf_s_l(format, NULL, valist);
587 __ms_va_end(valist);
588 return res;
591 /*********************************************************************
592 * _cwscanf_s_l (MSVCRT.@)
594 int CDECL _cwscanf_s_l(const char *format, MSVCRT__locale_t locale, ...)
596 __ms_va_list valist;
597 int res;
599 __ms_va_start(valist, locale);
600 res = MSVCRT_vcwscanf_s_l(format, locale, valist);
601 __ms_va_end(valist);
602 return res;
605 /*********************************************************************
606 * _snscanf (MSVCRT.@)
608 int CDECL MSVCRT__snscanf(char *input, MSVCRT_size_t length, const char *format, ...)
610 __ms_va_list valist;
611 int res;
613 __ms_va_start(valist, format);
614 res = MSVCRT_vsnscanf_l(input, length, format, NULL, valist);
615 __ms_va_end(valist);
616 return res;
619 /*********************************************************************
620 * _snscanf_l (MSVCRT.@)
622 int CDECL MSVCRT__snscanf_l(char *input, MSVCRT_size_t length,
623 const char *format, MSVCRT__locale_t locale, ...)
625 __ms_va_list valist;
626 int res;
628 __ms_va_start(valist, locale);
629 res = MSVCRT_vsnscanf_l(input, length, format, locale, valist);
630 __ms_va_end(valist);
631 return res;
634 /*********************************************************************
635 * _snscanf_s (MSVCRT.@)
637 int CDECL MSVCRT__snscanf_s(char *input, MSVCRT_size_t length, const char *format, ...)
639 __ms_va_list valist;
640 int res;
642 __ms_va_start(valist, format);
643 res = MSVCRT_vsnscanf_s_l(input, length, format, NULL, valist);
644 __ms_va_end(valist);
645 return res;
648 /*********************************************************************
649 * _snscanf_s_l (MSVCRT.@)
651 int CDECL MSVCRT__snscanf_s_l(char *input, MSVCRT_size_t length,
652 const char *format, MSVCRT__locale_t locale, ...)
654 __ms_va_list valist;
655 int res;
657 __ms_va_start(valist, locale);
658 res = MSVCRT_vsnscanf_s_l(input, length, format, locale, valist);
659 __ms_va_end(valist);
660 return res;
663 /*********************************************************************
664 * _snwscanf (MSVCRT.@)
666 int CDECL MSVCRT__snwscanf(MSVCRT_wchar_t *input, MSVCRT_size_t length,
667 const MSVCRT_wchar_t *format, ...)
669 __ms_va_list valist;
670 int res;
672 __ms_va_start(valist, format);
673 res = MSVCRT_vsnwscanf_l(input, length, format, NULL, valist);
674 __ms_va_end(valist);
675 return res;
678 /*********************************************************************
679 * _snwscanf_l (MSVCRT.@)
681 int CDECL MSVCRT__snwscanf_l(MSVCRT_wchar_t *input, MSVCRT_size_t length,
682 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
684 __ms_va_list valist;
685 int res;
687 __ms_va_start(valist, locale);
688 res = MSVCRT_vsnwscanf_l(input, length, format, locale, valist);
689 __ms_va_end(valist);
690 return res;
693 /*********************************************************************
694 * _snwscanf_s (MSVCRT.@)
696 int CDECL MSVCRT__snwscanf_s(MSVCRT_wchar_t *input, MSVCRT_size_t length,
697 const MSVCRT_wchar_t *format, ...)
699 __ms_va_list valist;
700 int res;
702 __ms_va_start(valist, format);
703 res = MSVCRT_vsnwscanf_s_l(input, length, format, NULL, valist);
704 __ms_va_end(valist);
705 return res;
708 /*********************************************************************
709 * _snscanf_s_l (MSVCRT.@)
711 int CDECL MSVCRT__snwscanf_s_l(MSVCRT_wchar_t *input, MSVCRT_size_t length,
712 const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, ...)
714 __ms_va_list valist;
715 int res;
717 __ms_va_start(valist, locale);
718 res = MSVCRT_vsnwscanf_s_l(input, length, format, locale, valist);
719 __ms_va_end(valist);
720 return res;