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.

101 lines
2.6 KiB

  1. /***
  2. *wperror.c - print system error message (wchar_t version)
  3. *
  4. * Copyright (c) 1993-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _wperror() - print wide system error message
  8. * System error message are indexed by errno.
  9. *
  10. *Revision History:
  11. * 12-07-93 CFW Module created from perror.
  12. * 02-07-94 CFW POSIXify.
  13. * 01-10-95 CFW Debug CRT allocs.
  14. * 01-06-98 GJF Exception-safe locking.
  15. * 09-23-98 GJF Fixed handling of NULL or empty string arg.
  16. * 01-06-99 GJF Changes for 64-bit size_t.
  17. *
  18. *******************************************************************************/
  19. #ifndef _POSIX_
  20. #include <cruntime.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <syserr.h>
  25. #include <mtdll.h>
  26. #include <io.h>
  27. #include <dbgint.h>
  28. /***
  29. *void _wperror(wmessage) - print system error message
  30. *
  31. *Purpose:
  32. * prints user's error message, then follows it with ": ", then the system
  33. * error message, then a newline. All output goes to stderr. If user's
  34. * message is NULL or a null string, only the system error message is
  35. * printer. If errno is weird, prints "Unknown error".
  36. *
  37. *Entry:
  38. * const wchar_t *wmessage - users message to prefix system error message
  39. *
  40. *Exit:
  41. * Prints message; no return value.
  42. *
  43. *Exceptions:
  44. *
  45. *******************************************************************************/
  46. void __cdecl _wperror (
  47. const wchar_t *wmessage
  48. )
  49. {
  50. int fh = 2;
  51. size_t size;
  52. char *amessage;
  53. /* convert WCS string into ASCII string */
  54. if ( wmessage && *wmessage )
  55. {
  56. size = wcslen(wmessage) + 1;
  57. if ( NULL == (amessage = (char *)_malloc_crt(size * sizeof(char))) )
  58. return;
  59. if ( 0 >= wcstombs(amessage, wmessage, size) )
  60. {
  61. _free_crt(amessage);
  62. return;
  63. }
  64. }
  65. else
  66. amessage = NULL;
  67. #ifdef _MT
  68. _lock_fh( fh ); /* acquire file handle lock */
  69. __try {
  70. #endif
  71. if ( amessage )
  72. {
  73. _write_lk(fh,(char *)amessage,(unsigned)strlen(amessage));
  74. _write_lk(fh,": ",2);
  75. }
  76. _free_crt(amessage); /* note: freeing NULL is legal and benign */
  77. amessage = _sys_err_msg( errno );
  78. _write_lk(fh,(char *)amessage,(unsigned)strlen(amessage));
  79. _write_lk(fh,"\n",1);
  80. #ifdef _MT
  81. }
  82. __finally {
  83. _unlock_fh( fh ); /* release file handle lock */
  84. }
  85. #endif
  86. }
  87. #endif /* _POSIX_ */