err.c 2.6 KB

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