8 #include "talloc/talloc.h"
14 char **strsplit(const void *ctx, const char *string, const char *delims,
18 unsigned int max = 64, num = 0;
20 lines = talloc_array(ctx, char *, max+1);
22 while (*string != '\0') {
23 unsigned int len = strcspn(string, delims);
24 lines[num] = talloc_array(lines, char, len + 1);
25 memcpy(lines[num], string, len);
26 lines[num][len] = '\0';
28 string += strspn(string, delims) ? 1 : 0;
30 lines = talloc_realloc(ctx, lines, char *, max*=2 + 1);
38 char *strjoin(const void *ctx, char *strings[], const char *delim)
41 char *ret = talloc_strdup(ctx, "");
43 for (i = 0; strings[i]; i++) {
44 ret = talloc_append_string(ret, strings[i]);
45 ret = talloc_append_string(ret, delim);
50 static int close_no_errno(int fd)
52 int ret = 0, serrno = errno;
59 void *grab_fd(const void *ctx, int fd)
62 unsigned int max = 16384, size = 0;
65 buffer = talloc_array(ctx, char, max+1);
66 while ((ret = read(fd, buffer + size, max - size)) > 0) {
69 buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
80 void *grab_file(const void *ctx, const char *filename)
85 if (streq(filename, "-"))
86 fd = dup(STDIN_FILENO);
88 fd = open(filename, O_RDONLY, 0);
93 buffer = grab_fd(ctx, fd);