--- /dev/null
+#ifndef UTIL_LINUX_FILEUTILS
+#define UTIL_LINUX_FILEUTILS
+
+extern FILE * xmkstemp(char **tmpname);
+
+#endif
test_at \
test_blkdev \
test_canonicalize \
+ test_fileutils \
test_ismounted \
test_mangle \
test_procutils \
test_loopdev_CFLAGS = -DTEST_PROGRAM_LOOPDEV
endif
+test_fileutils_SOURCES = fileutils.c
test_tt_SOURCES = tt.c $(top_srcdir)/lib/mbsalign.c
test_canonicalize_SOURCES = canonicalize.c
--- /dev/null
+/*
+ * Copyright (C) 2012 Sami Kerola <kerolasa@iki.fi>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "c.h"
+#include "pathnames.h"
+
+/* Create open temporary file in safe way. Please notice that the
+ * file permissions are -rw------- by default. */
+FILE *xmkstemp(char **tmpname)
+{
+ char *localtmp;
+ char *tmpenv;
+ mode_t old_mode;
+ int fd;
+ FILE *ret;
+
+ tmpenv = getenv("TMPDIR");
+ if (tmpenv)
+ asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
+ program_invocation_short_name);
+ else
+ asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
+ program_invocation_short_name);
+ old_mode = umask(077);
+ fd = mkstemp(localtmp);
+ umask(old_mode);
+ if (fd == -1)
+ return NULL;
+ if (!(ret = fdopen(fd, "w+")))
+ goto err;
+ *tmpname = localtmp;
+ return ret;
+ err:
+ close(fd);
+ return NULL;
+}
+
+#ifdef TEST_PROGRAM
+int main(void)
+{
+ FILE *f;
+ char *tmpname;
+ f = xmkstemp(&tmpname);
+ unlink(tmpname);
+ free(tmpname);
+ fclose(f);
+ return EXIT_FAILURE;
+}
+#endif