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