Download | Plain Text | No Line Numbers


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