-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
52 lines (46 loc) · 1.03 KB
/
utils.c
File metadata and controls
52 lines (46 loc) · 1.03 KB
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
/*
* Copyright (C) 2023 Christopher Lang
* See LICENSE for license details.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "utils.h"
void *
xmalloc(size_t len)
{
void *p;
if ( (p = malloc(len)) == NULL)
perror("malloc");
return p;
}
void *
xrealloc(void *p, size_t len)
{
if ( (p = realloc(p, len)) == NULL)
perror("realloc");
return p;
}
void
revsprintf(char **stream, long *allocated, long *length, const char *format, va_list args)
{
va_list args_copy;
int written;
va_copy(args_copy, args);
written = vsnprintf(*stream + *length, *allocated - *length, format, args);
if (written >= *allocated - *length) {
*allocated += written + 1024 * 4;
*stream = xrealloc(*stream, *allocated);
vsprintf(*stream + *length, format, args_copy);
}
*length += written;
va_end(args_copy);
}
void
resprintf(char **stream, long *allocated, long *length, const char *format, ...)
{
va_list args;
va_start(args, format);
revsprintf(stream, allocated, length, format, args);
va_end(args);
}