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