Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1369 lines
48 KiB

  1. /***
  2. *input.c - C formatted input, used by scanf, etc.
  3. *
  4. * Copyright (c) 1987-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _input() to do formatted input; called from scanf(),
  8. * etc. functions. This module defines _cscanf() instead when
  9. * CPRFLAG is defined. The file cscanf.c defines that symbol
  10. * and then includes this file in order to implement _cscanf().
  11. *
  12. *Revision History:
  13. * 09-26-83 RN author
  14. * 11-01-85 TC added %F? %N? %?p %n %i
  15. * 11-20-86 SKS enlarged "table" to 256 bytes, to support chars > 0x7F
  16. * 12-12-86 SKS changed "s_in()" to pushback whitespace or other delimiter
  17. * 03-24-87 BCM Evaluation Issues:
  18. * SDS - needs #ifdef SS_NE_DS for the "number" buffer
  19. * (for S/M models only)
  20. * GD/TS : (not evaluated)
  21. * other INIT : (not evaluated)
  22. * needs _cfltcvt_init to have been called if
  23. * floating-point i/o conversions are being done
  24. * TERM - nothing
  25. * 06-25-87 PHG added check_stack pragma
  26. * 08-31-87 JCR Made %n conform to ANSI standard: (1) %n is supposed to
  27. * return the # of chars read so far by the current scanf(),
  28. * NOT the total read on the stream since open; (2) %n is NOT
  29. * supposed to affect the # of items read that is returned by
  30. * scanf().
  31. * 09-24-87 JCR Made cscanf() use the va_ macros (fixes cl warnings).
  32. * 11-04-87 JCR Multi-thread support
  33. * 11-16-87 JCR Cscanf() now gets _CONIO_LOCK
  34. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  35. * 02-25-88 JCR If burn() char hits EOF, only return EOF if count==0.
  36. * 05-31-88 WAJ Now suports %Fs and %Ns
  37. * 06-01-88 PHG Merged DLL and normal versions
  38. * 06-08-88 SJM %D no longer means %ld. %[]ABC], %[^]ABC] work.
  39. * 06-14-88 SJM Fixed %p, and %F? and %N? code.
  40. * SJM Complete re-write of input/_input for 6.00
  41. * 09-15-88 JCR If we match a field but it's not assigned, then are
  42. * terminated by EOF, we must return 0 not EOF (ANSI).
  43. * 09-25-88 GJF Initial adaption for the 386
  44. * 10-04-88 JCR 386: Removed 'far' keyword
  45. * 11-30-88 GJF Cleanup, now specific to 386
  46. * 06-09-89 GJF Propagated fixes of 03-06-89 and 04-05-89
  47. * 11-20-89 GJF Added const attribute to type of format. Also, fixed
  48. * copyright.
  49. * 12-21-89 GJF Allow null character in scanset
  50. * 02-14-90 KRS Fix suppressed-assignment pattern matching.
  51. * 03-20-90 GJF Made _cscanf() _CALLTYPE2 and _input() _CALLTYPE1. Added
  52. * #include <cruntime.h> and #include <register.h>.
  53. * 03-26-90 GJF Made static functions _CALLTYPE4. Placed prototype for
  54. * _input() in internal.h and #include-d it. Changed type of
  55. * arglist from void ** to va_list (to get rid of annoying
  56. * warnings). Added #include <string.h>. Elaborated prototypes
  57. * of static functions to get rid of compiler warnings.
  58. * 05-21-90 GJF Fixed stack checking pragma syntax.
  59. * 07-23-90 SBM Compiles cleanly with -W3, replaced <assertm.h> by
  60. * <assert.h>, moved _cfltcvt_tab to new header
  61. * <fltintrn.h>, formerly named <struct.h>
  62. * 08-13-90 SBM Compiles cleanly with -W3 with new build of compiler
  63. * 08-27-90 SBM Minor cleanup to agree with CRT7 version
  64. * 10-02-90 GJF New-style function declarators. Also, rewrote expr. to
  65. * avoid using casts as lvalues.
  66. * 10-22-90 GJF Added arglistsave, used to save and restore arglist pointer
  67. * without using pointer arithmetic.
  68. * 12-28-90 SRW Added _CRUISER_ conditional around check_stack pragma
  69. * 01-16-91 GJF ANSI naming.
  70. * 03-14-91 GJF Fix to allow processing of %n, even at eof. Fix devised by
  71. * DanK of PSS.
  72. * 06-19-91 GJF Fixed execution of string, character and scan-set format
  73. * directives to avoid problem with line-buffered devices
  74. * (C700 bug 1441).
  75. * 10-22-91 ETC Int'l dec point; Under _INTL: wchar_t/mb support; fix bug
  76. * under !ALLOW_RANGE (never compiled).
  77. * 11-15-91 ETC Fixed bug with %f %lf %Lf (bad handling of longone).
  78. * 11-19-91 ETC Added support for _wsscanf with WPRFLAG; added %tc %ts.
  79. * 06-09-92 KRS Rip out %tc/%ts; conform to new ISO spec.
  80. * 08-17-92 KRS Further ISO changes: Add %lc/%ls/%hc/%hs/%C/%S.
  81. * 12-23-92 SKS Needed to handle %*n (suppressed storage of byte count)
  82. * 02-16-93 CFW Added wide character output for [] scanset.
  83. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  84. * 04-26-93 CFW Wide char enable.
  85. * 08-17-93 CFW Avoid mapping tchar macros incorrectly if _MBCS defined.
  86. * 09-15-93 CFW Use ANSI conformant "__" names.
  87. * 11-08-93 GJF Merged in NT SDK version (use __unaligned pointer
  88. * casts on MIPS and Alpha. Also, fixed #elif WPRFLAG to
  89. * be #elif defined(WPRFLAG), and removed old CRUISER
  90. * support.
  91. * 12-16-93 CFW Get rid of spurious compiler warnings.
  92. * 03-15-94 GJF Added support for I64 size modifier.
  93. * 04-21-94 GJF Must reinitialize integer64 flag.
  94. * 09-05-94 SKS Remove include of obsolete 16-bit file <sizeptr.h>
  95. * 12-14-94 GJF Changed test for (hex) digits so that when WPRFLAG is
  96. * defined, only zero-extended (hex) digits are
  97. * recognized. This way, the familar arithmetic to convert
  98. * from character rep. to binary integer value will work.
  99. * 01-10-95 CFW Debug CRT allocs.
  100. * 02-06-94 CFW assert -> _ASSERTE.
  101. * 02-22-95 GJF Appended Mac version of source file (somewhat cleaned
  102. * up), with appropriate #ifdef-s. Also, replaced
  103. * WPRFLAG with _UNICODE.
  104. * 08-01-96 RDK For PMac, added __int64 support for _input.
  105. * 02-27-98 RKP Added 64 bit support.
  106. * 07-07-98 RKP Corrected %P formatting for 64 bit.
  107. * 09-21-98 GJF Added support for %I and %I32 modifiers.
  108. * 05-17-99 PML Remove all Macintosh support.
  109. * 10-28-99 PML vs7#10705 Win64 %p was totally busted
  110. * 04-25-00 GB Adding support for _cwprintf.
  111. * 05-31-00 GB Changed scanf to match with standards. problem was
  112. * reading octal or hexa while %d was specified.
  113. * 10-20-00 GB Changed input not to use %[] case for %c and %s.
  114. * 02-19-01 GB Added check for return value of malloc.
  115. * 03-13-01 PML Fix heap leak on multiple %[] specs (vs7#224990)
  116. * 07-07-01 BWT Fix prefix bug - init pointer to a known value
  117. * when handling * formatting.
  118. * 02-20-02 BWT prefast fixes - don't use alloca
  119. *
  120. *******************************************************************************/
  121. #define ALLOW_RANGE /* allow "%[a-z]"-style scansets */
  122. /* temporary work-around for compiler without 64-bit support */
  123. #ifndef _INTEGRAL_MAX_BITS
  124. #define _INTEGRAL_MAX_BITS 64
  125. #endif
  126. #include <cruntime.h>
  127. #include <stdio.h>
  128. #include <ctype.h>
  129. #include <cvt.h>
  130. #include <conio.h>
  131. #include <stdarg.h>
  132. #include <string.h>
  133. #include <internal.h>
  134. #include <fltintrn.h>
  135. #include <malloc.h>
  136. #include <mtdll.h>
  137. #include <stdlib.h>
  138. #include <nlsint.h>
  139. #include <dbgint.h>
  140. #ifdef _MBCS /* always want either Unicode or SBCS for tchar.h */
  141. #undef _MBCS
  142. #endif
  143. #include <tchar.h>
  144. #if defined(_NTSUBSET_) || defined(_POSIX_)
  145. #if defined (UNICODE)
  146. #define malloc_crt(x) RtlAllocateHeap(RtlProcessHeap(), 0, x)
  147. #define free_crt(x) RtlFreeHeap(RtlProcessHeap(), 0, x)
  148. #define ALLOC_TABLE 1
  149. #else
  150. #define ALLOC_TABLE 0
  151. #endif
  152. #else
  153. #define ALLOC_TABLE 1
  154. #endif
  155. #define HEXTODEC(chr) _hextodec(chr)
  156. #define LEFT_BRACKET ('[' | ('a' - 'A')) /* 'lowercase' version */
  157. #ifdef _UNICODE
  158. static wchar_t __cdecl _hextodec(wchar_t);
  159. #else
  160. static int __cdecl _hextodec(int);
  161. #endif /* _UNICODE */
  162. #ifdef CPRFLAG
  163. #define INC() (++charcount, _inc())
  164. #define UN_INC(chr) (--charcount, _un_inc(chr))
  165. #define EAT_WHITE() _whiteout(&charcount)
  166. #ifndef _UNICODE
  167. static int __cdecl _inc(void);
  168. static void __cdecl _un_inc(int);
  169. static int __cdecl _whiteout(int *);
  170. #else /* _UNICODE */
  171. static wchar_t __cdecl _inc(void);
  172. static void __cdecl _un_inc(wchar_t);
  173. static wchar_t __cdecl _whiteout(int *);
  174. #endif /* _UNICODE */
  175. #else /* CPRFLAG */
  176. #define INC() (++charcount, _inc(stream))
  177. #define UN_INC(chr) (--charcount, _un_inc(chr, stream))
  178. #define EAT_WHITE() _whiteout(&charcount, stream)
  179. #ifndef _UNICODE
  180. static int __cdecl _inc(FILE *);
  181. static void __cdecl _un_inc(int, FILE *);
  182. static int __cdecl _whiteout(int *, FILE *);
  183. #else /* _UNICODE */
  184. static wchar_t __cdecl _inc(FILE *);
  185. static void __cdecl _un_inc(wchar_t, FILE *);
  186. static wchar_t __cdecl _whiteout(int *, FILE *);
  187. #endif /* _UNICODE */
  188. #endif /* CPRFLAG */
  189. #ifndef _UNICODE
  190. #define _ISDIGIT(chr) isdigit(chr)
  191. #define _ISXDIGIT(chr) isxdigit(chr)
  192. #else
  193. #define _ISDIGIT(chr) ( !(chr & 0xff00) && isdigit( ((chr) & 0x00ff) ) )
  194. #define _ISXDIGIT(chr) ( !(chr & 0xff00) && isxdigit( ((chr) & 0x00ff) ) )
  195. #endif
  196. #ifdef CPRFLAG
  197. #ifndef _UNICODE
  198. static int __cdecl input(const unsigned char *, va_list);
  199. #else
  200. static int __cdecl input(const wchar_t *, va_list);
  201. #endif
  202. /***
  203. *int _cscanf(format, arglist) - read formatted input direct from console
  204. *
  205. *Purpose:
  206. * Reads formatted data like scanf, but uses console I/O functions.
  207. *
  208. *Entry:
  209. * char *format - format string to determine data formats
  210. * arglist - list of POINTERS to where to put data
  211. *
  212. *Exit:
  213. * returns number of successfully matched data items (from input)
  214. *
  215. *Exceptions:
  216. *
  217. *******************************************************************************/
  218. #ifndef _UNICODE
  219. int __cdecl _cscanf (
  220. const char *format,
  221. #else /* _UNICODE */
  222. int __cdecl _cwscanf (
  223. const wchar_t *format,
  224. #endif /* _UNICODE */
  225. ...
  226. )
  227. {
  228. va_list arglist;
  229. va_start(arglist, format);
  230. _ASSERTE(format != NULL);
  231. return input(format,arglist); /* get the input */
  232. }
  233. #endif /* CPRFLAG */
  234. #define ASCII 32 /* # of bytes needed to hold 256 bits */
  235. #define SCAN_SHORT 0 /* also for FLOAT */
  236. #define SCAN_LONG 1 /* also for DOUBLE */
  237. #define SCAN_L_DOUBLE 2 /* only for LONG DOUBLE */
  238. #define SCAN_NEAR 0
  239. #define SCAN_FAR 1
  240. #ifndef _UNICODE
  241. #define TABLESIZE ASCII
  242. #else
  243. #define TABLESIZE (ASCII * 256)
  244. #endif
  245. /***
  246. *int _input(stream, format, arglist), static int input(format, arglist)
  247. *
  248. *Purpose:
  249. * get input items (data items or literal matches) from the input stream
  250. * and assign them if appropriate to the items thru the arglist. this
  251. * function is intended for internal library use only, not for the user
  252. *
  253. * The _input entry point is for the normal scanf() functions
  254. * The input entry point is used when compiling for _cscanf() [CPRFLAF
  255. * defined] and is a static function called only by _cscanf() -- reads from
  256. * console.
  257. *
  258. *Entry:
  259. * FILE *stream - file to read from
  260. * char *format - format string to determine the data to read
  261. * arglist - list of pointer to data items
  262. *
  263. *Exit:
  264. * returns number of items assigned and fills in data items
  265. * returns EOF if error or EOF found on stream before 1st data item matched
  266. *
  267. *Exceptions:
  268. *
  269. *******************************************************************************/
  270. #ifdef CPRFLAG
  271. #ifndef _UNICODE
  272. static int __cdecl input (
  273. const unsigned char *format,
  274. va_list arglist
  275. )
  276. #else
  277. static int __cdecl input (
  278. const wchar_t *format,
  279. va_list arglist
  280. )
  281. #endif /* _UNICODE */
  282. #else
  283. #if defined(_UNICODE)
  284. int __cdecl _winput (
  285. FILE *stream,
  286. const wchar_t *format,
  287. va_list arglist
  288. )
  289. #else
  290. int __cdecl _input (
  291. FILE *stream,
  292. const unsigned char *format,
  293. va_list arglist
  294. )
  295. #endif /* _UNICODE */
  296. #endif /* CPRFLAG */
  297. {
  298. #ifndef _UNICODE
  299. char floatstring[CVTBUFSIZE + 1]; /* ASCII buffer for floats */
  300. #else
  301. wchar_t floatstring[CVTBUFSIZE + 1];
  302. #endif
  303. unsigned long number; /* temp hold-value */
  304. #if ALLOC_TABLE
  305. char *table = NULL; /* which chars allowed for %[] */
  306. int malloc_flag = 0; /* is "table" allocated on the heap? */
  307. #else
  308. char AsciiTable[TABLESIZE];
  309. char *table = AsciiTable;
  310. #endif
  311. #if _INTEGRAL_MAX_BITS >= 64 /*IFSTRIP=IGN*/
  312. unsigned __int64 num64; /* temp for 64-bit integers */
  313. #endif
  314. void *pointer; /* points to user data receptacle */
  315. void *start; /* indicate non-empty string */
  316. #ifdef _UNICODE
  317. wchar_t *scanptr; /* for building "table" data */
  318. REG2 wchar_t ch = 0;
  319. #else
  320. wchar_t wctemp;
  321. unsigned char *scanptr; /* for building "table" data */
  322. REG2 int ch = 0;
  323. #endif
  324. int charcount; /* total number of chars read */
  325. REG1 int comchr; /* holds designator type */
  326. int count; /* return value. # of assignments */
  327. int started; /* indicate good number */
  328. int width; /* width of field */
  329. int widthset; /* user has specified width */
  330. /* Neither coerceshort nor farone are need for the 386 */
  331. char done_flag; /* general purpose loop monitor */
  332. char longone; /* 0 = SHORT, 1 = LONG, 2 = L_DOUBLE */
  333. #if _INTEGRAL_MAX_BITS >= 64 /*IFSTRIP=IGN*/
  334. int integer64; /* 1 for 64-bit integer, 0 otherwise */
  335. #endif
  336. signed char widechar; /* -1 = char, 0 = ????, 1 = wchar_t */
  337. char reject; /* %[^ABC] instead of %[ABC] */
  338. char negative; /* flag for '-' detected */
  339. char suppress; /* don't assign anything */
  340. char match; /* flag: !0 if any fields matched */
  341. va_list arglistsave; /* save arglist value */
  342. char fl_wchar_arg; /* flags wide char/string argument */
  343. #ifdef _UNICODE
  344. #ifdef ALLOW_RANGE
  345. wchar_t rngch; /* used while scanning range */
  346. #endif
  347. wchar_t last; /* also for %[a-z] */
  348. wchar_t prevchar; /* for %[a-z] */
  349. wchar_t wdecimal; /* wide version of decimal point */
  350. wchar_t *wptr; /* pointer traverses wide floatstring*/
  351. #else
  352. #ifdef ALLOW_RANGE
  353. unsigned char rngch; /* used while scanning range */
  354. #endif
  355. unsigned char last; /* also for %[a-z] */
  356. unsigned char prevchar; /* for %[a-z] */
  357. #endif
  358. _ASSERTE(format != NULL);
  359. #ifndef CPRFLAG
  360. _ASSERTE(stream != NULL);
  361. #endif
  362. /*
  363. count = # fields assigned
  364. charcount = # chars read
  365. match = flag indicating if any fields were matched
  366. [Note that we need both count and match. For example, a field
  367. may match a format but have assignments suppressed. In this case,
  368. match will get set, but 'count' will still equal 0. We need to
  369. distinguish 'match vs no-match' when terminating due to EOF.]
  370. */
  371. count = charcount = match = 0;
  372. while (*format) {
  373. if (_istspace((_TUCHAR)*format)) {
  374. UN_INC(EAT_WHITE()); /* put first non-space char back */
  375. while ((_istspace)(*++format)); /* NULL */
  376. /* careful: isspace macro may evaluate argument more than once! */
  377. continue;
  378. }
  379. if (_T('%') == *format) {
  380. number = 0;
  381. prevchar = 0;
  382. width = widthset = started = 0;
  383. fl_wchar_arg = done_flag = suppress = negative = reject = 0;
  384. widechar = 0;
  385. longone = 1;
  386. integer64 = 0;
  387. while (!done_flag) {
  388. comchr = *++format;
  389. if (_ISDIGIT((_TUCHAR)comchr)) {
  390. ++widthset;
  391. width = MUL10(width) + (comchr - _T('0'));
  392. } else
  393. switch (comchr) {
  394. case _T('F') :
  395. case _T('N') : /* no way to push NEAR in large model */
  396. break; /* NEAR is default in small model */
  397. case _T('h') :
  398. /* set longone to 0 */
  399. --longone;
  400. --widechar; /* set widechar = -1 */
  401. break;
  402. #if _INTEGRAL_MAX_BITS >= 64 /*IFSTRIP=IGN*/
  403. case _T('I'):
  404. if ( (*(format + 1) == _T('6')) &&
  405. (*(format + 2) == _T('4')) )
  406. {
  407. format += 2;
  408. ++integer64;
  409. num64 = 0;
  410. break;
  411. }
  412. else if ( (*(format + 1) == _T('3')) &&
  413. (*(format + 2) == _T('2')) )
  414. {
  415. format += 2;
  416. break;
  417. }
  418. else if ( (*(format + 1) == _T('d')) ||
  419. (*(format + 1) == _T('i')) ||
  420. (*(format + 1) == _T('o')) ||
  421. (*(format + 1) == _T('x')) ||
  422. (*(format + 1) == _T('X')) )
  423. {
  424. if (sizeof(void*) == sizeof(__int64))
  425. {
  426. ++integer64;
  427. num64 = 0;
  428. }
  429. break;
  430. }
  431. if (sizeof(void*) == sizeof(__int64))
  432. {
  433. ++integer64;
  434. num64 = 0;
  435. }
  436. goto DEFAULT_LABEL;
  437. #endif
  438. case _T('L') :
  439. /* ++longone; */
  440. ++longone;
  441. break;
  442. case _T('l') :
  443. ++longone;
  444. /* NOBREAK */
  445. case _T('w') :
  446. ++widechar; /* set widechar = 1 */
  447. break;
  448. case _T('*') :
  449. ++suppress;
  450. break;
  451. default:
  452. DEFAULT_LABEL:
  453. ++done_flag;
  454. break;
  455. }
  456. }
  457. if (!suppress) {
  458. arglistsave = arglist;
  459. pointer = va_arg(arglist,void *);
  460. } else {
  461. pointer = NULL; // doesn't matter what value we use here - we're only using it as a flag
  462. }
  463. done_flag = 0;
  464. if (!widechar) { /* use case if not explicitly specified */
  465. if ((*format == _T('S')) || (*format == _T('C')))
  466. #ifdef _UNICODE
  467. --widechar;
  468. else
  469. ++widechar;
  470. #else
  471. ++widechar;
  472. else
  473. --widechar;
  474. #endif
  475. }
  476. /* switch to lowercase to allow %E,%G, and to
  477. keep the switch table small */
  478. comchr = *format | (_T('a') - _T('A'));
  479. if (_T('n') != comchr)
  480. if (_T('c') != comchr && LEFT_BRACKET != comchr)
  481. ch = EAT_WHITE();
  482. else
  483. ch = INC();
  484. #ifdef _POSIX_
  485. if (_T('n') != comchr)
  486. {
  487. if (EOF == ch)
  488. goto error_return;
  489. }
  490. #endif
  491. if (!widthset || width) {
  492. switch(comchr) {
  493. case _T('c'):
  494. /* case _T('C'): */
  495. if (!widthset) {
  496. ++widthset;
  497. ++width;
  498. }
  499. if (widechar > 0)
  500. fl_wchar_arg++;
  501. goto scanit;
  502. case _T('s'):
  503. /* case _T('S'): */
  504. if(widechar > 0)
  505. fl_wchar_arg++;
  506. goto scanit;
  507. case LEFT_BRACKET : /* scanset */
  508. if (widechar>0)
  509. fl_wchar_arg++;
  510. scanptr = (_TCHAR *)(++format);
  511. if (_T('^') == *scanptr) {
  512. ++scanptr;
  513. --reject; /* set reject to 255 */
  514. }
  515. /* Allocate "table" on first %[] spec */
  516. #if ALLOC_TABLE
  517. if (table == NULL) {
  518. table = _malloc_crt(TABLESIZE);
  519. if ( table == NULL)
  520. goto error_return;
  521. malloc_flag = 1;
  522. }
  523. #endif
  524. memset(table, 0, TABLESIZE);
  525. #ifdef ALLOW_RANGE
  526. if (LEFT_BRACKET == comchr)
  527. if (_T(']') == *scanptr) {
  528. prevchar = _T(']');
  529. ++scanptr;
  530. table[ _T(']') >> 3] = 1 << (_T(']') & 7);
  531. }
  532. while (_T(']') != *scanptr) {
  533. rngch = *scanptr++;
  534. if (_T('-') != rngch ||
  535. !prevchar || /* first char */
  536. _T(']') == *scanptr) /* last char */
  537. table[(prevchar = rngch) >> 3] |= 1 << (rngch & 7);
  538. else { /* handle a-z type set */
  539. rngch = *scanptr++; /* get end of range */
  540. if (prevchar < rngch) /* %[a-z] */
  541. last = rngch;
  542. else { /* %[z-a] */
  543. last = prevchar;
  544. prevchar = rngch;
  545. }
  546. for (rngch = prevchar; rngch <= last; ++rngch)
  547. table[rngch >> 3] |= 1 << (rngch & 7);
  548. prevchar = 0;
  549. }
  550. }
  551. #else
  552. if (LEFT_BRACKET == comchr)
  553. if (_T(']') == *scanptr) {
  554. ++scanptr;
  555. table[(prevchar = _T(']')) >> 3] |= 1 << (_T(']') & 7);
  556. }
  557. while (_T(']') != *scanptr) {
  558. table[scanptr >> 3] |= 1 << (scanptr & 7);
  559. ++scanptr;
  560. }
  561. /* code under !ALLOW_RANGE is probably never compiled */
  562. /* and has probably never been tested */
  563. #endif
  564. if (!*scanptr)
  565. goto error_return; /* trunc'd format string */
  566. /* scanset completed. Now read string */
  567. if (LEFT_BRACKET == comchr)
  568. format = scanptr;
  569. scanit:
  570. start = pointer;
  571. /*
  572. * execute the format directive. that is, scan input
  573. * characters until the directive is fulfilled, eof
  574. * is reached, or a non-matching character is
  575. * encountered.
  576. *
  577. * it is important not to get the next character
  578. * unless that character needs to be tested! other-
  579. * wise, reads from line-buffered devices (e.g.,
  580. * scanf()) would require an extra, spurious, newline
  581. * if the first newline completes the current format
  582. * directive.
  583. */
  584. UN_INC(ch);
  585. while ( !widthset || width-- ) {
  586. ch = INC();
  587. if (
  588. #ifndef CPRFLAG
  589. #ifndef _UNICODE
  590. (EOF != ch) &&
  591. #else
  592. (WEOF != ch) &&
  593. #endif /* _UNICODE */
  594. #endif
  595. // char conditions
  596. ( ( comchr == _T('c')) ||
  597. // string conditions !isspace()
  598. ( ( comchr == _T('s') &&
  599. (!(ch >= _T('\t') && ch <= _T('\r')) &&
  600. ch != _T(' ')))) ||
  601. // BRACKET conditions
  602. ( (comchr == LEFT_BRACKET) &&
  603. ((table[ch >> 3] ^ reject) & (1 << (ch & 7)))
  604. )
  605. )
  606. )
  607. {
  608. if (!suppress) {
  609. #ifndef _UNICODE
  610. if (fl_wchar_arg) {
  611. char temp[2];
  612. temp[0] = (char) ch;
  613. if (isleadbyte(ch))
  614. temp[1] = (char) INC();
  615. mbtowc(&wctemp, temp, MB_CUR_MAX);
  616. *(wchar_t UNALIGNED *)pointer =
  617. wctemp;
  618. /* do nothing if mbtowc fails */
  619. pointer = (wchar_t *)pointer + 1;
  620. } else
  621. #else
  622. if (fl_wchar_arg) {
  623. *(wchar_t UNALIGNED *)pointer = ch;
  624. pointer = (wchar_t *)pointer + 1;
  625. } else
  626. #endif
  627. {
  628. #ifndef _UNICODE
  629. *(char *)pointer = (char)ch;
  630. pointer = (char *)pointer + 1;
  631. #else
  632. int temp;
  633. /* convert wide to multibyte */
  634. temp = wctomb((char *)pointer, ch);
  635. /* do nothing if wctomb fails */
  636. pointer = (char *)pointer + temp;
  637. #endif
  638. }
  639. } /* suppress */
  640. else {
  641. /* just indicate a match */
  642. start = (_TCHAR *)start + 1;
  643. }
  644. }
  645. else {
  646. UN_INC(ch);
  647. break;
  648. }
  649. }
  650. /* make sure something has been matched and, if
  651. assignment is not suppressed, null-terminate
  652. output string if comchr != c */
  653. if (start != pointer) {
  654. if (!suppress) {
  655. ++count;
  656. if ('c' != comchr) /* null-terminate strings */
  657. if (fl_wchar_arg)
  658. *(wchar_t UNALIGNED *)pointer = L'\0';
  659. else
  660. *(char *)pointer = '\0';
  661. } else /*NULL*/;
  662. }
  663. else
  664. goto error_return;
  665. break;
  666. case _T('i') : /* could be d, o, or x */
  667. comchr = _T('d'); /* use as default */
  668. case _T('x'):
  669. if (_T('-') == ch) {
  670. ++negative;
  671. goto x_incwidth;
  672. } else if (_T('+') == ch) {
  673. x_incwidth:
  674. if (!--width && widthset)
  675. ++done_flag;
  676. else
  677. ch = INC();
  678. }
  679. if (_T('0') == ch) {
  680. if (_T('x') == (_TCHAR)(ch = INC()) || _T('X') == (_TCHAR)ch) {
  681. ch = INC();
  682. if (widthset) {
  683. width -= 2;
  684. if (width < 1)
  685. ++done_flag;
  686. }
  687. comchr = _T('x');
  688. } else {
  689. ++started;
  690. if (_T('x') != comchr) {
  691. if (widthset && !--width)
  692. ++done_flag;
  693. comchr = _T('o');
  694. }
  695. else {
  696. /* scanning a hex number that starts */
  697. /* with a 0. push back the character */
  698. /* currently in ch and restore the 0 */
  699. UN_INC(ch);
  700. ch = _T('0');
  701. }
  702. }
  703. }
  704. goto getnum;
  705. /* NOTREACHED */
  706. case _T('p') :
  707. /* force %hp to be treated as %p */
  708. longone = 1;
  709. #ifdef _WIN64
  710. /* force %p to be 64 bit in WIN64 */
  711. ++integer64;
  712. num64 = 0;
  713. #endif
  714. case _T('o') :
  715. case _T('u') :
  716. case _T('d') :
  717. if (_T('-') == ch) {
  718. ++negative;
  719. goto d_incwidth;
  720. } else if (_T('+') == ch) {
  721. d_incwidth:
  722. if (!--width && widthset)
  723. ++done_flag;
  724. else
  725. ch = INC();
  726. }
  727. getnum:
  728. #if _INTEGRAL_MAX_BITS >= 64 /*IFSTRIP=IGN*/
  729. if ( integer64 ) {
  730. while (!done_flag) {
  731. if (_T('x') == comchr || _T('p') == comchr)
  732. if (_ISXDIGIT(ch)) {
  733. num64 <<= 4;
  734. ch = HEXTODEC(ch);
  735. }
  736. else
  737. ++done_flag;
  738. else if (_ISDIGIT(ch))
  739. if (_T('o') == comchr)
  740. if (_T('8') > ch)
  741. num64 <<= 3;
  742. else {
  743. ++done_flag;
  744. }
  745. else /* _T('d') == comchr */
  746. num64 = MUL10(num64);
  747. else
  748. ++done_flag;
  749. if (!done_flag) {
  750. ++started;
  751. num64 += ch - _T('0');
  752. if (widthset && !--width)
  753. ++done_flag;
  754. else
  755. ch = INC();
  756. } else
  757. UN_INC(ch);
  758. } /* end of WHILE loop */
  759. if (negative)
  760. num64 = (unsigned __int64 )(-(__int64)num64);
  761. }
  762. else {
  763. #endif
  764. while (!done_flag) {
  765. if (_T('x') == comchr || _T('p') == comchr)
  766. if (_ISXDIGIT(ch)) {
  767. number = (number << 4);
  768. ch = HEXTODEC(ch);
  769. }
  770. else
  771. ++done_flag;
  772. else if (_ISDIGIT(ch))
  773. if (_T('o') == comchr)
  774. if (_T('8') > ch)
  775. number = (number << 3);
  776. else {
  777. ++done_flag;
  778. }
  779. else /* _T('d') == comchr */
  780. number = MUL10(number);
  781. else
  782. ++done_flag;
  783. if (!done_flag) {
  784. ++started;
  785. number += ch - _T('0');
  786. if (widthset && !--width)
  787. ++done_flag;
  788. else
  789. ch = INC();
  790. } else
  791. UN_INC(ch);
  792. } /* end of WHILE loop */
  793. if (negative)
  794. number = (unsigned long)(-(long)number);
  795. #if _INTEGRAL_MAX_BITS >= 64 /*IFSTRIP=IGN*/
  796. }
  797. #endif
  798. if (_T('F')==comchr) /* expected ':' in long pointer */
  799. started = 0;
  800. if (started)
  801. if (!suppress) {
  802. ++count;
  803. assign_num:
  804. #if _INTEGRAL_MAX_BITS >= 64 /*IFSTRIP=IGN*/
  805. if ( integer64 )
  806. *(__int64 UNALIGNED *)pointer = (unsigned __int64)num64;
  807. else
  808. #endif
  809. if (longone)
  810. *(long UNALIGNED *)pointer = (unsigned long)number;
  811. else
  812. *(short UNALIGNED *)pointer = (unsigned short)number;
  813. } else /*NULL*/;
  814. else
  815. goto error_return;
  816. break;
  817. case _T('n') : /* char count, don't inc return value */
  818. number = charcount;
  819. if(!suppress)
  820. goto assign_num; /* found in number code above */
  821. break;
  822. case _T('e') :
  823. /* case _T('E') : */
  824. case _T('f') :
  825. case _T('g') : /* scan a float */
  826. /* case _T('G') : */
  827. #ifndef _UNICODE
  828. scanptr = floatstring;
  829. if (_T('-') == ch) {
  830. *scanptr++ = _T('-');
  831. goto f_incwidth;
  832. } else if (_T('+') == ch) {
  833. f_incwidth:
  834. --width;
  835. ch = INC();
  836. }
  837. if (!widthset || width > CVTBUFSIZE) /* must watch width */
  838. width = CVTBUFSIZE;
  839. /* now get integral part */
  840. while (_ISDIGIT(ch) && width--) {
  841. ++started;
  842. *scanptr++ = (char)ch;
  843. ch = INC();
  844. }
  845. /* now check for decimal */
  846. if (*___decimal_point == (char)ch && width--) {
  847. ch = INC();
  848. *scanptr++ = *___decimal_point;
  849. while (_ISDIGIT(ch) && width--) {
  850. ++started;
  851. *scanptr++ = (char)ch;
  852. ch = INC();
  853. }
  854. }
  855. /* now check for exponent */
  856. if (started && (_T('e') == ch || _T('E') == ch) && width--) {
  857. *scanptr++ = 'e';
  858. if (_T('-') == (ch = INC())) {
  859. *scanptr++ = '-';
  860. goto f_incwidth2;
  861. } else if (_T('+') == ch) {
  862. f_incwidth2:
  863. if (!width--)
  864. ++width;
  865. else
  866. ch = INC();
  867. }
  868. while (_ISDIGIT(ch) && width--) {
  869. ++started;
  870. *scanptr++ = (char)ch;
  871. ch = INC();
  872. }
  873. }
  874. UN_INC(ch);
  875. if (started)
  876. if (!suppress) {
  877. ++count;
  878. *scanptr = '\0';
  879. _fassign( longone-1, pointer , floatstring);
  880. } else /*NULL */;
  881. else
  882. goto error_return;
  883. #else /* _UNICODE */
  884. wptr = floatstring;
  885. if (L'-' == ch) {
  886. *wptr++ = L'-';
  887. goto f_incwidthw;
  888. } else if (L'+' == ch) {
  889. f_incwidthw:
  890. --width;
  891. ch = INC();
  892. }
  893. if (!widthset || width > CVTBUFSIZE)
  894. width = CVTBUFSIZE;
  895. /* now get integral part */
  896. while (_ISDIGIT(ch) && width--) {
  897. ++started;
  898. *wptr++ = ch;
  899. ch = INC();
  900. }
  901. /* now check for decimal */
  902. /* convert decimal point to wide-char */
  903. /* assume result is single wide-char */
  904. mbtowc (&wdecimal, ___decimal_point, MB_CUR_MAX);
  905. if (wdecimal == ch && width--) {
  906. ch = INC();
  907. *wptr++ = wdecimal;
  908. while (_ISDIGIT(ch) && width--) {
  909. ++started;
  910. *wptr++ = ch;
  911. ch = INC();
  912. }
  913. }
  914. /* now check for exponent */
  915. if (started && (L'e' == ch || L'E' == ch) && width--) {
  916. *wptr++ = L'e';
  917. if (L'-' == (ch = INC())) {
  918. *wptr++ = L'-';
  919. goto f_incwidth2w;
  920. } else if (L'+' == ch) {
  921. f_incwidth2w:
  922. if (!width--)
  923. ++width;
  924. else
  925. ch = INC();
  926. }
  927. while (_ISDIGIT(ch) && width--) {
  928. ++started;
  929. *wptr++ = ch;
  930. ch = INC();
  931. }
  932. }
  933. UN_INC(ch);
  934. if (started)
  935. if (!suppress) {
  936. ++count;
  937. *wptr = '\0';
  938. {
  939. /* convert floatstring to char string */
  940. /* and do the conversion */
  941. size_t cfslength;
  942. char *cfloatstring;
  943. cfslength =(size_t)(wptr-floatstring+1)*sizeof(wchar_t);
  944. if ((cfloatstring = (char *)_malloc_crt (cfslength)) == NULL)
  945. goto error_return;
  946. wcstombs (cfloatstring, floatstring, cfslength);
  947. _fassign( longone-1, pointer , cfloatstring);
  948. _free_crt (cfloatstring);
  949. }
  950. } else /*NULL */;
  951. else
  952. goto error_return;
  953. #endif /* _UNICODE */
  954. break;
  955. default: /* either found '%' or something else */
  956. if ((int)*format != (int)ch) {
  957. UN_INC(ch);
  958. goto error_return;
  959. }
  960. else
  961. match--; /* % found, compensate for inc below */
  962. if (!suppress)
  963. arglist = arglistsave;
  964. } /* SWITCH */
  965. match++; /* matched a format field - set flag */
  966. } /* WHILE (width) */
  967. else { /* zero-width field in format string */
  968. UN_INC(ch); /* check for input error */
  969. goto error_return;
  970. }
  971. ++format; /* skip to next char */
  972. } else /* ('%' != *format) */
  973. {
  974. if ((int)*format++ != (int)(ch = INC()))
  975. {
  976. UN_INC(ch);
  977. goto error_return;
  978. }
  979. #ifndef _UNICODE
  980. if (isleadbyte(ch))
  981. {
  982. int ch2;
  983. if ((int)*format++ != (ch2=INC()))
  984. {
  985. UN_INC(ch2);
  986. UN_INC(ch);
  987. goto error_return;
  988. }
  989. --charcount; /* only count as one character read */
  990. }
  991. #endif
  992. }
  993. #ifndef CPRFLAG
  994. if ( (EOF == ch) && ((*format != '%') || (*(format + 1) != 'n')) )
  995. break;
  996. #endif
  997. } /* WHILE (*format) */
  998. error_return:
  999. #if ALLOC_TABLE
  1000. if (malloc_flag == 1)
  1001. _free_crt(table);
  1002. #endif
  1003. #ifndef CPRFLAG
  1004. if (EOF == ch)
  1005. /* If any fields were matched or assigned, return count */
  1006. return ( (count || match) ? count : EOF);
  1007. else
  1008. #endif
  1009. return count;
  1010. }
  1011. /* _hextodec() returns a value of 0-15 and expects a char 0-9, a-f, A-F */
  1012. /* _inc() is the one place where we put the actual getc code. */
  1013. /* _whiteout() returns the first non-blank character, as defined by isspace() */
  1014. #ifndef _UNICODE
  1015. static int __cdecl _hextodec (
  1016. int chr
  1017. )
  1018. {
  1019. return _ISDIGIT(chr) ? chr : (chr & ~(_T('a') - _T('A'))) - _T('A') + 10 + _T('0');
  1020. }
  1021. #else
  1022. static _TCHAR __cdecl _hextodec (
  1023. _TCHAR chr
  1024. )
  1025. {
  1026. if (_ISDIGIT(chr))
  1027. return chr;
  1028. if (_istlower(chr))
  1029. return (_TCHAR)(chr - _T('a') + 10 + _T('0'));
  1030. else
  1031. return (_TCHAR)(chr - _T('A') + 10 + _T('0'));
  1032. }
  1033. #endif
  1034. #ifdef CPRFLAG
  1035. #ifndef _UNICODE
  1036. static int __cdecl _inc (
  1037. void
  1038. )
  1039. {
  1040. return(_getche_lk());
  1041. }
  1042. static void __cdecl _un_inc (
  1043. int chr
  1044. )
  1045. {
  1046. if (EOF != chr)
  1047. _ungetch_lk(chr);
  1048. }
  1049. static int __cdecl _whiteout (
  1050. REG1 int *counter
  1051. )
  1052. {
  1053. REG2 int ch;
  1054. while((_istspace)(ch = (++*counter, _inc())));
  1055. return ch;
  1056. }
  1057. #else /* _UNICODE */
  1058. static wchar_t __cdecl _inc (
  1059. void
  1060. )
  1061. {
  1062. return(_getwche_lk());
  1063. }
  1064. static void __cdecl _un_inc (
  1065. wchar_t chr
  1066. )
  1067. {
  1068. if (WEOF != chr)
  1069. _ungetwch_lk(chr);
  1070. }
  1071. static wchar_t __cdecl _whiteout (
  1072. REG1 int *counter
  1073. )
  1074. {
  1075. REG2 wchar_t ch;
  1076. while((iswspace)(ch = (++*counter, _inc())));
  1077. return ch;
  1078. }
  1079. #endif
  1080. #else /* CPRFLAG */
  1081. #ifdef _UNICODE
  1082. /*
  1083. * Manipulate wide-chars in a file.
  1084. * A wide-char is hard-coded to be two chars for efficiency.
  1085. */
  1086. static wchar_t __cdecl _inc (
  1087. REG1 FILE *fileptr
  1088. )
  1089. {
  1090. return(_getwc_lk(fileptr));
  1091. }
  1092. static void __cdecl _un_inc (
  1093. wchar_t chr,
  1094. FILE *fileptr
  1095. )
  1096. {
  1097. if (WEOF != chr)
  1098. _ungetwc_lk(chr, fileptr);
  1099. }
  1100. static wchar_t __cdecl _whiteout (
  1101. REG1 int *counter,
  1102. REG3 FILE *fileptr
  1103. )
  1104. {
  1105. REG2 wchar_t ch;
  1106. while((iswspace)(ch = (++*counter, _inc(fileptr))));
  1107. return ch;
  1108. }
  1109. #else /* _UNICODE */
  1110. static int __cdecl _inc (
  1111. REG1 FILE *fileptr
  1112. )
  1113. {
  1114. return(_getc_lk(fileptr));
  1115. }
  1116. static void __cdecl _un_inc (
  1117. int chr,
  1118. FILE *fileptr
  1119. )
  1120. {
  1121. if (EOF != chr)
  1122. _ungetc_lk(chr, fileptr);
  1123. }
  1124. static int __cdecl _whiteout (
  1125. REG1 int *counter,
  1126. REG3 FILE *fileptr
  1127. )
  1128. {
  1129. REG2 int ch;
  1130. while((_istspace)(ch = (++*counter, _inc(fileptr))));
  1131. return ch;
  1132. }
  1133. #endif /* _UNICODE */
  1134. #endif /* CPRFLAG */