- return talloc_asprintf(ctx, "%s/%u%s", tmpdir, count++, extension);
+char *maybe_temp_file(const void *ctx, const char *extension, bool keep,
+ const char *srcname)
+{
+ unsigned baselen;
+ char *f, *suffix = talloc_strdup(ctx, "");
+ struct stat st;
+ unsigned int count = 0;
+
+ if (!keep)
+ srcname = talloc_basename(ctx, srcname);
+ else
+ assert(srcname[0] == '/');
+
+ if (strrchr(srcname, '.'))
+ baselen = strrchr(srcname, '.') - srcname;
+ else
+ baselen = strlen(srcname);
+
+ do {
+ f = talloc_asprintf(ctx, "%s/%.*s%s%s",
+ keep ? "" : temp_dir(ctx),
+ baselen, srcname,
+ suffix, extension);
+ talloc_free(suffix);
+ suffix = talloc_asprintf(ctx, "-%u", ++count);
+ } while (lstat(f, &st) == 0);
+
+ if (tools_verbose)
+ printf("Creating file %s\n", f);
+
+ talloc_free(suffix);
+ return f;