2 * Replacement for a missing mkstemp.
4 * Provides the same functionality as the library function mkstemp for those
5 * systems that don't have it.
7 * The canonical version of this file is maintained in the rra-c-util package,
8 * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
10 * Written by Russ Allbery <eagle@eyrie.org>
11 * Copyright 2009, 2011, 2014
12 * The Board of Trustees of the Leland Stanford Junior University
14 * Copying and distribution of this file, with or without modification, are
15 * permitted in any medium without royalty provided the copyright notice and
16 * this notice are preserved. This file is offered as-is, without any
19 * SPDX-License-Identifier: FSFAP
23 #include <portable/system.h>
27 #ifdef HAVE_SYS_TIME_H
28 # include <sys/time.h>
33 * If we're running the test suite, rename mkstemp to avoid conflicts with the
34 * system version. #undef it first because some systems may define it to
39 # define mkstemp test_mkstemp
40 int test_mkstemp(char *);
43 /* Pick the longest available integer type. */
44 #if HAVE_LONG_LONG_INT
45 typedef unsigned long long long_int_type;
47 typedef unsigned long long_int_type;
51 mkstemp(char *template)
53 static const char letters[] =
54 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
58 long_int_type randnum, working;
62 * Make sure we have a valid template and initialize p to point at the
63 * beginning of the template portion of the string.
65 length = strlen(template);
70 XXXXXX = template + length - 6;
71 if (strcmp(XXXXXX, "XXXXXX") != 0) {
76 /* Get some more-or-less random information. */
77 gettimeofday(&tv, NULL);
78 randnum = ((long_int_type) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
81 * Now, try to find a working file name. We try no more than TMP_MAX file
84 for (tries = 0; tries < TMP_MAX; tries++) {
85 for (working = randnum, i = 0; i < 6; i++) {
86 XXXXXX[i] = letters[working % 62];
89 fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
90 if (fd >= 0 || (errno != EEXIST && errno != EISDIR))
94 * This is a relatively random increment. Cut off the tail end of
95 * tv_usec since it's often predictable.
97 randnum += (tv.tv_usec >> 10) & 0xfff;