1MKSTEMP(3)                 Linux Programmer's Manual                MKSTEMP(3)
2
3
4

NAME

6       mkstemp, mkostemp, mkstemps, mkostemps - create a unique temporary file
7

SYNOPSIS

9       #include <stdlib.h>
10
11       int mkstemp(char *template);
12
13       int mkostemp(char *template, int flags);
14
15       int mkstemps(char *template, int suffixlen);
16
17       int mkostemps(char *template, int suffixlen, int flags);
18
19   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21       mkstemp():
22           _XOPEN_SOURCE >= 500
23               || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
24               || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
25
26       mkostemp(): _GNU_SOURCE
27       mkstemps():
28           /* Glibc since 2.19: */ _DEFAULT_SOURCE
29               || /* Glibc versions <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE
30       mkostemps(): _GNU_SOURCE
31

DESCRIPTION

33       The  mkstemp() function generates a unique temporary filename from tem‐
34       plate, creates and opens the file, and returns an open file  descriptor
35       for the file.
36
37       The  last  six  characters  of  template must be "XXXXXX" and these are
38       replaced with a string that makes the filename unique.  Since  it  will
39       be  modified,  template  must  not  be a string constant, but should be
40       declared as a character array.
41
42       The file is created with permissions 0600, that is, read plus write for
43       owner  only.  The returned file descriptor provides both read and write
44       access to the file.  The file is opened with the open(2)  O_EXCL  flag,
45       guaranteeing that the caller is the process that creates the file.
46
47       The mkostemp() function is like mkstemp(), with the difference that the
48       following bits—with the same meaning as for open(2)—may be specified in
49       flags:  O_APPEND,  O_CLOEXEC,  and O_SYNC.  Note that when creating the
50       file, mkostemp() includes the values O_RDWR, O_CREAT, and O_EXCL in the
51       flags  argument  given  to open(2); including these values in the flags
52       argument given to mkostemp() is unnecessary,  and  produces  errors  on
53       some systems.
54
55       The  mkstemps()  function  is like mkstemp(), except that the string in
56       template contains a suffix of suffixlen characters.  Thus, template  is
57       of  the  form  prefixXXXXXXsuffix, and the string XXXXXX is modified as
58       for mkstemp().
59
60       The  mkostemps()  function  is  to  mkstemps()  as  mkostemp()  is   to
61       mkstemp().
62

RETURN VALUE

64       On success, these functions return the file descriptor of the temporary
65       file.  On error, -1 is returned, and errno is set appropriately.
66

ERRORS

68       EEXIST Could not create a unique temporary filename.  Now the  contents
69              of template are undefined.
70
71       EINVAL For  mkstemp()  and  mkostemp(): The last six characters of tem‐
72              plate were not XXXXXX; now template is unchanged.
73
74              For mkstemps() and mkostemps(): template is less than (6 +  suf‐
75              fixlen)  characters  long,  or  the last 6 characters before the
76              suffix in template were not XXXXXX.
77
78       These functions may also fail with any  of  the  errors  described  for
79       open(2).
80

VERSIONS

82       mkostemp()  is  available  since glibc 2.7.  mkstemps() and mkostemps()
83       are available since glibc 2.11.
84

ATTRIBUTES

86       For  an  explanation  of  the  terms  used   in   this   section,   see
87       attributes(7).
88
89       ┌────────────────────────┬───────────────┬─────────┐
90Interface               Attribute     Value   
91       ├────────────────────────┼───────────────┼─────────┤
92mkstemp(), mkostemp(),  │ Thread safety │ MT-Safe │
93mkstemps(), mkostemps() │               │         │
94       └────────────────────────┴───────────────┴─────────┘

CONFORMING TO

96       mkstemp(): 4.3BSD, POSIX.1-2001.
97
98       mkstemps(): unstandardized, but appears on several other systems.
99
100       mkostemp() and mkostemps(): are glibc extensions.
101

NOTES

103       In  glibc  versions  2.06 and earlier, the file is created with permis‐
104       sions 0666, that is, read and write for all users.  This  old  behavior
105       may  be  a security risk, especially since other UNIX flavors use 0600,
106       and  somebody  might  overlook  this  detail  when  porting   programs.
107       POSIX.1-2008  adds  a  requirement  that  the file be created with mode
108       0600.
109
110       More generally, the POSIX specification of mkstemp() does not say  any‐
111       thing  about  file  modes, so the application should make sure its file
112       mode creation mask (see umask(2)) is set appropriately  before  calling
113       mkstemp() (and mkostemp()).
114

SEE ALSO

116       mkdtemp(3), mktemp(3), tempnam(3), tmpfile(3), tmpnam(3)
117

COLOPHON

119       This  page  is  part of release 4.16 of the Linux man-pages project.  A
120       description of the project, information about reporting bugs,  and  the
121       latest     version     of     this    page,    can    be    found    at
122       https://www.kernel.org/doc/man-pages/.
123
124
125
126GNU                               2017-09-15                        MKSTEMP(3)
Impressum