-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.c
More file actions
58 lines (47 loc) · 1.03 KB
/
util.c
File metadata and controls
58 lines (47 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
53
54
55
56
57
58
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include "util.h"
char *smprintf(const char *fmt, ...) {
char *buf;
va_list ap;
int ret;
va_start(ap, fmt);
ret = vasprintf(&buf, fmt, ap);
va_end(ap);
if(ret < 0)
return NULL;
return buf;
}
void buffer_clear(buffer_t *buf) {
buf->len = 0;
if (buf->data)
buf->data[buf->len] = '\0';
}
void *buffer_new(void) {
buffer_t *p = malloc(sizeof(buffer_t));
if(!p) {
perror("malloc");
return NULL;
}
p->data = NULL;
p->len = 0;
return p;
}
size_t buffer_printf(buffer_t *buf, const char *fmt, ...) {
va_list ap;
size_t ret;
va_start(ap, fmt);
ret = vsnprintf(buf->data, buf->len, fmt, ap);
va_end(ap);
if (ret >= buf->len) {
buf->data = realloc(buf->data, ret + 1);
va_start(ap, fmt);
ret = vsnprintf(buf->data, ret + 1, fmt, ap);
va_end(ap);
buf->len = ret + 1;
}
return ret;
}