| 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 | 26514 |  |  |  |  |  | b_string *b_string_join(char *sep, b_stack *items) { | 
| 12 |  |  |  |  |  |  | b_string *ret; | 
| 13 | 26514 |  |  |  |  |  | size_t i = 0, count = 0, off = 0, len, seplen; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 26514 | 50 |  |  |  |  | if ((ret = malloc(sizeof(*ret))) == NULL) { | 
| 16 | 0 |  |  |  |  |  | goto error_malloc; | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 26514 |  |  |  |  |  | ret->str = NULL; | 
| 20 | 26514 |  |  |  |  |  | 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 | 26514 |  |  |  |  |  | count  = b_stack_count(items); | 
| 27 | 26514 |  |  |  |  |  | seplen = strlen(sep); | 
| 28 | 26514 | 100 |  |  |  |  | len    = count? seplen * (count - 1): 0; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 62704 | 100 |  |  |  |  | for (i=0; i | 
| 31 |  |  |  |  |  |  | b_string *item; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 36190 | 50 |  |  |  |  | if ((item = b_stack_item_at(items, i)) == NULL) { | 
| 34 | 0 |  |  |  |  |  | goto error_item_at_len; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 36190 |  |  |  |  |  | len += b_string_len(item); | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 26514 | 50 |  |  |  |  | if ((ret->str = malloc(len + 1)) == NULL) { | 
| 41 | 0 |  |  |  |  |  | goto error_malloc_str; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 26514 |  |  |  |  |  | ret->len = len; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | /* | 
| 47 |  |  |  |  |  |  | * Next, copy each item into the buffer. | 
| 48 |  |  |  |  |  |  | */ | 
| 49 | 62704 | 100 |  |  |  |  | for (i=0; i | 
| 50 |  |  |  |  |  |  | b_string *item; | 
| 51 |  |  |  |  |  |  | size_t itemlen; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 36190 | 50 |  |  |  |  | if ((item = b_stack_item_at(items, i)) == NULL) { | 
| 54 | 0 |  |  |  |  |  | goto error_item_at; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 36190 |  |  |  |  |  | itemlen = b_string_len(item); | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 36190 | 50 |  |  |  |  | if (seplen > 0 && i) { | 
|  |  | 100 |  |  |  |  |  | 
| 60 | 16339 | 50 |  |  |  |  | if (memcpy(ret->str + off, sep, seplen) == NULL) { | 
| 61 | 0 |  |  |  |  |  | goto error_memcpy_sep; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 16339 |  |  |  |  |  | off += seplen; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 36190 | 100 |  |  |  |  | if (itemlen > 0) { | 
| 68 | 29620 | 50 |  |  |  |  | if (memcpy(ret->str + off, item->str, itemlen) == NULL) { | 
| 69 | 0 |  |  |  |  |  | goto error_memcpy_item; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 29620 |  |  |  |  |  | off += itemlen; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | /* | 
| 77 |  |  |  |  |  |  | * Finally, add the nul terminator. | 
| 78 |  |  |  |  |  |  | */ | 
| 79 | 26514 |  |  |  |  |  | ret->str[len] = '\0'; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 26514 |  |  |  |  |  | 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 |  |  |  |  |  |  | } |