1MKSTEMP(P) POSIX Programmer's Manual MKSTEMP(P)
2
3
4
6 mkstemp - make a unique filename
7
9 #include <stdlib.h>
10
11 int mkstemp(char *template);
12
13
15 The mkstemp() function shall replace the contents of the string pointed
16 to by template by a unique filename, and return a file descriptor for
17 the file open for reading and writing. The function thus prevents any
18 possible race condition between testing whether the file exists and
19 opening it for use. The string in template should look like a filename
20 with six trailing 'X' s; mkstemp() replaces each 'X' with a character
21 from the portable filename character set. The characters are chosen
22 such that the resulting name does not duplicate the name of an existing
23 file at the time of a call to mkstemp().
24
26 Upon successful completion, mkstemp() shall return an open file
27 descriptor. Otherwise, -1 shall be returned if no suitable file could
28 be created.
29
31 No errors are defined.
32
33 The following sections are informative.
34
36 Generating a Filename
37 The following example creates a file with a 10-character name beginning
38 with the characters "file" and opens the file for reading and writing.
39 The value returned as the value of fd is a file descriptor that identiā
40 fies the file.
41
42
43 #include <stdlib.h>
44 ...
45 char template[] = "/tmp/fileXXXXXX";
46 int fd;
47
48
49 fd = mkstemp(template);
50
52 It is possible to run out of letters.
53
54 The mkstemp() function need not check to determine whether the filename
55 part of template exceeds the maximum allowable filename length.
56
58 None.
59
61 None.
62
64 getpid() , open() , tmpfile() , tmpnam() , the Base Definitions volume
65 of IEEE Std 1003.1-2001, <stdlib.h>
66
68 Portions of this text are reprinted and reproduced in electronic form
69 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
70 -- Portable Operating System Interface (POSIX), The Open Group Base
71 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
72 Electrical and Electronics Engineers, Inc and The Open Group. In the
73 event of any discrepancy between this version and the original IEEE and
74 The Open Group Standard, the original IEEE and The Open Group Standard
75 is the referee document. The original Standard can be obtained online
76 at http://www.opengroup.org/unix/online.html .
77
78
79
80IEEE/The Open Group 2003 MKSTEMP(P)