File: verror.c

package info (click to toggle)
myproxy 6.1.22-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,628 kB
  • ctags: 1,812
  • sloc: ansic: 25,183; sh: 11,726; perl: 3,673; makefile: 361
file content (282 lines) | stat: -rw-r--r-- 4,459 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * verror.c
 *
 * Simple error-handling interface. See verror.h for documentation.
 */

#include "myproxy_common.h"	/* all needed headers included here */

/**********************************************************************
 *
 * Internal variables.
 *
 */

struct verror_context 
{
    int is_set;
    char *string;
    int value;
    int number;
};

static struct verror_context my_context = { 0, NULL, 0, 0 };

/**********************************************************************
 *
 * Internal constants
 *
 */

/* Values for where_flag to verror_add_string() */
#define VERROR_PREPEND			0
#define VERROR_APPEND			1

/**********************************************************************
 *
 * Internal functions.
 *
 */


/*
 * Added a string to the current error.
 *
 * If where_flag == VERROR_PREPEND, then prepend the string.
 *               == VERROR_APPEND, then append the string.
 */
static void
verror_add_string(const char			*string,
		  int				where_flag)
{
    int				need_cr = 0;
    int				string_len;
    int				new_string_length;
    char			*new_string;
    
    assert(string != NULL);
    
    string_len = strlen(string);
    
    /* Do we need to add a carriage return to the string */
    if (string[string_len - 1] != '\n')
    {
	need_cr = 1;
    }
    
    /* Determine the length of the new string */
    new_string_length = (my_context.string == NULL ?
			 0 : strlen(my_context.string));
    
    new_string_length += strlen(string) + 1 /* NUL */;
    
    if (need_cr == 1)
    {
	new_string_length++;
    }
    
    new_string = malloc(new_string_length);
    
    if (new_string == NULL)
    {
	/* Punt */
	return;
    }

    new_string[0] = '\0';
    
    /* Fill in new_string */
    switch (where_flag) 
    {
      case VERROR_PREPEND:
	strcat(new_string, string);
	if (need_cr)
	{
	    strcat(new_string, "\n");
	}
	if (my_context.string != NULL)
	{
	    strcat(new_string, my_context.string);
	}
	break;
	
      default:
	/* Punt */
      case VERROR_APPEND:
	if (my_context.string != NULL)
	{
	    strcat(new_string, my_context.string);
	}
	strcat(new_string, string);
	if (need_cr)
	{
	    strcat(new_string, "\n");
	}
	break;
    }
    
    /* And put new_string in place */
    if (my_context.string != NULL)
    {
	free(my_context.string);
    }
    
    my_context.string = new_string;
    
    return;
}

	
    
    
/**********************************************************************
 *
 * API Functions
 *
 */

void
verror_prepend_string(const char *format, ...)
{
    char *string = NULL;
    va_list ap;
    
    my_context.is_set = 1;
    
    va_start(ap, format);
    
    string = my_vsnprintf(format, ap);
    
    va_end(ap);
    
    if (string == NULL)
    {
	/* Punt */
	goto error;
	
    }

    verror_add_string(string, VERROR_PREPEND);
    
  error:
    if (string != NULL)
    {
	free(string);
    }
    
    return;
}

void
verror_put_string(const char *format, ...)
{
    char *string = NULL;
    va_list ap;
    
    my_context.is_set = 1;
    
    va_start(ap, format);
    
    string = my_vsnprintf(format, ap);
    
    va_end(ap);
    
    if (string == NULL)
    {
	/* Punt */
	goto error;
	
    }

    verror_add_string(string, VERROR_APPEND);
    
  error:
    if (string != NULL)
    {
	free(string);
    }
    
    return;
}

void
verror_put_errno(const int error_number)
{
    my_context.is_set = 1;
    my_context.number = error_number;
}

void
verror_put_value(const int value)
{
    my_context.is_set = 1;
    my_context.value = value;
}

int
verror_is_error()
{
    return my_context.is_set;
}


char *
verror_get_string()
{
    if (!my_context.string) {
	return "unknown error";
    }
    return my_context.string;
}

int
verror_get_errno()
{
    return my_context.number;
}

char *
verror_strerror()
{
    char *return_string;
    
    if (my_context.number == 0)
    {
	return_string = "";
    }
    else
    {
	return_string = strerror(my_context.number);
    }
    return return_string;
}

int
verror_get_value()
{
    return my_context.value;
}

void
verror_clear()
{
    my_context.is_set = 0;

    if (my_context.string != NULL)
    {
	free(my_context.string);
	my_context.string = NULL;
    }
    my_context.value = 0;
    my_context.number = 0;
}

void
verror_print_error(FILE *stream)
{
    if (my_context.number) {
	fprintf(stream, "%s%s\n", verror_get_string(), verror_strerror());
    } else {
	fprintf(stream, "%s", verror_get_string());
    }
}