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.

152 lines
5.1 KiB

  1. /***
  2. *sscanf.c - read formatted data from string
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines scanf() - reads formatted data from string
  8. *
  9. *Revision History:
  10. * 09-02-83 RN initial version
  11. * 04-13-87 JCR added const to declaration
  12. * 06-24-87 JCR (1) Made declaration conform to ANSI prototype and use
  13. * the va_ macros; (2) removed SS_NE_DS conditionals.
  14. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  15. * 06-13-88 JCR Fake _iob entry is now static so that other routines can
  16. * assume _iob entries are in DGROUP.
  17. * 06-06-89 JCR 386 mthread support -- threads share one locked iob.
  18. * 08-18-89 GJF Clean up, now specific to OS/2 2.0 (i.e., 386 flat
  19. * model). Also fixed indents.
  20. * 02-16-90 GJF Fixed copyright
  21. * 03-19-90 GJF Made calling type _CALLTYPE2, added #include
  22. * <cruntime.h> and removed #include <register.h>.
  23. * 03-26-90 GJF Added #include <string.h>.
  24. * 07-23-90 SBM Replaced <assertm.h> by <assert.h>
  25. * 10-03-90 GJF New-style function declarator.
  26. * 02-18-93 SRW Make FILE a local and remove lock usage.
  27. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  28. * 02-06-94 CFW assert -> _ASSERTE.
  29. * 01-06-99 GJF Changes for 64-bit size_t.
  30. *
  31. *******************************************************************************/
  32. #include <cruntime.h>
  33. #include <stdio.h>
  34. #include <dbgint.h>
  35. #include <stdarg.h>
  36. #include <string.h>
  37. #include <internal.h>
  38. #include <mtdll.h>
  39. #include <tchar.h>
  40. /***
  41. *int sscanf(string, format, ...) - read formatted data from string
  42. *
  43. *Purpose:
  44. * Reads formatted data from string into arguments. _input does the real
  45. * work here. Sets up a FILE so file i/o operations can be used, makes
  46. * string look like a huge buffer to it, but _filbuf will refuse to refill
  47. * it if it is exhausted.
  48. *
  49. * Allocate the 'fake' _iob[] entryit statically instead of on
  50. * the stack so that other routines can assume that _iob[] entries are in
  51. * are in DGROUP and, thus, are near.
  52. *
  53. * Multi-thread: (1) Since there is no stream, this routine must never try
  54. * to get the stream lock (i.e., there is no stream lock either). (2)
  55. * Also, since there is only one staticly allocated 'fake' iob, we must
  56. * lock/unlock to prevent collisions.
  57. *
  58. *Entry:
  59. * char *string - string to read data from
  60. * char *format - format string
  61. * followed by list of pointers to storage for the data read. The number
  62. * and type are controlled by the format string.
  63. *
  64. *Exit:
  65. * returns number of fields read and assigned
  66. *
  67. *Exceptions:
  68. *
  69. *******************************************************************************/
  70. /***
  71. *int snscanf(string, size, format, ...) - read formatted data from string of
  72. * given length
  73. *
  74. *Purpose:
  75. * Reads formatted data from string into arguments. _input does the real
  76. * work here. Sets up a FILE so file i/o operations can be used, makes
  77. * string look like a huge buffer to it, but _filbuf will refuse to refill
  78. * it if it is exhausted.
  79. *
  80. * Allocate the 'fake' _iob[] entryit statically instead of on
  81. * the stack so that other routines can assume that _iob[] entries are in
  82. * are in DGROUP and, thus, are near.
  83. *
  84. * Multi-thread: (1) Since there is no stream, this routine must never try
  85. * to get the stream lock (i.e., there is no stream lock either). (2)
  86. * Also, since there is only one staticly allocated 'fake' iob, we must
  87. * lock/unlock to prevent collisions.
  88. *
  89. *Entry:
  90. * char *string - string to read data from
  91. * size_t count - length of string
  92. * char *format - format string
  93. * followed by list of pointers to storage for the data read. The number
  94. * and type are controlled by the format string.
  95. *
  96. *Exit:
  97. * returns number of fields read and assigned
  98. *
  99. *Exceptions:
  100. *
  101. *******************************************************************************/
  102. #ifdef _UNICODE
  103. #ifdef _SNSCANF
  104. int __cdecl _snwscanf (
  105. #else
  106. int __cdecl swscanf (
  107. #endif
  108. #else
  109. #ifdef _SNSCANF
  110. int __cdecl _snscanf (
  111. #else
  112. int __cdecl sscanf (
  113. #endif
  114. #endif
  115. REG2 const _TCHAR *string,
  116. #ifdef _SNSCANF
  117. size_t count,
  118. #endif
  119. const _TCHAR *format,
  120. ...
  121. )
  122. /*
  123. * 'S'tring 'SCAN', 'F'ormatted
  124. */
  125. {
  126. va_list arglist;
  127. FILE str;
  128. REG1 FILE *infile = &str;
  129. REG2 int retval;
  130. va_start(arglist, format);
  131. _ASSERTE(string != NULL);
  132. _ASSERTE(format != NULL);
  133. infile->_flag = _IOREAD|_IOSTRG|_IOMYBUF;
  134. infile->_ptr = infile->_base = (char *) string;
  135. #ifdef _SNSCANF
  136. infile->_cnt = (int)count*sizeof(_TCHAR);
  137. #else
  138. infile->_cnt = ((int)_tcslen(string))*sizeof(_TCHAR);
  139. #endif
  140. #ifdef _UNICODE
  141. retval = (_winput(infile,format,arglist));
  142. #else
  143. retval = (_input(infile,format,arglist));
  144. #endif
  145. return(retval);
  146. }