err.c 2.5 KB

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