master
 1#define _CRT_RAND_S
 2#include <stdlib.h>
 3#include <stdio.h>
 4#include <string.h>
 5#include <io.h>
 6#include <errno.h>
 7#include <share.h>
 8#include <fcntl.h>
 9#include <sys/stat.h>
10#include <limits.h>
11
12/*
13    The mkstemp() function generates a unique temporary filename from template,
14    creates and opens the file, and returns an open file descriptor for the
15    file.
16
17    The template may be any file name with at least six trailing Xs, for example
18    /tmp/temp.XXXXXXXX. The trailing Xs are replaced with a unique digit and
19    letter combination that makes the file name unique. Since it will be
20    modified, template must not be a string constant, but should be declared as
21    a character array.
22
23    The file is created with permissions 0600, that is, read plus write for
24    owner only. The returned file descriptor provides both read and write access
25    to the file.
26 */
27int __cdecl mkstemp (char *template_name)
28{
29    int j, fd, len, index;
30    unsigned int i, r;
31
32    /* These are the (62) characters used in temporary filenames. */
33    static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
34
35    /* The last six characters of template must be "XXXXXX" */
36    if (template_name == NULL || (len = strlen (template_name)) < 6
37            || memcmp (template_name + (len - 6), "XXXXXX", 6)) {
38        errno = EINVAL;
39        return -1;
40    }
41
42    /* User may supply more than six trailing Xs */
43    for (index = len - 6; index > 0 && template_name[index - 1] == 'X'; index--);
44
45    /* Like OpenBSD, mkstemp() will try 2 ** 31 combinations before giving up. */
46    for (i = 0; i <= INT_MAX; i++) {
47        for(j = index; j < len; j++) {
48            if (rand_s(&r))
49                r = rand();
50            template_name[j] = letters[r % 62];
51        }
52        fd = _sopen(template_name,
53                _O_RDWR | _O_CREAT | _O_EXCL | _O_BINARY,
54                _SH_DENYNO, _S_IREAD | _S_IWRITE);
55        if (fd != -1) return fd;
56        if (fd == -1 && errno != EEXIST) return -1;
57    }
58
59    return -1;
60}
61
62#if 0
63int main (int argc, char *argv[])
64{
65    int i, fd;
66
67    for (i = 0; i < 10; i++) {
68        char template_name[] = { "temp_XXXXXX" };
69        fd = mkstemp (template_name);
70        if (fd >= 0) {
71            fprintf (stderr, "fd=%d, name=%s\n", fd, template_name);
72            _close (fd);
73        } else {
74            fprintf (stderr, "errno=%d\n", errno);
75        }
76    }
77
78    for (i = 0; i < 10; i++) {
79        char template_name[] = { "temp_XXXXXXXX" };
80        fd = mkstemp (template_name);
81        if (fd >= 0) {
82            fprintf (stderr, "fd=%d, name=%s\n", fd, template_name);
83            _close (fd);
84        } else {
85            fprintf (stderr, "errno=%d\n", errno);
86        }
87    }
88
89    return 0;
90}
91#endif