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.

122 lines
3.1 KiB

  1. /***
  2. *strerror.c - Contains the strerror C runtime.
  3. *
  4. * Copyright (c) 1987-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * The strerror runtime accepts an error number as input
  8. * and returns the corresponding error string.
  9. *
  10. * NOTE: The "old" strerror C runtime resides in file _strerr.c
  11. * and is now called _strerror. The new strerror runtime
  12. * conforms to the ANSI standard.
  13. *
  14. *Revision History:
  15. * 02-24-87 JCR Module created.
  16. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  17. * 01-04-87 JCR Improved code.
  18. * 01-05-87 JCR Multi-thread support
  19. * 05-31-88 PHG Merge DLL and normal versions
  20. * 06-06-89 JCR 386 mthread support
  21. * 03-16-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  22. * <cruntime.h> and fixed the copyright. Also, cleaned
  23. * up the formatting a bit.
  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. * 11-24-99 GB Added support for wide char by adding wcserror()
  32. *
  33. *******************************************************************************/
  34. #include <cruntime.h>
  35. #include <errmsg.h>
  36. #include <stdlib.h>
  37. #include <syserr.h>
  38. #include <string.h>
  39. #include <mtdll.h>
  40. #include <tchar.h>
  41. #ifdef _MT
  42. #include <malloc.h>
  43. #include <stddef.h>
  44. #endif
  45. #include <dbgint.h>
  46. /* [NOTE: The _MT error message buffer is shared by both strerror
  47. and _strerror so must be the max length of both. */
  48. #ifdef _MT
  49. /* Max length of message = user_string(94)+system_string+2 */
  50. #define _ERRMSGLEN_ 94+_SYS_MSGMAX+2
  51. #else
  52. /* Max length of message = system_string+2 */
  53. #define _ERRMSGLEN_ _SYS_MSGMAX+2
  54. #endif
  55. #ifdef _UNICODE
  56. #define _terrmsg _werrmsg
  57. #else
  58. #define _terrmsg _errmsg
  59. #endif
  60. /***
  61. *char *strerror(errnum) - Map error number to error message string.
  62. *
  63. *Purpose:
  64. * The strerror runtime takes an error number for input and
  65. * returns the corresponding error message string. This routine
  66. * conforms to the ANSI standard interface.
  67. *
  68. *Entry:
  69. * int errnum - Integer error number (corresponding to an errno value).
  70. *
  71. *Exit:
  72. * char * - Strerror returns a pointer to the error message string.
  73. * This string is internal to the strerror routine (i.e., not supplied
  74. * by the user).
  75. *
  76. *Exceptions:
  77. * None.
  78. *
  79. *******************************************************************************/
  80. #ifdef _UNICODE
  81. wchar_t * cdecl _wcserror(
  82. #else
  83. char * __cdecl strerror (
  84. #endif
  85. int errnum
  86. )
  87. {
  88. #ifdef _MT
  89. _ptiddata ptd = _getptd();
  90. _TCHAR *errmsg;
  91. static _TCHAR errmsg_backup[_SYS_MSGMAX+2];
  92. #else
  93. static _TCHAR errmsg[_ERRMSGLEN_]; /* Longest errmsg + \0 */
  94. #endif
  95. #ifdef _MT
  96. if ( (ptd->_terrmsg == NULL) && ((ptd->_terrmsg =
  97. _malloc_crt(_ERRMSGLEN_ * sizeof(_TCHAR)))
  98. == NULL) )
  99. errmsg = errmsg_backup; /* error: use backup */
  100. else
  101. errmsg = ptd->_terrmsg;
  102. #endif
  103. #ifdef _UNICODE
  104. mbstowcs(errmsg, _sys_err_msg(errnum), _ERRMSGLEN_);
  105. #else
  106. strcpy(errmsg, _sys_err_msg(errnum));
  107. #endif
  108. return(errmsg);
  109. }