errstr-Nt.c 961 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "lib9.h"
  2. #include <Windows.h>
  3. static char errstring[ERRMAX];
  4. enum
  5. {
  6. Magic = 0xffffff
  7. };
  8. static void
  9. winerror(int e, char *buf, uint nerr)
  10. {
  11. int r;
  12. char buf2[ERRMAX], *p, *q;
  13. r = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  14. 0, e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  15. buf2, sizeof(buf2), 0);
  16. if(r == 0)
  17. snprint(buf2, ERRMAX, "windows error %d", e);
  18. q = buf2;
  19. for(p = buf2; *p; p++) {
  20. if(*p == '\r')
  21. continue;
  22. if(*p == '\n')
  23. *q++ = ' ';
  24. else
  25. *q++ = *p;
  26. }
  27. *q = '\0';
  28. utfecpy(buf, buf+nerr, buf2);
  29. }
  30. void
  31. werrstr(char *fmt, ...)
  32. {
  33. va_list arg;
  34. va_start(arg, fmt);
  35. vseprint(errstring, errstring+sizeof(errstring), fmt, arg);
  36. va_end(arg);
  37. SetLastError(Magic);
  38. }
  39. int
  40. errstr(char *buf, uint nerr)
  41. {
  42. DWORD le;
  43. le = GetLastError();
  44. if(le == Magic)
  45. utfecpy(buf, buf+nerr, errstring);
  46. else
  47. winerror(le, buf, nerr);
  48. return 1;
  49. }
  50. void
  51. oserrstr(char *buf, uint nerr)
  52. {
  53. winerror(GetLastError(), buf, nerr);
  54. }