err.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
  2. *
  3. * GNU Library General Public License (LGPL) version 2 or later.
  4. *
  5. * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
  6. */
  7. #define _GNU_SOURCE
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdarg.h>
  12. #include <errno.h>
  13. #include <err.h>
  14. #ifdef __UCLIBC_HAS_THREADS__
  15. #include <pthread.h>
  16. #endif
  17. #ifdef __UCLIBC_MJN3_ONLY__
  18. #warning REMINDER: Need a centralized __progname prototype.
  19. #warning REMINDER: Deal with wide oriented stderr case.
  20. #endif
  21. extern const char *__progname;
  22. static void vwarn_work(const char *format, va_list args, int showerr)
  23. {
  24. /* 0123 45678 9 a b*/
  25. static const char fmt[] = "%s: \0: %s\n\0\n";
  26. const char *f;
  27. char buf[64];
  28. __STDIO_AUTO_THREADLOCK_VAR;
  29. /* Do this first, in case something below changes errno. */
  30. f = fmt + 11; /* At 11. */
  31. if (showerr) {
  32. f -= 4; /* At 7. */
  33. __xpg_strerror_r(errno, buf, sizeof(buf));
  34. }
  35. __STDIO_AUTO_THREADLOCK(stderr);
  36. fprintf(stderr, fmt, __progname);
  37. if (format) {
  38. vfprintf(stderr, format, args);
  39. f -= 2; /* At 5 (showerr) or 9. */
  40. }
  41. fprintf(stderr, f, buf);
  42. __STDIO_AUTO_THREADUNLOCK(stderr);
  43. }
  44. extern void warn(const char *format, ...)
  45. {
  46. va_list args;
  47. va_start(args, format);
  48. vwarn(format, args);
  49. va_end(args);
  50. }
  51. extern void vwarn(const char *format, va_list args)
  52. {
  53. vwarn_work(format, args, 1);
  54. }
  55. extern void warnx(const char *format, ...)
  56. {
  57. va_list args;
  58. va_start(args, format);
  59. vwarnx(format, args);
  60. va_end(args);
  61. }
  62. extern void vwarnx(const char *format, va_list args)
  63. {
  64. vwarn_work(format, args, 0);
  65. }
  66. extern void err(int status, const char *format, ...)
  67. {
  68. va_list args;
  69. va_start(args, format);
  70. verr(status, format, args);
  71. /* This should get optimized away. We'll leave it now for safety. */
  72. va_end(args);
  73. }
  74. extern void verr(int status, const char *format, va_list args)
  75. {
  76. vwarn(format, args);
  77. exit(status);
  78. }
  79. extern void errx(int status, const char *format, ...)
  80. {
  81. va_list args;
  82. va_start(args, format);
  83. verrx(status, format, args);
  84. /* This should get optimized away. We'll leave it now for safety. */
  85. va_end(args);
  86. }
  87. extern void verrx(int status, const char *format, va_list args)
  88. {
  89. vwarnx(format, args);
  90. exit(status);
  91. }