err.c 2.6 KB

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