-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.c
44 lines (36 loc) · 868 Bytes
/
utils.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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
void efflu_strappf(char **old, size_t *old_size, size_t *old_offset, const char *format, ...)
{
char *new = *old;
size_t new_size = *old_size, new_offset = *old_offset;
va_list args;
if (NULL == new && NULL == (new = malloc(new_size = 128)))
return;
va_start(args, format);
new_offset += vsnprintf(new + new_offset, new_size - new_offset, format, args);
va_end(args);
if (new_offset >= new_size)
{
char *renew;
new_size = new_offset + 1;
if (NULL != (renew = realloc(new, new_size)))
{
new = renew;
va_start(args, format);
vsnprintf(new + *old_offset, new_size - *old_offset, format, args);
va_end(args);
}
else
{
free(new);
new = NULL;
new_size = new_offset = 0;
}
}
*old = new;
*old_size = new_size;
*old_offset = new_offset;
}