-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
54 lines (44 loc) · 1.07 KB
/
util.c
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
/*
* Copyright (C) 2010 Valery V. Vorotyntsev <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include "util.h"
#ifdef DEBUG
void
debug_hexdump(const char *msg, const void *addr, size_t size)
{
fputs("(DEBUG) ", stderr);
if (msg != NULL)
fprintf(stderr, "%s ", msg);
fputc('[', stderr);
if (size != 0) {
const uint8_t *p = addr;
fprintf(stderr, "%02x", *p);
while(--size != 0)
fprintf(stderr, " %02x", *(++p));
}
fputs("]\n", stderr);
}
#endif
void
xasprintf(char **strp, const char *format, ...)
{
assert(*strp == NULL);
va_list ap;
va_start(ap, format);
*strp = xmalloc(80);
const size_t nchars = vsnprintf(*strp, 80, format, ap);
if (nchars >= 80) {
/* Not enough space. Reallocate buffer .. */
*strp = xrealloc(*strp, nchars + 1);
/* .. and try again. */
vsnprintf(*strp, nchars + 1, format, ap);
}
va_end(ap);
}