err.c 2.4 KB

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