err.c 2.3 KB

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