- size_t baselen;
-
- if (!keep)
- return temp_file(ctx, extension);
-
- baselen = strrchr(srcname, '.') - srcname;
- return talloc_asprintf(ctx, "%.*s%s", baselen, srcname, extension);
+ unsigned baselen;
+ char *f, *suffix = talloc_strdup(ctx, "");
+ struct stat st;
+ unsigned int count = 0;
+
+ srcname = talloc_basename(ctx, srcname);
+ if (strrchr(srcname, '.'))
+ baselen = strrchr(srcname, '.') - srcname;
+ else
+ baselen = strlen(srcname);
+
+ do {
+ f = talloc_asprintf(ctx, "%s/%.*s%s%s",
+ 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;