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)
54 unsigned int max = 16384, size = 0;
57 buffer = talloc_array(ctx, char, max+1);
58 while ((ret = read(fd, buffer + size, max - size)) > 0) {
61 buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
72 void *grab_file(const void *ctx, const char *filename)
77 if (streq(filename, "-"))
78 fd = dup(STDIN_FILENO);
80 fd = open(filename, O_RDONLY, 0);
85 buffer = grab_fd(ctx, fd);