1
+
/*
 
2
+
** $Id: util.c,v 1.13 2010-01-01 01:34:07 rosinski Exp $
 
3
+
*/
 
4
+
 
5
+
#include <stdarg.h>
 
6
+
#include <stdio.h>
 
7
+
#include <stdlib.h>
 
8
+
 
9
+
#include "private.h"
 
10
+
 
11
+
static bool abort_on_error = false; /* flag says to abort on any error */
 
12
+
static int max_error = 500;         /* max number of error print msgs */
 
13
+
 
14
+
/*
 
15
+
** GPTLerror: error return routine to print a message and return a failure
 
16
+
** value.
 
17
+
**
 
18
+
** Input arguments:
 
19
+
**   fmt: format string
 
20
+
**   variable list of additional arguments for vfprintf
 
21
+
**
 
22
+
** Return value: -1 (failure)
 
23
+
*/
 
24
+
 
25
+
int GPTLerror (const char *fmt, ...)
 
26
+
{
 
27
+
  va_list args;
 
28
+
  
 
29
+
  va_start (args, fmt);
 
30
+
  static int num_error = 0;
 
31
+
  
 
32
+
  if (fmt != NULL && num_error < max_error) {
 
33
+
#ifndef NO_VPRINTF
 
34
+
    (void) vfprintf (stderr, fmt, args);
 
35
+
#else
 
36
+
    (void) fprintf (stderr, "GPTLerror: no vfprintf: fmt is %s\n", fmt);
 
37
+
#endif
 
38
+
    if (num_error == max_error)
 
39
+
      (void) fprintf (stderr, "Truncating further error print now after %d msgs",
 
40
+
              num_error);
 
41
+
    ++num_error;
 
42
+
  }    
 
43
+
  
 
44
+
  va_end (args);
 
45
+
  
 
46
+
  if (abort_on_error)
 
47
+
    exit (-1);
 
48
+
 
49
+
  return (-1);
 
50
+
}
 
51
+
 
52
+
/*
 
53
+
** GPTLset_abort_on_error: User-visible routine to set abort_on_error flag
 
54
+
**
 
55
+
** Input arguments:
 
56
+
**   val: true (abort on error) or false (don't)
 
57
+
*/
 
58
+
 
59
+
void GPTLset_abort_on_error (bool val)
 
60
+
{
 
61
+
  abort_on_error = val;
 
62
+
}
 
63
+
 
64
+
/*
 
65
+
** GPTLallocate: wrapper utility for malloc
 
66
+
**
 
67
+
** Input arguments:
 
68
+
**   nbytes: size to allocate
 
69
+
**
 
70
+
** Return value: pointer to the new space (or NULL)
 
71
+
*/
 
72
+
 
73
+
void *GPTLallocate (const int nbytes)
 
74
+
{
 
75
+
  void *ptr;
 
76
+
 
77
+
  if ( nbytes <= 0 || ! (ptr = malloc (nbytes)))
 
78
+
    (void) GPTLerror ("GPTLallocate: malloc failed for %d bytes\n", nbytes);
 
79
+
 
80
+
  return ptr;
 
81
+
}
 
82
+