MKTEMP(3)
๐
MKTEMP(3) โ like, the mktemp family, bestie โจ
okay so this is a Library Functions Manual page for the mktemp squad, and it lives in the Standard C Library (libc, linked with -lc). buckle up, we're making temp files ๐
โจ SYNOPSIS โจ
#include <stdlib.h>
char *
mktemp(char *template);
int
mkstemp(char *template);
int
mkostemp(char *template, int oflags);
int
mkostemps(char *template, int suffixlen, int oflags);
int
mkostempsat(int dfd, char *template, int suffixlen, int oflags);
char *
mkdtemp(char *template);
#include <unistd.h>
int
mkstemps(char *template, int suffixlen);
๐ธ DESCRIPTION ๐ธ
the OG: mktemp()
so mktemp() takes the file name template you hand it and overwrites a chunk of it to make a file name. and like, this name is guaranteed not to exist at the moment you call the function, so it's literally ready for your app to use ๐ซถ
your template can be any file name with some number of Xs tacked on the end โ for example /tmp/temp.XXXXXXXXXX. those trailing Xs get swapped out for a unique alphanumeric combo ๐ฅ
how many unique names can mktemp() give you? depends on how many Xs you put in, bestie. six Xs means mktemp() picks one out of 56800235584 (62 ** 6) possible temporary file names. yeah. that many ๐
mkstemp() โ the upgrade
mkstemp() does the exact same replacement to the template and actually creates the template file, mode 0600, returning a file descriptor opened for reading and writing. the whole point? it avoids the race between testing whether a file exists and opening it for use. main character behavior, honestly ๐
mkostemp() โ now with flags
mkostemp() is like mkstemp() but you get to specify extra open(2) flags (they live in <fcntl.h>). the permitted flags are:
O_APPEND, O_DIRECT, O_SHLOCK, O_EXLOCK, O_SYNC, O_CLOEXEC and O_CLOFORK
that's the whole list, don't try to sneak others in ๐
mkstemps() and mkostemps() โ suffix girlies
mkstemps() and mkostemps() act the same as mkstemp() and mkostemp() respectively, except they permit a suffix to exist in the template. your template should look like /tmp/tmpXXXXXXXXXXsuffix, and you tell mkstemps() / mkostemps() the length of that suffix string ๐
mkostempsat() โ the directory-aware one
mkostempsat() acts the same as mkostemps() but takes an extra directory descriptor as a parameter. the temp file gets created relative to that corresponding directory โ or relative to the current working directory if you pass the special value AT_FDCWD โจ
and if the template path is an absolute path? the dfd parameter is ignored and it behaves identically to mkostemps(). slay, consistency ๐
mkdtemp() โ but a directory
mkdtemp() makes the same replacement to the template as in mktemp() and creates the template directory, mode 0700. that's it. that's the vibe ๐ฆ
โญ RETURN VALUES โญ
mktemp()andmkdtemp()return a pointer to the template on success, andNULLon failure ๐ญmkstemp(),mkostemp()mkstemps()andmkostemps()return-1if no suitable file could be created
and if either call fails, an error code gets dropped into the global variable errno. we love a global variable ๐ซ
๐ ERRORS ๐
mkstemp(), mkostemp(), mkstemps(), mkostemps() and mkdtemp() may set errno to one of these:
[ENOTDIR]โ the pathname portion of the template is not an existing directory. like, that's not a folder, bestie ๐
mkostemp() and mkostemps() may also set errno to this:
[EINVAL]โ the oflags argument is invalid. you gave it something it can't work with ๐
mkstemp(), mkostemp(), mkstemps(), mkostemps() and mkdtemp() may also set errno to any value specified by the stat(2) function ๐
mkstemp(), mkostemp(), mkstemps() and mkostemps() may also set errno to any value specified by the open(2) function ๐ฅ
and mkdtemp() may also set errno to any value specified by the mkdir(2) function ๐ธ
๐ NOTES ๐
a very common problem that ends in a core dump: the programmer passes in a read-only string to mktemp(), mkstemp(), mkstemps() or mkdtemp(). this is super common with programs developed before ISO/IEC 9899:1990 (โISO C90โ) compilers were a normal thing ๐ง
for example, calling mkstemp() with an argument of "/tmp/tempfile.XXXXXXXXXX" will result in a core dump, because mkstemp() attempts to modify that string constant you so kindly handed it. do not do this ๐ญ
also good to know: themkdtemp(),mkstemp()andmktemp()function prototypes are also available from<unistd.h>๐ซถ
๐ SEE ALSO ๐
๐ STANDARDS ๐
mkstemp()andmkdtemp()are expected to conform toIEEE Std 1003.1-2008(โPOSIX.1โ) ๐mktemp()is expected to conform toIEEE Std 1003.1-2001(โPOSIX.1โ) and is not specified byIEEE Std 1003.1-2008(โPOSIX.1โ)- the
mkostemp()function conforms to . mkstemps(),mkostemps()andmkostempsat()do not conform to any standard. they're free spirits ๐ฆ
โญ HISTORY โญ
- a
mktemp()function appeared inVersion 7 AT&T UNIX mkstemp()appeared in4.4BSDmkdtemp()first appeared inOpenBSD 2.2, and later inFreeBSD 3.2mkstemps()first appeared inOpenBSD 2.4, and later inFreeBSD 3.4mkostemp()andmkostemps()appeared inFreeBSD 10.0mkostempsat()appeared inFreeBSD 13.0
like, a whole family tree. iconic lineage honestly ๐ณโจ
๐จ BUGS ๐จ
okay this is the serious part, bestie, pay attention ๐ค
this whole family of functions produces filenames that can be guessed โ though the risk is minimized when you use large numbers of Xs, because that increases the number of possible temporary filenames.
and that makes the race in mktemp() โ between testing for a file's existence (in the mktemp() function call) and opening it for use (later, in the user application) โ particularly dangerous from a security perspective. like, genuinely scary ๐ฉ
whenever it's possible,mkstemp(),mkostemp()ormkostempsat()should be used instead, since they do not have the race condition.
if mkstemp() cannot be used, the filename created by mktemp() should be created using the O_EXCL flag to open(2), and the return status of the call should be tested for failure. that way your program doesn't keep going blindly if an attacker has already created the file with the intention of manipulating or reading its contents ๐
and that's the page!! this was Debian's MKTEMP(3), dated July 07, 2025 โ go make a temp file, but like, the safe way ๐ โจ๐
๐ฆ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1989, 1991, 1993 The Regents of the University of California. All rights reserved
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms