Source code of Windows XP (NT5)
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.

125 lines
3.6 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. *
  33. *******************************************************************************/
  34. #include <cruntime.h>
  35. #include <stdlib.h>
  36. #include <errmsg.h>
  37. #include <syserr.h>
  38. #include <string.h>
  39. #include <tchar.h>
  40. #include <malloc.h>
  41. #include <mtdll.h>
  42. #include <dbgint.h>
  43. /* Max length of message = user_string(94)+system_string+2 */
  44. /* [NOTE: The mthread error message buffer is shared by both strerror
  45. and _strerror so must be the max length of both. */
  46. #define _ERRMSGLEN_ 94+_SYS_MSGMAX+2
  47. #ifdef _UNICODE
  48. #define _terrmsg _werrmsg
  49. #else
  50. #define _terrmsg _errmsg
  51. #endif
  52. /***
  53. *char *_strerror(message) - get system error message
  54. *
  55. *Purpose:
  56. * builds an error message consisting of the users error message
  57. * (the message parameter), followed by ": ", followed by the system
  58. * error message (index through errno), followed by a newline. If
  59. * message is NULL or a null string, returns a pointer to just
  60. * the system error message.
  61. *
  62. *Entry:
  63. * char *message - user's message to prefix system error message
  64. *
  65. *Exit:
  66. * returns pointer to static memory containing error message.
  67. * returns NULL if malloc() fails in multi-thread versions.
  68. *
  69. *Exceptions:
  70. *
  71. *******************************************************************************/
  72. #ifdef _UNICODE
  73. wchar_t * __cdecl __wcserror(
  74. #else
  75. char * __cdecl _strerror (
  76. #endif
  77. REG1 const _TCHAR *message
  78. )
  79. {
  80. #ifdef _MT
  81. _ptiddata ptd = _getptd();
  82. _TCHAR *bldmsg;
  83. #else
  84. static _TCHAR bldmsg[_ERRMSGLEN_];
  85. #endif
  86. #ifdef _MT
  87. /* Use per thread buffer area (malloc space, if necessary) */
  88. /* [NOTE: This buffer is shared between _strerror and streror.] */
  89. if ( (ptd->_terrmsg == NULL) && ((ptd->_terrmsg =
  90. _malloc_crt(_ERRMSGLEN_ * sizeof(_TCHAR))) == NULL) )
  91. return(NULL);
  92. bldmsg = ptd->_terrmsg;
  93. #endif
  94. /* Build the error message */
  95. bldmsg[0] = '\0';
  96. if (message && *message) {
  97. _tcscat( bldmsg, message );
  98. _tcscat( bldmsg, _T(": "));
  99. }
  100. #ifdef _UNICODE
  101. mbstowcs(bldmsg + wcslen(bldmsg), _sys_err_msg( errno ), _ERRMSGLEN_ - wcslen(bldmsg));
  102. #else
  103. strcat( bldmsg, _sys_err_msg( errno ) );
  104. #endif
  105. return( _tcscat( bldmsg, _T("\n")) );
  106. }