err.c 2.6 KB

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