+
+static int unlink_all(char *dir)
+{
+ char cmd[strlen(dir) + sizeof("rm -rf ")];
+ sprintf(cmd, "rm -rf %s", dir);
+ if (system(cmd) != 0)
+ warn("Could not remove temporary work in %s", dir);
+ return 0;
+}
+
+char *temp_file(const void *ctx, const char *extension)
+{
+ /* For first call, create dir. */
+ while (!tmpdir) {
+ tmpdir = getenv("TMPDIR");
+ if (!tmpdir)
+ tmpdir = "/tmp";
+ tmpdir = talloc_asprintf(talloc_autofree_context(),
+ "%s/ccanlint-%u.%lu",
+ tmpdir, getpid(), random());
+ if (mkdir(tmpdir, 0700) != 0) {
+ if (errno == EEXIST) {
+ talloc_free(tmpdir);
+ tmpdir = NULL;
+ continue;
+ }
+ err(1, "mkdir %s failed", tmpdir);
+ }
+ talloc_set_destructor(tmpdir, unlink_all);
+ }
+
+ return talloc_asprintf(ctx, "%s/%u%s", tmpdir, count++, extension);
+}