xref: /freebsd/lib/libc/stdio/mktemp.3 (revision 71207448cf3eb42617320efb36e7aa9d76c4c98e)
158f0484fSRodney W. Grimes.\" Copyright (c) 1989, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
658f0484fSRodney W. Grimes.\" are met:
758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
858f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1058f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1158f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
1258f0484fSRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software
1358f0484fSRodney W. Grimes.\"    must display the following acknowledgement:
1458f0484fSRodney W. Grimes.\"	This product includes software developed by the University of
1558f0484fSRodney W. Grimes.\"	California, Berkeley and its contributors.
1658f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes.\"    without specific prior written permission.
1958f0484fSRodney W. Grimes.\"
2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3158f0484fSRodney W. Grimes.\"
3258f0484fSRodney W. Grimes.\"     @(#)mktemp.3	8.1 (Berkeley) 6/4/93
337f3dea24SPeter Wemm.\" $FreeBSD$
3458f0484fSRodney W. Grimes.\"
352f253e75SWarner Losh.Dd February 11, 1998
3658f0484fSRodney W. Grimes.Dt MKTEMP 3
3758f0484fSRodney W. Grimes.Os
3858f0484fSRodney W. Grimes.Sh NAME
3958f0484fSRodney W. Grimes.Nm mktemp
4058f0484fSRodney W. Grimes.Nd make temporary file name (unique)
4158f0484fSRodney W. Grimes.Sh SYNOPSIS
4258f0484fSRodney W. Grimes.Fd #include <unistd.h>
4358f0484fSRodney W. Grimes.Ft char *
4458f0484fSRodney W. Grimes.Fn mktemp "char *template"
4558f0484fSRodney W. Grimes.Ft int
4658f0484fSRodney W. Grimes.Fn mkstemp "char *template"
472ee87840SWarner Losh.Ft int
4826ba999eSWarner Losh.Fn mkstemps "char *template" "int suffixlen"
492f253e75SWarner Losh.Ft char *
502f253e75SWarner Losh.Fn mkdtemp "char *template"
5158f0484fSRodney W. Grimes.Sh DESCRIPTION
5258f0484fSRodney W. GrimesThe
5358f0484fSRodney W. Grimes.Fn mktemp
5458f0484fSRodney W. Grimesfunction
5558f0484fSRodney W. Grimestakes the given file name template and overwrites a portion of it
5658f0484fSRodney W. Grimesto create a file name.
5758f0484fSRodney W. GrimesThis file name is unique and suitable for use
5858f0484fSRodney W. Grimesby the application.
5958f0484fSRodney W. GrimesThe template may be any file name with some number of
6058f0484fSRodney W. Grimes.Ql X Ns s
6158f0484fSRodney W. Grimesappended
6258f0484fSRodney W. Grimesto it, for example
6358f0484fSRodney W. Grimes.Pa /tmp/temp.XXXX .
6458f0484fSRodney W. GrimesThe trailing
6558f0484fSRodney W. Grimes.Ql X Ns s
6658f0484fSRodney W. Grimesare replaced with the current process number and/or a
6758f0484fSRodney W. Grimesunique letter combination.
6858f0484fSRodney W. GrimesThe number of unique file names
6958f0484fSRodney W. Grimes.Fn mktemp
7058f0484fSRodney W. Grimescan return depends on the number of
7158f0484fSRodney W. Grimes.Ql X Ns s
7258f0484fSRodney W. Grimesprovided; six
7358f0484fSRodney W. Grimes.Ql X Ns s
7458f0484fSRodney W. Grimeswill
7558f0484fSRodney W. Grimesresult in
7658f0484fSRodney W. Grimes.Fn mktemp
7758f0484fSRodney W. Grimestesting roughly 26 ** 6 combinations.
7858f0484fSRodney W. Grimes.Pp
7958f0484fSRodney W. GrimesThe
8058f0484fSRodney W. Grimes.Fn mkstemp
8158f0484fSRodney W. Grimesfunction
8258f0484fSRodney W. Grimesmakes the same replacement to the template and creates the template file,
8358f0484fSRodney W. Grimesmode 0600, returning a file descriptor opened for reading and writing.
8458f0484fSRodney W. GrimesThis avoids the race between testing for a file's existence and opening it
8558f0484fSRodney W. Grimesfor use.
862f253e75SWarner Losh.Pp
872f253e75SWarner LoshThe
882ee87840SWarner Losh.Fn mkstemps
892ee87840SWarner Loshfunction acts the same as
902ee87840SWarner Losh.Fn mkstemp ,
9126ba999eSWarner Loshexcept it permits a suffix to exist in the template.
9226ba999eSWarner LoshThe template should be of the form
932ee87840SWarner Losh.Pa /tmp/tmpXXXXXXsuffix .
942ee87840SWarner Losh.Fn mkstemps
9526ba999eSWarner Loshis told the length of the suffix string.
962ee87840SWarner Losh.Pp
972ee87840SWarner LoshThe
982f253e75SWarner Losh.Fn mkdtemp
992f253e75SWarner Loshfunction makes the same replacement to the template as in
1002f253e75SWarner Losh.Xr mktemp 3
1012f253e75SWarner Loshand creates the template directory, mode 0700.
10258f0484fSRodney W. Grimes.Sh RETURN VALUES
10358f0484fSRodney W. GrimesThe
10458f0484fSRodney W. Grimes.Fn mktemp
1052f253e75SWarner Loshand
1062f253e75SWarner Losh.Fn mkdtemp
1072f253e75SWarner Loshfunctions return a pointer to the template on success and
10858f0484fSRodney W. Grimes.Dv NULL
10958f0484fSRodney W. Grimeson failure.
11058f0484fSRodney W. GrimesThe
11158f0484fSRodney W. Grimes.Fn mkstemp
11226ba999eSWarner Loshand
11326ba999eSWarner Losh.Fn mkstemps
11426ba999eSWarner Loshfunctions
11526ba999eSWarner Loshreturn \-1 if no suitable file could be created.
11658f0484fSRodney W. GrimesIf either call fails an error code is placed in the global variable
11758f0484fSRodney W. Grimes.Va errno .
11858f0484fSRodney W. Grimes.Sh ERRORS
11958f0484fSRodney W. GrimesThe
12026ba999eSWarner Losh.Fn mkstemp ,
12126ba999eSWarner Losh.Fn mkstemps
1222f253e75SWarner Loshand
1232f253e75SWarner Losh.Fn mkdtemp
12458f0484fSRodney W. Grimesfunctions
12558f0484fSRodney W. Grimesmay set
12658f0484fSRodney W. Grimes.Va errno
12758f0484fSRodney W. Grimesto one of the following values:
12858f0484fSRodney W. Grimes.Bl -tag -width [ENOTDIR]
12958f0484fSRodney W. Grimes.It Bq Er ENOTDIR
13058f0484fSRodney W. GrimesThe pathname portion of the template is not an existing directory.
13158f0484fSRodney W. Grimes.El
13258f0484fSRodney W. Grimes.Pp
13358f0484fSRodney W. GrimesThe
13426ba999eSWarner Losh.Fn mkstemp ,
13526ba999eSWarner Losh.Fn mkstemps
1362f253e75SWarner Loshand
1372f253e75SWarner Losh.Fn mkdtemp
13858f0484fSRodney W. Grimesfunctions
13958f0484fSRodney W. Grimesmay also set
14058f0484fSRodney W. Grimes.Va errno
14158f0484fSRodney W. Grimesto any value specified by the
14258f0484fSRodney W. Grimes.Xr stat 2
14358f0484fSRodney W. Grimesfunction.
14458f0484fSRodney W. Grimes.Pp
14558f0484fSRodney W. GrimesThe
14658f0484fSRodney W. Grimes.Fn mkstemp
14726ba999eSWarner Loshand
14826ba999eSWarner Losh.Fn mkstemps
14926ba999eSWarner Loshfunctions
15058f0484fSRodney W. Grimesmay also set
15158f0484fSRodney W. Grimes.Va errno
15258f0484fSRodney W. Grimesto any value specified by the
15358f0484fSRodney W. Grimes.Xr open 2
15458f0484fSRodney W. Grimesfunction.
1552f253e75SWarner Losh.Pp
1562f253e75SWarner LoshThe
1572f253e75SWarner Losh.Fn mkdtemp
1582f253e75SWarner Loshfunction
1592f253e75SWarner Loshmay also set
1602f253e75SWarner Losh.Va errno
1612f253e75SWarner Loshto any value specified by the
1622f253e75SWarner Losh.Xr mkdir 2
1632f253e75SWarner Loshfunction.
164d428a98dSMike Pritchard.Sh NOTES
165d428a98dSMike PritchardA common problem that results in a core dump is that the programmer
166d428a98dSMike Pritchardpasses in a read-only string to
1672f253e75SWarner Losh.Fn mktemp ,
16826ba999eSWarner Losh.Fn mkstemp ,
16926ba999eSWarner Losh.Fn mkstemps
170d428a98dSMike Pritchardor
1712f253e75SWarner Losh.Fn mkdtemp .
172d428a98dSMike PritchardThis is common with programs that were developed before
173d428a98dSMike Pritchard.St -ansiC
174417349c4SMike Pritchardcompilers were common.
175d428a98dSMike PritchardFor example, calling
176d428a98dSMike Pritchard.Fn mkstemp
177d428a98dSMike Pritchardwith an argument of
178d428a98dSMike Pritchard.Qq /tmp/tempfile.XXXXXX
179d428a98dSMike Pritchardwill result in a core dump due to
180d428a98dSMike Pritchard.Fn mkstemp
181d428a98dSMike Pritchardattempting to modify the string constant that was given.
182d428a98dSMike PritchardIf the program in question makes heavy use of that type
183d428a98dSMike Pritchardof function call, you do have the option of compiling the program
184d428a98dSMike Pritchardso that it will store string constants in a writable segment of memory.
185d428a98dSMike PritchardSee
186d428a98dSMike Pritchard.Xr gcc 1
187d428a98dSMike Pritchardfor more information.
1882f253e75SWarner Losh.Sh BUGS
18971207448SSheldon HearnThis family of functions produces filenames which can be guessed.
19071207448SSheldon HearnThis makes the race in
19171207448SSheldon Hearn.Fn mktemp ,
19271207448SSheldon Hearnbetween testing for a file's existence and opening it for use,
19371207448SSheldon Hearnparticularly dangerous from a security perspective.
19471207448SSheldon HearnWhenever it is possible,
1952f253e75SWarner Losh.Fn mkstemp
1962f253e75SWarner Loshshould be used instead.
19758f0484fSRodney W. Grimes.Sh SEE ALSO
19858f0484fSRodney W. Grimes.Xr chmod 2 ,
19958f0484fSRodney W. Grimes.Xr getpid 2 ,
2002f253e75SWarner Losh.Xr mkdir 2 ,
20158f0484fSRodney W. Grimes.Xr open 2 ,
20258f0484fSRodney W. Grimes.Xr stat 2
20358f0484fSRodney W. Grimes.Sh HISTORY
20458f0484fSRodney W. GrimesA
2052f253e75SWarner Losh.Fn mktemp
20658f0484fSRodney W. Grimesfunction appeared in
20758f0484fSRodney W. Grimes.At v7 .
2082f253e75SWarner LoshThe
2092f253e75SWarner Losh.Fn mkdtemp
2102f253e75SWarner Loshfunction first appeared in
211b6419b66SDavid E. O'Brien.Ox 2.2 ,
212b6419b66SDavid E. O'Brienand later in
213b6419b66SDavid E. O'Brien.Fx 3.2 .
214b6419b66SDavid E. O'BrienThe
215b6419b66SDavid E. O'Brien.Fn mkstemps
2163c0c03cdSDavid E. O'Brienfunction first appeared in
2173c0c03cdSDavid E. O'Brien.Ox 2.4 ,
2183c0c03cdSDavid E. O'Brienand later in
2193c0c03cdSDavid E. O'Brien.Fx 3.4 .
220