File Coverage

src/b_util.c
Criterion Covered Total %
statement 24 47 51.0
branch 17 30 56.6
condition n/a
subroutine n/a
pod n/a
total 41 77 53.2


line stmt bran cond sub pod time code
1             #include
2             #include
3             #include
4             #include
5             #include
6             #include
7             #include "b_string.h"
8             #include "b_stack.h"
9             #include "b_util.h"
10              
11 27314           b_string *b_string_join(char *sep, b_stack *items) {
12             b_string *ret;
13 27314           size_t i = 0, count = 0, off = 0, len, seplen;
14              
15 27314 50         if ((ret = malloc(sizeof(*ret))) == NULL) {
16 0           goto error_malloc;
17             }
18              
19 27314           ret->str = NULL;
20 27314           ret->len = 0;
21              
22             /*
23             * First, calculate the length of the string buffer to be returned, even
24             * if there are zero items on the input stack.
25             */
26 27314           count = b_stack_count(items);
27 27314           seplen = strlen(sep);
28 27314 100         len = count? seplen * (count - 1): 0;
29              
30 64706 100         for (i=0; i
31             b_string *item;
32              
33 37392 50         if ((item = b_stack_item_at(items, i)) == NULL) {
34 0           goto error_item_at_len;
35             }
36              
37 37392           len += b_string_len(item);
38             }
39              
40 27314 50         if ((ret->str = malloc(len + 1)) == NULL) {
41 0           goto error_malloc_str;
42             }
43              
44 27314           ret->len = len;
45              
46             /*
47             * Next, copy each item into the buffer.
48             */
49 64706 100         for (i=0; i
50             b_string *item;
51             size_t itemlen;
52              
53 37392 50         if ((item = b_stack_item_at(items, i)) == NULL) {
54 0           goto error_item_at;
55             }
56              
57 37392           itemlen = b_string_len(item);
58              
59 37392 50         if (seplen > 0 && i) {
    100          
60 17140 50         if (memcpy(ret->str + off, sep, seplen) == NULL) {
61 0           goto error_memcpy_sep;
62             }
63              
64 17140           off += seplen;
65             }
66              
67 37392 100         if (itemlen > 0) {
68 30820 50         if (memcpy(ret->str + off, item->str, itemlen) == NULL) {
69 0           goto error_memcpy_item;
70             }
71              
72 30820           off += itemlen;
73             }
74             }
75              
76             /*
77             * Finally, add the nul terminator.
78             */
79 27314           ret->str[len] = '\0';
80              
81 27314           return ret;
82              
83             error_memcpy_item:
84             error_memcpy_sep:
85             error_item_at:
86 0           free(ret->str);
87              
88             error_malloc_str:
89             error_item_at_len:
90 0           free(ret);
91              
92             error_malloc:
93 0           return NULL;
94             }
95              
96 0           b_string *b_readlink(b_string *path, struct stat *st) {
97             b_string *ret;
98 0           char *buf = NULL;
99              
100 0 0         if ((buf = malloc(st->st_size + 1)) == NULL) {
101 0           goto error_malloc_buf;
102             }
103              
104 0 0         if (readlink(path->str, buf, st->st_size) < 0) {
105 0           goto error_readlink;
106             }
107              
108 0           buf[st->st_size] = '\x00';
109              
110 0 0         if ((ret = malloc(sizeof(*ret))) == NULL) {
111 0           goto error_malloc_ret;
112             }
113              
114 0           ret->str = buf;
115 0           ret->len = st->st_size;
116              
117 0           return ret;
118              
119             error_malloc_ret:
120             error_readlink:
121 0           free(buf);
122              
123             error_malloc_buf:
124 0           return NULL;
125             }