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

  1. /***
  2. *_strerr.c - routine for indexing into system error list
  3. *
  4. *       Copyright (c) 1985-1997, 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. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <stdlib.h>
  14. #include <errmsg.h>
  15. #include <syserr.h>
  16. #include <string.h>
  17. #include <malloc.h>
  18. #include <mtdll.h>
  19. #include <dbgint.h>
  20.  
  21. /* Max length of message = user_string(94)+system_string+2 */
  22. /* [NOTE: The mthread error message buffer is shared by both strerror
  23.    and _strerror so must be the max length of both. */
  24. #define _ERRMSGLEN_ 94+_SYS_MSGMAX+2
  25.  
  26. /***
  27. *char *_strerror(message) - get system error message
  28. *
  29. *Purpose:
  30. *       builds an error message consisting of the users error message
  31. *       (the message parameter), followed by ": ", followed by the system
  32. *       error message (index through errno), followed by a newline.  If
  33. *       message is NULL or a null string, returns a pointer to just
  34. *       the system error message.
  35. *
  36. *Entry:
  37. *       char *message - user's message to prefix system error message
  38. *
  39. *Exit:
  40. *       returns pointer to static memory containing error message.
  41. *       returns NULL if malloc() fails in multi-thread versions.
  42. *
  43. *Exceptions:
  44. *
  45. *******************************************************************************/
  46.  
  47. char * __cdecl _strerror (
  48.         REG1 const char *message
  49.         )
  50. {
  51. #ifdef _MT
  52.  
  53.         _ptiddata ptd = _getptd();
  54.         char *bldmsg;
  55.  
  56. #else  /* _MT */
  57.  
  58.         static char bldmsg[_ERRMSGLEN_];
  59.  
  60. #endif  /* _MT */
  61.  
  62.  
  63. #ifdef _MT
  64.  
  65.         /* Use per thread buffer area (malloc space, if necessary) */
  66.         /* [NOTE: This buffer is shared between _strerror and streror.] */
  67.  
  68.         if ( (ptd->_errmsg == NULL) && ((ptd->_errmsg =
  69.             _malloc_crt(_ERRMSGLEN_)) == NULL) )
  70.                     return(NULL);
  71.         bldmsg = ptd->_errmsg;
  72.  
  73. #endif  /* _MT */
  74.  
  75.         /* Build the error message */
  76.  
  77.         bldmsg[0] = '\0';
  78.  
  79.         if (message && *message) {
  80.                 strcat( bldmsg, message );
  81.                 strcat( bldmsg, ": " );
  82.         }
  83.  
  84.         strcat( bldmsg, _sys_err_msg( errno ) );
  85.  
  86.         return( strcat( bldmsg, "\n" ) );
  87. }
  88.