forked from capeprivacy/capejail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.c
58 lines (51 loc) · 1.22 KB
/
vec.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
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "banned.h"
#include "vec.h"
int cape_string_vec_push(struct cape_string_vec *vec, const char *string) {
char *str = NULL;
if (string) {
str = strdup(string);
if (!str) {
perror("malloc");
goto fail;
}
}
if (vec->cap == 0) {
char **new_data = malloc(sizeof(char *));
if (!new_data) {
perror("malloc");
goto fail;
}
vec->cap = 1;
vec->data = new_data;
} else if (vec->len >= vec->cap) {
size_t new_cap = vec->cap * 2;
char **new_data = NULL;
new_data = realloc(vec->data, new_cap * sizeof(char *));
if (!new_data) {
perror("realloc");
goto fail;
}
vec->data = new_data;
vec->cap = new_cap;
}
vec->data[vec->len] = str;
vec->len++;
return 0;
fail:
free(str);
return -1;
}
void cape_string_vec_free(struct cape_string_vec *vec) {
if (vec) {
for (size_t i = 0; i < vec->len; i++) {
free(vec->data[i]);
}
free(vec->data);
vec->data = NULL;
vec->cap = 0;
vec->len = 0;
}
}