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.

173 lines
4.6 KiB

  1. /***
  2. *fputwc.c - write a wide character to an output stream
  3. *
  4. * Copyright (c) 1993-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines fputwc() - writes a wide character to a stream
  8. *
  9. *Revision History:
  10. * 04-26-93 CFW Module created.
  11. * 04-30-93 CFW Bring wide char support from fputc.c.
  12. * 05-03-93 CFW Add putwc function.
  13. * 05-10-93 CFW Optimize, fix error handling.
  14. * 06-02-93 CFW Wide get/put use wint_t.
  15. * 07-16-93 SRW ALPHA Merge
  16. * 09-15-93 CFW Use ANSI conformant "__" names.
  17. * 10-01-93 CFW Test only for TEXT.
  18. * 10-28-93 CFW Test for both IOSTRG and TEXT.
  19. * 11-05-93 GJF Merged with NT SDK version (fix to a cast expr).
  20. * 02-07-94 CFW POSIXify.
  21. * 08-31-94 CFW Fix for "C" locale, call wctomb().
  22. * 02-06-94 CFW assert -> _ASSERTE.
  23. * 03-07-95 GJF _[un]lock_str macros now take FILE * arg.
  24. * 06-12-95 GJF Replaced _osfile[] with _osfile() (macro referencing
  25. * field in ioinfo struct).
  26. * 07-28-95 GJF Replaced _osfile() with _osfile_safe().
  27. * 04-18-97 JWM Explicit cast added to avoid new C4242 warnings.
  28. * 02-27-98 GJF Exception-safe locking.
  29. * 12-16-99 GB Modified for the case when return value from wctomb is
  30. * greater then 2.
  31. * 11-22-00 PML Wide-char *putwc* functions take a wchar_t, not wint_t.
  32. *
  33. *******************************************************************************/
  34. #ifndef _POSIX_
  35. #include <cruntime.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <dbgint.h>
  39. #include <file2.h>
  40. #include <internal.h>
  41. #include <mtdll.h>
  42. #include <msdos.h>
  43. #include <errno.h>
  44. #include <wchar.h>
  45. #include <tchar.h>
  46. #include <setlocal.h>
  47. #ifdef _MT /* multi-thread; define both fputwc and _putwc_lk */
  48. /***
  49. *wint_t fputwc(ch, stream) - write a wide character to a stream
  50. *
  51. *Purpose:
  52. * Writes a wide character to a stream. Function version of putwc().
  53. *
  54. *Entry:
  55. * wchar_t ch - wide character to write
  56. * FILE *stream - stream to write to
  57. *
  58. *Exit:
  59. * returns the wide character if successful
  60. * returns WEOF if fails
  61. *
  62. *Exceptions:
  63. *
  64. *******************************************************************************/
  65. wint_t __cdecl fputwc (
  66. wchar_t ch,
  67. FILE *str
  68. )
  69. {
  70. REG1 FILE *stream;
  71. REG2 wint_t retval;
  72. _ASSERTE(str != NULL);
  73. /* Init stream pointer */
  74. stream = str;
  75. #ifdef _MT
  76. _lock_str(stream);
  77. __try {
  78. #endif
  79. retval = _putwc_lk(ch,stream);
  80. #ifdef _MT
  81. }
  82. __finally {
  83. _unlock_str(stream);
  84. }
  85. #endif
  86. return(retval);
  87. }
  88. /***
  89. *_putwc_lk() - putwc() core routine (locked version)
  90. *
  91. *Purpose:
  92. * Core putwc() routine; assumes stream is already locked.
  93. *
  94. * [See putwc() above for more info.]
  95. *
  96. *Entry: [See putwc()]
  97. *
  98. *Exit: [See putwc()]
  99. *
  100. *Exceptions:
  101. *
  102. *******************************************************************************/
  103. wint_t __cdecl _putwc_lk (
  104. wchar_t ch,
  105. FILE *str
  106. )
  107. {
  108. #else /* non multi-thread; just define fputwc */
  109. wint_t __cdecl fputwc (
  110. wchar_t ch,
  111. FILE *str
  112. )
  113. {
  114. #endif /* rejoin common code */
  115. #ifndef _NTSUBSET_
  116. if (!(str->_flag & _IOSTRG) && (_osfile_safe(_fileno(str)) & FTEXT))
  117. {
  118. int size, i;
  119. char mbc[MB_LEN_MAX];
  120. /* text (multi-byte) mode */
  121. if ((size = wctomb(mbc, ch)) == -1)
  122. {
  123. /*
  124. * Conversion failed! Set errno and return
  125. * failure.
  126. */
  127. errno = EILSEQ;
  128. return WEOF;
  129. }
  130. for ( i = 0; i < size; i++)
  131. {
  132. if (_putc_lk(mbc[i], str) == EOF)
  133. return WEOF;
  134. }
  135. return (wint_t)(0xffff & ch);
  136. }
  137. #endif
  138. /* binary (Unicode) mode */
  139. if ( (str->_cnt -= sizeof(wchar_t)) >= 0 )
  140. return (wint_t) (0xffff & (*((wchar_t *)(str->_ptr))++ = (wchar_t)ch));
  141. else
  142. return (wint_t) _flswbuf(ch, str);
  143. }
  144. #undef putwc
  145. wint_t __cdecl putwc (
  146. wchar_t ch,
  147. FILE *str
  148. )
  149. {
  150. return fputwc(ch, str);
  151. }
  152. #endif /* _POSIX_ */