8 #include "talloc/talloc.h"
13 #include "noerr/noerr.h"
15 char **strsplit(const void *ctx, const char *string, const char *delims,
19 unsigned int max = 64, num = 0;
21 lines = talloc_array(ctx, char *, max+1);
23 while (*string != '\0') {
24 unsigned int len = strcspn(string, delims);
25 lines[num] = talloc_array(lines, char, len + 1);
26 memcpy(lines[num], string, len);
27 lines[num][len] = '\0';
29 string += strspn(string, delims) ? 1 : 0;
31 lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
39 char *strjoin(const void *ctx, char *strings[], const char *delim)
42 char *ret = talloc_strdup(ctx, "");
44 for (i = 0; strings[i]; i++) {
45 ret = talloc_append_string(ret, strings[i]);
46 ret = talloc_append_string(ret, delim);
51 void *grab_fd(const void *ctx, int fd, size_t *size)
54 size_t max = 16384, s;
61 buffer = talloc_array(ctx, char, max+1);
62 while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
65 buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
76 void *grab_file(const void *ctx, const char *filename, size_t *size)
82 fd = dup(STDIN_FILENO);
84 fd = open(filename, O_RDONLY, 0);
89 buffer = grab_fd(ctx, fd, size);