A mail formatter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

111 lines
2.3 KiB

  1. /*
  2. * err - Incenp.org Notch Library: (v)(err|warn)(x) replacement
  3. * Copyright (C) 2011 Damien Goutte-Gattat
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <stdarg.h>
  26. #define ERR_EXIT_ON_ERROR 0x01
  27. #define ERR_PRINT_ERRNO 0x02
  28. const char *getprogname(void);
  29. static void
  30. do_error(int opt, int eval, const char *fmt, va_list ap)
  31. {
  32. fprintf(stderr, "%s: ", getprogname());
  33. if ( fmt )
  34. vfprintf(stderr, fmt, ap);
  35. if ( opt & ERR_PRINT_ERRNO )
  36. fprintf(stderr, ": %s", strerror(errno));
  37. fputc('\n', stderr);
  38. if ( opt & ERR_EXIT_ON_ERROR )
  39. exit(eval);
  40. }
  41. void
  42. err(int eval, const char *fmt, ...)
  43. {
  44. va_list ap;
  45. va_start(ap, fmt);
  46. do_error(ERR_EXIT_ON_ERROR | ERR_PRINT_ERRNO, eval, fmt, ap);
  47. va_end(ap);
  48. }
  49. void
  50. errx(int eval, const char *fmt, ...)
  51. {
  52. va_list ap;
  53. va_start(ap, fmt);
  54. do_error(ERR_EXIT_ON_ERROR, eval, fmt, ap);
  55. va_end(ap);
  56. }
  57. void
  58. verr(int eval, const char *fmt, va_list ap)
  59. {
  60. do_error(ERR_EXIT_ON_ERROR | ERR_PRINT_ERRNO, eval, fmt, ap);
  61. }
  62. void
  63. verrx(int eval, const char *fmt, va_list ap)
  64. {
  65. do_error(ERR_EXIT_ON_ERROR, eval, fmt, ap);
  66. }
  67. void
  68. warn(const char *fmt, ...)
  69. {
  70. va_list ap;
  71. va_start(ap, fmt);
  72. do_error(ERR_PRINT_ERRNO, 0, fmt, ap);
  73. va_end(ap);
  74. }
  75. void
  76. warnx(const char *fmt, ...)
  77. {
  78. va_list ap;
  79. va_start(ap, fmt);
  80. do_error(0, 0, fmt, ap);
  81. va_end(ap);
  82. }
  83. void
  84. vwarn(const char *fmt, va_list ap)
  85. {
  86. do_error(ERR_PRINT_ERRNO, 0, fmt, ap);
  87. }
  88. void
  89. vwarnx(const char *fmt, va_list ap)
  90. {
  91. do_error(0, 0, fmt, ap);
  92. }