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.

128 lines
3.8 KiB

  1. /***
  2. *_strerr.c - routine for indexing into system error list
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Returns system error message index by errno; conforms to the
  8. * XENIX standard, much compatibility with 1983 uniforum draft standard.
  9. *
  10. *Revision History:
  11. * 02-24-87 JCR Renamed this routine from "strerror" to "_strerror"
  12. * for MS. The new "strerror" routine conforms to the
  13. * ANSI interface.
  14. * 11-10-87 SKS Remove IBMC20 switch
  15. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  16. * 01-05-87 JCR Mthread support
  17. * 05-31-88 PHG Merged DLL and normal versions
  18. * 06-06-89 JCR 386 mthread support
  19. * 11-20-89 GJF Fixed copyright, indents. Removed unreferenced local.
  20. * Added const attribute to type of message
  21. * 03-13-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  22. * <cruntime.h> and removed #include <register.h>
  23. * 07-25-90 SBM Removed redundant include (stdio.h)
  24. * 10-04-90 GJF New-style function declarator.
  25. * 07-18-91 GJF Multi-thread support for Win32 [_WIN32_].
  26. * 02-17-93 GJF Changed for new _getptd().
  27. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  28. * 09-06-94 CFW Remove Cruiser support.
  29. * 09-06-94 CFW Replace MTHREAD with _MT.
  30. * 01-10-95 CFW Debug CRT allocs.
  31. * 29-11-99 GB Added support for wide char by adding _wcserror()
  32. * 12-12-01 BWT Use getptd_noexit and return NULL if it fails.
  33. *
  34. *******************************************************************************/
  35. #include <cruntime.h>
  36. #include <stdlib.h>
  37. #include <errmsg.h>
  38. #include <syserr.h>
  39. #include <string.h>
  40. #include <tchar.h>
  41. #include <malloc.h>
  42. #include <mtdll.h>
  43. #include <dbgint.h>
  44. /* Max length of message = user_string(94)+system_string+2 */
  45. /* [NOTE: The mthread error message buffer is shared by both strerror
  46. and _strerror so must be the max length of both. */
  47. #define _ERRMSGLEN_ 94+_SYS_MSGMAX+2
  48. #ifdef _UNICODE
  49. #define _terrmsg _werrmsg
  50. #else
  51. #define _terrmsg _errmsg
  52. #endif
  53. /***
  54. *char *_strerror(message) - get system error message
  55. *
  56. *Purpose:
  57. * builds an error message consisting of the users error message
  58. * (the message parameter), followed by ": ", followed by the system
  59. * error message (index through errno), followed by a newline. If
  60. * message is NULL or a null string, returns a pointer to just
  61. * the system error message.
  62. *
  63. *Entry:
  64. * char *message - user's message to prefix system error message
  65. *
  66. *Exit:
  67. * returns pointer to static memory containing error message.
  68. * returns NULL if malloc() fails in multi-thread versions.
  69. *
  70. *Exceptions:
  71. *
  72. *******************************************************************************/
  73. #ifdef _UNICODE
  74. wchar_t * __cdecl __wcserror(
  75. #else
  76. char * __cdecl _strerror (
  77. #endif
  78. REG1 const _TCHAR *message
  79. )
  80. {
  81. #ifdef _MT
  82. _TCHAR *bldmsg;
  83. _ptiddata ptd = _getptd_noexit();
  84. if (!ptd)
  85. return NULL;
  86. #else
  87. static _TCHAR bldmsg[_ERRMSGLEN_];
  88. #endif
  89. #ifdef _MT
  90. /* Use per thread buffer area (malloc space, if necessary) */
  91. /* [NOTE: This buffer is shared between _strerror and streror.] */
  92. if ( (ptd->_terrmsg == NULL) && ((ptd->_terrmsg =
  93. _malloc_crt(_ERRMSGLEN_ * sizeof(_TCHAR))) == NULL) )
  94. return(NULL);
  95. bldmsg = ptd->_terrmsg;
  96. #endif
  97. /* Build the error message */
  98. bldmsg[0] = '\0';
  99. if (message && *message) {
  100. _tcscat( bldmsg, message );
  101. _tcscat( bldmsg, _T(": "));
  102. }
  103. #ifdef _UNICODE
  104. mbstowcs(bldmsg + wcslen(bldmsg), _sys_err_msg( errno ), _ERRMSGLEN_ - wcslen(bldmsg));
  105. #else
  106. strcat( bldmsg, _sys_err_msg( errno ) );
  107. #endif
  108. return( _tcscat( bldmsg, _T("\n")) );
  109. }