Download | Plain Text | No Line Numbers


  1. /*
  2.  * Name: global
  3.  * Author: Manuel Mausz, 0728348
  4.  * Description: Shared definitions and functions
  5.  * Created: 24.04.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 "global.h"
  14.  
  15. /* global variables */
  16. volatile sig_atomic_t error = 0; /* error flag */
  17. char *me = NULL; /* name of myself (argv[0]) */
  18.  
  19. /* function declerations */
  20. void free_resources(void);
  21.  
  22. /*
  23.  * NAME: vprintferr
  24.  * PURPOSE:
  25.  * prints error message to stderr and sets global error flag to 1. if errno
  26.  * is set the error message string corresponding to errno will be appended
  27.  * output format: "argv[0]: fmt\n" respectively "argv[0]: fmt: errorstr\n"
  28.  *
  29.  * PARAMETERS:
  30.  * char *fmt ... format string to be passed to printf
  31.  * va_list ap ... variable argument list structure
  32.  *
  33.  * RETURN VALUE:
  34.  * void
  35.  *
  36.  * GLOBAL VARS:
  37.  * me, error
  38.  */
  39. void vprintferr(const char *fmt, va_list ap)
  40. {
  41. (void) fprintf(stderr, "%s: ", me);
  42. (void) vfprintf(stderr, fmt, ap);
  43. if (errno)
  44. (void) fprintf(stderr, ": %s", strerror(errno));
  45. (void) fprintf(stderr, "\n");
  46. error = 1;
  47. }
  48.  
  49. /*
  50.  * NAME: printferr
  51.  * PURPOSE:
  52.  * wrapper for vprintferr to support variable argument list (like printf)
  53.  * see vprintferr for more information
  54.  *
  55.  * PARAMETERS:
  56.  * char *fmt ... format string to be passed to vprintferr (see printf)
  57.  * ... ... arguments according to fmt (see printf)
  58.  *
  59.  * RETURN VALUE:
  60.  * void
  61.  */
  62. void printferr(const char *fmt, ...)
  63. {
  64. va_list ap;
  65.  
  66. (void) va_start(ap, fmt);
  67. (void) vprintferr(fmt, ap);
  68. (void) va_end(ap);
  69. }
  70.  
  71. /*
  72.  * NAME: bailout
  73.  * PURPOSE:
  74.  * prints formated error message using vprintferr and exits with EXIT_FAILURE
  75.  * printing can be omitted by setting fmt to NULL
  76.  * see vprintferr for more information
  77.  *
  78.  * PARAMETERS:
  79.  * char *fmt ... format string to be passed to vprintferr (see printf)
  80.  * ... ... arguments according to fmt (see printf)
  81.  *
  82.  * RETURN VALUE:
  83.  * void
  84.  */
  85. void bailout(const char *fmt, ...)
  86. {
  87. va_list ap;
  88.  
  89. if (fmt != NULL)
  90. {
  91. (void) va_start(ap, fmt);
  92. (void) vprintferr(fmt, ap);
  93. (void) va_end(ap);
  94. }
  95. (void) free_resources();
  96. (void) exit(EXIT_FAILURE);
  97. }
  98.