scanf.c 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * general implementation of scanf used by scanf, sscanf, fscanf,
 * _cscanf, wscanf, swscanf and fwscanf
 *
 * Copyright 1996,1998 Marcus Meissner
 * Copyright 1996 Jukka Iivonen
 * Copyright 1997,2000 Uwe Bonnes
 * Copyright 2000 Jon Griffiths
 * Copyright 2002 Daniel Gudbjartsson
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 29

#include "windef.h"
30
#include "winbase.h"
31
#include "winternl.h"
32 33 34 35 36
#include "msvcrt.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);

37 38
extern MSVCRT_FILE MSVCRT__iob[];

39 40 41 42 43 44 45 46 47 48 49 50 51 52
/* helper function for *scanf.  Returns the value of character c in the
 * given base, or -1 if the given character is not a digit of the base.
 */
static int char2digit(char c, int base) {
    if ((c>='0') && (c<='9') && (c<='0'+base-1)) return (c-'0');
    if (base<=10) return -1;
    if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
    if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
    return -1;
}

/* helper function for *wscanf.  Returns the value of character c in the
 * 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 57
    if ((c>='A') && (c<='Z') && (c<='A'+base-11)) return (c-'A'+10);
    if ((c>='a') && (c<='z') && (c<='a'+base-11)) return (c-'a'+10);
58 59 60
    return -1;
}

61
/* vfscanf_l */
62 63 64
#undef WIDE_SCANF
#undef CONSOLE
#undef STRING
65
#undef SECURE
66 67
#include "scanf.h"

68
/* vfscanf_l */
69 70 71
#define SECURE 1
#include "scanf.h"

72
/* vfwscanf_l */
73 74 75
#define WIDE_SCANF 1
#undef CONSOLE
#undef STRING
76
#undef SECURE
77 78
#include "scanf.h"

79 80 81 82
/* vfwscanf_s_l */
#define SECURE 1
#include "scanf.h"

83
/* vsscanf_l */
84 85 86
#undef WIDE_SCANF
#undef CONSOLE
#define STRING 1
87
#undef SECURE
88 89
#include "scanf.h"

90 91 92 93
/* vsscanf_s_l */
#define SECURE 1
#include "scanf.h"

94
/* vswscanf_l */
95 96 97
#define WIDE_SCANF 1
#undef CONSOLE
#define STRING 1
98
#undef SECURE
99 100
#include "scanf.h"

101 102 103 104
/* vswscanf_s_l */
#define SECURE 1
#include "scanf.h"

105
/* vcscanf_l */
106 107 108
#undef WIDE_SCANF
#define CONSOLE 1
#undef STRING
109
#undef SECURE
110
#include "scanf.h"
111

112 113 114 115
/* vcscanf_s_l */
#define SECURE 1
#include "scanf.h"

116 117 118 119 120 121 122 123 124 125 126
/* vcwscanf_l */
#define WIDE_SCANF 1
#define CONSOLE 1
#undef STRING
#undef SECURE
#include "scanf.h"

/* vcwscanf_s_l */
#define SECURE 1
#include "scanf.h"

127 128 129 130

/*********************************************************************
 *		fscanf (MSVCRT.@)
 */
131
int CDECL MSVCRT_fscanf(MSVCRT_FILE *file, const char *format, ...)
132
{
133
    __ms_va_list valist;
134 135
    int res;

136
    __ms_va_start(valist, format);
137
    res = MSVCRT_vfscanf_l(file, format, NULL, valist);
138
    __ms_va_end(valist);
139
    return res;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}

/*********************************************************************
 *		_fscanf_l (MSVCRT.@)
 */
int CDECL MSVCRT__fscanf_l(MSVCRT_FILE *file, const char *format,
        MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vfscanf_l(file, format, locale, valist);
    __ms_va_end(valist);
    return res;
155 156
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*********************************************************************
 *		fscanf_s (MSVCRT.@)
 */
int CDECL MSVCRT_fscanf_s(MSVCRT_FILE *file, const char *format, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, format);
    res = MSVCRT_vfscanf_s_l(file, format, NULL, valist);
    __ms_va_end(valist);
    return res;
}

/*********************************************************************
 *		_fscanf_s_l (MSVCRT.@)
 */
int CDECL MSVCRT__fscanf_s_l(MSVCRT_FILE *file, const char *format,
        MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vfscanf_s_l(file, format, locale, valist);
    __ms_va_end(valist);
    return res;
}

186 187 188
/*********************************************************************
 *		scanf (MSVCRT.@)
 */
189
int CDECL MSVCRT_scanf(const char *format, ...)
190
{
191
    __ms_va_list valist;
192 193
    int res;

194
    __ms_va_start(valist, format);
195
    res = MSVCRT_vfscanf_l(MSVCRT_stdin, format, NULL, valist);
196
    __ms_va_end(valist);
197 198 199
    return res;
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213
/*********************************************************************
 *		_scanf_l (MSVCRT.@)
 */
int CDECL MSVCRT__scanf_l(const char *format, MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vfscanf_l(MSVCRT_stdin, format, locale, valist);
    __ms_va_end(valist);
    return res;
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
/*********************************************************************
 *		scanf_s (MSVCRT.@)
 */
int CDECL MSVCRT_scanf_s(const char *format, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, format);
    res = MSVCRT_vfscanf_s_l(MSVCRT_stdin, format, NULL, valist);
    __ms_va_end(valist);
    return res;
}

/*********************************************************************
 *		_scanf_s_l (MSVCRT.@)
 */
int CDECL MSVCRT__scanf_s_l(const char *format, MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vfscanf_s_l(MSVCRT_stdin, format, locale, valist);
    __ms_va_end(valist);
    return res;
}

242 243 244
/*********************************************************************
 *		fwscanf (MSVCRT.@)
 */
245
int CDECL MSVCRT_fwscanf(MSVCRT_FILE *file, const MSVCRT_wchar_t *format, ...)
246
{
247
    __ms_va_list valist;
248 249
    int res;

250
    __ms_va_start(valist, format);
251
    res = MSVCRT_vfwscanf_l(file, format, NULL, valist);
252
    __ms_va_end(valist);
253 254 255
    return res;
}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
/*********************************************************************
 *		_fwscanf_l (MSVCRT.@)
 */
int CDECL MSVCRT__fwscanf_l(MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
        MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vfwscanf_l(file, format, locale, valist);
    __ms_va_end(valist);
    return res;
}

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
/*********************************************************************
 *		fwscanf_s (MSVCRT.@)
 */
int CDECL MSVCRT_fwscanf_s(MSVCRT_FILE *file, const MSVCRT_wchar_t *format, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, format);
    res = MSVCRT_vfwscanf_s_l(file, format, NULL, valist);
    __ms_va_end(valist);
    return res;
}

/*********************************************************************
 *		_fwscanf_s_l (MSVCRT.@)
 */
int CDECL MSVCRT__fwscanf_s_l(MSVCRT_FILE *file, const MSVCRT_wchar_t *format,
        MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vfwscanf_s_l(file, format, locale, valist);
    __ms_va_end(valist);
    return res;
}

300 301 302
/*********************************************************************
 *		wscanf (MSVCRT.@)
 */
303
int CDECL MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...)
304
{
305
    __ms_va_list valist;
306 307
    int res;

308
    __ms_va_start(valist, format);
309
    res = MSVCRT_vfwscanf_l(MSVCRT_stdin, format, NULL, valist);
310
    __ms_va_end(valist);
311 312 313
    return res;
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327
/*********************************************************************
 *		_wscanf_l (MSVCRT.@)
 */
int CDECL MSVCRT__wscanf_l(const MSVCRT_wchar_t *format,
        MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vfwscanf_l(MSVCRT_stdin, format, locale, valist);
    __ms_va_end(valist);
    return res;
}
328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
/*********************************************************************
 *		wscanf_s (MSVCRT.@)
 */
int CDECL MSVCRT_wscanf_s(const MSVCRT_wchar_t *format, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, format);
    res = MSVCRT_vfwscanf_s_l(MSVCRT_stdin, format, NULL, valist);
    __ms_va_end(valist);
    return res;
}

/*********************************************************************
 *		_wscanf_s_l (MSVCRT.@)
 */
int CDECL MSVCRT__wscanf_s_l(const MSVCRT_wchar_t *format,
        MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vfwscanf_s_l(MSVCRT_stdin, format, locale, valist);
    __ms_va_end(valist);
    return res;
}

358 359 360
/*********************************************************************
 *		sscanf (MSVCRT.@)
 */
361
int CDECL MSVCRT_sscanf(const char *str, const char *format, ...)
362
{
363
    __ms_va_list valist;
364 365
    int res;

366
    __ms_va_start(valist, format);
367
    res = MSVCRT_vsscanf_l(str, format, NULL, valist);
368
    __ms_va_end(valist);
369 370 371
    return res;
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385
/*********************************************************************
 *		_sscanf_l (MSVCRT.@)
 */
int CDECL MSVCRT__sscanf_l(const char *str, const char *format,
        MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vsscanf_l(str, format, locale, valist);
    __ms_va_end(valist);
    return res;
}
386

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
/*********************************************************************
 *		sscanf_s (MSVCRT.@)
 */
int CDECL MSVCRT_sscanf_s(const char *str, const char *format, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, format);
    res = MSVCRT_vsscanf_s_l(str, format, NULL, valist);
    __ms_va_end(valist);
    return res;
}

/*********************************************************************
 *		_sscanf_s_l (MSVCRT.@)
 */
int CDECL MSVCRT__sscanf_s_l(const char *str, const char *format,
        MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vsscanf_s_l(str, format, locale, valist);
    __ms_va_end(valist);
    return res;
}

416 417 418
/*********************************************************************
 *		swscanf (MSVCRT.@)
 */
419
int CDECL MSVCRT_swscanf(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ...)
420
{
421
    __ms_va_list valist;
422 423
    int res;

424
    __ms_va_start(valist, format);
425
    res = MSVCRT_vswscanf_l(str, format, NULL, valist);
426
    __ms_va_end(valist);
427 428 429
    return res;
}

430 431 432 433 434 435 436 437 438 439 440 441 442 443
/*********************************************************************
 *		_swscanf_l (MSVCRT.@)
 */
int CDECL MSVCRT__swscanf_l(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
        MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vswscanf_l(str, format, locale, valist);
    __ms_va_end(valist);
    return res;
}
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
/*********************************************************************
 *		swscanf_s (MSVCRT.@)
 */
int CDECL MSVCRT_swscanf_s(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, format);
    res = MSVCRT_vswscanf_s_l(str, format, NULL, valist);
    __ms_va_end(valist);
    return res;
}

/*********************************************************************
 *		_swscanf_s_l (MSVCRT.@)
 */
int CDECL MSVCRT__swscanf_s_l(const MSVCRT_wchar_t *str, const MSVCRT_wchar_t *format,
        MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vswscanf_s_l(str, format, locale, valist);
    __ms_va_end(valist);
    return res;
}

474 475 476
/*********************************************************************
 *		_cscanf (MSVCRT.@)
 */
477
int CDECL _cscanf(const char *format, ...)
478
{
479
    __ms_va_list valist;
480 481
    int res;

482
    __ms_va_start(valist, format);
483
    res = MSVCRT_vcscanf_l(format, NULL, valist);
484
    __ms_va_end(valist);
485 486
    return res;
}
487 488 489 490 491 492 493 494 495 496 497 498 499 500

/*********************************************************************
 *		_cscanf_l (MSVCRT.@)
 */
int CDECL _cscanf_l(const char *format, MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vcscanf_l(format, locale, valist);
    __ms_va_end(valist);
    return res;
}
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

/*********************************************************************
 *		_cscanf_s (MSVCRT.@)
 */
int CDECL _cscanf_s(const char *format, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, format);
    res = MSVCRT_vcscanf_s_l(format, NULL, valist);
    __ms_va_end(valist);
    return res;
}

/*********************************************************************
 *		_cscanf_s_l (MSVCRT.@)
 */
int CDECL _cscanf_s_l(const char *format, MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vcscanf_s_l(format, locale, valist);
    __ms_va_end(valist);
    return res;
}
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

/*********************************************************************
 *		_cwscanf (MSVCRT.@)
 */
int CDECL _cwscanf(const char *format, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, format);
    res = MSVCRT_vcwscanf_l(format, NULL, valist);
    __ms_va_end(valist);
    return res;
}

/*********************************************************************
 *		_cwscanf_l (MSVCRT.@)
 */
int CDECL _cwscanf_l(const char *format, MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vcwscanf_l(format, locale, valist);
    __ms_va_end(valist);
    return res;
}

/*********************************************************************
 *		_cwscanf_s (MSVCRT.@)
 */
int CDECL _cwscanf_s(const char *format, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, format);
    res = MSVCRT_vcwscanf_s_l(format, NULL, valist);
    __ms_va_end(valist);
    return res;
}

/*********************************************************************
 *		_cwscanf_s_l (MSVCRT.@)
 */
int CDECL _cwscanf_s_l(const char *format, MSVCRT__locale_t locale, ...)
{
    __ms_va_list valist;
    int res;

    __ms_va_start(valist, locale);
    res = MSVCRT_vcwscanf_s_l(format, locale, valist);
    __ms_va_end(valist);
    return res;
}