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.

111 lines
3.4 KiB

  1. /***
  2. *perror.c - print system error message
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines perror() - print system error message
  8. * System error message are indexed by errno; conforms to XENIX
  9. * standard, with much compatability with 1983 uniforum draft standard.
  10. *
  11. *Revision History:
  12. * 09-02-83 RN initial version
  13. * 04-13-87 JCR added const to declaration
  14. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  15. * 12-29-87 JCR Multi-thread support
  16. * 05-31-88 PHG Merged DLL and normal versions
  17. * 06-03-88 JCR Added <io.h> to so _write_lk evaluates correctly and
  18. * added (char *)message casts to get rid of warnings
  19. * 03-15-90 GJF Replace _LOAD_DS with _CALLTYPE1, added #include
  20. * <cruntime.h>, removed #include <register.h> and fixed
  21. * the copyright. Also, cleaned up the formatting a bit.
  22. * 04-05-90 GJF Added #include <string.h>.
  23. * 08-14-90 SBM Removed unneeded #include <errmsg.h>
  24. * 10-04-90 GJF New-style function declarator.
  25. * 08-26-92 GJF Include unistd.h for POSIX build.
  26. * 10-16-92 XY Mac version: use buffered fprintf, can't assume stderr
  27. * is 2
  28. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  29. * 02-16-95 JWM Mac merge.
  30. * 03-29-95 BWT Add write_lk prototype for POSIX build.
  31. * 09-26-97 BWT Fix POSIX
  32. * 01-06-98 GJF Exception-safe locking.
  33. * 01-04-99 GJF Changes for 64-bit size_t.
  34. * 05-17-99 PML Remove all Macintosh support.
  35. *
  36. *******************************************************************************/
  37. #include <cruntime.h>
  38. #ifdef _POSIX_
  39. #include <unistd.h>
  40. #endif
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <syserr.h>
  45. #include <mtdll.h>
  46. #include <io.h>
  47. /***
  48. *void perror(message) - print system error message
  49. *
  50. *Purpose:
  51. * prints user's error message, then follows it with ": ", then the system
  52. * error message, then a newline. All output goes to stderr. If user's
  53. * message is NULL or a null string, only the system error message is
  54. * printer. If errno is weird, prints "Unknown error".
  55. *
  56. *Entry:
  57. * const char *message - users message to prefix system error message
  58. *
  59. *Exit:
  60. * Prints message; no return value.
  61. *
  62. *Exceptions:
  63. *
  64. *******************************************************************************/
  65. void __cdecl perror (
  66. REG1 const char *message
  67. )
  68. {
  69. #if !defined(_POSIX_)
  70. REG2 int fh = 2;
  71. #ifdef _MT
  72. _lock_fh( fh ); /* acquire file handle lock */
  73. __try {
  74. #endif
  75. #endif /* !_POSIX_ */
  76. if (message && *message)
  77. {
  78. #if !defined(_POSIX_)
  79. _write_lk(fh,(char *)message,(unsigned int)strlen(message));
  80. _write_lk(fh,": ",2);
  81. #else /* !_POSIX_ */
  82. fprintf(stderr,"%s", (char *)message);
  83. fprintf(stderr,": ");
  84. #endif /* !_POSIX_ */
  85. }
  86. message = _sys_err_msg( errno );
  87. #if !defined(_POSIX_)
  88. _write_lk(fh,(char *)message,(unsigned int)strlen(message));
  89. _write_lk(fh,"\n",1);
  90. #ifdef _MT
  91. }
  92. __finally {
  93. _unlock_fh( fh ); /* release file handle lock */
  94. }
  95. #endif
  96. #else /* !_POSIX_ */
  97. fprintf(stderr,"%s\n", (char *)message);
  98. #endif /* !_POSIX_ */
  99. }