Download | Plain Text | No Line Numbers


  1. /*
  2.  * Name: error
  3.  * Author: Manuel Mausz, 0728348
  4.  * Description: Shared definitions and functions
  5.  * Created: 24.05.2009
  6.  */
  7.  
  8. #include <stdarg.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <signal.h>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include "error.h"
  17.  
  18. /* error variables */
  19. int error = 0; /* error flag */
  20. char *me = NULL; /* name of myself (argv[0]) */
  21. int child = 0; /* flag being the child */
  22.  
  23. /* function declerations */
  24. void free_resources(void);
  25.  
  26. /*
  27.  * NAME: vprintferr
  28.  * PURPOSE:
  29.  * prints error message to stderr and sets global error flag to 1. if perrno
  30.  * and errno is set the error message string corresponding to errno will be
  31.  * appended
  32.  * output format: "argv[0]: fmt\n" respectively "argv[0]: fmt: errorstr\n"
  33.  *
  34.  * PARAMETERS:
  35.  * int perrno ... print errno if errno is set
  36.  * char *fmt ... format string to be passed to printf
  37.  * va_list ap ... variable argument list structure
  38.  *
  39.  * RETURN VALUE:
  40.  * void
  41.  *
  42.  * GLOBAL VARS:
  43.  * me, error, child
  44.  */
  45. void vprintferr(int perrno, const char *fmt, va_list ap)
  46. {
  47. (void) fprintf(stderr, "%s: ", me);
  48. #ifdef DEBUG
  49. if (child)
  50. (void) fprintf(stderr, "[CHILD] ");
  51. #endif
  52. (void) vfprintf(stderr, fmt, ap);
  53. if (perrno && errno)
  54. (void) fprintf(stderr, ": %s", strerror(errno));
  55. (void) fprintf(stderr, "\n");
  56. error = 1;
  57. }
  58.  
  59. /*
  60.  * NAME: printferr
  61.  * PURPOSE:
  62.  * wrapper for vprintferr to support variable argument list (like printf)
  63.  * see vprintferr for more information
  64.  *
  65.  * PARAMETERS:
  66.  * int perrno ... print errno if errno is set
  67.  * char *fmt ... format string to be passed to vprintferr (see printf)
  68.  * ... ... arguments according to fmt (see printf)
  69.  *
  70.  * RETURN VALUE:
  71.  * void
  72.  */
  73. void printferr(int perrno, const char *fmt, ...)
  74. {
  75. va_list ap;
  76.  
  77. (void) va_start(ap, fmt);
  78. (void) vprintferr(perrno, fmt, ap);
  79. (void) va_end(ap);
  80. }
  81.  
  82. /*
  83.  * NAME: bailout
  84.  * PURPOSE:
  85.  * prints formated error message using vprintferr, frees resources and exits
  86.  * with EXIT_FAILURE. Also signal parent using SIGPIPE if its the child.
  87.  * printing can be omitted by setting fmt to NULL
  88.  * see vprintferr for more information
  89.  *
  90.  * PARAMETERS:
  91.  * int perrno ... print errno if errno is set
  92.  * char *fmt ... format string to be passed to vprintferr (see printf)
  93.  * ... ... arguments according to fmt (see printf)
  94.  *
  95.  * RETURN VALUE:
  96.  * void
  97.  *
  98.  * GLOBAL VARS:
  99.  * child
  100.  */
  101. void bailout(int perrno, const char *fmt, ...)
  102. {
  103. va_list ap;
  104.  
  105. if (fmt != NULL)
  106. {
  107. (void) va_start(ap, fmt);
  108. (void) vprintferr(perrno, fmt, ap);
  109. (void) va_end(ap);
  110. }
  111. if (child)
  112. kill(getppid(), SIGPIPE);
  113. (void) free_resources();
  114. (void) exit(EXIT_FAILURE);
  115. }
  116.