+ return tmpdir;
+}
+
+void keep_temp_dir(void)
+{
+ tal_del_destructor(temp_dir(), unlink_all);
+}
+
+char *temp_file(const void *ctx, const char *extension, const char *srcname)
+{
+ char *f, *base, *suffix;
+ struct stat st;
+ unsigned int count = 0;
+
+ base = path_join(ctx, temp_dir(), take(path_basename(ctx, srcname)));
+ /* Trim extension. */
+ base[path_ext_off(base)] = '\0';
+ suffix = tal_strdup(ctx, extension);
+
+ do {
+ f = tal_strcat(ctx, base, suffix);
+ suffix = tal_fmt(base, "-%u%s", ++count, extension);
+ } while (lstat(f, &st) == 0);