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

NAME

6       mkstemp, mkostemp - 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(): _BSD_SOURCE || _SVID_SOURCE || _XOPEN_SOURCE >= 500
22       mkostemp(): _GNU_SOURCE
23       mkstemps(): _BSD_SOURCE || _SVID_SOURCE
24       mkostemps(): _GNU_SOURCE
25

DESCRIPTION

27       The  mkstemp() function generates a unique temporary filename from tem‐
28       plate, creates and opens the file, and returns an open file  descriptor
29       for the file.
30
31       The  last  six  characters  of  template must be "XXXXXX" and these are
32       replaced with a string that makes the filename unique.  Since  it  will
33       be  modified,  template  must  not  be a string constant, but should be
34       declared as a character array.
35
36       The file is created with permissions 0600, that is, read plus write for
37       owner  only.   (In glibc versions 2.06 and earlier, the file is created
38       with permissions 0666, that is, read and write  for  all  users.)   The
39       returned  file  descriptor  provides  both read and write access to the
40       file.  The file is opened with the open(2)  O_EXCL  flag,  guaranteeing
41       that the caller is the process that creates the file.
42
43       The  mkostemp()  function  is  like mkstemp(), with the difference that
44       flags as for  open(2)  may  be  specified  in  flags  (e.g.,  O_APPEND,
45       O_SYNC).
46
47       The  mkstemps()  function  is like mkstemp(), except that the string in
48       template contains a suffix of suffixlen characters.  Thus, template  is
49       of  the  form  prefixXXXXXXsuffix, and the string XXXXXX is modified as
50       for mkstemp().
51
52       The  mkostemps()  function  is  to  mkstemps()  as  mkostemp()  is   to
53       mkstemp().
54

RETURN VALUE

56       On success, these functions return the file descriptor of the temporary
57       file.  On error, -1 is returned, and errno is set appropriately.
58

ERRORS

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

VERSIONS

74       mkostemp()  is  available  since glibc 2.7.  mkstemps() and mkostemps()
75       are available since glibc 2.11.
76

CONFORMING TO

78       mkstemp(): 4.3BSD, POSIX.1-2001.
79
80       mkstemps(): unstandardized, but appears on several other systems.
81
82       mkostemp() and mkostemps(): are glibc extensions.
83

NOTES

85       The old behavior of creating a file with mode 0666 may  be  a  security
86       risk,  especially since other Unix flavors use 0600, and somebody might
87       overlook this detail when porting programs.
88
89       More generally, the POSIX specification of mkstemp() does not say  any‐
90       thing  about  file  modes, so the application should make sure its file
91       mode creation mask (see umask(2)) is set appropriately  before  calling
92       mkstemp() (and mkostemp()).
93
94       The  prototype  for mktemp() is in <unistd.h> for libc4, libc5, glibc1;
95       glibc2 follows POSIX.1 and has the prototype in <stdlib.h>.
96

SEE ALSO

98       mkdtemp(3), mktemp(3), tempnam(3), tmpfile(3), tmpnam(3)
99

COLOPHON

101       This page is part of release 3.25 of the Linux  man-pages  project.   A
102       description  of  the project, and information about reporting bugs, can
103       be found at http://www.kernel.org/doc/man-pages/.
104
105
106
107GNU                               2010-06-19                        MKSTEMP(3)
Impressum