1 #include <ccan/talloc/talloc.h>
2 #include <ccan/grab_file/grab_file.h>
9 char *talloc_basename(const void *ctx, const char *dir)
11 char *p = strrchr(dir, '/');
15 return talloc_strdup(ctx, p+1);
18 char *talloc_dirname(const void *ctx, const char *dir)
20 char *p = strrchr(dir, '/');
23 return talloc_strdup(ctx, ".");
24 return talloc_strndup(ctx, dir, p - dir);
27 char *talloc_getcwd(const void *ctx)
32 /* *This* is why people hate C. */
34 cwd = talloc_array(ctx, char, len);
35 while (!getcwd(cwd, len)) {
36 if (errno != ERANGE) {
40 cwd = talloc_realloc(ctx, cwd, char, len *= 2);
45 char *run_command(const void *ctx, const char *fmt, ...)
52 cmd = talloc_vasprintf(ctx, fmt, ap);
55 /* Ensure stderr gets to us too. */
56 cmd = talloc_asprintf_append(cmd, " 2>&1");
58 pipe = popen(cmd, "r");
60 return talloc_asprintf(ctx, "Failed to run '%s'", cmd);
62 contents = grab_fd(cmd, fileno(pipe), NULL);
63 if (pclose(pipe) != 0)
64 return talloc_asprintf(ctx, "Running '%s':\n%s",