]> eyrie.org Git - kerberos/krb5-strength.git/blob - portable/mkstemp.c
Merge branch 'debian' into squeeze
[kerberos/krb5-strength.git] / portable / mkstemp.c
1 /*
2  * Replacement for a missing mkstemp.
3  *
4  * Provides the same functionality as the library function mkstemp for those
5  * systems that don't have it.
6  *
7  * The canonical version of this file is maintained in the rra-c-util package,
8  * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
9  *
10  * Written by Russ Allbery <eagle@eyrie.org>
11  *
12  * The authors hereby relinquish any claim to any copyright that they may have
13  * in this work, whether granted under contract or by operation of law or
14  * international treaty, and hereby commit to the public, at large, that they
15  * shall not, at any time in the future, seek to enforce any copyright in this
16  * work against any person or entity, or prevent any person or entity from
17  * copying, publishing, distributing or creating derivative works of this
18  * work.
19  */
20
21 #include <config.h>
22 #include <portable/system.h>
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <sys/time.h>
27
28 /*
29  * If we're running the test suite, rename mkstemp to avoid conflicts with the
30  * system version.  #undef it first because some systems may define it to
31  * another name.
32  */
33 #if TESTING
34 # undef mkstemp
35 # define mkstemp test_mkstemp
36 int test_mkstemp(char *);
37 #endif
38
39 /* Pick the longest available integer type. */
40 #if HAVE_LONG_LONG_INT
41 typedef unsigned long long long_int_type;
42 #else
43 typedef unsigned long long_int_type;
44 #endif
45
46 int
47 mkstemp(char *template)
48 {
49     static const char letters[] =
50         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
51     size_t length;
52     char *XXXXXX;
53     struct timeval tv;
54     long_int_type randnum, working;
55     int i, tries, fd;
56
57     /*
58      * Make sure we have a valid template and initialize p to point at the
59      * beginning of the template portion of the string.
60      */
61     length = strlen(template);
62     if (length < 6) {
63         errno = EINVAL;
64         return -1;
65     }
66     XXXXXX = template + length - 6;
67     if (strcmp(XXXXXX, "XXXXXX") != 0) {
68         errno = EINVAL;
69         return -1;
70     }
71
72     /* Get some more-or-less random information. */
73     gettimeofday(&tv, NULL);
74     randnum = ((long_int_type) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
75
76     /*
77      * Now, try to find a working file name.  We try no more than TMP_MAX file
78      * names.
79      */
80     for (tries = 0; tries < TMP_MAX; tries++) {
81         for (working = randnum, i = 0; i < 6; i++) {
82             XXXXXX[i] = letters[working % 62];
83             working /= 62;
84         }
85         fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
86         if (fd >= 0 || (errno != EEXIST && errno != EISDIR))
87             return fd;
88
89         /*
90          * This is a relatively random increment.  Cut off the tail end of
91          * tv_usec since it's often predictable.
92          */
93         randnum += (tv.tv_usec >> 10) & 0xfff;
94     }
95     errno = EEXIST;
96     return -1;
97 }