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