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.

168 lines
4.9 KiB

  1. /***
  2. *wcsftime.c - String Format Time
  3. *
  4. * Copyright (c) 1993-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *Revision History:
  9. * 03-08-93 CFW Module Created.
  10. * 03-10-93 CFW Fixed up properly.
  11. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  12. * 02-07-94 CFW POSIXify.
  13. * 12-16-94 CFW Format must be wchar_t!
  14. * 01-10-95 CFW Debug CRT allocs.
  15. * 08-27-98 GJF Revised multithread support based on threadlocinfo
  16. * struct. Also, use _alloca rather than malloc/free,
  17. * when possible.
  18. * 01-06-99 GJF Changes for 64-bit size_t.
  19. * 12-10-99 GB Added support for recovery from stack overflow around
  20. * _alloca().
  21. * 12-11-01 BWT Replace _getptd with _getptd_noexit - we can return 0/ENOMEM
  22. * here instead of exiting.
  23. *
  24. *******************************************************************************/
  25. #ifndef _POSIX_
  26. #include <cruntime.h>
  27. #include <internal.h>
  28. #include <mtdll.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <wchar.h>
  32. #include <time.h>
  33. #include <dbgint.h>
  34. #include <malloc.h>
  35. #include <excpt.h>
  36. #include <errno.h>
  37. #ifdef _MT
  38. size_t __cdecl _Strftime_mt (pthreadlocinfo ptloci, char *string, size_t maxsize,
  39. const char *format, const struct tm *timeptr, void *lc_time_arg);
  40. #else
  41. size_t __cdecl _Strftime (char *string, size_t maxsize, const char *format,
  42. const struct tm *timeptr, void *lc_time_arg);
  43. #endif
  44. /***
  45. *size_t wcsftime(wstring, maxsize, format, timeptr) - Format a time string
  46. *
  47. *Purpose:
  48. * The wcsftime functions is equivalent to to the strftime function, except
  49. * that the argument 'wstring' specifies an array of a wide string into
  50. * which the generated output is to be placed. The wcsftime acts as if
  51. * strftime were called and the result string converted by mbstowcs().
  52. * [ISO]
  53. *
  54. *Entry:
  55. * wchar_t *wstring = pointer to output string
  56. * size_t maxsize = max length of string
  57. * const wchar_t *format = format control string
  58. * const struct tm *timeptr = pointer to tb data structure
  59. *
  60. *Exit:
  61. * !0 = If the total number of resulting characters including the
  62. * terminating null is not more than 'maxsize', then return the
  63. * number of wide chars placed in the 'wstring' array (not including the
  64. * null terminator).
  65. *
  66. * 0 = Otherwise, return 0 and the contents of the string are
  67. * indeterminate.
  68. *
  69. *Exceptions:
  70. *
  71. *******************************************************************************/
  72. size_t __cdecl wcsftime (
  73. wchar_t *wstring,
  74. size_t maxsize,
  75. const wchar_t *wformat,
  76. const struct tm *timeptr
  77. )
  78. {
  79. size_t retval = 0;
  80. char *format = NULL;
  81. char *string = NULL;
  82. size_t flen = wcslen(wformat) + 1;
  83. int malloc_flag1 = 0;
  84. int malloc_flag2 = 0;
  85. #ifdef _MT
  86. pthreadlocinfo ptloci;
  87. _ptiddata ptd = _getptd_noexit();
  88. if (!ptd) {
  89. errno = ENOMEM;
  90. return (0);
  91. }
  92. ptloci = ptd->ptlocinfo;
  93. if ( ptloci != __ptlocinfo )
  94. ptloci = __updatetlocinfo();
  95. #endif
  96. __try {
  97. string = (char *)_alloca(sizeof(char) * maxsize * 2);
  98. }
  99. __except( EXCEPTION_EXECUTE_HANDLER ) {
  100. _resetstkoflw();
  101. string = NULL;
  102. }
  103. if ( string == NULL ) {
  104. if ((string = (char *)_malloc_crt(sizeof(char) * maxsize * 2)) == NULL)
  105. return 0;
  106. else
  107. malloc_flag1++;
  108. }
  109. __try {
  110. format = (char *)_alloca(sizeof(char) * flen * 2);
  111. }
  112. __except( EXCEPTION_EXECUTE_HANDLER ) {
  113. _resetstkoflw();
  114. format = NULL;
  115. }
  116. if ( format == NULL ) {
  117. if ((format = (char *)_malloc_crt(sizeof(char) * flen * 2)) == NULL)
  118. goto done;
  119. else
  120. malloc_flag2++;
  121. }
  122. #ifdef _MT
  123. if (__wcstombs_mt(ptloci, format, wformat, flen * 2) == -1)
  124. #else
  125. if (wcstombs(format, wformat, flen * 2) == -1)
  126. #endif
  127. goto done;
  128. #ifdef _MT
  129. if (_Strftime_mt(ptloci, string, maxsize * 2, format, timeptr, 0))
  130. #else
  131. if (_Strftime(string, maxsize * 2, format, timeptr, 0))
  132. #endif
  133. {
  134. #ifdef _MT
  135. if ((retval = __mbstowcs_mt(ptloci, wstring, string, maxsize))
  136. == -1)
  137. #else
  138. if ((retval = mbstowcs(wstring, string, maxsize)) == -1)
  139. #endif
  140. retval = 0;
  141. }
  142. done:
  143. if ( malloc_flag1 )
  144. _free_crt(string);
  145. if ( malloc_flag2 )
  146. _free_crt(format);
  147. return retval;
  148. }
  149. #endif /* _POSIX_ */