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