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.

126 lines
3.5 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. * 10-19-01 BWT If we're unable to allocate space for the error message
  33. * just return string from ENOMEM (Not enough space).
  34. * 12-12-01 BWT Replace getptd with getptd_noexit - return not enough
  35. * space if we can't retrieve the ptd - don't exit.
  36. *
  37. *******************************************************************************/
  38. #include <cruntime.h>
  39. #include <errmsg.h>
  40. #include <stdlib.h>
  41. #include <syserr.h>
  42. #include <string.h>
  43. #include <mtdll.h>
  44. #include <tchar.h>
  45. #ifdef _MT
  46. #include <malloc.h>
  47. #include <stddef.h>
  48. #endif
  49. #include <dbgint.h>
  50. /* [NOTE: The _MT error message buffer is shared by both strerror
  51. and _strerror so must be the max length of both. */
  52. #ifdef _MT
  53. /* Max length of message = user_string(94)+system_string+2 */
  54. #define _ERRMSGLEN_ 94+_SYS_MSGMAX+2
  55. #else
  56. /* Max length of message = system_string+2 */
  57. #define _ERRMSGLEN_ _SYS_MSGMAX+2
  58. #endif
  59. #ifdef _UNICODE
  60. #define _terrmsg _werrmsg
  61. #else
  62. #define _terrmsg _errmsg
  63. #endif
  64. /***
  65. *char *strerror(errnum) - Map error number to error message string.
  66. *
  67. *Purpose:
  68. * The strerror runtime takes an error number for input and
  69. * returns the corresponding error message string. This routine
  70. * conforms to the ANSI standard interface.
  71. *
  72. *Entry:
  73. * int errnum - Integer error number (corresponding to an errno value).
  74. *
  75. *Exit:
  76. * char * - Strerror returns a pointer to the error message string.
  77. * This string is internal to the strerror routine (i.e., not supplied
  78. * by the user).
  79. *
  80. *Exceptions:
  81. * None.
  82. *
  83. *******************************************************************************/
  84. #ifdef _UNICODE
  85. wchar_t * cdecl _wcserror(
  86. #else
  87. char * __cdecl strerror (
  88. #endif
  89. int errnum
  90. )
  91. {
  92. #ifdef _MT
  93. _TCHAR *errmsg;
  94. _ptiddata ptd = _getptd_noexit();
  95. if (!ptd)
  96. return _T("Not enough space");
  97. #else
  98. static _TCHAR errmsg[_ERRMSGLEN_]; /* Longest errmsg + \0 */
  99. #endif
  100. #ifdef _MT
  101. if ( (ptd->_terrmsg == NULL) && ((ptd->_terrmsg =
  102. _malloc_crt(_ERRMSGLEN_ * sizeof(_TCHAR)))
  103. == NULL) )
  104. return _T("Not enough space");
  105. else
  106. errmsg = ptd->_terrmsg;
  107. #endif
  108. #ifdef _UNICODE
  109. mbstowcs(errmsg, _sys_err_msg(errnum), _ERRMSGLEN_);
  110. #else
  111. strcpy(errmsg, _sys_err_msg(errnum));
  112. #endif
  113. return(errmsg);
  114. }