home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / strerror.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  91 lines

  1. /***
  2. *strerror.c - Contains the strerror C runtime.
  3. *
  4. *       Copyright (c) 1987-1997, 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. *******************************************************************************/
  15.  
  16. #include <cruntime.h>
  17. #include <errmsg.h>
  18. #include <stdlib.h>
  19. #include <syserr.h>
  20. #include <string.h>
  21. #include <mtdll.h>
  22. #ifdef _MT
  23. #include <malloc.h>
  24. #include <stddef.h>
  25. #endif  /* _MT */
  26. #include <dbgint.h>
  27.  
  28. /* [NOTE: The _MT error message buffer is shared by both strerror
  29.    and _strerror so must be the max length of both. */
  30. #ifdef _MT
  31. /* Max length of message = user_string(94)+system_string+2 */
  32. #define _ERRMSGLEN_ 94+_SYS_MSGMAX+2
  33. #else  /* _MT */
  34. /* Max length of message = system_string+2 */
  35. #define _ERRMSGLEN_ _SYS_MSGMAX+2
  36. #endif  /* _MT */
  37.  
  38.  
  39. /***
  40. *char *strerror(errnum) - Map error number to error message string.
  41. *
  42. *Purpose:
  43. *       The strerror runtime takes an error number for input and
  44. *       returns the corresponding error message string.  This routine
  45. *       conforms to the ANSI standard interface.
  46. *
  47. *Entry:
  48. *       int errnum - Integer error number (corresponding to an errno value).
  49. *
  50. *Exit:
  51. *       char * - Strerror returns a pointer to the error message string.
  52. *       This string is internal to the strerror routine (i.e., not supplied
  53. *       by the user).
  54. *
  55. *Exceptions:
  56. *       None.
  57. *
  58. *******************************************************************************/
  59.  
  60. char * __cdecl strerror (
  61.         int errnum
  62.         )
  63. {
  64. #ifdef _MT
  65.  
  66.         _ptiddata ptd = _getptd();
  67.  
  68.         char *errmsg;
  69.         static char errmsg_backup[_SYS_MSGMAX+2];
  70.  
  71. #else  /* _MT */
  72.  
  73.         static char errmsg[_ERRMSGLEN_];  /* Longest errmsg + \0 */
  74.  
  75. #endif  /* _MT */
  76.  
  77. #ifdef _MT
  78.  
  79.         if ( (ptd->_errmsg == NULL) && ((ptd->_errmsg =
  80.             _malloc_crt(_ERRMSGLEN_))
  81.             == NULL) )
  82.                 errmsg = errmsg_backup; /* error: use backup */
  83.         else
  84.                 errmsg = ptd->_errmsg;
  85.  
  86. #endif  /* _MT */
  87.  
  88.         strcpy(errmsg, _sys_err_msg(errnum));
  89.         return(errmsg);
  90. }
  91.