err.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdarg.h>
  11. #include <errno.h>
  12. #include <err.h>
  13. #ifdef __UCLIBC_HAS_THREADS__
  14. #include <pthread.h>
  15. #endif
  16. #ifdef __UCLIBC_MJN3_ONLY__
  17. #warning REMINDER: Deal with wide oriented stderr case.
  18. #endif
  19. #if defined __USE_BSD
  20. static void vwarn_work(const char *format, va_list args, int showerr)
  21. {
  22. /* 0123 45678 9 a b*/
  23. static const char fmt[] = "%s: \0: %s\n\0\n";
  24. const char *f;
  25. char buf[64];
  26. __STDIO_AUTO_THREADLOCK_VAR;
  27. /* Do this first, in case something below changes errno. */
  28. f = fmt + 11; /* At 11. */
  29. if (showerr) {
  30. f -= 4; /* At 7. */
  31. __xpg_strerror_r(errno, buf, sizeof(buf));
  32. }
  33. __STDIO_AUTO_THREADLOCK(stderr);
  34. fprintf(stderr, fmt, __uclibc_progname);
  35. if (format) {
  36. vfprintf(stderr, format, args);
  37. f -= 2; /* At 5 (showerr) or 9. */
  38. }
  39. fprintf(stderr, f, buf);
  40. __STDIO_AUTO_THREADUNLOCK(stderr);
  41. }
  42. void vwarn(const char *format, va_list args)
  43. {
  44. vwarn_work(format, args, 1);
  45. }
  46. libc_hidden_def(vwarn)
  47. void warn(const char *format, ...)
  48. {
  49. va_list args;
  50. va_start(args, format);
  51. vwarn(format, args);
  52. va_end(args);
  53. }
  54. void vwarnx(const char *format, va_list args)
  55. {
  56. vwarn_work(format, args, 0);
  57. }
  58. libc_hidden_def(vwarnx)
  59. void warnx(const char *format, ...)
  60. {
  61. va_list args;
  62. va_start(args, format);
  63. vwarnx(format, args);
  64. va_end(args);
  65. }
  66. void verr(int status, const char *format, va_list args)
  67. {
  68. vwarn(format, args);
  69. exit(status);
  70. }
  71. libc_hidden_def(verr)
  72. void attribute_noreturn err(int status, const char *format, ...)
  73. {
  74. va_list args;
  75. va_start(args, format);
  76. verr(status, format, args);
  77. /* This should get optimized away. We'll leave it now for safety. */
  78. /* The loop is added only to keep gcc happy. */
  79. while(1)
  80. va_end(args);
  81. }
  82. void verrx(int status, const char *format, va_list args)
  83. {
  84. vwarnx(format, args);
  85. exit(status);
  86. }
  87. libc_hidden_def(verrx)
  88. void attribute_noreturn errx(int status, const char *format, ...)
  89. {
  90. va_list args;
  91. va_start(args, format);
  92. verrx(status, format, args);
  93. /* This should get optimized away. We'll leave it now for safety. */
  94. /* The loop is added only to keep gcc happy. */
  95. while(1)
  96. va_end(args);
  97. }
  98. #endif